Skip to content

Instantly share code, notes, and snippets.

@Dreamersoul
Last active August 8, 2022 06:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Dreamersoul/311542606276b347c38c to your computer and use it in GitHub Desktop.
Save Dreamersoul/311542606276b347c38c to your computer and use it in GitHub Desktop.
you need to have already initialized trakt api with store=True
from __future__ import print_function
import httplib2
import os
from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools
import datetime
from trakt.calendar import MyShowCalendar
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
SCOPES = 'https://www.googleapis.com/auth/calendar'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Google Calendar API Quickstart'
TIMEZONE = 'YOUR-OWN-TIMEZONE'
def get_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'calendar-quickstart.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatability with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def main():
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('calendar', 'v3', http=http)
s_calendar = MyShowCalendar(days=30)
for episode in s_calendar:
episode_sting = "{} S{}E{}".format(episode.show,episode.season,episode.number)
start_date = episode.airs_at
end_date = start_date + datetime.timedelta(days=1)
event = {
'summary': episode_sting,
'start': {
'date': start_date.strftime('%Y-%m-%d'),
'timeZone': TIMEZONE
},
'end': {
'date': end_date.strftime('%Y-%m-%d'),
'timeZone': TIMEZONE
},
'reminders': {
'useDefault': False,
'overrides': [
{'method': 'popup', 'minutes': 12*60},
],
},
}
event = service.events().insert(calendarId='primary', body=event).execute()
print("Event created: {}".format(event.get('htmlLink')))
print("done")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment