Skip to content

Commit

Permalink
Merge pull request #14 from azai91/create-files
Browse files Browse the repository at this point in the history
Create files
  • Loading branch information
azai91 committed Apr 23, 2016
2 parents 1f47416 + f3cf781 commit bc88992
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 11 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 :)
42 changes: 39 additions & 3 deletions src/config.py
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -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',
Expand Down
50 changes: 47 additions & 3 deletions src/drive_api.py
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -29,7 +29,6 @@ def exchange_tokens(cls, code):
Store tokens in workflow
"""


response = requests.post(TOKEN_URL, {
'code': code,
'client_id' : CLIENT_ID,
Expand Down Expand Up @@ -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
Expand All @@ -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')

Expand All @@ -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"""
Expand Down Expand Up @@ -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']



8 changes: 8 additions & 0 deletions src/drive_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
File renamed without changes
Binary file added src/icons/form.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/icons/pdf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
2 changes: 1 addition & 1 deletion src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
12 changes: 9 additions & 3 deletions src/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1
1.2

0 comments on commit bc88992

Please sign in to comment.