-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovies_release_tracker.py
211 lines (181 loc) · 8.6 KB
/
movies_release_tracker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from __future__ import print_function
import requests
from datetime import datetime, timedelta
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/calendar']
# Define your TMDB API key
api_key = "YOUR_TMDB_API_KEY"
# Define your Google Calendar ID
calendarId = 'YOUR_GOOGLE_CALENDAR_ID'
# Define the company IDs
companies_id = ["YOUR_COMPANIES_ID_HERE","YOUR_COMPANIES_ID2_HERE"]
# Define the region ("US","FR",...) for which you want to retrieve release dates and the output language ("us-US" for United-States, "fr-FR" for France...)
first_region = "US"
second_region = "FR"
language = "en-US"
def main():
# Iterate through the movie results for each company ID
for id in companies_id:
data_movies = requete_tmdb_movie(api_key, id, "US")
for movie in data_movies["results"]:
title = movie["title"]
release_date = movie["release_date"]
poster = movie["poster_path"]
synopsis = movie["overview"]
release_date = requete_tmdb_movie_date(api_key, movie['id'], first_region)
if release_date != "none":
fr_release_date = release_date
print(f"\n{title} {movie['id']} {fr_release_date} in {first_region}")
providers = requete_tmdb_watch_providers(api_key, movie['id'], "movie", first_region)
add_event(title, fr_release_date, creds, "movie", movie['id'], first_region, synopsis, providers=providers_string(providers))
elif release_date == "none":
us_release_date = requete_tmdb_movie_date(api_key, movie['id'], second_region)
print(f"\n{title} {movie['id']} {us_release_date} in {second_region}")
providers = requete_tmdb_watch_providers(api_key, movie['id'], "movie", first_region)
add_event(title, us_release_date, creds, "movie", movie['id'], second_region, synopsis, providers=providers_string(providers))
else:
print(f"No release dates in {first_region} or {second_region}")
# Iterate through the series results for each company ID
for id in companies_id:
data_series = requete_tmdb_serie(api_key, id, "US")
if data_series["total_results"] >= 1:
for serie in data_series["results"]:
title = serie["name"]
first_air_date = serie["first_air_date"]
poster = serie["poster_path"]
synopsis = serie["overview"]
if first_air_date:
print(f"\n{title} {serie['id']} {first_air_date}")
providers = requete_tmdb_watch_providers(api_key, movie['id'], "TV", {first_region})
add_event(title, first_air_date, creds, "tv", serie['id'], "", synopsis, providers=providers_string(providers))
else:
print("No planned series")
# Function to refresh the access token if it's expired
def refresh_token(creds):
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
return True
return False
# Function to get the user credentials
def get_credentials():
creds = None
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
else:
print("No file named 'token.json'")
return creds
# Function to save the user credentials
def save_credentials(creds):
with open('token.json', 'w') as token:
token.write(creds.to_json())
# Load credentials
creds = get_credentials()
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if refresh_token(creds):
save_credentials(creds)
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
save_credentials(creds)
def requete_tmdb_movie(api_key, company_id, region):
url_movies = f"https://api.themoviedb.org/3/discover/movie?api_key={api_key}&with_companies={company_id}®ion={region}&sort_by=release_date.asc&primary_release_date.gte=now&language={language}"
#url_movies = f"https://api.themoviedb.org/3/discover/movie?api_key={api_key}&with_companies={company_id}®ion={region}&sort_by=release_date.asc&primary_release_year=2023&language=fr-FR"
response_movies = requests.get(url_movies)
data_movies = response_movies.json()
return data_movies
def requete_tmdb_serie(api_key, company_id, region):
url_series = f"https://api.themoviedb.org/3/discover/tv?api_key={api_key}&with_companies={company_id}®ion={region}&sort_by=first_air_date.asc&first_air_date.gte=now&language={language}"
#url_series = f"https://api.themoviedb.org/3/discover/tv?api_key={api_key}&with_companies={company_id}®ion={region}&sort_by=first_air_date.asc&first_air_date_year=2023&language=fr-FR"
response_series = requests.get(url_series)
data_series = response_series.json()
return data_series
def requete_tmdb_movie_date(api_key, movie_id, region):
url = f"https://api.themoviedb.org/3/movie/{movie_id}/release_dates?api_key={api_key}"
response = requests.get(url)
data = response.json()
spc_release_date = "none"
for release in data["results"]:
if release["iso_3166_1"] == region:
for release_date in release["release_dates"]:
spc_release_date = release_date["release_date"].split("T")[0]
break
return spc_release_date
def requete_tmdb_watch_providers(api_key, movie_id, type, region):
url = f"https://api.themoviedb.org/3/{type}/{movie_id}/watch/providers?api_key={api_key}"
response = requests.get(url)
data = response.json()
fr_providers = {}
for i in ["flatrate", "buy", "free"]:
try:
fr_providers[i] = ([provider['provider_name'] for provider in data['results'][region][i]])
except:
continue
return fr_providers
def providers_string(providers):
if providers:
provider_txt = 'Watch on: '
for i in ["flatrate", "buy", "free"]:
try:
provider_txt += ', '.join(providers[i])
provider_txt += f' ({i}) '
except:
continue
else:
provider_txt = ""
return provider_txt
def add_event(title, release_date, creds, type_show, ID, region, synopsis, providers):
service = build('calendar', 'v3', credentials=creds)
if type_show == 'tv':
title_and_type = f"{title} (TV Series)"
else:
title_and_type = f"{title} (Movie)"
if synopsis == "":
synopsis = ""
else:
synopsis = f'\nSynopsis: {synopsis}\n{providers}'
event = {
'summary': title_and_type,
'description': f'{region} Date {synopsis}',
'start': {
'date': datetime.strptime(release_date, '%Y-%m-%d').strftime("%Y-%m-%d"),
'timeZone': 'Europe/Paris',
},
'end': {
'date': (datetime.strptime(release_date, '%Y-%m-%d') + timedelta(days=1)).strftime("%Y-%m-%d"),
'timeZone': 'Europe/Paris',
},
'source': {
'url': f"https://www.themoviedb.org/{type_show}/{ID}",
'title': 'TMDB Page'
},
'transparency': 'transparent'
}
# Search for events with the same title
events_result = service.events().list(calendarId=calendarId, q=title_and_type).execute()
events = events_result.get('items', [])
if not events:
try:
event = service.events().insert(calendarId=calendarId, body=event).execute()
#print ('Event created: %s' % (event.get('htmlLink')))
print('Event created')
except HttpError as error:
print('An error occurred' % error)
else:
# If one or more events were found with the same title, update the first event
event_id = events[0]['id']
try:
event = service.events().update(calendarId=calendarId, eventId=event_id, body=event).execute()
#print('Event updated: %s' % (event.get('htmlLink')))
print('Event updated')
except HttpError as error:
print('An error occurred' % error)
if __name__ == '__main__':
main()