Skip to content

Commit

Permalink
Merge pull request #7 from windhamwong/master
Browse files Browse the repository at this point in the history
Adding support of Google Drive folder
  • Loading branch information
niqdev authored Jun 12, 2016
2 parents 45682bb + c0266ce commit c52959c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/prod_example.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ drive.oauth2_scope=https://www.googleapis.com/auth/drive
drive.client_secrets=config/client_secrets.json
drive.auth_token=config/auth_token.json
drive.gmail[email protected]
drive.default_folder=packtpub

[notify]
notify.host=smtp.gmail.com
Expand Down
39 changes: 37 additions & 2 deletions script/drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,54 @@ def __save_credentials(self, auth_token):
storage.put(credentials)
log_info('[+] new credentials saved')

def __create_folder(self): #Create folder with provided name
try: #Check default folder name
default_folder_name = self.__config.get('drive', 'drive.default_folder')
except:
default_folder_name = 'packtpub'

metadata = {
'title': default_folder_name,
'mimeType' : 'application/vnd.google-apps.folder'
}
folder = self.__drive_service.files().insert(body = metadata).execute()
self.__config.set('drive', 'drive.upload_folder', folder['id'])
log_success('[+] creating new directory...')
print '[+] updating folder permissions...'
permissions = {
'role': 'reader',
'type': 'anyone',
'value': self.__config.get('drive', 'drive.gmail')
}
self.__drive_service.permissions().insert(fileId=folder['id'], body=permissions).execute()
log_dict({'folder_name': default_folder_name,
'id': folder['id'],})
#'share_link': folder['webContentLink']}) #TODO Fix
log_success('[+] Please add this line after [drive] in your configuration file:')
log_info('drive.upload_folder=' + folder.get('id'))

return folder.get('id') #Return folder object ID

def __get_folder(self): #Get folder name settings
try:
return self.__config.get('drive', 'drive.upload_folder')
except:
return self.__create_folder() #new folder ID

def __insert_file(self):
print '[+] uploading file...'
media_body = MediaFileUpload(
self.info['path'], mimetype=self.info['mime_type'], resumable=True)
body = {
'title': self.info['name'],
'description': 'uploaded with packtpub-crawler',
'mimeType': self.info['mime_type']
'mimeType': self.info['mime_type'],
'parents': [{'id': self.__get_folder()}]
}
file = self.__drive_service.files().insert(body=body, media_body=media_body).execute()
# log_dict(file)

print '\b[+] updating file permissions...'
print '[+] updating file permissions...'
permissions = {
'role': 'reader',
'type': 'anyone',
Expand Down

0 comments on commit c52959c

Please sign in to comment.