Created
June 13, 2023 17:55
-
-
Save mtik00/a6ac64839f855f701bcfdac86829606f to your computer and use it in GitHub Desktop.
Create "ipsum" test using Star Wars scripts from forceipsum.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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