-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomuser.py
executable file
·131 lines (117 loc) · 4.19 KB
/
randomuser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/python
import argparse # handling CLI arguments
import os # directories and files
import sys # clean exit
import requests # contacting APIs
import json # parsing downloaded data
import shutil # copying images
from yattag import Doc # generating html
def main():
"""
randomuser.py output -n N -v
output - files output directory
N - number of users
v - verbose flag
fetches users from RandomAPI,
saves their thumbnails and generates index html
"""
# prepare and parse the CLI arguments
path, verbose, users_number = process_command_line()
## set verbose logging
verboseprint = print if verbose else lambda *a, **k: None
# abort if the output directory exists
try:
verboseprint("Creating output path...")
os.mkdir(path)
except OSError as e:
print("Failure: Directory exists")
sys.exit(1)
# try to fetch the users
verboseprint("Trying to fetch the users:")
url = 'https://randomuser.me/api/?results=' + str(users_number)
users = get_users(url)
verboseprint("Data fetched.")
# Save the thumbnails
verboseprint("Saving users' pictures...")
for user in users:
verboseprint("Copying " + user['name']['first'] + "'s data...")
thumbnail_url = user['picture']['thumbnail']
user_pic_response = requests.get(thumbnail_url, stream=True)
with open(thumbnail_path(user, path), 'wb') as user_pic:
shutil.copyfileobj(user_pic_response.raw, user_pic)
del user_pic_response
# Generate index html
verboseprint("Generating HTML code...")
html_string = generate_html_string(users)
## Create the .html file
verboseprint("Saving HTML file...")
with open(path + '/' + 'index.html', 'w') as html_file:
html_file.write(html_string)
verboseprint("Done. " + str(users_number) + " users fetched.")
def process_command_line():
"""
process CLI arguments
returns triple tuple (path:string, verbose:bool, number_of_users:int)
"""
parser = argparse.ArgumentParser()
parser.add_argument('path', help='file path')
parser.add_argument('-n', type=int, default=25,
help='number of users to fetch')
parser.add_argument('-v', action='store_true', help='verbose')
args = vars(parser.parse_args())
return args['path'], args['v'], args['n']
def get_users(url):
"""
fetches data from url
returns as dictionary
"""
response = requests.get(url)
# For successful API call
if(response.ok):
# load json to python dict
jData = json.loads(response.content.decode('utf-8'))
return jData['results']
# Else raise an exception
else:
response.raise_for_status()
def generate_html_string(users):
"""
generate html table from users dictionary
returns html code string
"""
headers = ['First Name', 'Last Name', 'Picture']
doc, tag, text = Doc().tagtext()
doc.asis('<!DOCTYPE html>')
with tag('html'):
with tag('head'):
with tag('title'):
text('10clouds_1sun')
with tag('body'):
with tag('h1'):
text('Random users:')
with tag('table', style='width:50%'):
# header
with tag('tr'):
for header in headers:
with tag('th'):
text(header)
# data
for user in users:
with tag('tr'):
with tag('td'):
text(user['name']['first'])
with tag('td'):
text(user['name']['last'])
with tag('td'):
doc.stag('img', src=thumbnail_path(user))
return doc.getvalue()
# HELPERS
def thumbnail_path(user, directory='.'):
"""returns 'directory/firstname_lastname.png'"""
return directory + '/' + full_name(user) + '.png'
def full_name(user):
"""returns 'firstname_lastname'"""
return user['name']['first'] + "_" + user['name']['last']
if __name__ == '__main__':
status = main()
sys.exit(status)