Spaces:
Sleeping
Sleeping
Update src/routes/redirectdownload.py
Browse files- src/routes/redirectdownload.py +90 -90
src/routes/redirectdownload.py
CHANGED
@@ -1,90 +1,90 @@
|
|
1 |
-
import base64
|
2 |
-
import datetime
|
3 |
-
import json
|
4 |
-
import urllib
|
5 |
-
|
6 |
-
import flask
|
7 |
-
import requests
|
8 |
-
import src.functions.config
|
9 |
-
|
10 |
-
redirectdownloadBP = flask.Blueprint("redirectdownload", __name__)
|
11 |
-
|
12 |
-
|
13 |
-
@redirectdownloadBP.route("/api/v1/redirectdownload/<name>")
|
14 |
-
async def redirectdownloadFunction(name):
|
15 |
-
id = flask.request.args.get("id")
|
16 |
-
itag = flask.request.args.get("itag")
|
17 |
-
|
18 |
-
config = src.functions.config.readConfig()
|
19 |
-
if config.get("kill_switch") == True:
|
20 |
-
return
|
21 |
-
|
22 |
-
if (
|
23 |
-
datetime.datetime.strptime(
|
24 |
-
config.get("token_expiry", datetime.datetime.utcnow()),
|
25 |
-
"%Y-%m-%d %H:%M:%S.%f",
|
26 |
-
)
|
27 |
-
<= datetime.datetime.utcnow()
|
28 |
-
):
|
29 |
-
config, drive = src.functions.credentials.refreshCredentials(config)
|
30 |
-
with open("config.json", "w+") as w:
|
31 |
-
|
32 |
-
|
33 |
-
tmp_metadata = src.functions.metadata.jsonExtract(
|
34 |
-
src.functions.metadata.readMetadata(config), "id", id, False
|
35 |
-
)
|
36 |
-
if tmp_metadata:
|
37 |
-
name = tmp_metadata.get("name", name)
|
38 |
-
args = "?"
|
39 |
-
for arg in flask.request.args:
|
40 |
-
args += "%s=%s&" % (
|
41 |
-
arg,
|
42 |
-
urllib.parse.quote(flask.request.args.get(arg, "").encode("utf-8")),
|
43 |
-
)
|
44 |
-
session = {
|
45 |
-
"access_token": config.get("access_token"),
|
46 |
-
"client_id": config.get("client_id"),
|
47 |
-
"client_secret": config.get("client_secret"),
|
48 |
-
"refresh_token": config.get("refresh_token"),
|
49 |
-
"token_expiry": config.get("token_expiry"),
|
50 |
-
}
|
51 |
-
|
52 |
-
session["url"] = "https://www.googleapis.com/drive/v3/files/%s?alt=media" % (id)
|
53 |
-
if itag and itag != "" and config.get("transcoded") == True:
|
54 |
-
req = requests.get(
|
55 |
-
"https://drive.google.com/get_video_info?docid=%s" % (id),
|
56 |
-
headers={"Authorization": "Bearer %s" % (config.get("access_token"))},
|
57 |
-
)
|
58 |
-
parsed = urllib.parse.parse_qs(urllib.parse.unquote(req.text))
|
59 |
-
if parsed.get("status") == ["ok"]:
|
60 |
-
for stream in parsed["url"]:
|
61 |
-
if ("itag=%s" % (itag)) in stream:
|
62 |
-
url = stream
|
63 |
-
break
|
64 |
-
cookie_string = "; ".join(
|
65 |
-
[str(x) + "=" + str(y) for x, y in req.cookies.items()]
|
66 |
-
)
|
67 |
-
session["cookie"] = cookie_string
|
68 |
-
session["transcoded"] = config.get("transcoded")
|
69 |
-
session["url"] = url
|
70 |
-
|
71 |
-
sessionB64 = base64.b64encode(json.dumps(session).encode("ascii")).decode("ascii")
|
72 |
-
|
73 |
-
if config.get("cloudflare") not in ["local", "", None]:
|
74 |
-
return flask.redirect(
|
75 |
-
config.get("cloudflare")
|
76 |
-
+ "/api/v1/download/%s%ssession=%s&" % (name, args, sessionB64),
|
77 |
-
code=302,
|
78 |
-
)
|
79 |
-
elif config.get("cloudflare") == "local":
|
80 |
-
return flask.redirect(
|
81 |
-
"http://localhost:31146/api/v1/download/%s%ssession=%s&"
|
82 |
-
% (name, args, sessionB64),
|
83 |
-
code=302,
|
84 |
-
)
|
85 |
-
else:
|
86 |
-
return flask.redirect(
|
87 |
-
"/api/v1/download/%s%ssession=%s&"
|
88 |
-
% (urllib.parse.quote(name.encode("utf-8")), args, sessionB64),
|
89 |
-
code=302,
|
90 |
-
)
|
|
|
1 |
+
import base64
|
2 |
+
import datetime
|
3 |
+
import json
|
4 |
+
import urllib
|
5 |
+
|
6 |
+
import flask
|
7 |
+
import requests
|
8 |
+
import src.functions.config
|
9 |
+
|
10 |
+
redirectdownloadBP = flask.Blueprint("redirectdownload", __name__)
|
11 |
+
|
12 |
+
|
13 |
+
@redirectdownloadBP.route("/api/v1/redirectdownload/<name>")
|
14 |
+
async def redirectdownloadFunction(name):
|
15 |
+
id = flask.request.args.get("id")
|
16 |
+
itag = flask.request.args.get("itag")
|
17 |
+
|
18 |
+
config = src.functions.config.readConfig()
|
19 |
+
if config.get("kill_switch") == True:
|
20 |
+
return
|
21 |
+
|
22 |
+
if (
|
23 |
+
datetime.datetime.strptime(
|
24 |
+
config.get("token_expiry", datetime.datetime.utcnow()),
|
25 |
+
"%Y-%m-%d %H:%M:%S.%f",
|
26 |
+
)
|
27 |
+
<= datetime.datetime.utcnow()
|
28 |
+
):
|
29 |
+
config, drive = src.functions.credentials.refreshCredentials(config)
|
30 |
+
#with open("config.json", "w+") as w:
|
31 |
+
# json.dump(obj=config, fp=w, sort_keys=True, indent=4)
|
32 |
+
|
33 |
+
tmp_metadata = src.functions.metadata.jsonExtract(
|
34 |
+
src.functions.metadata.readMetadata(config), "id", id, False
|
35 |
+
)
|
36 |
+
if tmp_metadata:
|
37 |
+
name = tmp_metadata.get("name", name)
|
38 |
+
args = "?"
|
39 |
+
for arg in flask.request.args:
|
40 |
+
args += "%s=%s&" % (
|
41 |
+
arg,
|
42 |
+
urllib.parse.quote(flask.request.args.get(arg, "").encode("utf-8")),
|
43 |
+
)
|
44 |
+
session = {
|
45 |
+
"access_token": config.get("access_token"),
|
46 |
+
"client_id": config.get("client_id"),
|
47 |
+
"client_secret": config.get("client_secret"),
|
48 |
+
"refresh_token": config.get("refresh_token"),
|
49 |
+
"token_expiry": config.get("token_expiry"),
|
50 |
+
}
|
51 |
+
|
52 |
+
session["url"] = "https://www.googleapis.com/drive/v3/files/%s?alt=media" % (id)
|
53 |
+
if itag and itag != "" and config.get("transcoded") == True:
|
54 |
+
req = requests.get(
|
55 |
+
"https://drive.google.com/get_video_info?docid=%s" % (id),
|
56 |
+
headers={"Authorization": "Bearer %s" % (config.get("access_token"))},
|
57 |
+
)
|
58 |
+
parsed = urllib.parse.parse_qs(urllib.parse.unquote(req.text))
|
59 |
+
if parsed.get("status") == ["ok"]:
|
60 |
+
for stream in parsed["url"]:
|
61 |
+
if ("itag=%s" % (itag)) in stream:
|
62 |
+
url = stream
|
63 |
+
break
|
64 |
+
cookie_string = "; ".join(
|
65 |
+
[str(x) + "=" + str(y) for x, y in req.cookies.items()]
|
66 |
+
)
|
67 |
+
session["cookie"] = cookie_string
|
68 |
+
session["transcoded"] = config.get("transcoded")
|
69 |
+
session["url"] = url
|
70 |
+
|
71 |
+
sessionB64 = base64.b64encode(json.dumps(session).encode("ascii")).decode("ascii")
|
72 |
+
|
73 |
+
if config.get("cloudflare") not in ["local", "", None]:
|
74 |
+
return flask.redirect(
|
75 |
+
config.get("cloudflare")
|
76 |
+
+ "/api/v1/download/%s%ssession=%s&" % (name, args, sessionB64),
|
77 |
+
code=302,
|
78 |
+
)
|
79 |
+
elif config.get("cloudflare") == "local":
|
80 |
+
return flask.redirect(
|
81 |
+
"http://localhost:31146/api/v1/download/%s%ssession=%s&"
|
82 |
+
% (name, args, sessionB64),
|
83 |
+
code=302,
|
84 |
+
)
|
85 |
+
else:
|
86 |
+
return flask.redirect(
|
87 |
+
"/api/v1/download/%s%ssession=%s&"
|
88 |
+
% (urllib.parse.quote(name.encode("utf-8")), args, sessionB64),
|
89 |
+
code=302,
|
90 |
+
)
|