Advertisement
gabrielpreite

main,py

Nov 23rd, 2020
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. from __future__ import print_function
  2. import datetime
  3. import secrets
  4. import pickle
  5. import os.path
  6. import json
  7. import hashlib
  8. import bencode
  9. from googleapiclient.discovery import build
  10. from google_auth_oauthlib.flow import InstalledAppFlow
  11. from google.auth.transport.requests import Request
  12.  
  13. # If modifying these scopes, delete the file token.pickle.
  14. SCOPES = ['https://www.googleapis.com/auth/calendar']
  15.  
  16.  
  17. def main():
  18.     creds = None
  19.     # The file token.pickle stores the user's access and refresh tokens, and is
  20.     # created automatically when the authorization flow completes for the first
  21.     # time.
  22.     if os.path.exists('token.pickle'):
  23.         with open('token.pickle', 'rb') as token:
  24.             creds = pickle.load(token)
  25.     # If there are no (valid) credentials available, let the user log in.
  26.     if not creds or not creds.valid:
  27.         if creds and creds.expired and creds.refresh_token:
  28.             creds.refresh(Request())
  29.         else:
  30.             flow = InstalledAppFlow.from_client_secrets_file(
  31.                 'credentials.json', SCOPES)
  32.             creds = flow.run_local_server(port=0)
  33.         # Save the credentials for the next run
  34.         with open('token.pickle', 'wb') as token:
  35.             pickle.dump(creds, token)
  36.     service = build('calendar', 'v3', credentials=creds)
  37.  
  38.     #get calendar list
  39.     """calendars_result = service.calendarList().list(showHidden=True).execute()
  40.    calendars = calendars_result.get('items', [])
  41.    print(calendars)"""
  42.  
  43.     with open("primo1.json") as f:
  44.         cal = json.loads(f.read())
  45.     calId = cal[0]["title"]
  46.     print(calId)
  47.  
  48.     try:
  49.         resCalendar = service.calendars().get(calendarId=calId).execute()
  50.     except:
  51.         newCal = {}
  52.         newCal["timeZone"] = "Europe/Rome"
  53.         newCal["summary"] = str(calId.split(" ", 1)[0])
  54.         newCal["kind"] = "calendar#calendar"
  55.         newCal["id"] = str(calId.replace(" ", ""))
  56.         newCal["etag"] = str(hashlib.md5(bencode.bencode(newCal)).hexdigest())
  57.         #print(newCal["etag"])
  58.  
  59.         newCal = json.dumps(newCal)
  60.         res = service.calendars().insert(body=newCal).execute()
  61.     #print(resCalendar)
  62.  
  63.     """if not events:
  64.        print('No upcoming events found.')
  65.    for event in events:
  66.        start = event['start'].get('dateTime', event['start'].get('date'))
  67.        print(start, event['summary'])"""
  68.  
  69.  
  70. if __name__ == '__main__':
  71.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement