|
| 1 | +import uplink |
| 2 | +from pprint import pprint |
| 3 | + |
| 4 | + |
| 5 | +@uplink.response_handler |
| 6 | +def raise_for_status(response): |
| 7 | + response.raise_for_status() |
| 8 | + return response |
| 9 | + |
| 10 | + |
| 11 | +@uplink.json |
| 12 | +@raise_for_status |
| 13 | +class MovieSearchClient(uplink.Consumer): |
| 14 | + |
| 15 | + def __init__(self): |
| 16 | + super().__init__(base_url='http://movie_service.talkpython.fm') |
| 17 | + |
| 18 | + @uplink.get('/api/search/{keyword}') |
| 19 | + def movie_name(self, keyword): |
| 20 | + """ Search Movies """ |
| 21 | + |
| 22 | + @uplink.get('/api/director/{director_name}') |
| 23 | + def director_name(self, director_name): |
| 24 | + """ Search Movies by Director """ |
| 25 | + |
| 26 | + @uplink.get('/api/movie/{imdb_number}') |
| 27 | + def imdb_number(self, imdb_number): |
| 28 | + """ Search Movie by IMDB code """ |
| 29 | + |
| 30 | + |
| 31 | +def search_movie_name(): |
| 32 | + get_name = input('Enter movie name to search for ') |
| 33 | + response = get_movies.movie_name(get_name) |
| 34 | + results = response.json() |
| 35 | + for idx in results['hits']: |
| 36 | + print(f'{idx["title"]} - {idx["director"]} - {idx["imdb_code"]}') |
| 37 | + |
| 38 | + |
| 39 | +def search_director_name(): |
| 40 | + get_name = input('Enter Director name to search for ') |
| 41 | + response = get_movies.director_name(get_name) |
| 42 | + results = response.json() |
| 43 | + for idx in results['hits']: |
| 44 | + print(f'{idx["title"]} - {idx["director"]} - {idx["imdb_code"]}') |
| 45 | + |
| 46 | + |
| 47 | +def search_imdb_number(): |
| 48 | + get_name = input('Enter IMDB Number to search for ') |
| 49 | + response = get_movies.imdb_number(get_name) |
| 50 | + results = response.json() |
| 51 | + # pprint(results) |
| 52 | + print(f'{results["title"]} - {results["director"]} - {results["imdb_code"]}') |
| 53 | + |
| 54 | + |
| 55 | +get_movies = MovieSearchClient() |
| 56 | + |
| 57 | +print('Select how would you like to search by') |
| 58 | +selection_list = ['Movie Title', 'Director Name', 'IMDB Code'] |
| 59 | + |
| 60 | +for idx, idx_title in enumerate(selection_list, start=1): |
| 61 | + print(f'{idx} {idx_title}') |
| 62 | + |
| 63 | +while True: |
| 64 | + user_selection = input() |
| 65 | + if user_selection and user_selection.isdigit(): |
| 66 | + if int(user_selection) <= len(selection_list): |
| 67 | + if user_selection == '1': |
| 68 | + search_movie_name() |
| 69 | + break |
| 70 | + elif user_selection == '2': |
| 71 | + search_director_name() |
| 72 | + break |
| 73 | + elif user_selection == '3': |
| 74 | + search_imdb_number() |
| 75 | + break |
| 76 | + |
| 77 | + print('Incorrect selection. Try again') |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +# |
| 82 | +# response = get_movies.movie_name('spider') |
| 83 | +# results = response.json() |
| 84 | +# |
| 85 | +# # get_all_movies = MovieSearchClient.all_entries('spider') |
| 86 | +# pprint(results) |
| 87 | +# |
| 88 | +# for idx in results['hits']: |
| 89 | +# print(idx['title']) |
| 90 | + |
0 commit comments