Spaces:
Sleeping
Sleeping
asdfaskhf;alskhflshdf
Browse files
app.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
from fastapi import FastAPI, HTTPException
|
| 5 |
+
from fastapi.responses import FileResponse
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
ALL_COOKIES = {}
|
| 10 |
+
print("loadaaadingggg")
|
| 11 |
+
|
| 12 |
+
REQUIRED_COOKIES = [
|
| 13 |
+
'ka_sessionid',
|
| 14 |
+
'__Host-KAGGLEID',
|
| 15 |
+
'CLIENT-TOKEN',
|
| 16 |
+
'XSRF-TOKEN',
|
| 17 |
+
'CSRF-TOKEN',
|
| 18 |
+
'build-hash'
|
| 19 |
+
]
|
| 20 |
+
|
| 21 |
+
for key, value in os.environ.items():
|
| 22 |
+
if key.startswith("KAGGLE_COOKIES_") and key.rsplit('_', 1)[-1].isdigit():
|
| 23 |
+
account_id = key.rsplit('_', 1)[-1]
|
| 24 |
+
account_cookies = {}
|
| 25 |
+
netscape_file_content = value
|
| 26 |
+
|
| 27 |
+
for line in netscape_file_content.splitlines():
|
| 28 |
+
if not line.strip() or line.startswith('#'):
|
| 29 |
+
continue
|
| 30 |
+
|
| 31 |
+
parts = line.split('\t')
|
| 32 |
+
if len(parts) == 7:
|
| 33 |
+
cookie_name = parts[5]
|
| 34 |
+
cookie_value = parts[6]
|
| 35 |
+
if cookie_name in REQUIRED_COOKIES:
|
| 36 |
+
account_cookies[cookie_name] = cookie_value
|
| 37 |
+
|
| 38 |
+
if all(c in account_cookies for c in REQUIRED_COOKIES):
|
| 39 |
+
ALL_COOKIES[account_id] = account_cookies
|
| 40 |
+
print(f"negro caca '{account_id}' passed.")
|
| 41 |
+
else:
|
| 42 |
+
missing = [c for c in REQUIRED_COOKIES if c not in account_cookies]
|
| 43 |
+
print(f"negro '{account_id}' poo poo'd. needs: {missing}")
|
| 44 |
+
|
| 45 |
+
if not ALL_COOKIES:
|
| 46 |
+
print("retardation alert 1")
|
| 47 |
+
|
| 48 |
+
@app.post("/get-file-list")
|
| 49 |
+
async def get_file_list(request_body: dict):
|
| 50 |
+
try:
|
| 51 |
+
account_id = str(request_body.get("accountId"))
|
| 52 |
+
resource_type = request_body.get("resourceType")
|
| 53 |
+
metadata = request_body.get("metadata")
|
| 54 |
+
cookies = ALL_COOKIES.get(account_id)
|
| 55 |
+
if not cookies: raise HTTPException(status_code=404, detail=f"ass '{account_id}' not found")
|
| 56 |
+
|
| 57 |
+
api_url = "https://www.kaggle.com/api/i/datasets.databundles.DatabundleService/GetDatabundleExternal"
|
| 58 |
+
|
| 59 |
+
if resource_type == 'dataset':
|
| 60 |
+
body = {"verificationInfo": {"databundleVersionId": metadata["versionId"], "datasetId": metadata["id"]}}
|
| 61 |
+
else:
|
| 62 |
+
body = {"verificationInfo": {"databundleVersionId": metadata["versionId"], "modelInstanceVersionId": metadata["instanceVersionId"]}}
|
| 63 |
+
|
| 64 |
+
headers = {
|
| 65 |
+
"accept": "application/json", "content-type": "application/json",
|
| 66 |
+
"origin": "https://www.kaggle.com", "referer": "https://www.kaggle.com/",
|
| 67 |
+
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
|
| 68 |
+
"x-kaggle-build-version": cookies["build-hash"], "x-xsrf-token": cookies["XSRF-TOKEN"]
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
response = requests.post(api_url, headers=headers, cookies=cookies, json=body)
|
| 72 |
+
response.raise_for_status()
|
| 73 |
+
data = response.json()
|
| 74 |
+
|
| 75 |
+
files = data.get('dataSource', {}).get('databundleVersion', {}).get('filesetInfo', {}).get('files', {}).get('children', [])
|
| 76 |
+
file_list = [{"name": f['name'], "firestorePath": f['path']} for f in files if f['name'].lower().endswith(('.mp4', '.mkv', '.mov', '.avi'))]
|
| 77 |
+
|
| 78 |
+
return { "files": file_list }
|
| 79 |
+
except requests.exceptions.HTTPError as err:
|
| 80 |
+
error_detail = f"{str(err)} - Response: {err.response.text}"
|
| 81 |
+
raise HTTPException(status_code=err.response.status_code, detail=error_detail)
|
| 82 |
+
except Exception as e:
|
| 83 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 84 |
+
|
| 85 |
+
@app.post("/get-download-url")
|
| 86 |
+
async def get_download_url(request_body: dict):
|
| 87 |
+
try:
|
| 88 |
+
account_id = str(request_body.get("accountId"))
|
| 89 |
+
resource_type = request_body.get("resourceType")
|
| 90 |
+
metadata = request_body.get("metadata")
|
| 91 |
+
file_info = request_body.get("file")
|
| 92 |
+
cookies = ALL_COOKIES.get(account_id)
|
| 93 |
+
if not cookies: raise HTTPException(status_code=404, detail=f"Account ID '{account_id}' not found or cookies not set.")
|
| 94 |
+
|
| 95 |
+
api_url = "https://www.kaggle.com/api/i/datasets.DatasetService/GetDataViewExternal"
|
| 96 |
+
|
| 97 |
+
if resource_type == 'dataset':
|
| 98 |
+
body = {"verificationInfo": {"datasetId": metadata["id"], "databundleVersionId": metadata["versionId"]}, "firestorePath": file_info["firestorePath"]}
|
| 99 |
+
else:
|
| 100 |
+
body = {"verificationInfo": {"modelInstanceVersionId": metadata["instanceVersionId"], "databundleVersionId": metadata["versionId"]}, "firestorePath": file_info["firestorePath"]}
|
| 101 |
+
|
| 102 |
+
headers = {
|
| 103 |
+
"accept": "application/json", "content-type": "application/json",
|
| 104 |
+
"origin": "https://www.kaggle.com", "referer": "https://www.kaggle.com/",
|
| 105 |
+
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
|
| 106 |
+
"x-kaggle-build-version": cookies["build-hash"], "x-xsrf-token": cookies["XSRF-TOKEN"]
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
response = requests.post(api_url, headers=headers, cookies=cookies, json=body)
|
| 110 |
+
response.raise_for_status()
|
| 111 |
+
data = response.json()
|
| 112 |
+
return {"direct_url": data['dataView']['dataUrl']['url']}
|
| 113 |
+
except requests.exceptions.HTTPError as err:
|
| 114 |
+
error_detail = f"{str(err)} - Response: {err.response.text}"
|
| 115 |
+
raise HTTPException(status_code=err.response.status_code, detail=error_detail)
|
| 116 |
+
except Exception as e:
|
| 117 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 118 |
+
|
| 119 |
+
@app.get("/")
|
| 120 |
+
async def read_index():
|
| 121 |
+
return FileResponse('index.html')
|