File size: 13,660 Bytes
c87f53a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import pickle
import os
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from google.auth.transport.requests import Request
import json
from googleapiclient.http import MediaFileUpload, MediaIoBaseUpload
from datetime import datetime, timedelta
import gdown
from token_operation.driveToken import Create_Drive_Token

global API_VERSION, CLIENT_FILE_NAME, API_DRIVE, SCOPES 
CLIENT_FILE_NAME = "./client_secret.json"
API_DRIVE = "drive"
API_VERSION = "v3"
SCOPES = ["https://www.googleapis.com/auth/drive"]

file_metadata = {
    "name": "Token_Folder",
    "mimeType": "application/vnd.google-apps.folder",
}

# ///////////////////////// CHECK THE PATH OF TOKEN FOLDER /////////////////////////////////
def Check_Token_Folder(service, path="root", Folder_Name="Token"):
    resource = service.files()
    result = resource.list(
        q=f"mimeType = 'application/vnd.google-apps.folder' and '{path}' in parents",
        fields="nextPageToken, files(id, name)",
    ).execute()
    list_folders = result.get("files")
    token_folder_id = None
    for folder in list_folders:
        if folder["name"] == Folder_Name:
            token_folder_id = folder["id"]
            break
    if not token_folder_id:
        token_folder = service.files().create(body=file_metadata, fields="id").execute()
        token_folder_id= token_folder["id"]

        result_folder = (
            service.files()
            .create(
                body={
                    "name": Folder_Name,
                    "mimeType": "application/vnd.google-apps.folder",
                    "parents": [token_folder_id],
                    "type": "anyone",
                    "role": "reader",
                },
                fields="id",
            )
            .execute()
        )
        token_folder_id = result_folder["id"]

    return token_folder_id


def Check_Token_Main_Folder(path="root",Folder_Name="Token_Folder"):
    service = Create_Drive_Token()
    resource = service.files()
    result = resource.list(
        q=f"mimeType = 'application/vnd.google-apps.folder' and '{path}' in parents",
        fields="nextPageToken, files(id, name)",
    ).execute()

    list_folders = result.get("files")
    token_main_folder_id = None
    token_folder_id = None

    for folder in list_folders:
        if folder["name"] == Folder_Name:
            token_main_folder_id = folder["id"]
            token_folder_id = Check_Token_Folder(service, path=token_main_folder_id, Folder_Name="Token") 
            break

    if not token_main_folder_id:
        token_main_folder = service.files().create(body=file_metadata, fields="id").execute()
        token_main_folder_id= token_main_folder["id"]

        result_folder = (
            service.files()
            .create(
                body={
                    "name": "Token",
                    "mimeType": "application/vnd.google-apps.folder",
                    "parents": [token_main_folder_id],
                    "type": "anyone",
                    "role": "reader",
                },
                fields="id",
            )
            .execute()
        )
        token_folder_id = result_folder["id"]
        permission = {
            'type': 'anyone',
            'role': 'reader'
        }
        service.permissions().create(fileId=token_folder_id, body=permission).execute()

    return token_main_folder_id, token_folder_id

def Create_Token(name):
    CLIENT_SECRET_FILE = CLIENT_FILE_NAME
    API_SERVICE_NAME = API_DRIVE
    # API_VERSION = API_VERSION
    # SCOPES = SCOPES

    cred = None
    pickle_file = f'{name}.pickle'

    if os.path.exists(pickle_file):
        with open(pickle_file, 'rb') as token:
            cred = pickle.load(token)

    if not cred or not cred.valid:
        if cred and cred.expired and cred.refresh_token:
            cred.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
            cred = flow.run_local_server()

        with open(pickle_file, 'wb') as token:
            pickle.dump(cred, token)

    try:
        # service = build(API_SERVICE_NAME, API_VERSION, credentials=cred)
        return cred
    except Exception as e:
        print('Unable to connect.')
        print(e)
        return None
    
# ///////////////////////////// FROM HERE IT STARTS //////////////////////////////////

def Create_Token_Drive(name="current"):
    service = Create_Drive_Token()
    token_main_folder_id, token_folder_id = Check_Token_Main_Folder()
    try:
        response = (
            service.files()
        .list(
            q=f"'{token_main_folder_id}' in parents and name contains 'token_json'",
            spaces="drive",
            fields="files(id, name)",
        )
            .execute()
        )
        list_all_files = response.get("files", [])  
        is_selected = False
        file_name = ""
        for list_files in list_all_files:
            print("List Files = ", list_files["name"])
            if list_files["name"].startswith("token_json"):
                latest_file_id = str(list_files["id"])
                existing_data = service.files().get_media(fileId=latest_file_id).execute()
                if existing_data:  
                    existing_details = json.loads(existing_data.decode("utf-8"))
                    with open(list_files["name"], "w", encoding="utf-8") as json_file:
                        json.dump(existing_details, json_file, ensure_ascii=False)
                    if existing_details.get(name):
                        is_selected = True
                        token_id = existing_details[name]["token_file_id"]
                        url = f"https://drive.google.com/file/d/{token_id}/view?usp=sharing"
                        output = "current_token.pickle"
                        gdown.download(url=url, output=output, fuzzy=True)
                        with open(output, "rb") as cred:
                            creds = pickle.load(cred)
                            service = build(API_DRIVE, API_VERSION, credentials=creds)
                        os.remove(output)
                        return service, existing_details[name]["token_name"]
                    
                    else:
      
                        is_selected = True
                        token_file = Create_Token(name)
                        token_file_path = f'{name}.pickle' 
                        file_metadata = {"name": name, "parents": [token_folder_id]}
                        media = MediaFileUpload(token_file_path, mimetype="application/pdf")
                        newFile = (
                            service.files()
                            .create(body=file_metadata, media_body=media, fields="id")
                            .execute()
                        )
                        token_id = newFile.get("id")
                        expiry_time = datetime.now() + timedelta(hours=24)
                        existing_details[name]={
                            "token_name": name,
                            "token_file_id": token_id,
                            "expires_at": expiry_time.strftime("%Y-%m-%dT%H:%M:%SZ")
                        }
                        file_name = list_files["name"] 
                        with open(file_name, "w", encoding="utf-8") as json_file:
                            json.dump(existing_details, json_file)
                        json_file.close()
                        file_metadata = {"name": file_name, "parents": [token_main_folder_id]}
                        media = MediaFileUpload(file_name, mimetype="application/json")
                        service.files().update(fileId=latest_file_id, media_body=media).execute()
                        token_id = existing_details[name]["token_file_id"]
                        url = f"https://drive.google.com/file/d/{token_id}/view?usp=sharing"
                        output = "current_token.pickle"
                        gdown.download(url=url, output=output, fuzzy=True)
                        with open(output, "rb") as cred:
                            creds = pickle.load(cred)
                            service = build(API_DRIVE, API_VERSION, credentials=creds)
                        os.remove(output)
                        os.remove(token_file_path)
                        return service, existing_details[name]["token_name"]

        if not is_selected:
            token_file = Create_Token(name)
            file_metadata = {"name": name, "parents": [token_folder_id]}
            token_file_path = f'{name}.pickle' 
            media = MediaFileUpload(token_file_path, mimetype="application/pdf")
            # media = MediaFileUpload(token_file, mimetype="application/pdf")
            newFile = (
                service.files()
                .create(body=file_metadata, media_body=media, fields="id")
                .execute()
            )
            token_id = newFile.get("id")
            expiry_time = datetime.now() + timedelta(hours=24)
            existing_details = {}
            existing_details[name]={
                "token_name": name,
                "token_file_id": token_id,
                "expires_at": expiry_time.strftime("%Y-%m-%dT%H:%M:%SZ")
            }
            file_name = f"token_json.json"
            json_data = json.dumps(existing_details)
            with open(file_name, "w") as f:
                f.write(json_data)
            file_metadata = {"name": file_name, "parents": [token_main_folder_id]}
            media = MediaFileUpload(
                file_name, mimetype="application/json", resumable=True
            )
            service.files().create(
                body=file_metadata, media_body=media, fields="id"
            ).execute()
            token_id = existing_details[name]["token_file_id"]
            url = f"https://drive.google.com/file/d/{token_id}/view?usp=sharing"
            output = "current_token.pickle"
            gdown.download(url=url, output=output, fuzzy=True)

            with open(output, "rb") as cred:
                service = build(API_DRIVE, API_VERSION, credentials=cred)
            os.remove(output)
            os.remove(file_name)
            return service, existing_details[name]["token_name"]

    except Exception as e:
        print(f"An error occurred here: {str(e)}")

def Expire_Token_File():
    service = Create_Drive_Token()
    token_main_folder_id, token_folder_id = Check_Token_Main_Folder()
    try:
        response = (
            service.files()
            .list(
            q=f"'{token_main_folder_id}' in parents and name contains 'token_json'",
                spaces="drive",
                fields="files(id, name)",
            )
            .execute()
        )
        list_all_files = response.get("files", [])
        for list_files in list_all_files:
            if list_files["name"].startswith("token_json"):
                latest_file_id = str(list_files["id"])
                existing_data = service.files().get_media(fileId=latest_file_id).execute()
                if existing_data:  
                    existing_details = json.loads(existing_data.decode("utf-8"))
                    
                    with open(list_files["name"], "w", encoding="utf-8") as json_file:
                        json.dump(existing_details, json_file, ensure_ascii=False)
                    
                    for details in existing_details:
                        if datetime.strptime(details["expires_at"], "%Y-%m-%dT%H:%M:%SZ") < datetime.now():
                            drive_id = details["token_file_id"]
                            service.files().delete(fileId=details["token_file_id"]).execute()
                            del existing_details[details["token_name"]]
                            service.files().delete(fileId=drive_id).execute()
                            return True
        return False
    except Exception as e:
        print(f"An error occurred here: {str(e)}")

def Delete_Token_File(name):
    service = Create_Drive_Token()
    token_main_folder_id, token_folder_id = Check_Token_Main_Folder()
    try:
        response = (
            service.files()
            .list(
                q=f"'{token_main_folder_id}' in parents and name contains 'token_json'",
                spaces="drive",
                fields="files(id, name)",
            )
            .execute()
        )
        list_all_files = response.get("files", [])
        for list_files in list_all_files:
            if list_files["name"].startswith("token_json"):
                latest_file_id = str(list_files["id"])
                existing_data = service.files().get_media(fileId=latest_file_id).execute()
                if existing_data:  
                    existing_details = json.loads(existing_data.decode("utf-8"))
                    
                    with open(list_files["name"], "w", encoding="utf-8") as json_file:
                        json.dump(existing_details, json_file, ensure_ascii=False)
                    
                    for details in existing_details:
                        if details[name] == name:
                            drive_id = details["token_file_id"]
                            service.files().delete(fileId=details["token_file_id"]).execute()
                            del existing_details[details["token_name"]]
                            service.files().delete(fileId=drive_id).execute()
                            return True
        return False
    except Exception as e:
        print(f"An error occurred here: {str(e)}")