Skip to content

Instantly share code, notes, and snippets.

@bastibl
Created March 21, 2017 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bastibl/dc8150339298914a7e61e326870ad136 to your computer and use it in GitHub Desktop.
Save bastibl/dc8150339298914a7e61e326870ad136 to your computer and use it in GitHub Desktop.
GR Calendar
#!/usr/bin/env python
from __future__ import print_function
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
import datetime
import httplib2
import json
import os
import re
import urllib2
hdr = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept-Encoding': 'none',
}
request = urllib2.Request('http://gnuradio.org/events-calendar/', headers=hdr)
response = urllib2.urlopen(request)
html = response.read()
event_string = "["
add = False
for l in html.splitlines(True):
if add:
event_string += l
match = re.search("^\s*events: \[", l)
if match:
add = True
match = re.search("^\s*\]\s*", l)
if add and match:
add = False
event_string = " ".join(event_string.split())
event_string = re.sub(r", (\]|\})", r"\1", event_string)
event_string = re.sub(r"\s(\w*):", r'"\1":', event_string)
event_string = re.sub(r"'", r'"', event_string)
#print(event_string)
events = json.loads(event_string)
# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/calendar-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/calendar'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'api'
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-python-quickstart.json')
store = 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
credentials = tools.run_flow(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('calendar', 'v3', http=http)
for e in events:
start = e['start'].split('/')
start, = '%s-%s-%s' % (start[2], start[0], start[1]),
end = e['end'].split('/')
end1 = datetime.date(int(end[2]), int(end[0]), int(end[1]))
end1 = end1 + datetime.timedelta(days=1)
end1 = end1.strftime("%Y-%m-%d")
end, = '%s-%s-%s' % (end[2], end[0], end[1]),
if start == end:
end = end1
eventsResult = service.events().list(
calendarId='pt7g8c2vj2unimlhgq8pmjp7cs@group.calendar.google.com', timeMin=start+"T00:00:00.000Z", timeMax=end+"T00:00:00.000Z").execute()
existing_events = eventsResult.get('items', [])
for event in existing_events:
if event['summary'] == e['title']:
print("Event already exists: %s" % e['title'])
break
else:
event = {
'summary': e['title'],
'location': e['location'],
'htmlLink': e['link'],
'start': {
'date': start
},
'end': {
'date': end
},
}
print(event)
event = service.events().insert(calendarId='pt7g8c2vj2unimlhgq8pmjp7cs@group.calendar.google.com', body=event).execute()
print('Event created: %s' % (event.get('htmlLink')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment