dsfcraig Posted November 14, 2015 Share Posted November 14, 2015 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)) Link to comment
deanishe Posted November 15, 2015 Share Posted November 15, 2015 No closing quote: Don't forget about Alfred's debugger. Seeing as you're using Alfred-Workflow, you can also use the query workflow:openlog to view the log file (which will contain any errors in your Python scripts). Link to comment
dsfcraig Posted November 15, 2015 Author Share Posted November 15, 2015 Thanks deanish. I got trigger happy with the screenshot and noticed I had accidentally deleted the end quote That said, turned out not to be the problem and the debugger absolutely helped. Immediately it threw errors for my import python packages. Turns out that I needed to have the correct packages installed in the workflow dir (http://alfredworkflow.readthedocs.org/en/latest/user-manual/third-party.html). Fixed that and now it all works great! Thanks for your help. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now