Skip to content

Commit a302216

Browse files
committed
Merge branch 'recreate-movie-search-app'
* recreate-movie-search-app: Your turn instructions updated for new API. Final recorded version (take 2, with movie_service.talkpython.fm) base error handling in place. play around version done and pythonic. starter (again) ignore .envs
2 parents 861ab9a + 452dd75 commit a302216

File tree

6 files changed

+81
-101
lines changed

6 files changed

+81
-101
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,4 @@ target/
6565
.ipynb_checkpoints
6666
.DS_Store
6767
.idea
68+
.env

apps/10_movie_search/final/movie_client.py

-44
This file was deleted.
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
11+
if not search_text or not search_text.strip():
12+
raise ValueError("Search text is required")
13+
14+
url = 'http://movie_service.talkpython.fm/api/search/{}'.format(search_text)
15+
16+
resp = requests.get(url)
17+
resp.raise_for_status()
18+
19+
movie_data = resp.json()
20+
movies_list = movie_data.get('hits')
21+
22+
movies = [
23+
MovieResult(**md)
24+
for md in movies_list
25+
]
26+
27+
movies.sort(key=lambda m: -m.year)
28+
29+
return movies

apps/10_movie_search/final/play.py

+28-31
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,47 @@
33

44
MovieResult = collections.namedtuple(
55
'MovieResult',
6-
'Title,Poster,Type,imdbID,Year'
7-
)
6+
"imdb_code,title,duration,director,year,rating,imdb_score,keywords,genres")
87

9-
search = 'capital'
10-
url = 'http://www.omdbapi.com/?s={}&y=&plot=short&r=json'.format(search)
8+
search = input("What movie do you want to search for? ")
9+
url = 'http://movie_service.talkpython.fm/api/search/{}'.format(search)
1110

12-
r = requests.get(url)
13-
data = r.json()
11+
resp = requests.get(url)
12+
resp.raise_for_status()
1413

15-
results = data['Search']
14+
movie_data = resp.json()
15+
movies_list = movie_data.get('hits')
1616

1717
# movies = []
18-
# for result in results:
18+
# for md in movies_list:
1919
# m = MovieResult(
20-
# Title=result['Title'],
21-
# Poster=result['Poster'],
22-
# Type=result['Type'],
23-
# imdbID=result['imdbID'],
24-
# Year=result['Year']
20+
# imdb_code=md.get('imdb_code'),
21+
# title=md.get('title'),
22+
# duration=md.get('duration'),
23+
# director=md.get('director'),
24+
# year=md.get('year', 0),
25+
# rating=md.get('rating', 0),
26+
# imdb_score=md.get('imdb_score', 0.0),
27+
# keywords=md.get('keywords'),
28+
# genres=md.get('genres')
2529
# )
2630
# movies.append(m)
2731

28-
# def method_with_kws(pos1, **kwargs)
29-
# pass
32+
# def method(x, y, z, **kwargs):
33+
# print("kwargs=", kwargs)
34+
#
35+
# method(7, 1, z=2, format=True, age=7)
3036

3137
# movies = []
32-
# for result in results:
33-
# m = MovieResult(**result)
38+
# for md in movies_list:
39+
# m = MovieResult(**md)
3440
# movies.append(m)
3541

3642
movies = [
37-
MovieResult(**m)
38-
for m in results
43+
MovieResult(**md)
44+
for md in movies_list
3945
]
4046

41-
print(movies)
42-
43-
44-
45-
46-
47-
48-
49-
50-
51-
52-
47+
print("Found {} movies for search {}".format(len(movies), search))
48+
for m in movies:
49+
print("{} -- {}".format(m.year, m.title))

apps/10_movie_search/final/program.py

+10-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from movie_client import MovieClient
1+
import movie_svc
22
import requests.exceptions
33

4+
45
def main():
56
print_header()
67
search_event_loop()
@@ -18,37 +19,24 @@ def search_event_loop():
1819

1920
while search != 'x':
2021
try:
21-
search = input('Title search text (x to exit): ')
22+
search = input('Movie search text (x to exit): ')
2223
if search != 'x':
23-
client = MovieClient(search)
24-
25-
results = client.perform_search()
24+
results = movie_svc.find_movies(search)
2625
print("Found {} results.".format(len(results)))
2726
for r in results:
2827
print('{} -- {}'.format(
29-
r.Year, r.Title
28+
r.year, r.title
3029
))
30+
print()
31+
except ValueError:
32+
print("Error: Search text is required")
3133
except requests.exceptions.ConnectionError:
32-
print('ERROR: Cannot search, you network is down.')
33-
except ValueError as ve:
34-
print('ERROR: Your search string is invalid: {}'.format(ve))
34+
print("Error: Your network is down.")
3535
except Exception as x:
36-
print("ERROR: {}".format(x))
36+
print("Unexpected error. Details: {}".format(x))
3737

3838
print('exiting...')
3939

4040

4141
if __name__ == '__main__':
4242
main()
43-
44-
45-
46-
47-
48-
49-
50-
51-
52-
53-
54-

apps/10_movie_search/you_try/README.md

+13-4
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,29 @@
22

33
![image](app-10-screenshot.png)
44

5-
If you want to try this yourself, try to build the interactive app above.
5+
Your turn. Try to build the interactive app above.
66

7-
This application use the OMDb API to search live movie data for movies matching the title in your search text. It focuses on being reliable even when the network fails or the user enters bad information.
7+
This application use the Talk Python MovieDb API to search live movie data for movies
8+
matching the title in your search text. It focuses on being reliable
9+
even when the network fails or the user enters bad information.
810

911
Key concepts introduced
1012
=================
1113

1214
**The API**
1315

14-
You can find the details of the JSON HTTP API at [www.omdbapi.com](http://www.omdbapi.com).
16+
You can find the details of the JSON HTTP API at [movie_service.talkpython.fm](http://movie_service.talkpython.fm/).
1517

1618
**Try/Except Error Handling**
1719

18-
try: method1() method2() method3() except ConnectionError as ce: # handle network error except Exception as x: # handle general error
20+
try:
21+
method1()
22+
method2()
23+
method3()
24+
except ConnectionError as ce:
25+
# handle network error
26+
except Exception as x:
27+
# handle general error
1928

2029
**Raising your own errors and exceptions**
2130

0 commit comments

Comments
 (0)