Skip to content

Commit 4bd2be1

Browse files
committed
base error handling in place.
1 parent 6f3d86c commit 4bd2be1

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import collections
2+
import requests
3+
4+
MovieResult = collections.namedtuple(
5+
'MovieResult',
6+
"imdb_code,title,duration,director,year,rating,imdb_score,keywords,genres")
7+
8+
9+
def find_movies(search_text):
10+
url = 'http://movie_service.talkpython.fm/api/search/{}'.format(search_text)
11+
12+
resp = requests.get(url)
13+
resp.raise_for_status()
14+
15+
movie_data = resp.json()
16+
movies_list = movie_data.get('hits')
17+
18+
movies = [
19+
MovieResult(**md)
20+
for md in movies_list
21+
]
22+
23+
movies.sort(key=lambda m: -m.year)
24+
25+
return movies

apps/10_movie_search/final/program.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import movie_svc
2+
3+
4+
def main():
5+
print_header()
6+
search_event_loop()
7+
8+
9+
def print_header():
10+
print('--------------------------------')
11+
print(' MOVIE SEARCH APP')
12+
print('--------------------------------')
13+
print()
14+
15+
16+
def search_event_loop():
17+
search = 'ONCE_THROUGH_LOOP'
18+
19+
while search != 'x':
20+
try:
21+
search = input('Movie search text (x to exit): ')
22+
if search != 'x':
23+
results = movie_svc.find_movies(search)
24+
print("Found {} results.".format(len(results)))
25+
for r in results:
26+
print('{} -- {}'.format(
27+
r.year, r.title
28+
))
29+
print()
30+
except:
31+
print("YIKES, that didn't work!")
32+
33+
print('exiting...')
34+
35+
36+
if __name__ == '__main__':
37+
main()

0 commit comments

Comments
 (0)