-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
86 lines (74 loc) · 2.04 KB
/
main.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
"""
Fetches rotten tomatoes from http://www.omdbapi.com/ website
"""
import argparse
import sys
import os
import requests
#consts
VERSION = '0.0.1'
URL = "http://www.omdbapi.com/"
#envs
apikey = os.environ.get('API_KEY')
if not apikey:
sys.exit("Please set API_KEY environment variable first")
def requester(get_args: dict) -> dict:
"""
Appends apikey to the params and requests to the url
"""
get_args.update(dict(apikey = apikey))
response = requests.get(URL, params = get_args)
return response.json()
def rotten_rate_fetcher(imdb_id: str) -> int:
"""
Fetches the rotten tomatoes removes % and also will return None if no rotten tomatoes found
"""
movie = requester(dict(i = imdb_id))
for rate in movie["Ratings"]:
if "Rotten Tomatoes" in rate["Source"]:
return int(rate["Value"][:-1])
return None
def populate(json_response: list) -> list:
"""
Creates a list for output
"""
output = []
for movie in json_response:
imdb_id = movie["imdbID"]
output.append(dict(
title = movie["Title"],
imdbID = imdb_id,
rottenTomatoesPercentage = rotten_rate_fetcher(imdb_id)
))
return output
def pretty_print(output: list):
"""
Human readable print for output
"""
for movie in output:
for item in movie.items():
print(item[0]+":", item[1])
print()
#argParser
arg_parser = argparse.ArgumentParser(
usage = '%(prog)s name',
description = 'Get Rotten Tomato of movie',
epilog='Enjoy the program! :)')
arg_parser.version = VERSION
arg_parser.add_argument(
'name',
metavar = 'name',
type = str,
help = 'the name of the movie for search')
arg_parser.add_argument(
'-j',
'--json',
action="store_true",
help = 'json output')
args = arg_parser.parse_args()
if __name__ == '__main__':
movies = requester(dict(s = args.name))
if args.json:
print(populate(movies["Search"]))
else:
pretty_print(populate(movies["Search"]))