Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support of Google Drive folder #7

Merged
merged 1 commit into from
Jun 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
[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