ChandimaPrabath commited on
Commit
6e5685b
·
1 Parent(s): e2deb31

test tvdb new impro

Browse files
Files changed (3) hide show
  1. README.md +138 -281
  2. app.py +7 -15
  3. tvdb.py +15 -1
README.md CHANGED
@@ -10,299 +10,156 @@ pinned: false
10
  ---
11
 
12
 
13
- ## Scripts
14
- ```
15
- app.py -> main script that run flask server
16
- hf_scrapper.py -> script for interacting with huggingface
17
- indexer.py -> script to index the repo structure
18
- tvdb.py -> script to interact with TheTVDB
19
- ```
20
- ## Film and TV API
21
-
22
- This API provides endpoints for accessing and managing film and TV show data, including downloading, caching, and retrieving metadata.
23
 
24
- ## Table of Contents
25
 
 
26
 
27
- - [Base URL](#base-url)
28
- - [Endpoints](#endpoints)
29
- - [Film Endpoints](#film-endpoints)
30
- - [TV Show Endpoints](#tv-show-endpoints)
31
- - [Cache Endpoints](#cache-endpoints)
32
- - [Metadata Endpoints](#metadata-endpoints)
33
- - [Miscellaneous Endpoints](#miscellaneous-endpoints)
34
- - [Error Handling](#error-handling)
35
- - [Running the Server](#running-the-server)
36
 
 
37
 
38
- ## Base URL
 
39
 
40
- All endpoints are accessed through the base URL:
41
-
42
- ```markdown
43
- http://<server-address>:7860
44
- ```
45
 
46
- Replace `<server-address>` with your server's address.
47
-
48
- ## Endpoints
49
-
50
- ### Film Endpoints
51
-
52
- #### `GET /api/film`
53
-
54
- **Description:** Starts the download of a film if it's not already cached.
55
-
56
- **Query Parameters:**
57
- - `title` (string): The title of the film.
58
-
59
- **Responses:**
60
- - `200 OK`: Download started successfully.
61
- ```json
62
- {
63
- "status": "Download started",
64
- "film_id": "film_id_here"
65
- }
66
- ```
67
- - `400 Bad Request`: Title parameter is required.
68
- ```json
69
- {
70
- "error": "Title parameter is required"
71
- }
72
- ```
73
- - `404 Not Found`: Movie not found.
74
-
75
- #### `GET /api/film/store`
76
-
77
- **Description:** Retrieves the JSON data for the film store.
78
-
79
- **Responses:**
80
- - `200 OK`: Returns the film store JSON data.
81
- ```json
82
- {
83
- "film_title": "cache_path_here"
84
- }
85
- ```
86
- - `404 Not Found`: Film store JSON not found.
87
-
88
- #### `GET /api/film/metadata`
89
-
90
- **Description:** Retrieves metadata for a film by title.
91
-
92
- **Query Parameters:**
93
- - `title` (string): The title of the film.
94
-
95
- **Responses:**
96
- - `200 OK`: Returns the metadata JSON for the film.
97
- ```json
98
- {
99
- "title": "Film Title",
100
- "year": 2024,
101
- "metadata": { ... }
102
- }
103
- ```
104
- - `400 Bad Request`: No title provided.
105
- ```json
106
- {
107
- "error": "No title provided"
108
- }
109
- ```
110
- - `404 Not Found`: Metadata not found.
111
-
112
- ### TV Show Endpoints
113
-
114
- #### `GET /api/tv`
115
-
116
- **Description:** Starts the download of a TV show episode if it's not already cached.
117
-
118
- **Query Parameters:**
119
- - `title` (string): The title of the TV show.
120
- - `season` (string): The season number.
121
- - `episode` (string): The episode number.
122
-
123
- **Responses:**
124
- - `200 OK`: Download started successfully.
125
- ```json
126
- {
127
- "status": "Download started",
128
- "episode_id": "episode_id_here"
129
- }
130
- ```
131
- - `400 Bad Request`: Title, season, and episode parameters are required.
132
- ```json
133
- {
134
- "error": "Title, season, and episode parameters are required"
135
- }
136
- ```
137
- - `404 Not Found`: TV show or episode not found.
138
-
139
- #### `GET /api/tv/store`
140
-
141
- **Description:** Retrieves the JSON data for the TV store.
142
-
143
- **Responses:**
144
- - `200 OK`: Returns the TV store JSON data.
145
- ```json
146
- {
147
- "show_title": {
148
- "season": {
149
- "episode": "cache_path_here"
150
- }
151
- }
152
- }
153
- ```
154
- - `404 Not Found`: TV store JSON not found.
155
-
156
- #### `GET /api/tv/metadata`
157
-
158
- **Description:** Retrieves metadata for a TV show by title.
159
-
160
- **Query Parameters:**
161
- - `title` (string): The title of the TV show.
162
-
163
- **Responses:**
164
- - `200 OK`: Returns the metadata JSON for the TV show.
165
- ```json
166
- {
167
- "title": "TV Show Title",
168
- "seasons": [ ... ],
169
- "metadata": { ... }
170
- }
171
- ```
172
- - `400 Bad Request`: No title provided.
173
- ```json
174
- {
175
- "error": "No title provided"
176
- }
177
- ```
178
- - `404 Not Found`: Metadata not found.
179
-
180
- ### Cache Endpoints
181
-
182
- #### `GET /api/cache/size`
183
-
184
- **Description:** Retrieves the total size of the cache.
185
-
186
- **Responses:**
187
- - `200 OK`: Returns the cache size in a human-readable format.
188
- ```json
189
- {
190
- "cache_size": "10.5 MB"
191
- }
192
- ```
193
-
194
- #### `POST /api/cache/clear`
195
-
196
- **Description:** Clears the entire cache.
197
-
198
- **Responses:**
199
- - `200 OK`: Cache cleared successfully.
200
- ```json
201
- {
202
- "status": "Cache cleared"
203
- }
204
- ```
205
-
206
- ### Metadata Endpoints
207
-
208
- #### `GET /api/filmid`
209
-
210
- **Description:** Retrieves the film ID by title.
211
-
212
- **Query Parameters:**
213
- - `title` (string): The title of the film.
214
-
215
- **Responses:**
216
- - `200 OK`: Returns the film ID.
217
- ```json
218
- {
219
- "film_id": "film_id_here"
220
- }
221
- ```
222
- - `400 Bad Request`: Title parameter is required.
223
- ```json
224
- {
225
- "error": "Title parameter is required"
226
- }
227
- ```
228
-
229
- #### `GET /api/episodeid`
230
-
231
- **Description:** Retrieves the episode ID by title, season, and episode.
232
-
233
- **Query Parameters:**
234
- - `title` (string): The title of the TV show.
235
- - `season` (string): The season number.
236
- - `episode` (string): The episode number.
237
-
238
- **Responses:**
239
- - `200 OK`: Returns the episode ID.
240
- ```json
241
- {
242
- "episode_id": "episode_id_here"
243
- }
244
- ```
245
- - `400 Bad Request`: Title, season, and episode parameters are required.
246
- ```json
247
- {
248
- "error": "Title, season, and episode parameters are required"
249
- }
250
- ```
251
-
252
- ### Miscellaneous Endpoints
253
-
254
- #### `GET /api/film/all`
255
-
256
- **Description:** Retrieves a list of all films.
257
-
258
- **Responses:**
259
- - `200 OK`: Returns a list of film paths.
260
- ```json
261
- [
262
- "film_path_1",
263
- "film_path_2"
264
- ]
265
- ```
266
-
267
- #### `GET /api/tv/all`
268
-
269
- **Description:** Retrieves a list of all TV shows.
270
-
271
- **Responses:**
272
- - `200 OK`: Returns a list of TV shows with their episodes.
273
- ```json
274
- {
275
- "show_title": [
276
- {
277
- "season": "season_number",
278
- "episode": "episode_title"
279
- }
280
- ]
281
- }
282
- ```
283
-
284
- ## Error Handling
285
-
286
- All endpoints return standard HTTP status codes:
287
- - `200 OK` for successful requests.
288
- - `400 Bad Request` for invalid requests.
289
- - `404 Not Found` for missing resources.
290
-
291
- Errors are returned in the following format:
292
- ```json
293
- {
294
- "error": "Error message here"
295
- }
296
- ```
297
 
298
- ## Running the Server
299
 
300
- To run the server, ensure you have all required dependencies installed and use the following command:
301
 
302
  ```bash
303
  python app.py
304
  ```
305
 
306
- The server will start on `http://0.0.0.0:7860` by default.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
307
 
308
- ---
 
10
  ---
11
 
12
 
13
+ # API Documentation
 
 
 
 
 
 
 
 
 
14
 
15
+ This is a Flask-based API application that serves various endpoints to manage and retrieve information about films and TV shows, including caching, metadata, and download progress. The application functions as a load balancer to manage media content.
16
 
17
+ ## Prerequisites
18
 
19
+ - Python 3.10
20
+ - Flask
21
+ - Flask-CORS
22
+ - requests
23
+ - python-dotenv
24
+ - tqdm
 
 
 
25
 
26
+ ## Installation
27
 
28
+ 1. Clone the repository.
29
+ 2. Install the required packages using pip:
30
 
31
+ ```bash
32
+ pip install -r requirements.txt
33
+ ```
 
 
34
 
35
+ 3. Set up the necessary environment variables:
36
+ - `CACHE_DIR`: Directory where cached files are stored.
37
+ - `INDEX_FILE`: Path to the index file.
38
+ - `TOKEN`: API token for Huggingface.
39
+ - `REPO`: Huggingface Repository URL.
40
+ - `THETVDB_API_KEY` : TheTVDB API key.
41
+ - `THETVDB_API_URL` : TheTVDB API url
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
+ ## Usage
44
 
45
+ To start the Flask server, run:
46
 
47
  ```bash
48
  python app.py
49
  ```
50
 
51
+ The server will run on `http://0.0.0.0:7860`.
52
+
53
+ ## API Endpoints
54
+
55
+ ### Get Movie by Title
56
+
57
+ - **URL:** `/api/film/<title>`
58
+ - **Method:** `GET`
59
+ - **Description:** Retrieves the URL of the movie by its title. If not cached, it starts the download process.
60
+ - **Parameters:**
61
+ - `title` (string): The title of the movie.
62
+
63
+ ### Get TV Show Episode by Title, Season, and Episode
64
+
65
+ - **URL:** `/api/tv/<title>/<season>/<episode>`
66
+ - **Method:** `GET`
67
+ - **Description:** Retrieves the URL of the TV show episode by its title, season, and episode. If not cached, it starts the download process.
68
+ - **Parameters:**
69
+ - `title` (string): The title of the TV show.
70
+ - `season` (string): The season number.
71
+ - `episode` (string): The episode number.
72
+
73
+ ### Get Film ID by Title
74
+
75
+ - **URL:** `/api/filmid/<title>`
76
+ - **Method:** `GET`
77
+ - **Description:** Retrieves the film ID by providing the movie title.
78
+ - **Parameters:**
79
+ - `title` (string): The title of the movie.
80
+
81
+ ### Get Episode ID by Title, Season, and Episode
82
+
83
+ - **URL:** `/api/episodeid/<title>/<season>/<episode>`
84
+ - **Method:** `GET`
85
+ - **Description:** Retrieves the episode ID by providing the TV show title, season, and episode.
86
+ - **Parameters:**
87
+ - `title` (string): The title of the TV show.
88
+ - `season` (string): The season number.
89
+ - `episode` (string): The episode number.
90
+
91
+ ### Get Cache Size
92
+
93
+ - **URL:** `/api/cache/size`
94
+ - **Method:** `GET`
95
+ - **Description:** Retrieves the total size of the cache in a human-readable format.
96
+
97
+ ### Clear Cache
98
+
99
+ - **URL:** `/api/cache/clear`
100
+ - **Method:** `POST`
101
+ - **Description:** Clears the entire cache.
102
+
103
+ ### Get TV Store JSON
104
+
105
+ - **URL:** `/api/tv/store`
106
+ - **Method:** `GET`
107
+ - **Description:** Retrieves the TV store JSON.
108
+
109
+ ### Get Film Store JSON
110
+
111
+ - **URL:** `/api/film/store`
112
+ - **Method:** `GET`
113
+ - **Description:** Retrieves the film store JSON.
114
+
115
+ ### Get Film Metadata by Title
116
+
117
+ - **URL:** `/api/film/metadata/<title>`
118
+ - **Method:** `GET`
119
+ - **Description:** Retrieves the metadata of a film by its title.
120
+ - **Parameters:**
121
+ - `title` (string): The title of the movie.
122
+
123
+ ### Get TV Show Metadata by Title
124
+
125
+ - **URL:** `/api/tv/metadata/<title>`
126
+ - **Method:** `GET`
127
+ - **Description:** Retrieves the metadata of a TV show by its title.
128
+ - **Parameters:**
129
+ - `title` (string): The title of the TV show.
130
+
131
+ ### Get All Films
132
+
133
+ - **URL:** `/api/film/all`
134
+ - **Method:** `GET`
135
+ - **Description:** Retrieves a list of all films.
136
+
137
+ ### Get All TV Shows
138
+
139
+ - **URL:** `/api/tv/all`
140
+ - **Method:** `GET`
141
+ - **Description:** Retrieves a list of all TV shows.
142
+
143
+ ### Get Instances
144
+
145
+ - **URL:** `/api/instances`
146
+ - **Method:** `GET`
147
+ - **Description:** Retrieves a list of all registered instances.
148
+
149
+ ### Get Instances Health
150
+
151
+ - **URL:** `/api/instances/health`
152
+ - **Method:** `GET`
153
+ - **Description:** Retrieves the health status of all registered instances.
154
+
155
+ ### Register Instance (Only for Instance)
156
+
157
+ - **URL:** `/api/register`
158
+ - **Method:** `POST`
159
+ - **Description:** Registers a new instance.
160
+ - **Request Body:**
161
+ - `url` (string): The URL of the instance.
162
+
163
+ ## License
164
 
165
+ This project is licensed under the `MIT License`
app.py CHANGED
@@ -92,12 +92,6 @@ def get_tv_show_api(title, season, episode):
92
  if response:
93
  return jsonify(response)
94
 
95
- @app.route('/api/progress/<id>', methods=['GET'])
96
- def get_progress_api(id):
97
- """Endpoint to get the download progress of a movie or TV show episode."""
98
- progress = load_balancer.get_download_progress(id)
99
- return jsonify({"id": id, "progress": progress})
100
-
101
  @app.route('/api/filmid/<title>', methods=['GET'])
102
  def get_film_id_by_title_api(title):
103
  """Endpoint to get the film ID by providing the movie title."""
@@ -195,9 +189,15 @@ def get_all_films_api():
195
  def get_all_tvshows_api():
196
  return load_balancer.get_all_tv_shows()
197
 
 
 
 
198
 
 
 
 
199
  #############################################################
200
- # unique api's
201
  @app.route('/api/register', methods=['POST'])
202
  def register_instance():
203
  try:
@@ -219,14 +219,6 @@ def register_instance():
219
  logging.error(f"Error registering instance: {e}")
220
  return jsonify({"error": "Failed to register instance"}), 500
221
 
222
- @app.route('/api/instances',methods=["GET"])
223
- def get_instances():
224
- return load_balancer.instances
225
-
226
- @app.route('/api/instances/health',methods=["GET"])
227
- def get_instances_health():
228
- return load_balancer.instances_health
229
-
230
  #############################################################
231
  # Routes
232
  @app.route('/')
 
92
  if response:
93
  return jsonify(response)
94
 
 
 
 
 
 
 
95
  @app.route('/api/filmid/<title>', methods=['GET'])
96
  def get_film_id_by_title_api(title):
97
  """Endpoint to get the film ID by providing the movie title."""
 
189
  def get_all_tvshows_api():
190
  return load_balancer.get_all_tv_shows()
191
 
192
+ @app.route('/api/instances',methods=["GET"])
193
+ def get_instances():
194
+ return load_balancer.instances
195
 
196
+ @app.route('/api/instances/health',methods=["GET"])
197
+ def get_instances_health():
198
+ return load_balancer.instances_health
199
  #############################################################
200
+ # This API is only for instances
201
  @app.route('/api/register', methods=['POST'])
202
  def register_instance():
203
  try:
 
219
  logging.error(f"Error registering instance: {e}")
220
  return jsonify({"error": "Failed to register instance"}), 500
221
 
 
 
 
 
 
 
 
 
222
  #############################################################
223
  # Routes
224
  @app.route('/')
tvdb.py CHANGED
@@ -57,14 +57,28 @@ def fetch_and_cache_json(original_title, title, media_type, year=None):
57
  }
58
 
59
  try:
 
60
  response = requests.get(search_url, headers=headers, proxies=proxies)
61
  response.raise_for_status()
62
  data = response.json()
63
 
64
  if 'data' in data and data['data']:
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  json_cache_path = os.path.join(CACHE_DIR, f"{urllib.parse.quote(original_title)}.json")
66
  with open(json_cache_path, 'w') as f:
67
- json.dump(data, f)
68
 
69
  except requests.RequestException as e:
70
  print(f"Error fetching data: {e}")
 
57
  }
58
 
59
  try:
60
+ # Fetch initial search results
61
  response = requests.get(search_url, headers=headers, proxies=proxies)
62
  response.raise_for_status()
63
  data = response.json()
64
 
65
  if 'data' in data and data['data']:
66
+ # Extract the TVDB ID from the first result
67
+ tvdb_id = data['data'][0]['tvdb_id']
68
+ if not tvdb_id:
69
+ print("TVDB ID not found in the search results")
70
+ return
71
+
72
+ # Request the extended information using the TVDB ID
73
+ extended_url = f"{THETVDB_API_URL}/movies/{tvdb_id}/extended?meta=translations"
74
+ response = requests.get(extended_url, headers=headers, proxies=proxies)
75
+ response.raise_for_status()
76
+ extended_data = response.json()
77
+
78
+ # Cache the extended JSON response
79
  json_cache_path = os.path.join(CACHE_DIR, f"{urllib.parse.quote(original_title)}.json")
80
  with open(json_cache_path, 'w') as f:
81
+ json.dump(extended_data, f)
82
 
83
  except requests.RequestException as e:
84
  print(f"Error fetching data: {e}")