Spaces:
Paused
Paused
Commit
·
2f37eba
1
Parent(s):
0ae30c8
major update not sure if works
Browse files
app.py
CHANGED
@@ -17,6 +17,7 @@ CACHE_DIR = os.getenv("CACHE_DIR")
|
|
17 |
INDEX_FILE = os.getenv("INDEX_FILE")
|
18 |
TOKEN = os.getenv("TOKEN")
|
19 |
FILM_STORE_JSON_PATH = os.path.join(CACHE_DIR, "film_store.json")
|
|
|
20 |
REPO = os.getenv("REPO")
|
21 |
download_threads = {}
|
22 |
|
@@ -24,9 +25,10 @@ download_threads = {}
|
|
24 |
if not os.path.exists(CACHE_DIR):
|
25 |
os.makedirs(CACHE_DIR)
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
30 |
|
31 |
# Index the file structure
|
32 |
indexer()
|
@@ -56,6 +58,15 @@ def find_movie_path(json_data, title):
|
|
56 |
return item['path']
|
57 |
return None
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
def get_film_id(title):
|
60 |
"""Generate a film ID based on the title."""
|
61 |
return title.replace(" ", "_").lower()
|
@@ -138,11 +149,60 @@ def get_movie():
|
|
138 |
|
139 |
return jsonify({"status": "Download started", "film_id": film_id})
|
140 |
|
141 |
-
@app.route('/api/
|
142 |
-
def
|
143 |
-
"""Endpoint to get the
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
@app.route('/api/filmid', methods=['GET'])
|
148 |
def get_film_id_by_title():
|
@@ -153,18 +213,52 @@ def get_film_id_by_title():
|
|
153 |
film_id = get_film_id(title)
|
154 |
return jsonify({"film_id": film_id})
|
155 |
|
156 |
-
@app.route('/api/
|
157 |
-
def
|
158 |
-
"""Endpoint to get the
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
return jsonify({}), 404
|
164 |
|
165 |
-
@app.route('/api/metadata', methods=['GET'])
|
166 |
-
def
|
167 |
-
"""Endpoint to get the metadata by title."""
|
168 |
title = request.args.get('title')
|
169 |
if not title:
|
170 |
return jsonify({'error': 'No title provided'}), 400
|
@@ -178,45 +272,27 @@ def get_metadata():
|
|
178 |
|
179 |
return jsonify({'error': 'Metadata not found'}), 404
|
180 |
|
181 |
-
@app.route('/api/
|
182 |
-
def
|
183 |
-
"""Endpoint to get
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
"ram": {
|
203 |
-
"total": ram_total,
|
204 |
-
"available": ram_available,
|
205 |
-
"used": ram_used,
|
206 |
-
"percent": ram_percent
|
207 |
-
},
|
208 |
-
"cpu": {
|
209 |
-
"percent": cpu_percent
|
210 |
-
},
|
211 |
-
"disk": {
|
212 |
-
"total": disk_total,
|
213 |
-
"used": disk_used,
|
214 |
-
"free": disk_free,
|
215 |
-
"percent": disk_percent
|
216 |
-
}
|
217 |
-
}
|
218 |
-
|
219 |
-
return jsonify(stats)
|
220 |
|
221 |
# Routes
|
222 |
@app.route('/')
|
|
|
17 |
INDEX_FILE = os.getenv("INDEX_FILE")
|
18 |
TOKEN = os.getenv("TOKEN")
|
19 |
FILM_STORE_JSON_PATH = os.path.join(CACHE_DIR, "film_store.json")
|
20 |
+
TV_STORE_JSON_PATH = os.path.join(CACHE_DIR, "tv_store.json")
|
21 |
REPO = os.getenv("REPO")
|
22 |
download_threads = {}
|
23 |
|
|
|
25 |
if not os.path.exists(CACHE_DIR):
|
26 |
os.makedirs(CACHE_DIR)
|
27 |
|
28 |
+
for path in [FILM_STORE_JSON_PATH, TV_STORE_JSON_PATH]:
|
29 |
+
if not os.path.exists(path):
|
30 |
+
with open(path, 'w') as json_file:
|
31 |
+
json.dump({}, json_file)
|
32 |
|
33 |
# Index the file structure
|
34 |
indexer()
|
|
|
58 |
return item['path']
|
59 |
return None
|
60 |
|
61 |
+
def find_tv_path(json_data, title):
|
62 |
+
"""Find the path of the TV show in the JSON data based on the title."""
|
63 |
+
for directory in json_data:
|
64 |
+
if directory['type'] == 'directory' and directory['path'] == 'tv':
|
65 |
+
for sub_directory in directory['contents']:
|
66 |
+
if sub_directory['type'] == 'directory' and title.lower() in sub_directory['path'].lower():
|
67 |
+
return sub_directory['path']
|
68 |
+
return None
|
69 |
+
|
70 |
def get_film_id(title):
|
71 |
"""Generate a film ID based on the title."""
|
72 |
return title.replace(" ", "_").lower()
|
|
|
149 |
|
150 |
return jsonify({"status": "Download started", "film_id": film_id})
|
151 |
|
152 |
+
@app.route('/api/tv', methods=['GET'])
|
153 |
+
def get_tv_show():
|
154 |
+
"""Endpoint to get the TV show by title, season, and episode."""
|
155 |
+
title = request.args.get('title')
|
156 |
+
season = request.args.get('season')
|
157 |
+
episode = request.args.get('episode')
|
158 |
+
if not title or not season or not episode:
|
159 |
+
return jsonify({"error": "Title, season, and episode parameters are required"}), 400
|
160 |
+
|
161 |
+
# Load the TV store JSON
|
162 |
+
with open(TV_STORE_JSON_PATH, 'r') as json_file:
|
163 |
+
tv_store_data = json.load(json_file)
|
164 |
+
|
165 |
+
# Check if the episode is already cached
|
166 |
+
episode_key = f"{title}_S{season}_E{episode}"
|
167 |
+
if episode_key in tv_store_data:
|
168 |
+
cache_path = tv_store_data[episode_key]
|
169 |
+
if os.path.exists(cache_path):
|
170 |
+
return send_from_directory(os.path.dirname(cache_path), os.path.basename(cache_path))
|
171 |
+
|
172 |
+
tv_path = find_tv_path(file_structure, title)
|
173 |
+
|
174 |
+
if not tv_path:
|
175 |
+
return jsonify({"error": "TV show not found"}), 404
|
176 |
+
|
177 |
+
episode_path = None
|
178 |
+
for season_dir in file_structure['contents']:
|
179 |
+
if season_dir['type'] == 'directory' and season_dir['path'].endswith(f'Season {season}'):
|
180 |
+
for episode_file in season_dir['contents']:
|
181 |
+
if episode_file['type'] == 'file' and episode_file['path'].endswith(f'Episode {episode}.mp4'):
|
182 |
+
episode_path = episode_file['path']
|
183 |
+
break
|
184 |
+
|
185 |
+
if not episode_path:
|
186 |
+
return jsonify({"error": "Episode not found"}), 404
|
187 |
+
|
188 |
+
cache_path = os.path.join(CACHE_DIR, episode_path)
|
189 |
+
file_url = f"https://huggingface.co/{REPO}/resolve/main/{episode_path}"
|
190 |
+
proxies = get_system_proxies()
|
191 |
+
episode_id = f"{title}_S{season}_E{episode}"
|
192 |
+
|
193 |
+
# Start the download in a separate thread if not already downloading
|
194 |
+
if episode_id not in download_threads or not download_threads[episode_id].is_alive():
|
195 |
+
thread = threading.Thread(target=download_file, args=(file_url, TOKEN, cache_path, proxies, episode_id, title))
|
196 |
+
download_threads[episode_id] = thread
|
197 |
+
thread.start()
|
198 |
+
|
199 |
+
return jsonify({"status": "Download started", "episode_id": episode_id})
|
200 |
+
|
201 |
+
@app.route('/api/progress/<id>', methods=['GET'])
|
202 |
+
def get_progress(id):
|
203 |
+
"""Endpoint to get the download progress of a movie or TV show episode."""
|
204 |
+
progress = get_download_progress(id)
|
205 |
+
return jsonify({"id": id, "progress": progress})
|
206 |
|
207 |
@app.route('/api/filmid', methods=['GET'])
|
208 |
def get_film_id_by_title():
|
|
|
213 |
film_id = get_film_id(title)
|
214 |
return jsonify({"film_id": film_id})
|
215 |
|
216 |
+
@app.route('/api/episodeid', methods=['GET'])
|
217 |
+
def get_episode_id():
|
218 |
+
"""Endpoint to get the episode ID by providing the TV show title, season, and episode."""
|
219 |
+
title = request.args.get('title')
|
220 |
+
season = request.args.get('season')
|
221 |
+
episode = request.args.get('episode')
|
222 |
+
if not title or not season or not episode:
|
223 |
+
return jsonify({"error": "Title, season, and episode parameters are required"}), 400
|
224 |
+
episode_id = f"{title}_S{season}_E{episode}"
|
225 |
+
return jsonify({"episode_id": episode_id})
|
226 |
+
|
227 |
+
@app.route('/api/proxy', methods=['GET'])
|
228 |
+
def get_proxy():
|
229 |
+
proxies = get_system_proxies()
|
230 |
+
return jsonify({"proxies": proxies})
|
231 |
+
|
232 |
+
@app.route('/api/size', methods=['GET'])
|
233 |
+
def get_cache_size():
|
234 |
+
total_size = 0
|
235 |
+
for dirpath, dirnames, filenames in os.walk(CACHE_DIR):
|
236 |
+
for f in filenames:
|
237 |
+
fp = os.path.join(dirpath, f)
|
238 |
+
total_size += os.path.getsize(fp)
|
239 |
+
readable_size = bytes_to_human_readable(total_size)
|
240 |
+
return jsonify({"cache_size": readable_size})
|
241 |
+
|
242 |
+
@app.route('/api/clear_cache', methods=['POST'])
|
243 |
+
def clear_cache():
|
244 |
+
for dirpath, dirnames, filenames in os.walk(CACHE_DIR):
|
245 |
+
for f in filenames:
|
246 |
+
fp = os.path.join(dirpath, f)
|
247 |
+
os.remove(fp)
|
248 |
+
return jsonify({"status": "Cache cleared"})
|
249 |
+
|
250 |
+
@app.route('/api/store/tv', methods=['GET'])
|
251 |
+
def get_tv_store():
|
252 |
+
"""Endpoint to get the TV store JSON."""
|
253 |
+
if os.path.exists(TV_STORE_JSON_PATH):
|
254 |
+
with open(TV_STORE_JSON_PATH, 'r') as json_file:
|
255 |
+
tv_store_data = json.load(json_file)
|
256 |
+
return jsonify(tv_store_data)
|
257 |
return jsonify({}), 404
|
258 |
|
259 |
+
@app.route('/api/film/metadata', methods=['GET'])
|
260 |
+
def get_film_metadata():
|
261 |
+
"""Endpoint to get the film metadata by title."""
|
262 |
title = request.args.get('title')
|
263 |
if not title:
|
264 |
return jsonify({'error': 'No title provided'}), 400
|
|
|
272 |
|
273 |
return jsonify({'error': 'Metadata not found'}), 404
|
274 |
|
275 |
+
@app.route('/api/tv/metadata', methods=['GET'])
|
276 |
+
def get_tv_metadata():
|
277 |
+
"""Endpoint to get the TV show metadata by title."""
|
278 |
+
title = request.args.get('title')
|
279 |
+
if not title:
|
280 |
+
return jsonify({'error': 'No title provided'}), 400
|
281 |
+
|
282 |
+
json_cache_path = os.path.join(CACHE_DIR, f"{urllib.parse.quote(title)}.json")
|
283 |
+
|
284 |
+
if os.path.exists(json_cache_path):
|
285 |
+
with open(json_cache_path, 'r') as f:
|
286 |
+
data = json.load(f)
|
287 |
+
|
288 |
+
# Add the file structure to the metadata
|
289 |
+
tv_path = find_tv_path(file_structure, title)
|
290 |
+
if tv_path:
|
291 |
+
data['file_structure'] = tv_path
|
292 |
+
|
293 |
+
return jsonify(data)
|
294 |
+
|
295 |
+
return jsonify({'error': 'Metadata not found'}), 404
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
296 |
|
297 |
# Routes
|
298 |
@app.route('/')
|