namkuner commited on
Commit
bf36b8e
·
verified ·
1 Parent(s): 5bf6e43

Upload 2 files

Browse files
Files changed (2) hide show
  1. Google.py +116 -0
  2. client_secret.json +1 -0
Google.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pickle
2
+ import os
3
+ import datetime
4
+ from collections import namedtuple
5
+ from google_auth_oauthlib.flow import Flow, InstalledAppFlow
6
+ from googleapiclient.discovery import build
7
+ from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
8
+ from google.auth.transport.requests import Request
9
+
10
+
11
+ def Create_Service(client_secret_file, api_name, api_version, *scopes, prefix=''):
12
+ CLIENT_SECRET_FILE = client_secret_file
13
+ API_SERVICE_NAME = api_name
14
+ API_VERSION = api_version
15
+ SCOPES = [scope for scope in scopes[0]]
16
+
17
+ cred = None
18
+ working_dir = os.getcwd()
19
+ token_dir = 'token files'
20
+ pickle_file = f'token_{API_SERVICE_NAME}_{API_VERSION}{prefix}.pickle'
21
+
22
+ ### Check if token dir exists first, if not, create the folder
23
+ if not os.path.exists(os.path.join(working_dir, token_dir)):
24
+ os.mkdir(os.path.join(working_dir, token_dir))
25
+
26
+ if os.path.exists(os.path.join(working_dir, token_dir, pickle_file)):
27
+ with open(os.path.join(working_dir, token_dir, pickle_file), 'rb') as token:
28
+ cred = pickle.load(token)
29
+
30
+ if not cred or not cred.valid:
31
+ if cred and cred.expired and cred.refresh_token:
32
+ cred.refresh(Request())
33
+ else:
34
+ flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
35
+ cred = flow.run_local_server()
36
+
37
+ with open(os.path.join(working_dir, token_dir, pickle_file), 'wb') as token:
38
+ pickle.dump(cred, token)
39
+
40
+ try:
41
+ service = build(API_SERVICE_NAME, API_VERSION, credentials=cred)
42
+ print(API_SERVICE_NAME, API_VERSION, 'service created successfully')
43
+ return service
44
+ except Exception as e:
45
+ print(e)
46
+ print(f'Failed to create service instance for {API_SERVICE_NAME}')
47
+ os.remove(os.path.join(working_dir, token_dir, pickle_file))
48
+ return None
49
+
50
+
51
+ def convert_to_RFC_datetime(year=1900, month=1, day=1, hour=0, minute=0):
52
+ dt = datetime.datetime(year, month, day, hour, minute, 0).isoformat() + 'Z'
53
+ return dt
54
+
55
+
56
+ class GoogleSheetsHelper:
57
+ # --> spreadsheets().batchUpdate()
58
+ Paste_Type = namedtuple('_Paste_Type',
59
+ ('normal', 'value', 'format', 'without_borders',
60
+ 'formula', 'date_validation', 'conditional_formatting')
61
+ )('PASTE_NORMAL', 'PASTE_VALUES', 'PASTE_FORMAT', 'PASTE_NO_BORDERS',
62
+ 'PASTE_FORMULA', 'PASTE_DATA_VALIDATION', 'PASTE_CONDITIONAL_FORMATTING')
63
+
64
+ Paste_Orientation = namedtuple('_Paste_Orientation', ('normal', 'transpose'))('NORMAL', 'TRANSPOSE')
65
+
66
+ Merge_Type = namedtuple('_Merge_Type', ('merge_all', 'merge_columns', 'merge_rows')
67
+ )('MERGE_ALL', 'MERGE_COLUMNS', 'MERGE_ROWS')
68
+
69
+ Delimiter_Type = namedtuple('_Delimiter_Type', ('comma', 'semicolon', 'period', 'space', 'custom', 'auto_detect')
70
+ )('COMMA', 'SEMICOLON', 'PERIOD', 'SPACE', 'CUSTOM', 'AUTODETECT')
71
+
72
+ # --> Types
73
+ Dimension = namedtuple('_Dimension', ('rows', 'columns'))('ROWS', 'COLUMNS')
74
+
75
+ Value_Input_Option = namedtuple('_Value_Input_Option', ('raw', 'user_entered'))('RAW', 'USER_ENTERED')
76
+
77
+ Value_Render_Option = namedtuple('_Value_Render_Option', ["formatted", "unformatted", "formula"]
78
+ )("FORMATTED_VALUE", "UNFORMATTED_VALUE", "FORMULA")
79
+
80
+ @staticmethod
81
+ def define_cell_range(
82
+ sheet_id,
83
+ start_row_number=1, end_row_number=0,
84
+ start_column_number=None, end_column_number=0):
85
+ """GridRange object"""
86
+ json_body = {
87
+ 'sheetId': sheet_id,
88
+ 'startRowIndex': start_row_number - 1,
89
+ 'endRowIndex': end_row_number,
90
+ 'startColumnIndex': start_column_number - 1,
91
+ 'endColumnIndex': end_column_number
92
+ }
93
+ return json_body
94
+
95
+ @staticmethod
96
+ def define_dimension_range(sheet_id, dimension, start_index, end_index):
97
+ json_body = {
98
+ 'sheetId': sheet_id,
99
+ 'dimension': dimension,
100
+ 'startIndex': start_index,
101
+ 'endIndex': end_index
102
+ }
103
+ return json_body
104
+
105
+
106
+ class GoogleCalendarHelper:
107
+ ...
108
+
109
+
110
+ class GoogleDriverHelper:
111
+ ...
112
+
113
+
114
+ if __name__ == '__main__':
115
+ g = GoogleSheetsHelper()
116
+ print(g.Delimiter_Type)
client_secret.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"installed":{"client_id":"529655390517-hetu3lc90fhqofavkdvt6v49fn9spm4i.apps.googleusercontent.com","project_id":"ghostfacenets-429308","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"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","redirect_uris":["http://localhost"]}}