-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgdrive.py
90 lines (75 loc) · 2.56 KB
/
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
import requests
from telethon import events
from . import *
async def download_file_from_google_drive(id):
URL = "https://docs.google.com/uc?export=download"
session = requests.Session()
response = session.get(URL, params={"id": id}, stream=True)
token = await get_confirm_token(response)
if token:
params = {"id": id, "confirm": token}
response = session.get(URL, params=params, stream=True)
headers = response.headers
content = headers["Content-Disposition"]
destination = await get_file_name(content)
file_name = await save_response_content(response, destination)
return file_name
async def get_confirm_token(response):
for key, value in response.cookies.items():
if key.startswith("download_warning"):
return value
return None
async def save_response_content(response, destination):
CHUNK_SIZE = 32768
with open(destination, "wb") as f:
for chunk in response.iter_content(CHUNK_SIZE):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
return destination
async def get_id(link): # Extract File Id from G-Drive Link
file_id = ""
c_append = False
if link[1:33] == "https://drive.google.com/file/d/":
link = link[33:]
fid = ""
for c in link:
if c == "/":
break
fid = fid + c
return fid
for c in link:
if c == "=":
c_append = True
if c == "&":
break
if c_append:
file_id = file_id + c
file_id = file_id[1:]
return file_id
async def get_file_name(content):
file_name = ""
c_append = False
for c in str(content):
if c == '"':
c_append = True
if c == ";":
c_append = False
if c_append:
file_name = file_name + c
file_name = file_name.replace('"', "")
print("File Name: " + str(file_name))
return file_name
@bot.on(d3vil_cmd(pattern=r"gdl"))
@bot.on(sudo_cmd(pattern=r"gdl", allow_sudo=True))
async def g_download(event):
if event.fwd_from:
return
drive_link = event.text[4:]
print("Drive Link: " + drive_link)
file_id = await get_id(drive_link)
event = await eor(event, "Downloading Requested File from G-Drive...")
file_name = await download_file_from_google_drive(file_id)
await event.edit("File Downloaded.\nName: `" + str(file_name) + "`")
CmdHelp("gdrive").add_command(
"gdl", "gdrive link", "Downloads the file from gdirve to D3vilBot's local storage. Use .upload to upload it."
).add()