Skip to content

Commit

Permalink
Merge pull request #4 from vscg/master
Browse files Browse the repository at this point in the history
Functionality to add shortcut to drive files
  • Loading branch information
matAlmeida committed Jan 29, 2021
2 parents 66d16ab + f621c78 commit b9ea9d4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
26 changes: 25 additions & 1 deletion pygdrive3/service.py
Expand Up @@ -5,7 +5,7 @@
from oauth2client import file, client, tools
import mimetypes
import os

import re

class DriveService:
def __init__(self, client_secret):
Expand Down Expand Up @@ -33,6 +33,10 @@ def auth(self):
flow = client.flow_from_clientsecrets(self.client_secret, SCOPES)
creds = tools.run_flow(flow, store)
self.drive_service = build('drive', 'v3', http=creds.authorize(Http()))

def getIdFromUrl(self, url):
regex = "(?<=/folders/)([\w-]+)|(?<=%2Ffolders%2F)([\w-]+)|(?<=/file/d/)([\w-]+)|(?<=%2Ffile%2Fd%2F)([\w-]+)|(?<=id=)([\w-]+)|(?<=id%3D)([\w-]+)"
return re.search(regex,url)

def create_folder(self, name, parent_id=None):
if parent_id != None:
Expand All @@ -54,6 +58,26 @@ def create_folder(self, name, parent_id=None):

return folder.get('id')

def add_file_shortcut(self, file_id, file_name = '', folder_id = ''):
mime_type = 'application/vnd.google-apps.shortcut'

shortcut_metadata = {
'mimeType': mime_type,
'shortcutDetails': {
'targetId': file_id
}
}

if(len(file_name) > 0):
shortcut_metadata['name'] = file_name

if(len(folder_id) > 0):
shortcut_metadata['parents'] = [folder_id]

shortcut = self.drive_service.files().create(body=shortcut_metadata, fields='id,shortcutDetails').execute()

return shortcut.get('id')

def upload_file(self, name, file_path, folder_id, mime_type = None):
fileType = mime_type
if(mime_type == None):
Expand Down
12 changes: 12 additions & 0 deletions test/addMultipleFileShortcuts.py
@@ -0,0 +1,12 @@
from context import service

drive_service = service.DriveService('.\credentials.json')
drive_service.auth()

fileURLs = open('fileURLs.txt')

for url in fileURLs:
match = drive_service.getIdFromUrl(url)
if match:
shortcut = drive_service.add_file_shortcut(file_id = match[0], folder_id = '')
print(shortcut)

0 comments on commit b9ea9d4

Please sign in to comment.