File size: 2,866 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
import re

import chardet
import flask
import requests
import src.functions.config

subtitledownloadBP = flask.Blueprint("subtitledownload", __name__)


@subtitledownloadBP.route("/api/v1/subtitledownload/<name>")
async def subtitledownloadFunction(name):
    a = flask.request.args.get("a")  # AUTH
    id = flask.request.args.get("id")  # ID
    config = src.functions.config.readConfig()

    def download_file(streamable):
        with streamable as stream:
            stream.raise_for_status()
            for chunk in stream.iter_content(chunk_size=4096):
                if name.endswith("srt"):
                    encoding = chardet.detect(chunk).get("encoding")
                    replacement = "WEBVTT FILE\r\n\r\n" + str(chunk, encoding)
                    replacement = re.sub(
                        r"(\d\d:\d\d:\d\d),(\d\d\d)", r"\1.\2", replacement
                    )
                    lines = replacement.split("\n")
                    i = 0
                    output = ""
                    for line in lines:
                        if "-->" in line:
                            lines[i - 1] = ""
                        elif i == 0:
                            pass
                        else:
                            output += lines[i - 1] + "\n"
                        i += 1
                    chunk = output.encode(encoding)
                yield chunk

    if (
        any(a == account["auth"] for account in config["account_list"])
        or config.get("auth") == False
    ):
        config, drive = src.functions.credentials.refreshCredentials(
            src.functions.config.readConfig()
        )
        headers = {
            key: value for (key, value) in flask.request.headers if key != "Host"
        }
        headers["Authorization"] = "Bearer %s" % (config.get("access_token"))
        resp = requests.request(
            method=flask.request.method,
            url="https://www.googleapis.com/drive/v3/files/%s?alt=media" % (id),
            headers=headers,
            data=flask.request.get_data(),
            cookies=flask.request.cookies,
            allow_redirects=False,
            stream=True,
        )
        excluded_headers = [
            "content-encoding",
            "content-length",
            "transfer-encoding",
            "connection",
        ]
        headers = [
            (name, value)
            for (name, value) in resp.raw.headers.items()
            if name.lower() not in excluded_headers
        ]
        headers.append(("cache-control", "no-cache, no-store, must-revalidate"))
        headers.append(("pragma", "no-cache"))
        return flask.Response(
            flask.stream_with_context(download_file(resp)),
            resp.status_code,
            headers,
        )