File size: 3,233 Bytes
dcb132a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import base64
import datetime
import json
import urllib

import flask
import requests
import src.functions.config

redirectdownloadBP = flask.Blueprint("redirectdownload", __name__)


@redirectdownloadBP.route("/api/v1/redirectdownload/<name>")
async def redirectdownloadFunction(name):
    id = flask.request.args.get("id")
    itag = flask.request.args.get("itag")

    config = src.functions.config.readConfig()
    if config.get("kill_switch") == True:
        return

    if (
        datetime.datetime.strptime(
            config.get("token_expiry", datetime.datetime.utcnow()),
            "%Y-%m-%d %H:%M:%S.%f",
        )
        <= datetime.datetime.utcnow()
    ):
        config, drive = src.functions.credentials.refreshCredentials(config)
        with open("config.json", "w+") as w:
            json.dump(obj=config, fp=w, sort_keys=True, indent=4)

    tmp_metadata = src.functions.metadata.jsonExtract(
        src.functions.metadata.readMetadata(config), "id", id, False
    )
    if tmp_metadata:
        name = tmp_metadata.get("name", name)
    args = "?"
    for arg in flask.request.args:
        args += "%s=%s&" % (
            arg,
            urllib.parse.quote(flask.request.args.get(arg, "").encode("utf-8")),
        )
    session = {
        "access_token": config.get("access_token"),
        "client_id": config.get("client_id"),
        "client_secret": config.get("client_secret"),
        "refresh_token": config.get("refresh_token"),
        "token_expiry": config.get("token_expiry"),
    }

    session["url"] = "https://www.googleapis.com/drive/v3/files/%s?alt=media" % (id)
    if itag and itag != "" and config.get("transcoded") == True:
        req = requests.get(
            "https://drive.google.com/get_video_info?docid=%s" % (id),
            headers={"Authorization": "Bearer %s" % (config.get("access_token"))},
        )
        parsed = urllib.parse.parse_qs(urllib.parse.unquote(req.text))
        if parsed.get("status") == ["ok"]:
            for stream in parsed["url"]:
                if ("itag=%s" % (itag)) in stream:
                    url = stream
                    break
            cookie_string = "; ".join(
                [str(x) + "=" + str(y) for x, y in req.cookies.items()]
            )
            session["cookie"] = cookie_string
            session["transcoded"] = config.get("transcoded")
            session["url"] = url

    sessionB64 = base64.b64encode(json.dumps(session).encode("ascii")).decode("ascii")

    if config.get("cloudflare") not in ["local", "", None]:
        return flask.redirect(
            config.get("cloudflare")
            + "/api/v1/download/%s%ssession=%s&" % (name, args, sessionB64),
            code=302,
        )
    elif config.get("cloudflare") == "local":
        return flask.redirect(
            "http://localhost:31146/api/v1/download/%s%ssession=%s&"
            % (name, args, sessionB64),
            code=302,
        )
    else:
        return flask.redirect(
            "/api/v1/download/%s%ssession=%s&"
            % (urllib.parse.quote(name.encode("utf-8")), args, sessionB64),
            code=302,
        )