Skip to content

Instantly share code, notes, and snippets.

@mtik00
Created June 13, 2023 17:55
Show Gist options
  • Save mtik00/a6ac64839f855f701bcfdac86829606f to your computer and use it in GitHub Desktop.
Save mtik00/a6ac64839f855f701bcfdac86829606f to your computer and use it in GitHub Desktop.
Create "ipsum" test using Star Wars scripts from forceipsum.com
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""A script to print "ipsum" text from the Star Wars universe through Force Ipsum.
https://forcemipsum.com
"""
import argparse
import json
import sys
import urllib.request
from secrets import SystemRandom
from typing import List
def parse_args(args: List[str] = sys.argv[1:]) -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument(
"-c", "--count", help="Number of lines to return", type=int, default=5
)
return parser.parse_args(args)
def get(url):
with urllib.request.urlopen(url) as response:
html = response.read().decode("utf-8")
return json.loads(html)
class Force:
url: str = "https://forcemipsum.com/api"
episodes: list[int] = [1, 4, 5, 6, 7] # According to the website
def get(self, count: int = 5):
episode = SystemRandom().choice(self.episodes)
response = [f"[Episode {episode}]"] + get(f"{self.url}/{episode}/{count}")
return "\n\n".join(response)
def main():
args = parse_args()
print(Force().get(args.count))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment