File size: 4,948 Bytes
e264f26
5efa5ee
e264f26
 
 
86769fb
487a828
 
5efa5ee
e264f26
 
 
 
5efa5ee
 
e264f26
5efa5ee
feb5ce7
5efa5ee
 
 
e264f26
e4f04cc
86769fb
e264f26
5efa5ee
 
 
 
 
 
e264f26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
487a828
e264f26
487a828
 
 
 
 
 
 
 
e264f26
487a828
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e264f26
487a828
 
e264f26
 
487a828
 
 
e264f26
da44638
e264f26
 
 
5efa5ee
4e15391
e264f26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d35386
e264f26
0aa883f
e264f26
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from flask import Flask, jsonify, request, send_from_directory
import os
import json
import threading
from hf_scrapper import download_file, get_system_proxies, get_download_progress
from indexer import indexer
from tvdb import fetch_and_cache_json
import re

app = Flask(__name__)

# Constants and Configuration
CACHE_DIR = os.getenv("CACHE_DIR")
INDEX_FILE = os.getenv("INDEX_FILE")
TOKEN = os.getenv("TOKEN")
download_threads = {}

# Ensure CACHE_DIR exists
if not os.path.exists(CACHE_DIR):
    os.makedirs(CACHE_DIR)

# Index the file structure
indexer()

# Load the file structure JSON
if not os.path.exists(INDEX_FILE):
    raise FileNotFoundError(f"{INDEX_FILE} not found. Please make sure the file exists.")

with open(INDEX_FILE, 'r') as f:
    file_structure = json.load(f)

# Function Definitions

def load_json(file_path):
    """Load JSON data from a file."""
    with open(file_path, 'r') as file:
        return json.load(file)

def find_movie_path(json_data, title):
    """Find the path of the movie in the JSON data based on the title."""
    for directory in json_data:
        if directory['type'] == 'directory' and directory['path'] == 'films':
            for sub_directory in directory['contents']:
                if sub_directory['type'] == 'directory':
                    for item in sub_directory['contents']:
                        if item['type'] == 'file' and title.lower() in item['path'].lower():
                            return item['path']
    return None

def get_film_id(title):
    """Generate a film ID based on the title."""
    return title.replace(" ", "_").lower()

def prefetch_metadata():
    """Prefetch metadata for all items in the file structure."""
    for item in file_structure:
        if 'contents' in item:
            for sub_item in item['contents']:
                original_title = sub_item['path'].split('/')[-1]
                media_type = 'series' if item['path'].startswith('tv') else 'movie'
                title = original_title
                year = None
                
                # Extract year from the title if available
                match = re.search(r'\((\d{4})\)', original_title)
                if match:
                    year_str = match.group(1)
                    if year_str.isdigit() and len(year_str) == 4:
                        title = original_title[:match.start()].strip()
                        year = int(year_str)
                else:
                    parts = original_title.rsplit(' ', 1)
                    if len(parts) > 1 and parts[-1].isdigit() and len(parts[-1]) == 4:
                        title = parts[0].strip()
                        year = int(parts[-1])
                
                fetch_and_cache_json(original_title, title, media_type, year)

def start_prefetching():
    """Start the metadata prefetching in a separate thread."""
    prefetch_metadata()

# Start prefetching metadata
thread = threading.Thread(target=start_prefetching)
thread.daemon = True
thread.start()

# API Endpoints

@app.route('/api/movie', methods=['GET'])
def get_movie():
    """Endpoint to get the movie by title."""
    title = request.args.get('title')
    if not title:
        return jsonify({"error": "Title parameter is required"}), 400
    
    json_data = load_json(INDEX_FILE)
    movie_path = find_movie_path(json_data, title)
    
    if not movie_path:
        return jsonify({"error": "Movie not found"}), 404
    
    cache_path = os.path.join(CACHE_DIR, movie_path)
    if not os.path.exists(cache_path):
        file_url = f"https://huggingface.co/Unicone-Studio/jellyfin_media/resolve/main/{movie_path}"
        proxies = get_system_proxies()
        film_id = get_film_id(title)
        
        # Start the download in a separate thread if not already downloading
        if film_id not in download_threads or not download_threads[film_id].is_alive():
            thread = threading.Thread(target=download_file, args=(file_url, TOKEN, cache_path, proxies, film_id))
            download_threads[film_id] = thread
            thread.start()
        
        return jsonify({"status": "Download started", "film_id": film_id})
    
    return send_from_directory(os.path.dirname(cache_path), os.path.basename(cache_path))

@app.route('/api/progress/<film_id>', methods=['GET'])
def get_progress(film_id):
    """Endpoint to get the download progress of a movie."""
    progress = get_download_progress(film_id)
    return jsonify({"film_id": film_id, "progress": progress})

@app.route('/api/filmid', methods=['GET'])
def get_film_id_by_title():
    """Endpoint to get the film ID by providing the movie title."""
    title = request.args.get('title')
    if not title:
        return jsonify({"error": "Title parameter is required"}), 400
    film_id = get_film_id(title)
    return jsonify({"film_id": film_id})

# Main entry point
if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0", port=7860)