Skip to content

Commit 2138bdf

Browse files
committed
026
1 parent 17e56a2 commit 2138bdf

File tree

6 files changed

+129
-1
lines changed

6 files changed

+129
-1
lines changed

026/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cache*

026/README.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## 026
2+
3+
Interactive script to query the [OMDb API](http://www.omdbapi.com):
4+
5+
$ python omdb.py
6+
Script to query OMDb API
7+
Enter IMDB ID or title (q to exit): tt0104348
8+
9+
Title: Glengarry Glen Ross
10+
Year: 1992
11+
Genre: Crime, Drama, Mystery
12+
Director: James Foley
13+
Actors: Al Pacino, Jack Lemmon, Alec Baldwin, Alan Arkin
14+
IMDB score: 7.8
15+
Plot: An examination of the machinations behind the scenes at a real estate office.
16+
17+
18+
Enter IMDB ID or title (q to exit): scarface
19+
Year of release? 1983
20+
21+
Title: Scarface
22+
Year: 1983
23+
Genre: Crime, Drama
24+
Director: Brian De Palma
25+
Actors: Al Pacino, Steven Bauer, Michelle Pfeiffer, Mary Elizabeth Mastrantonio
26+
IMDB score: 8.3
27+
Plot: In Miami in 1980, a determined Cuban immigrant takes over a drug cartel and succumbs to greed.
28+
29+
30+
Enter IMDB ID or title (q to exit): nonsense
31+
Year of release? 111
32+
Error: Movie not found!
33+
Enter IMDB ID or title (q to exit): q
34+
Bye
35+
$ python omdb.py -h
36+
Script to query OMDb API
37+
Enter IMDB ID or title (q to exit): scarface
38+
Year of release? 1983
39+
40+
<li>
41+
<h1>Scarface (1983)</h1>
42+
<h2>Genre: Crime, Drama / Director: Brian De Palma</h2>
43+
<h3>Actors: Al Pacino, Steven Bauer, Michelle Pfeiffer, Mary Elizabeth Mastrantonio</h3>
44+
<h4>IMDB score: 8.3</h4>
45+
<p>In Miami in 1980, a determined Cuban immigrant takes over a drug cartel and succumbs to greed.</p>
46+
</li>
47+
48+
Enter IMDB ID or title (q to exit): q
49+
Bye

026/omdb.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from pprint import pprint as pp
2+
import sys
3+
4+
import requests
5+
import requests_cache
6+
7+
from templates import TEMPLATE, HTML_TEMPLATE
8+
9+
OMDB = 'http://www.omdbapi.com'
10+
OMDB_BY_IMDB = OMDB + '/?i={imdb}'
11+
OMDB_BY_TITLE_YEAR = OMDB + '/?t={title}&y={year}'
12+
13+
# cache repeated calls to OMDB (not sure about API limits)
14+
requests_cache.install_cache()
15+
16+
17+
def query_omdb(**kwargs):
18+
if 'imdb' in kwargs:
19+
url = OMDB_BY_IMDB.format(**kwargs)
20+
else:
21+
url = OMDB_BY_TITLE_YEAR.format(**kwargs)
22+
return requests.get(url).json()
23+
24+
25+
if __name__ == '__main__':
26+
args = sys.argv[1:]
27+
html = True if '-h' in args else False
28+
verbose = True if '-v' in args else False
29+
answer = None
30+
31+
print('Script to query OMDb API')
32+
33+
while answer != 'q':
34+
answer = input('Enter IMDB ID or title (q to exit): ').lower()
35+
params = dict()
36+
37+
if answer == 'q':
38+
print('Bye')
39+
break
40+
elif 'tt' in answer:
41+
params['imdb'] = answer
42+
else:
43+
params['title'] = answer
44+
params['year'] = input('Year of release? ')
45+
46+
resp = query_omdb(**params)
47+
if verbose:
48+
pp(resp)
49+
50+
if 'Error' in resp:
51+
print('Error: {}'.format(resp['Error']))
52+
else:
53+
tmpl = HTML_TEMPLATE if html else TEMPLATE
54+
print(tmpl.format(**dict(resp)))

026/requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
requests==2.13.0
2+
requests-cache==0.4.13

026/templates.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import os
2+
import sys
3+
4+
TEMPLATE = '''
5+
Title: {Title}
6+
Year: {Year}
7+
Genre: {Genre}
8+
Director: {Director}
9+
Actors: {Actors}
10+
IMDB score: {imdbRating}
11+
Plot: {Plot}
12+
13+
'''
14+
HTML_TEMPLATE = '''
15+
<li>
16+
<h1>{Title} ({Year})</h1>
17+
<h2>Genre: {Genre} / Director: {Director}</h2>
18+
<h3>Actors: {Actors}</h3>
19+
<h4>IMDB score: {imdbRating}</h4>
20+
<p>{Plot}</p>
21+
</li>
22+
'''

LOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
| 023 | Apr 21, 2017 | [use Counter to count the most common words in a file](023) | we did some time ago, collections module is awesome |
2828
| 024 | Apr 22, 2017 | [generate color hex codes from random RGBs and color terminal text](024) | nice play with generators, RGB to hex with format, and colorizing the terminal with the [colored](https://pypi.python.org/pypi/colored) package. This could be useful for future cli apps |
2929
| 025 | Apr 23, 2017 | [Simple test #database generator script #python #sqlite #contextmanager](025) | Script to generate a quick, test sqlite3 database with 1 table and 3 columns. Can be customised and expanded to suit your needs. Definitely useful for playing around with persistent data. |
30-
| 026 | Apr 24, 2017 | [TITLE](026) | LEARNING |
30+
| 026 | Apr 24, 2017 | [Simple script to retrieve #movie data from OMDb #API](026) | Get OMDB for movie data, query API by IMDB ID or title. Returns text or html. Uses request_cache to limit repeated calls to API. Could help some folks taking this [this week's challenge](http://pybit.es/codechallenge16.html) |
3131
| 027 | Apr 25, 2017 | [TITLE](027) | LEARNING |
3232
| 028 | Apr 26, 2017 | [TITLE](028) | LEARNING |
3333
| 029 | Apr 27, 2017 | [TITLE](029) | LEARNING |

0 commit comments

Comments
 (0)