Spaces:
Sleeping
Sleeping
Update src/routes/download.py
Browse files- src/routes/download.py +80 -80
src/routes/download.py
CHANGED
@@ -1,80 +1,80 @@
|
|
1 |
-
from oauth2client import client
|
2 |
-
import base64
|
3 |
-
import datetime
|
4 |
-
import json
|
5 |
-
|
6 |
-
import flask
|
7 |
-
from flask import request, Response
|
8 |
-
import requests
|
9 |
-
from pydrive.auth import GoogleAuth
|
10 |
-
from pydrive.drive import GoogleDrive
|
11 |
-
import src.functions.config
|
12 |
-
|
13 |
-
downloadBP = flask.Blueprint("download", __name__)
|
14 |
-
|
15 |
-
|
16 |
-
def save_response_content(response, destination):
|
17 |
-
CHUNK_SIZE = 32768
|
18 |
-
|
19 |
-
with open(destination, "wb") as f:
|
20 |
-
for chunk in response.iter_content(CHUNK_SIZE):
|
21 |
-
if chunk: # filter out keep-alive new chunks
|
22 |
-
f.write(chunk)
|
23 |
-
|
24 |
-
|
25 |
-
@downloadBP.route("/api/v1/download/<name>")
|
26 |
-
async def downloadFunction(name):
|
27 |
-
a = flask.request.args.get("a") # AUTH
|
28 |
-
config = src.functions.config.readConfig()
|
29 |
-
|
30 |
-
gauth = GoogleAuth()
|
31 |
-
content = '{"access_token": "ya29.a0AXooCgtLK5HzYMtRs4R9J7FRZSGR3i5jUkeMeVhGjorlrgq_BupFi8d9upA2skYC5FofxUqo23Nivk_P_Hy8eRn0DWM3deSKoiWMhA3lsy05JVakD0vd2fPRaFOXfRV20jAEGt6ql9yy_0up3Y9z8u9yXZ28IUxRRZAHaCgYKASQSARISFQHGX2MipVyGD4fFFZJWXGvyd-sJnQ0171", "client_id": "895306463817-h14aujg3ohgptue5safg2d81530qs4c3.apps.googleusercontent.com", "client_secret": "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", "refresh_token": "1//0gsu0CorccmScCgYIARAAGBASNwF-L9IrF-TDYDXR_MTQGAGGf4fY4BBBSBUipsz_7c0B6HjmRYZV3uxPVU4CAJjqWoWBm0T4pxA", "token_expiry": "2024-05-25T11:14:56Z", "token_uri": "https://oauth2.googleapis.com/token", "user_agent": null, "revoke_uri": "https://oauth2.googleapis.com/revoke", "id_token": null, "id_token_jwt": null, "token_response": {"access_token": "ya29.a0AXooCgtLK5HzYMtRs4R9J7FRZSGR3i5jUkeMeVhGjorlrgq_BupFi8d9upA2skYC5FofxUqo23Nivk_P_Hy8eRn0DWM3deSKoiWMhA3lsy05JVakD0vd2fPRaFOXfRV20jAEGt6ql9yy_0up3Y9z8u9yXZ28IUxRRZAHaCgYKASQSARISFQHGX2MipVyGD4fFFZJWXGvyd-sJnQ0171", "expires_in": 3599, "refresh_token": "1//0gsu0CorccmScCgYIARAAGBASNwF-L9IrF-TDYDXR_MTQGAGGf4fY4BBBSBUipsz_7c0B6HjmRYZV3uxPVU4CAJjqWoWBm0T4pxA", "scope": "https://www.googleapis.com/auth/drive", "token_type": "Bearer"}, "scopes": ["https://www.googleapis.com/auth/drive"], "token_info_uri": "https://oauth2.googleapis.com/tokeninfo", "invalid": false, "_class": "OAuth2Credentials", "_module": "oauth2client.client"}'
|
32 |
-
gauth.credentials = client.Credentials.new_from_json(content)
|
33 |
-
if gauth.access_token_expired:
|
34 |
-
# Refresh them if expired
|
35 |
-
gauth.Refresh()
|
36 |
-
else:
|
37 |
-
gauth.Authorize()
|
38 |
-
DRIVE = GoogleDrive(gauth)
|
39 |
-
|
40 |
-
def download_file(response):
|
41 |
-
print("Started Streamming")
|
42 |
-
for chunk in response.iter_content(4096):
|
43 |
-
yield chunk
|
44 |
-
|
45 |
-
def get_confirm_token(response):
|
46 |
-
for key, value in response.cookies.items():
|
47 |
-
if key.startswith("download_warning"):
|
48 |
-
return value
|
49 |
-
|
50 |
-
return None
|
51 |
-
|
52 |
-
if (
|
53 |
-
datetime.datetime.strptime(
|
54 |
-
config.get("token_expiry", datetime.datetime.utcnow()),
|
55 |
-
"%Y-%m-%d %H:%M:%S.%f",
|
56 |
-
)
|
57 |
-
<= datetime.datetime.utcnow()
|
58 |
-
):
|
59 |
-
config, drive = src.functions.credentials.refreshCredentials(config)
|
60 |
-
with open("config.json", "w+") as w:
|
61 |
-
|
62 |
-
|
63 |
-
file_id = flask.request.args.get("id")
|
64 |
-
file_obj = DRIVE.CreateFile({'id': file_id})
|
65 |
-
file_obj.Upload()
|
66 |
-
download_url = file_obj.metadata.get('downloadUrl')
|
67 |
-
# download_url = download_url.replace("v2", "v3").replace("&source=downloadUrl", "") + "&supportsAllDrives=true&key=AIzaSyDF0v4AzL3y0beyAadnhbF131CGVlFpFOo"
|
68 |
-
print(download_url)
|
69 |
-
try:
|
70 |
-
file_obj.http = file_obj.auth.Get_Http_Object()
|
71 |
-
credentials = file_obj.http.request.credentials.__dict__
|
72 |
-
# response = requests.get(download_url, stream=True)
|
73 |
-
response = requests.get(download_url, headers={'Authorization': f'{credentials["token_response"]["token_type"]} {credentials["access_token"]}'}, stream=True)
|
74 |
-
|
75 |
-
return download_file(response), {"Content-Type": "video/mp4"}
|
76 |
-
# return flask.Response(
|
77 |
-
# flask.stream_with_context(download_file(response))
|
78 |
-
# )
|
79 |
-
except Exception as e:
|
80 |
-
print("error", e)
|
|
|
1 |
+
from oauth2client import client
|
2 |
+
import base64
|
3 |
+
import datetime
|
4 |
+
import json
|
5 |
+
|
6 |
+
import flask
|
7 |
+
from flask import request, Response
|
8 |
+
import requests
|
9 |
+
from pydrive.auth import GoogleAuth
|
10 |
+
from pydrive.drive import GoogleDrive
|
11 |
+
import src.functions.config
|
12 |
+
|
13 |
+
downloadBP = flask.Blueprint("download", __name__)
|
14 |
+
|
15 |
+
|
16 |
+
def save_response_content(response, destination):
|
17 |
+
CHUNK_SIZE = 32768
|
18 |
+
|
19 |
+
with open(destination, "wb") as f:
|
20 |
+
for chunk in response.iter_content(CHUNK_SIZE):
|
21 |
+
if chunk: # filter out keep-alive new chunks
|
22 |
+
f.write(chunk)
|
23 |
+
|
24 |
+
|
25 |
+
@downloadBP.route("/api/v1/download/<name>")
|
26 |
+
async def downloadFunction(name):
|
27 |
+
a = flask.request.args.get("a") # AUTH
|
28 |
+
config = src.functions.config.readConfig()
|
29 |
+
|
30 |
+
gauth = GoogleAuth()
|
31 |
+
content = '{"access_token": "ya29.a0AXooCgtLK5HzYMtRs4R9J7FRZSGR3i5jUkeMeVhGjorlrgq_BupFi8d9upA2skYC5FofxUqo23Nivk_P_Hy8eRn0DWM3deSKoiWMhA3lsy05JVakD0vd2fPRaFOXfRV20jAEGt6ql9yy_0up3Y9z8u9yXZ28IUxRRZAHaCgYKASQSARISFQHGX2MipVyGD4fFFZJWXGvyd-sJnQ0171", "client_id": "895306463817-h14aujg3ohgptue5safg2d81530qs4c3.apps.googleusercontent.com", "client_secret": "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", "refresh_token": "1//0gsu0CorccmScCgYIARAAGBASNwF-L9IrF-TDYDXR_MTQGAGGf4fY4BBBSBUipsz_7c0B6HjmRYZV3uxPVU4CAJjqWoWBm0T4pxA", "token_expiry": "2024-05-25T11:14:56Z", "token_uri": "https://oauth2.googleapis.com/token", "user_agent": null, "revoke_uri": "https://oauth2.googleapis.com/revoke", "id_token": null, "id_token_jwt": null, "token_response": {"access_token": "ya29.a0AXooCgtLK5HzYMtRs4R9J7FRZSGR3i5jUkeMeVhGjorlrgq_BupFi8d9upA2skYC5FofxUqo23Nivk_P_Hy8eRn0DWM3deSKoiWMhA3lsy05JVakD0vd2fPRaFOXfRV20jAEGt6ql9yy_0up3Y9z8u9yXZ28IUxRRZAHaCgYKASQSARISFQHGX2MipVyGD4fFFZJWXGvyd-sJnQ0171", "expires_in": 3599, "refresh_token": "1//0gsu0CorccmScCgYIARAAGBASNwF-L9IrF-TDYDXR_MTQGAGGf4fY4BBBSBUipsz_7c0B6HjmRYZV3uxPVU4CAJjqWoWBm0T4pxA", "scope": "https://www.googleapis.com/auth/drive", "token_type": "Bearer"}, "scopes": ["https://www.googleapis.com/auth/drive"], "token_info_uri": "https://oauth2.googleapis.com/tokeninfo", "invalid": false, "_class": "OAuth2Credentials", "_module": "oauth2client.client"}'
|
32 |
+
gauth.credentials = client.Credentials.new_from_json(content)
|
33 |
+
if gauth.access_token_expired:
|
34 |
+
# Refresh them if expired
|
35 |
+
gauth.Refresh()
|
36 |
+
else:
|
37 |
+
gauth.Authorize()
|
38 |
+
DRIVE = GoogleDrive(gauth)
|
39 |
+
|
40 |
+
def download_file(response):
|
41 |
+
print("Started Streamming")
|
42 |
+
for chunk in response.iter_content(4096):
|
43 |
+
yield chunk
|
44 |
+
|
45 |
+
def get_confirm_token(response):
|
46 |
+
for key, value in response.cookies.items():
|
47 |
+
if key.startswith("download_warning"):
|
48 |
+
return value
|
49 |
+
|
50 |
+
return None
|
51 |
+
|
52 |
+
if (
|
53 |
+
datetime.datetime.strptime(
|
54 |
+
config.get("token_expiry", datetime.datetime.utcnow()),
|
55 |
+
"%Y-%m-%d %H:%M:%S.%f",
|
56 |
+
)
|
57 |
+
<= datetime.datetime.utcnow()
|
58 |
+
):
|
59 |
+
config, drive = src.functions.credentials.refreshCredentials(config)
|
60 |
+
#with open("config.json", "w+") as w:
|
61 |
+
# json.dump(obj=config, fp=w, sort_keys=True, indent=4)
|
62 |
+
|
63 |
+
file_id = flask.request.args.get("id")
|
64 |
+
file_obj = DRIVE.CreateFile({'id': file_id})
|
65 |
+
file_obj.Upload()
|
66 |
+
download_url = file_obj.metadata.get('downloadUrl')
|
67 |
+
# download_url = download_url.replace("v2", "v3").replace("&source=downloadUrl", "") + "&supportsAllDrives=true&key=AIzaSyDF0v4AzL3y0beyAadnhbF131CGVlFpFOo"
|
68 |
+
print(download_url)
|
69 |
+
try:
|
70 |
+
file_obj.http = file_obj.auth.Get_Http_Object()
|
71 |
+
credentials = file_obj.http.request.credentials.__dict__
|
72 |
+
# response = requests.get(download_url, stream=True)
|
73 |
+
response = requests.get(download_url, headers={'Authorization': f'{credentials["token_response"]["token_type"]} {credentials["access_token"]}'}, stream=True)
|
74 |
+
|
75 |
+
return download_file(response), {"Content-Type": "video/mp4"}
|
76 |
+
# return flask.Response(
|
77 |
+
# flask.stream_with_context(download_file(response))
|
78 |
+
# )
|
79 |
+
except Exception as e:
|
80 |
+
print("error", e)
|