diff --git a/README.md b/README.md index 726d1c3..c21a160 100644 --- a/README.md +++ b/README.md @@ -31,4 +31,24 @@ Responses from Google Drive API are cached to increase the speed. Use this optio - ```d > set cache [seconds]``` Sets the length of the duration for how long responses are held in cache before a fresh request is made. Default is 3600 seconds (1 hour) +- ```d > Create Google Doc``` +Create Google Doc and opens in default browser + +- ```d > Create Google Sheet``` +Create Google Sheet and opens in default browser + +- ```d > Create Google Slide``` +Create Google Slide and opens in default browser + +- ```d > Create Google Form``` +Create Google Form and opens in default browser + +##Supported files types + +- Google Docs +- Google Sheets +- Google Slides +- Google Forms +- PDFs + ###Please leave issues if you encounter any problems or star this repo if you found it useful :) diff --git a/src/config.py b/src/config.py index eabb04e..ead3f8d 100644 --- a/src/config.py +++ b/src/config.py @@ -1,18 +1,27 @@ - +import urllib from workflow import ICON_ACCOUNT, ICON_EJECT, ICON_WARNING, ICON_SYNC, ICON_CLOCK CLIENT_ID = '978117856621-tvpnqtr02b8u0bgnh75sqb1loq1f5527.apps.googleusercontent.com' CLIENT_SECRET = 'rty2NIATZfWFWSDX-XPs2usX' -SCOPE = 'https://www.googleapis.com/auth/drive.readonly' +SCOPE = 'https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.appdata https://www.googleapis.com/auth/drive.file' +FILTER = urllib.quote("mimeType='application/vnd.google-apps.document' or mimeType='application/vnd.google-apps.spreadsheet' or mimeType='application/vnd.google-apps.presentation' or mimeType='application/vnd.google-apps.form' or mimeType='application/pdf'") REDIRECT_URI = 'http://127.0.0.1:1337' AUTH_URL = 'https://accounts.google.com/o/oauth2/auth?scope=%(scope)s&redirect_uri=%(redirect_uri)s&response_type=code&client_id=%(client_id)s&access_type=offline&approval_prompt=force' % {'scope' : SCOPE, 'redirect_uri' : REDIRECT_URI, 'client_id' : CLIENT_ID} TOKEN_URL = 'https://www.googleapis.com/oauth2/v3/token' -FILES_URL = 'https://www.googleapis.com/drive/v2/files?maxResults=1000&q=mimeType%3D%22application%2Fvnd.google-apps.document%22+or+mimeType%3D%22application%2Fvnd.google-apps.spreadsheet%22+or+mimeType%3D%22application%2Fvnd.google-apps.presentation%22&fields=items,nextPageToken' +FILES_URL = 'https://www.googleapis.com/drive/v2/files?maxResults=1000&q=%(filter)s&fields=items,nextPageToken' % {'filter' : FILTER} +CREATE_URL = 'https://www.googleapis.com/drive/v3/files?fields=webViewLink' CACHE_MAX_AGE = 60*60*24*30 # cache set to 1 month +MIMETYPES = { + 'DOC' : 'application/vnd.google-apps.document', + 'SHEET' : 'application/vnd.google-apps.spreadsheet', + 'SLIDE' : 'application/vnd.google-apps.presentation', + 'FORM' : 'application/vnd.google-apps.form' +} + SETTINGS = { 'LOGIN' : { 'title' : 'Login', @@ -40,6 +49,33 @@ } } +CREATE_SETTINGS = { + 'DOC' : { + 'title' : 'Create Google Doc', + 'autocomplete' : '> Create Google Doc', + 'arg' : 'create_doc', + 'icon' : './icons/doc.png' + }, + 'SHEET' : { + 'title' : 'Create Google Sheet', + 'autocomplete' : '> Create Google Sheet', + 'arg' : 'create_sheet', + 'icon' : './icons/sheet.png' + }, + 'SLIDE' : { + 'title' : 'Create Google Slide', + 'autocomplete' : '> Create Google Slide', + 'arg' : 'create_slide', + 'icon' : './icons/slide.png' + }, + 'FORM' : { + 'title' : 'Create Google Form', + 'autocomplete' : '> Create Google Form', + 'arg' : 'create_form', + 'icon' : './icons/form.png' + } +} + OPTIONS = [ { 'title' : 'Search Google Drive', diff --git a/src/drive_api.py b/src/drive_api.py index b4d1f66..4f83b62 100644 --- a/src/drive_api.py +++ b/src/drive_api.py @@ -1,10 +1,10 @@ import json import subprocess -from config import CLIENT_ID, CLIENT_SECRET, SCOPE, REDIRECT_URI, FILES_URL, AUTH_URL, TOKEN_URL, TOKEN_URL, CACHE_MAX_AGE, ERRORS, OPTIONS, SETTINGS +from config import * import requests import util from time import sleep -from workflow import Workflow, ICON_EJECT, ICON_ACCOUNT, ICON_BURN, ICON_CLOCK, ICON_WARNING, PasswordNotFound +from workflow import * from workflow.background import is_running, run_in_background UPDATE_SETTINGS = {'github_slug' : 'azai91/alfred-drive-workflow'} HELP_URL = 'https://github.com/azai91/alfred-drive-workflow/issues' @@ -29,7 +29,6 @@ def exchange_tokens(cls, code): Store tokens in workflow """ - response = requests.post(TOKEN_URL, { 'code': code, 'client_id' : CLIENT_ID, @@ -137,6 +136,7 @@ def show_options(cls): for option in OPTIONS: wf.add_item(title=option['title'], autocomplete=option['autocomplete']) + wf.send_feedback() @classmethod @@ -157,6 +157,19 @@ def show_settings(cls, user_input): if 'set cache length'.startswith(user_input[:16].lower()): cls.show_set_cache_length(user_input[17:]) + # if account is already set + try: + wf.get_password('drive_access_token') + if 'create google doc'.startswith(user_input.lower()): + cls.show_create_setting('DOC') + if 'create google sheet'.startswith(user_input.lower()): + cls.show_create_setting('SHEET') + if 'create google slide'.startswith(user_input.lower()): + cls.show_create_setting('SLIDE') + if 'create google form'.startswith(user_input.lower()): + cls.show_create_setting('FORM') + except: pass + if len(wf._items) == 0: cls.show_error('InvalidOption') @@ -172,6 +185,17 @@ def show_setting(cls, setting): autocomplete=SETTINGS[setting]['autocomplete'], valid=True) + @classmethod + def show_create_setting(cls, setting): + """Show settings""" + + wf.add_item(title=CREATE_SETTINGS[setting]['title'], + arg=CREATE_SETTINGS[setting]['arg'], + icon=CREATE_SETTINGS[setting]['icon'], + autocomplete=CREATE_SETTINGS[setting]['autocomplete'], + valid=True) + + @classmethod def show_set_cache_length(cls, length): """Show set cache length setting""" @@ -228,3 +252,23 @@ def add_items(cls, links): icon=icon, valid=True) + @classmethod + def create_file(cls, type): + """ + Returns: + a list of all spreadsheets and documents from Google Drive + """ + + access_token = wf.get_password('drive_access_token') + headers = { + 'Authorization' : 'Bearer %s' % access_token, + 'Content-Type': 'application/json' + } + response = requests.post(CREATE_URL, headers=headers, data=json.dumps({ + 'mimeType' : MIMETYPES[type] + })).json() + + return response['webViewLink'] + + + diff --git a/src/drive_launcher.py b/src/drive_launcher.py index e67cf7a..401e109 100644 --- a/src/drive_launcher.py +++ b/src/drive_launcher.py @@ -18,6 +18,14 @@ def main(wf): elif url == 'clear': Drive.clear_cache() return sys.stdout.write('cache cleared') + elif url == 'create_doc': + url = Drive.create_file('DOC') + elif url == 'create_sheet': + url = Drive.create_file('SHEET') + elif url == 'create_slide': + url = Drive.create_file('SLIDE') + elif url == 'create_form': + url = Drive.create_file('FORM') elif url.startswith('set'): length = int(url[3:]) Drive.set_cache_length(length) diff --git a/src/icons/docs.png b/src/icons/doc.png similarity index 100% rename from src/icons/docs.png rename to src/icons/doc.png diff --git a/src/icons/form.png b/src/icons/form.png new file mode 100644 index 0000000..11b5612 Binary files /dev/null and b/src/icons/form.png differ diff --git a/src/icons/pdf.png b/src/icons/pdf.png new file mode 100644 index 0000000..8fd2d28 Binary files /dev/null and b/src/icons/pdf.png differ diff --git a/src/icons/sheets.png b/src/icons/sheet.png similarity index 100% rename from src/icons/sheets.png rename to src/icons/sheet.png diff --git a/src/icons/slides.png b/src/icons/slide.png similarity index 100% rename from src/icons/slides.png rename to src/icons/slide.png diff --git a/src/server.py b/src/server.py index c9afcfc..59b03b1 100644 --- a/src/server.py +++ b/src/server.py @@ -18,7 +18,7 @@ def do_GET(s): subprocess.call(['python','./drive_refresh.py']) sys.stdout.write('Account Saved') logging.info('Account Saved') - # s.wfile.write('Your code has been saved in Alfred') + s.wfile.write('Your code has been saved in Alfred') except: s.wfile.write('Error with setting code') diff --git a/src/util.py b/src/util.py index f38fb26..0de7303 100644 --- a/src/util.py +++ b/src/util.py @@ -16,9 +16,15 @@ def internet_on(): def find_icon(link): if link['mimeType'] == 'application/vnd.google-apps.document': - icon = './icons/docs.png' + icon = './icons/doc.png' elif link['mimeType'] == 'application/vnd.google-apps.spreadsheet': - icon = './icons/sheets.png' + icon = './icons/sheet.png' elif link['mimeType'] == 'application/vnd.google-apps.presentation': - icon = './icons/slides.png' + icon = './icons/slide.png' + elif link['mimeType'] == 'application/vnd.google-apps.form': + icon = './icons/form.png' + elif link['mimeType'] == 'application/pdf': + icon = './icons/pdf.png' + else: + icon = './icons/pdf.png' return icon diff --git a/src/version b/src/version index b123147..ea710ab 100644 --- a/src/version +++ b/src/version @@ -1 +1 @@ -1.1 \ No newline at end of file +1.2 \ No newline at end of file