-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupload_gdrive.py
executable file
·196 lines (166 loc) · 6.17 KB
/
upload_gdrive.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
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
#!/usr/bin/env python3
import os
import sys
import time
from subprocess import check_output, Popen, PIPE
import update_token
import random
import json
import asyncio
import requests
from constants import GDRIVE_APIKEY
from signal import SIGKILL
S = 16
class InvalidCredentials(Exception):
pass
class TokenError(Exception):
pass
global urlForCompletedDownloads, gidsWithCompletedDownloads
class getUploads:
def __init__(self) -> None:
global gidsWithFilename, gidsWithCompletedDownloads, urlForCompletedDownloads
gidsWithFilename = {}
gidsWithCompletedDownloads = []
urlForCompletedDownloads = {}
@classmethod
async def genRandomString(cls):
return "".join(random.choice('1234567890abcdef') for _ in range(16))
@classmethod
def genRandomStringSync(cls):
return "".join([random.choice("1234567890abcdef") for _ in range(16)])
@classmethod
async def addUpload(cls, filename: str):
cls.filename = filename
cls.epoch_time = str(time.time())
cls.curl_filename = f"{cls.epoch_time}.curl"
await asyncio.create_subprocess_shell(
f"./upload_gdrive.sh '{cls.filename}' > '{cls.curl_filename}' 2>&1", shell=True
)
await asyncio.sleep(1)
with open(cls.curl_filename, "r") as progressFile:
if "{" not in progressFile.readlines()[-1]:
cls.data = {
"curl_filename": cls.curl_filename,
"filename": cls.filename,
}
gidsWithFilename[await cls.genRandomString()] = cls.data
else:
print(progressFile.readlines())
async def getFilenameBasedOnGid(self, gid: str) -> str:
self.gid = gid
return gidsWithFilename[self.gid]["filename"]
async def getListOfGids(self):
return [i.split(":")[0] for i in gidsWithFilename]
async def getFileId(self, url):
self.url = url
if "uc?id" not in self.url:
try:
self.fileId = self.url.split("/")[5]
except:
return False
if self.fileId != "":
return self.fileId
else:
return False
else:
try:
self.fileId = self.url.split("id=")[1].split("&")[0]
except:
return False
return self.fileId
async def cloneFile(self, fileId: str):
self.fileId = fileId
headers = {
"Authorization": f'Bearer {open("gdrivetoken","r").read()}',
"Accept": "application/json",
}
params = {
"supportsAllDrives": "true",
"supportsTeamDrives": "true",
"key": GDRIVE_APIKEY,
}
json_data = {}
response = requests.post(
f"https://www.googleapis.com/drive/v3/files/{self.fileId}/copy",
params=params,
headers=headers,
json=json_data,
)
if response.status_code == 200:
self.id = response.json()["id"]
self.name = response.json()["name"]
return (self.id, self.name)
else:
return False
async def getStatusBasedOnGid(self, gid, filename) -> dict:
self.gid = gid
self.filename = filename
with open(self.filename, "r") as prgfile:
self.progress = prgfile.readlines()[-1]
if "{" not in self.progress:
self.progress = self.progress.split()
if self.progress[0] != "100":
self.data = {
"Name": (gidsWithFilename[self.gid]["filename"]),
"Speed": ("".join(self.progress[-1]) + "/s"),
"GID": f"{self.gid}",
"Status": "Uploading...",
"Progress": (self.progress[0] + "%"),
"totalSize": (self.progress[1]),
"TimeRemaining": (self.progress[-2]),
}
print(self.data)
return self.data
else:
try:
k = json.loads(self.progress)
if k["error"]["message"] == "Invalid Credentials":
raise TokenError
except TokenError:
print("Auth Token Expired")
except KeyError:
filename = gidsWithFilename[self.gid]
Popen([f"rm '{filename['curl_filename']}'"], shell=True)
Popen([f"rm '{filename['filename']}'"], shell=True)
gidsWithCompletedDownloads.append(self.gid)
urlForCompletedDownloads[
self.gid
] = f"https://drive.google.com/file/d/{k['id']}/view"
async def getStatusOfAllDownloads(self):
uploadStatuses = []
for self.gid, self.filename in gidsWithFilename.items():
self.filename = self.filename["curl_filename"]
uploadStatuses.append(
await self.getStatusBasedOnGid(self.gid, self.filename)
)
return uploadStatuses
async def getCompletedUploads(self) -> list:
return gidsWithCompletedDownloads
async def cleanUp(self):
k = gidsWithCompletedDownloads
for i in k:
del gidsWithFilename[i]
gidsWithCompletedDownloads.clear()
async def cancelUpload(self, gid: str):
self.gid = gid
self.filename = await self.getFilenameBasedOnGid(self.gid)
self.filename = self.filename.replace("]", "\]").replace("[", "\[")
self.output = (
check_output(
f"ps -aux | grep '{self.filename}' | grep -v 'upload_gdrive.sh'",
shell=True,
)
.decode()
.split()
)
self.pid = self.output[1]
print(self.output)
os.kill(int(self.pid), SIGKILL)
gidsWithCompletedDownloads.append(self.gid)
def cronUpdateGDriveToken(self):
while 1 != 0:
update_token.genToken()
time.sleep(3400)
if __name__ == "__main__":
k = getUploads()
asyncio.run(k.addUpload(sys.argv[1]))