-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathomdbtool.py
executable file
·193 lines (149 loc) · 4.61 KB
/
omdbtool.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import argparse
import os
import sys
import json
try:
# Python 3
from urllib.request import urlopen
from urllib.parse import urlencode
to_unicode = lambda s: s
mk_trans = str.maketrans
except ImportError:
# Python 2
from urllib2 import urlopen
from urllib import urlencode
to_unicode = lambda s: unicode(s) # noqa
mk_trans = lambda a, b: {ord(ca): ord(cb) for ca, cb in zip(a, b)}
parser = argparse.ArgumentParser(description='Get OMDb data for a movie')
parser.add_argument(
"-t",
help="Movie title")
parser.add_argument(
"-y",
help="Year of release",
type=int)
parser.add_argument(
"-i",
help="IMDb movie id")
parser.add_argument(
"-r",
help="Return raw XML/JSON response",
choices=['JSON', 'XML'])
parser.add_argument(
"--plot",
help="Length of plot summary",
choices=['short', 'full'])
parser.add_argument(
"--tomatoes",
help="Include Rotten Tomatoes data too",
action="store_true")
parser.add_argument(
"--type",
help="movie, series, episode",
choices=['movie', 'series', 'episode'])
parser.add_argument(
"--season",
help="season number",
type=int)
parser.add_argument(
"--episode",
help="episode number",
type=int)
parser.add_argument(
"--format",
help="Output formated in html, markdown or csv, leave out for text",
choices=['html', 'markdown', 'csv'])
parser.add_argument(
"--apikey",
help="Your API key (will try to use env var OMDB_API_KEY if omitted)")
args = parser.parse_args()
params = {}
keys = ['t', 'y', 'i', 'plot', 'r', 'tomatoes', 'type', 'season', 'episode']
# prepare query string keyvals (excluding apikey, which is handled below)
for k in keys:
if args.__getattribute__(k):
params[k] = args.__getattribute__(k)
if len(params) == 0:
parser.print_help()
sys.exit()
# try to get API key, fall back to env var if not passed as argument
if args.__getattribute__('apikey'):
params['apikey'] = args.__getattribute__('apikey')
elif 'OMDB_API_KEY' in os.environ.keys():
params['apikey'] = os.environ['OMDB_API_KEY']
else:
print("Error: API key must be passed via --apikey=YOUR_KEY or set "
"as OMDB_API_KEY environment variable", file=sys.stderr)
sys.exit(1)
# call OMDb API
apicall = urlopen('https://www.omdbapi.com/?%s' % urlencode(params), timeout=6)
result = apicall.read()
apicall.close()
# print raw output and exit, if raw output was requested
if args.r:
print(result)
sys.exit()
if args.format == 'csv':
result = result.replace('","', ';')
chars_to_remove = ['"', '[', ']', '{', '}']
result = result.translate(None, ''.join(chars_to_remove))
print(result)
sys.exit()
# formats data as html
elif args.format == 'html':
result = result.replace('",', '<br>')
result = result.replace('{', '<br><br><br><p>')
chars_to_remove = ['"', '[', ']']
result = result.translate(None, ''.join(chars_to_remove))
result = result.replace('}', '</p>')
print(result)
sys.exit()
# formats the data as markdown
if args.format == 'markdown':
result = result.replace('",', '\n')
result = result.replace('{', '##')
chars_to_remove = ['"', '[', ']', '}']
result = result.translate(None, ''.join(chars_to_remove))
print(result)
sys.exit()
# known problematic characters to replace
char_map = mk_trans(
u'–',
u'-'
)
def fmt(s):
# get rid of weird characters in output, which also cause errors on Windows
# first use the preferred character mapping for known characters, then fall
# back to encode + decode for unexpected ones
return (to_unicode(s)
.translate(char_map)
.encode('ascii', errors='replace')
.decode('utf-8'))
def fmt_ratings(ls):
return '\n'.join([' {}: {}'.format(d['Source'], d['Value']) for d in val])
def fmt_single_episode(dct):
header = ' {} {}:'.format('Episode', dct['Episode'])
epinfo = [' {}: {}'.format(k, v)
for k, v in dct.items() if k != 'Episode']
return '\n'.join([header] + epinfo + [""])
def fmt_episodes(ls):
return '\n'.join(fmt_single_episode(dct) for dct in ls)
# print requested info
data = json.loads(result.decode('utf-8'))
for key, val in data.items():
key = key.lower()
# don't include "response: True" in output
if key == 'response' and val:
continue
print(key + ":")
if key == 'ratings':
s = fmt_ratings(val)
elif key == 'episodes':
s = fmt_episodes(val)
else:
s = val
print(fmt(s))
print("\n")