Spaces:
Paused
Paused
ChandimaPrabath
commited on
Commit
•
9b708eb
1
Parent(s):
a6f96dc
fix
Browse files- app.py +15 -0
- hf_scrapper.py +30 -10
app.py
CHANGED
@@ -129,6 +129,7 @@ def list_tv():
|
|
129 |
def film_page(title):
|
130 |
title = urllib.parse.unquote(title)
|
131 |
film_file_path = get_film_file_path(title)
|
|
|
132 |
if not film_file_path:
|
133 |
return jsonify({'error': 'Film not found'}), 404
|
134 |
|
@@ -199,6 +200,20 @@ def cache_film(title):
|
|
199 |
def film_details(title):
|
200 |
return render_template('film_details_page.html', title=title)
|
201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
202 |
@app.route('/cached_films/<path:title>')
|
203 |
def serve_cached_film(title):
|
204 |
cached_file_path = os.path.join(CACHE_DIR, urllib.parse.unquote(title))
|
|
|
129 |
def film_page(title):
|
130 |
title = urllib.parse.unquote(title)
|
131 |
film_file_path = get_film_file_path(title)
|
132 |
+
|
133 |
if not film_file_path:
|
134 |
return jsonify({'error': 'Film not found'}), 404
|
135 |
|
|
|
200 |
def film_details(title):
|
201 |
return render_template('film_details_page.html', title=title)
|
202 |
|
203 |
+
@app.route('/film_player/<path:title>')
|
204 |
+
def film_player(title):
|
205 |
+
title = urllib.parse.unquote(title)
|
206 |
+
film_file_path = get_film_file_path(title)
|
207 |
+
|
208 |
+
if not film_file_path:
|
209 |
+
return jsonify({'error': 'Film not found'}), 404
|
210 |
+
|
211 |
+
cached_file_path = os.path.join(CACHE_DIR, film_file_path)
|
212 |
+
if not os.path.exists(cached_file_path):
|
213 |
+
return jsonify({'error': 'Film not cached'}), 404
|
214 |
+
|
215 |
+
return render_template('film_player.html', title=title)
|
216 |
+
|
217 |
@app.route('/cached_films/<path:title>')
|
218 |
def serve_cached_film(title):
|
219 |
cached_file_path = os.path.join(CACHE_DIR, urllib.parse.unquote(title))
|
hf_scrapper.py
CHANGED
@@ -1,10 +1,9 @@
|
|
1 |
import os
|
2 |
-
import subprocess
|
3 |
import requests
|
4 |
import json
|
5 |
import urllib.request
|
6 |
from requests.exceptions import RequestException
|
7 |
-
from
|
8 |
|
9 |
def get_system_proxies():
|
10 |
try:
|
@@ -18,15 +17,21 @@ def get_system_proxies():
|
|
18 |
print(f"Error getting system proxies: {e}")
|
19 |
return {}
|
20 |
|
21 |
-
|
22 |
def download_and_cache_file(file_url, token, cache_path, proxies=None):
|
23 |
print(f"Downloading file from URL: {file_url} to {cache_path} with proxies: {proxies}")
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
try:
|
25 |
-
response =
|
26 |
response.raise_for_status()
|
27 |
os.makedirs(os.path.dirname(cache_path), exist_ok=True)
|
|
|
28 |
with open(cache_path, 'wb') as f:
|
29 |
-
for chunk in response.iter_content(chunk_size=
|
30 |
if chunk:
|
31 |
f.write(chunk)
|
32 |
print(f'File cached to {cache_path} successfully.')
|
@@ -41,6 +46,7 @@ def get_file_structure(repo, token, path="", proxies=None):
|
|
41 |
api_url = f"https://huggingface.co/api/models/{repo}/tree/main/{path}"
|
42 |
headers = {'Authorization': f'Bearer {token}'}
|
43 |
print(f"Fetching file structure from URL: {api_url} with proxies: {proxies}")
|
|
|
44 |
try:
|
45 |
response = requests.get(api_url, headers=headers, proxies=proxies)
|
46 |
response.raise_for_status()
|
@@ -57,10 +63,24 @@ def write_file_structure_to_json(file_structure, file_path):
|
|
57 |
except IOError as e:
|
58 |
print(f"Error writing file structure to JSON: {e}")
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
if __name__ == "__main__":
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
1 |
import os
|
|
|
2 |
import requests
|
3 |
import json
|
4 |
import urllib.request
|
5 |
from requests.exceptions import RequestException
|
6 |
+
from concurrent.futures import ThreadPoolExecutor
|
7 |
|
8 |
def get_system_proxies():
|
9 |
try:
|
|
|
17 |
print(f"Error getting system proxies: {e}")
|
18 |
return {}
|
19 |
|
|
|
20 |
def download_and_cache_file(file_url, token, cache_path, proxies=None):
|
21 |
print(f"Downloading file from URL: {file_url} to {cache_path} with proxies: {proxies}")
|
22 |
+
|
23 |
+
# Create a requests session for better performance
|
24 |
+
session = requests.Session()
|
25 |
+
session.headers.update({'Authorization': f'Bearer {token}'})
|
26 |
+
session.proxies.update(proxies)
|
27 |
+
|
28 |
try:
|
29 |
+
response = session.get(file_url, stream=True)
|
30 |
response.raise_for_status()
|
31 |
os.makedirs(os.path.dirname(cache_path), exist_ok=True)
|
32 |
+
|
33 |
with open(cache_path, 'wb') as f:
|
34 |
+
for chunk in response.iter_content(chunk_size=16384): # Larger chunk size
|
35 |
if chunk:
|
36 |
f.write(chunk)
|
37 |
print(f'File cached to {cache_path} successfully.')
|
|
|
46 |
api_url = f"https://huggingface.co/api/models/{repo}/tree/main/{path}"
|
47 |
headers = {'Authorization': f'Bearer {token}'}
|
48 |
print(f"Fetching file structure from URL: {api_url} with proxies: {proxies}")
|
49 |
+
|
50 |
try:
|
51 |
response = requests.get(api_url, headers=headers, proxies=proxies)
|
52 |
response.raise_for_status()
|
|
|
63 |
except IOError as e:
|
64 |
print(f"Error writing file structure to JSON: {e}")
|
65 |
|
66 |
+
# Function to download files in parallel
|
67 |
+
def parallel_downloads(file_urls, token, cache_dir, proxies=None):
|
68 |
+
with ThreadPoolExecutor() as executor:
|
69 |
+
futures = []
|
70 |
+
for file_url in file_urls:
|
71 |
+
filename = file_url.split("/")[-1]
|
72 |
+
cache_path = os.path.join(cache_dir, filename)
|
73 |
+
futures.append(executor.submit(download_and_cache_file, file_url, token, cache_path, proxies))
|
74 |
+
# Wait for all futures to complete
|
75 |
+
for future in futures:
|
76 |
+
future.result()
|
77 |
|
78 |
if __name__ == "__main__":
|
79 |
+
file_urls = [
|
80 |
+
"https://huggingface.co/Unicone-Studio/jellyfin_media/resolve/main/films/Funky%20Monkey%202004/Funky%20Monkey%20(2004)%20Web-dl%201080p.mp4"
|
81 |
+
]
|
82 |
+
token = os.getenv("TOKEN")
|
83 |
+
cache_dir = "tmp/cache/films"
|
84 |
+
proxies = get_system_proxies()
|
85 |
+
|
86 |
+
parallel_downloads(file_urls, token, cache_dir, proxies)
|