Created
January 15, 2021 11:20
-
-
Save lamchau/177e35edcb6923f62dec25aa1a26f75d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
import argparse | |
import requests | |
import sys | |
TYPES = { | |
'bytes': { | |
'param': 'bytes', | |
'count': 256 | |
}, | |
'lists': { | |
'param': 'lists', | |
'count': 3, | |
'bullet': ' - ' | |
}, | |
'paragraphs': { | |
'param': 'paras', | |
'count': 5 | |
}, | |
'words': { | |
'param': 'words', | |
'count': 15 | |
} | |
} | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
subparsers = parser.add_subparsers(dest='type') | |
for k, v in TYPES.items(): | |
command = subparsers.add_parser(k, help=f'generate {k}') | |
command.add_argument('count', type=int, default=v.get('count'), nargs='?', | |
help='the number of items to generate') | |
if k == 'lists': | |
command.add_argument('bullet', default=v.get('bullet'), nargs='?', | |
help='a bullet string') | |
command.add_argument('-s', '--start', | |
action='store_true', | |
help='start with the text "Lorem ipsum" (default=%(default)s)') | |
# nb: sys.argv will never be empty, the first the script name | |
if len(sys.argv) > 1: | |
args = parser.parse_args(sys.argv[1:]) | |
else: | |
args = parser.parse_args() | |
if not args.type: | |
parser.print_help() | |
sys.exit(0) | |
response = requests.get('https://www.lipsum.com/feed/json', { | |
'start': 'yes' if args.start else 'no', | |
'what': TYPES.get(args.type).get('param'), | |
'amount': args.count | |
}) | |
if response.status_code != 200: | |
print(response) | |
sys.exit(1) | |
response = response.json().get('feed') | |
generated = response.get('generated') | |
text = response.get('lipsum') | |
if args.type == 'lists': | |
print(generated.replace('paragraph', 'list') + '\n') | |
for paragraphs in text.split('\n'): | |
for item in paragraphs.split('.'): | |
item = item.strip() | |
if item: | |
print(f'{args.bullet}{item}.') | |
print() | |
else: | |
print(generated + '\n') | |
print(text.replace('\n', '\n\n')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment