From 2c0cbcba288661c8ffec6f1b8f4c7b244e0a8e20 Mon Sep 17 00:00:00 2001 From: Justin Date: Mon, 9 Jan 2023 16:23:25 +0800 Subject: [PATCH] movie feature --- database_caller.py | 133 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 130 insertions(+), 3 deletions(-) diff --git a/database_caller.py b/database_caller.py index d69c5fb..653ef38 100755 --- a/database_caller.py +++ b/database_caller.py @@ -157,6 +157,114 @@ def updateCommodities(api_key, logf): #logf.close() +def updateMovies(api_key, logf): + + f = open('csv/movie_settings.json', 'r') + all_settings = json.load(f) + f.close() + + + if all_settings['category'] == 'Popular Movies': + url = 'https://api.themoviedb.org/3/trending/movie/day?' + movieGenre_url = 'https://api.themoviedb.org/3/genre/movie/list?api_key=' + api_key + '&language=en-US' + movieGenre_response = requests.get(movieGenre_url) + movie_genres = movieGenre_response.json() + + elif all_settings['category'] == 'Popular TV': + url = 'https://api.themoviedb.org/3/trending/tv/day?' + tvGenre_url = 'https://api.themoviedb.org/3/genre/tv/list?api_key=' + api_key + '&language=en-US' + tvGenre_response = requests.get(tvGenre_url) + tv_genres = tvGenre_response.json() + + elif all_settings['category'] == 'Popular All': + url = 'https://api.themoviedb.org/3/trending/all/day?' + movieGenre_url = 'https://api.themoviedb.org/3/genre/movie/list?api_key=' + api_key + '&language=en-US' + movieGenre_response = requests.get(movieGenre_url) + movie_genres = movieGenre_response.json() + tvGenre_url = 'https://api.themoviedb.org/3/genre/tv/list?api_key=' + api_key + '&language=en-US' + tvGenre_response = requests.get(tvGenre_url) + tv_genres = tvGenre_response.json() + + url += 'api_key=' + api_key + response = requests.get(url) + data = response.json() + + this_out = [] + logo_files = [] + + if len(data) > 0: + movies = data['results'] + + for movie in movies: + movie_language = movie['original_language'] + movie_votes = movie['vote_average'] + movie_votes = "{:.1f}".format(movie_votes) + try: + movie_titles = movie['title'] + movie_date = movie['release_date'] + except KeyError: + movie_titles = movie['name'] + movie_date = movie['first_air_date'] + movie_type = movie['media_type'] + movie_genre = movie['genre_ids'] + movie_logo = 'https://image.tmdb.org/t/p/w500' + movie['backdrop_path'] + genrefinal = [] + + if all_settings['category'] == 'Popular Movies': + for i in movie_genre: + for genre in movie_genres['genres']: + if genre['name'] == 'Science Fiction': + genre['name'] = 'Sci-Fi' + if i == genre['id']: + i = genre['name'] + genrefinal.append(i) + elif all_settings['category'] == 'Popular TV': + for i in movie_genre: + for genre in tv_genres['genres']: + if i == genre['id']: + i = genre['name'] + genrefinal.append(i) + elif all_settings['category'] == 'Popular All': + if movie['media_type'] == 'movie': + for i in movie_genre: + for genre in movie_genres['genres']: + if genre['name'] == 'Science Fiction': + genre['name'] = 'Sci-Fi' + if i == genre['id']: + i = genre['name'] + genrefinal.append(i) + elif movie['media_type'] == 'tv': + for i in movie_genre: + for genre in tv_genres['genres']: + if i == genre['id']: + i = genre['name'] + genrefinal.append(i) + + this_out.append({'title':movie_titles, + 'language':movie_language.upper(), + 'votes':str(movie_votes), + 'date':movie_date, + 'media_type':movie_type.capitalize(), + 'genre':genrefinal, + 'backdrop':movie['backdrop_path'][1:], + 'logo': movie_logo + }) + + logo_files.append(movie['backdrop_path'][1:]) + + urllib.request.urlretrieve(movie_logo,'logos/movies/' + movie['backdrop_path']) + + for file in os.listdir('logos/movies/'): + if file not in logo_files: + os.remove('logos/movies/'+file) + + all_settings['movies'] = this_out + + f = open('csv/movie_settings.json', 'w+') + json.dump(all_settings, f) + f.close() + + def updateIndices(api_key, logf): try: @@ -721,7 +829,7 @@ if __name__ == '__main__': - update_frequencies = {'stocks':2, 'crypto':5, 'forex':60, 'news':120, 'weather': 120, 'sports': 1440, 'commodities': 15, 'indices': 15} #minutes + update_frequencies = {'stocks':2, 'crypto':5, 'forex':60, 'news':120, 'weather': 120, 'sports': 1440, 'commodities': 15, 'indices': 15, 'movies': 1440} #minutes NY_zone = pytz.timezone('America/New_York') CET_zone = pytz.timezone('EST') @@ -758,7 +866,10 @@ if __name__ == '__main__': logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))) logf.close() - + try: + movie_key = open('movie_api_key.txt').readlines()[0] + except Exception as e: + movie_key = False t = time.time() @@ -792,7 +903,7 @@ if __name__ == '__main__': "news": {"time": "06/03/2022 04:07:09", "force": True}, "weather": {"time": "06/03/2022 04:08:20", "force": True}, "forex": {"time": "06/03/2022 03:54:02", "force": True}, "sports_l": {"time": "06/03/2022 04:10:09", "force": True}, "sports_p": {"time": "06/03/2022 04:10:09", "force": True}, - "sports_u": {"time": "06/03/2022 04:10:09", "force": True},"sports_t": {"time": "06/03/2022 04:10:09", "force": True}, "commodities": {"time": "06/03/2022 04:10:09", "force": True}, "indices": {"time": "06/03/2022 04:10:09", "force": True}} + "sports_u": {"time": "06/03/2022 04:10:09", "force": True},"sports_t": {"time": "06/03/2022 04:10:09", "force": True}, "commodities": {"time": "06/03/2022 04:10:09", "force": True}, "indices": {"time": "06/03/2022 04:10:09", "force": True}, "movies": {"time": "06/03/2022 04:10:09", "force": True}} @@ -879,6 +990,22 @@ if __name__ == '__main__': update_process.start() update_processes.append(update_process) + # movies + movies_time = datetime.strptime(last_updates['movies']['time'], "%d/%m/%Y %H:%M:%S") + + + NY_time = datetime.now(NY_zone).replace(tzinfo=None) + diff = (NY_time - movies_time).total_seconds()/60 #minutes + + + if last_updates['movies']['force'] or diff >= update_frequencies['movies']: + movies_time = NY_time.strftime("%d/%m/%Y %H:%M:%S") + last_updates['movies']['time'] = movies_time + last_updates['movies']['force'] = False + update_process = Process(target = updateMovies, args = (movie_key,logf)) + update_process.start() + update_processes.append(update_process) + # weather weather_time = datetime.strptime(last_updates['weather']['time'], "%d/%m/%Y %H:%M:%S")