-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmaker.py
112 lines (94 loc) · 3.83 KB
/
gmaker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""
This class establishes a connection with the Google API,
create a dir structure and create the file on it.
"""
import os
import mimetypes
import re
import boto3
from google.oauth2 import service_account
import googleapiclient.discovery
import googleapiclient.errors
from googleapiclient.http import MediaFileUpload
import datetime
class GDocsMaker():
def __init__(self, company_name, cred_bucket_name, cred_file_name, parent_folder_id):
"""
@var company_name: the name of the company
@var cred_bucket_name: the name of the bucket where the GAPI credential file is stored
@var cred_file_name: the name of the GAPI credential file
@var parent_folder_id: the parent folder id where the dir structure will be created.
"""
self.__folder_struct = self.__get_folder_struct(company_name)
self.__cred_file_name = cred_file_name
self.__parent_folder_id = parent_folder_id
self.__download_credential_file(cred_bucket_name)
def __get_folder_struct(self, company_name):
"""
Retrive all the elements which compose the
dir structure
"""
return [
company_name.lower().title(),
str(datetime.datetime.now().year),
datetime.datetime.now().strftime("%B")
]
def __get_service(self):
"""
Establish a connection with the Google API
and authorize the access to the Google Drive APIs
"""
scopes = [
'https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/drive.metadata.readonly']
service_account_file = '/tmp/'+self.__cred_file_name
credentials = service_account.Credentials.from_service_account_file(
service_account_file, scopes=scopes
)
return googleapiclient.discovery.build('drive', 'v3', credentials=credentials, cache_discovery=False)
def __download_credential_file(self, cred_bucket_name):
"""
Download the Google API Credential file from the
remote S3 bucket and stored in a local folder
"""
client = boto3.client('s3')
with open('/tmp/'+self.__cred_file_name, 'wb') as file:
client.download_fileobj(cred_bucket_name, self.__cred_file_name, file)
def __create_folder(self, folder, folder_id):
"""
Create the directory structure
"""
parent = self.__get_service().files().list(q="name='{}' and '{}' in parents".format(folder, folder_id), fields='files(id, name)').execute()
if not parent['files']:
file_metadata = {
'name': folder,
'mimeType':'application/vnd.google-apps.folder',
'parents':[folder_id]
}
file = self.__get_service().files().create(body=file_metadata,fields='id').execute()
return file.get('id')
else:
return parent['files'][0]['id']
def __upload(self, file, parent_id):
"""
Upload the file in the directory structured created
on the Google Drive
"""
mimetypes.init()
mime_type = mimetypes.guess_type(file)
filename = re.sub(r'\/tmp\/', '', file)
if mime_type[0] is not None:
file_metadata = {
'name':filename,
'parents':[parent_id]
}
media = MediaFileUpload(file, mimetype=mime_type[0])
uploaded_file = self.__get_service().files().create(body=file_metadata, media_body=media, fields='id').execute()
return uploaded_file.get('id')
def archive(self, file):
"""
Put all together
"""
folder_id = self.__parent_folder_id
for folder in self.__folder_struct:
folder_id = self.__create_folder(folder, folder_id)
self.__upload(file, folder_id)