def get_data(title="Game of Thrones"): client = OMDBClient(apikey="cc71ac81") client.set_default("tomatoes", True) xml_content = client.get(title=title) response = requests.get(xml_content["poster"]) img = Image.open(BytesIO(response.content)) img.save("static/{}.png".format(title.lower()), format="png") return xml_content
def get_ratings(title="Game of Thrones"): try: # check if we already have the ratings of the listing data = np.load("data/{}.npy".format(title.lower())) print("Loaded data from disk") return data, title except: client = OMDBClient(apikey="cc71ac81") client.set_default("tomatoes", True) # ratings for season 1 xml_content = client.get(title=title, season=1) num_episodes = len(xml_content["episodes"]) # total number of seasons total_seasons = int(xml_content["total_seasons"]) print("Total seasons: {}".format(total_seasons)) list = [] listoflists = [] for item in xml_content["episodes"]: list.append(item["imdb_rating"]) listoflists.append(list) print(listoflists) # get the ratings of other seasons for i in range(2, total_seasons + 1): list = [] xml_content = client.get(title=title, season=i) for item in xml_content["episodes"]: list.append(item["imdb_rating"]) listoflists.append(list) print(listoflists) maxlen = 0 for item in listoflists: if len(item) > maxlen: maxlen = len(item) for item in listoflists: while len(item) < maxlen: item.append(None) data = np.array(listoflists).transpose() np.place(data, data == "N/A", None) print(data) data = data.astype(np.float) print("Loaded data from omdb") np.save("data/{}".format(title.lower()), data) return data, title
def omdb_data(arguments): movie_name, year = arguments client = OMDBClient(apikey=OMDB_API_KEY) try: result = client.get(title=movie_name, year=year, fullplot=True, tomatoes=True) except HTTPError as e: print(e) client.set_default("apikey", OMDB_API_KEY_fallback) result = client.get(title=movie_name, year=year, fullplot=True, tomatoes=True) result_to_keep = {} for key in requested_flat_fields: result_to_keep[key] = result.get(key, None) for nested_field in requested_nested_fields: requested_nested_list = requested_nested_fields[nested_field] nested_list = result.get(nested_field, None) if nested_list: for nested_dict in nested_list: source = nested_dict.get("source", None) if source: value = nested_dict.get("value", None) if source in requested_nested_list: source_formatted = to_snake_case(source) key = f"{nested_field}_{source_formatted}" result_to_keep[key] = value requested_sources = requested_nested_fields[nested_field] for requested_source in requested_sources: source_formatted = to_snake_case(requested_source) key = f"{nested_field}_{source_formatted}" if not key in result_to_keep: result_to_keep[key] = None else: requested_sources = requested_nested_fields[nested_field] for requested_source in requested_sources: source_formatted = to_snake_case(requested_source) key = f"{nested_field}_{source_formatted}" result_to_keep[key] = None return t.Row(*list(result_to_keep.keys()))(*list(result_to_keep.values()))
import sys import json # To access the OMDb API, we can create an ``OMDBClient`` instance: from omdb import OMDBClient # must use OMDb API parameters client = OMDBClient(apikey='8ce3c73f') print("Success, API is connected") # Default for Rotten Tomato rating... client.set_default('tomatoes', True) cmd_Argument = sys.argv[1:] try: # print(cmd_Argument) if cmd_Argument: print("The command line argument is : %s" % (cmd_Argument)) # User Input # movie_name = str(input("Please enter movie name : ")) # movie_year = input("Enter year as well : ") res1 = client.request(t=cmd_Argument, plot='full', tomatoes='true', timeout=50) # year = movie_year xml_content = res1.content my_json = xml_content.decode('utf8') # .replace("'", '"') print('* ' * 20)