I'm feeling both irritated and dumb right now, so hoping somebody can point me to where I'm going wrong, because I'm sure it's something obvious.
I'm trying to create a workflow which displays the Rooms available to me through Google Calendar.
My keyword is 'rooms' and my argument is a number (15, 30 or 60). The workflow should return/display the rooms which are not booked for that number of minutes.
I taught myself Python (badly...), figured out OAuth, figured out how to return and parse the busy/free results from Google's Cal API and figured out how to name my rooms to return/print the room name. Where I'm stuck, is returning the room names to Alfred.
I've read through the examples and documentation of Workflow, so I am trying, but pretty much stuck.
Right now, when I type in "Rooms", hit spacebar and type in my first number, it just pushes me back to the default google search.
I know my code sucks, it's a hacky copy paste and there will be a LOT wrong with it. I'm not looking for comments on my awful python skills, but am looking to understand why it's not returning results.
What am I missing??
#!/usr/bin/python
# encoding: utf-8
from __future__ import print_function, unicode_literals
import httplib2
import os
import json
import sys
import oauth2client
from pprint import pprint
from workflow import Workflow, web
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from datetime import datetime, timedelta
log = None
SCOPES = 'https://www.googleapis.com/auth/calendar.readonly'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Google Calendar API Python Quickstart'
ROOM_IDS = {"someroom" : "somename", "someroom" : "somename", "someroom" : "somename", "someroom" : "somename"}
def get_credentials():
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 = 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 compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def main(wf):
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('calendar', 'v3', http=http)
args = wf.args[0]
starting = datetime.utcnow().isoformat() + 'Z'
ending = (datetime.utcnow() + timedelta(minutes=args).isoformat() + 'Z'
freebusy_query = {"timeMin" : starting, "timeMax" : ending, "items" : [{
"id": "someroom"
},
{
"id": "someroom"
},
{
"id": "someroom"
},
{
"id": "someroom"
}]}
Result = service.freebusy().query(body=freebusy_query).execute()
rooms = Result.get('calendars', [])
rooms.raise_for_status()
for room in rooms:
busy = rooms[room]
for times in busy:
listing = busy[times]
if listing == []:
wf.add_item(title=ROOM_IDS[room])
wf.send_feedback()
if __name__ == '__main__':
wf = Workflow()
log = wf.logger
sys.exit(wf.run(main))