8000 Movie search script. · shescloud/practice-python@313bfd2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 313bfd2

Browse files
committed
Movie search script.
1 parent dc43586 commit 313bfd2

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

experiments/movie.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import json
2+
import requests
3+
4+
5+
def do_search(search_term):
6+
req = requests.get('http://www.omdbapi.com/', params={'s': search_term, 'type': 'movie'})
7+
if req.status_code == requests.codes.ok:
8+
movies = json.loads(req.text) # not using req.json in order to sort
9+
if 'Search' in movies:
10+
lines = sorted(movies['Search'], key=lambda m: m['Year'], reverse=True)
11+
12+
for movie in lines:
13+
print("{} - {}".format(movie['Year'], movie['Title']))
14+
else:
15+
print('Nothing found')
16+
17+
18+
def main():
19+
keep_going = True
20+
while keep_going:
21+
search = input('Search for a movie [enter to stop]: ')
22+
if search.strip():
23+
do_search(search)
24+
else:
25+
keep_going = False
26+
27+
28+
if __name__ == '__main__':
29+
main()

0 commit comments

Comments
 (0)
0