ChandimaPrabath commited on
Commit
f0de3f7
·
1 Parent(s): 4b1ee51

init player

Browse files
app.py CHANGED
@@ -1,11 +1,11 @@
1
- from flask import Flask, jsonify, render_template, redirect, request, send_file
2
  import os
3
  import json
4
  import requests
5
  import urllib.parse
6
  from datetime import datetime, timedelta
7
  from threading import Thread
8
- from hf_scraper import get_system_proxies
9
  from indexer import indexer
10
  from dotenv import load_dotenv
11
 
@@ -55,7 +55,7 @@ def get_thetvdb_token():
55
  authenticate_thetvdb()
56
  return THETVDB_TOKEN
57
 
58
- def fetch_and_cache_image(title, media_type, year=None):
59
  if year:
60
  search_url = f"{THETVDB_API_URL}/search?query={title}&type={media_type}&year={year}"
61
  else:
@@ -77,38 +77,34 @@ def fetch_and_cache_image(title, media_type, year=None):
77
  data = response.json()
78
 
79
  if 'data' in data and data['data']:
80
- img_url = data['data'][0].get('thumbnail')
81
- if img_url:
82
- img_content = requests.get(img_url, proxies=proxies).content
83
- cache_path = os.path.join(CACHE_DIR, f"{urllib.parse.quote(title)}.jpg")
84
- with open(cache_path, 'wb') as f:
85
- f.write(img_content)
86
- # Save JSON response to cache
87
- json_cache_path = os.path.join(CACHE_DIR, f"{urllib.parse.quote(title)}.json")
88
- with open(json_cache_path, 'w') as f:
89
- json.dump(data, f)
90
  except requests.RequestException as e:
91
  print(f"Error fetching data: {e}")
92
 
93
- def prefetch_images():
94
  for item in file_structure:
95
  if 'contents' in item:
96
  for sub_item in item['contents']:
97
- title = sub_item['path'].split('/')[-1]
98
  media_type = 'series' if item['path'].startswith('tv') else 'movie'
 
99
  year = None
100
- if any(char.isdigit() for char in title):
101
  # Strip year from title if present
102
- parts = title.split()
103
  year_str = parts[-1]
104
  if year_str.isdigit() and len(year_str) == 4:
105
- title = ' '.join(parts[:-1])
106
  year = int(year_str)
107
- fetch_and_cache_image(title, media_type, year)
108
 
109
- # Run prefetch_images in a background thread
110
  def start_prefetching():
111
- prefetch_images()
112
 
113
  # Start prefetching before running the Flask app
114
  thread = Thread(target=start_prefetching)
@@ -121,6 +117,10 @@ app = Flask(__name__)
121
  def home():
122
  return render_template('index.html')
123
 
 
 
 
 
124
  @app.route('/films')
125
  def list_films():
126
  films = [item for item in file_structure if item['path'].startswith('films')]
@@ -136,19 +136,36 @@ def play(file_path):
136
  file_url = f"https://huggingface.co/{REPO}/resolve/main/{file_path}"
137
  return redirect(file_url)
138
 
139
- @app.route('/get_image')
140
- def get_image():
141
  title = request.args.get('title')
142
  if not title:
143
  return jsonify({'error': 'No title provided'}), 400
144
 
145
- cache_path = os.path.join(CACHE_DIR, f"{urllib.parse.quote(title)}.jpg")
146
 
147
- if os.path.exists(cache_path):
148
- return send_file(cache_path, mimetype='image/jpeg')
 
 
149
 
150
- # If image is not found in cache, return a placeholder
151
- return send_file('path/to/placeholder.jpg', mimetype='image/jpeg') # Ensure you have a placeholder image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
 
153
  if __name__ == '__main__':
154
  app.run(debug=True, host="0.0.0.0", port=7860)
 
1
+ from flask import Flask, jsonify, render_template, redirect, request, Response
2
  import os
3
  import json
4
  import requests
5
  import urllib.parse
6
  from datetime import datetime, timedelta
7
  from threading import Thread
8
+ from hf_scrapper import get_system_proxies, stream_file
9
  from indexer import indexer
10
  from dotenv import load_dotenv
11
 
 
55
  authenticate_thetvdb()
56
  return THETVDB_TOKEN
57
 
58
+ def fetch_and_cache_json(original_title, title, media_type, year=None):
59
  if year:
60
  search_url = f"{THETVDB_API_URL}/search?query={title}&type={media_type}&year={year}"
61
  else:
 
77
  data = response.json()
78
 
79
  if 'data' in data and data['data']:
80
+ # Use original_title to save JSON response to cache
81
+ json_cache_path = os.path.join(CACHE_DIR, f"{urllib.parse.quote(original_title)}.json")
82
+ with open(json_cache_path, 'w') as f:
83
+ json.dump(data, f)
84
+
 
 
 
 
 
85
  except requests.RequestException as e:
86
  print(f"Error fetching data: {e}")
87
 
88
+ def prefetch_metadata():
89
  for item in file_structure:
90
  if 'contents' in item:
91
  for sub_item in item['contents']:
92
+ original_title = sub_item['path'].split('/')[-1]
93
  media_type = 'series' if item['path'].startswith('tv') else 'movie'
94
+ title = original_title
95
  year = None
96
+ if any(char.isdigit() for char in original_title):
97
  # Strip year from title if present
98
+ parts = original_title.rsplit(' ', 1)
99
  year_str = parts[-1]
100
  if year_str.isdigit() and len(year_str) == 4:
101
+ title = parts[0]
102
  year = int(year_str)
103
+ fetch_and_cache_json(original_title, title, media_type, year)
104
 
105
+ # Run prefetch_metadata in a background thread
106
  def start_prefetching():
107
+ prefetch_metadata()
108
 
109
  # Start prefetching before running the Flask app
110
  thread = Thread(target=start_prefetching)
 
117
  def home():
118
  return render_template('index.html')
119
 
120
+ @app.route('/player')
121
+ def player():
122
+ return render_template('player.html')
123
+
124
  @app.route('/films')
125
  def list_films():
126
  films = [item for item in file_structure if item['path'].startswith('films')]
 
136
  file_url = f"https://huggingface.co/{REPO}/resolve/main/{file_path}"
137
  return redirect(file_url)
138
 
139
+ @app.route('/get_metadata')
140
+ def get_metadata():
141
  title = request.args.get('title')
142
  if not title:
143
  return jsonify({'error': 'No title provided'}), 400
144
 
145
+ json_cache_path = os.path.join(CACHE_DIR, f"{urllib.parse.quote(title)}.json")
146
 
147
+ if os.path.exists(json_cache_path):
148
+ with open(json_cache_path, 'r') as f:
149
+ data = json.load(f)
150
+ return jsonify(data)
151
 
152
+ # If metadata is not found in cache, return an error
153
+ return jsonify({'error': 'Metadata not found'}), 404
154
+
155
+ @app.route('/stream')
156
+ def stream_video():
157
+ file_path = request.args.get('path')
158
+ if not file_path:
159
+ return "File path not provided", 400
160
+
161
+ proxies = get_system_proxies()
162
+ file_url = f"https://huggingface.co/{REPO}/resolve/main/{file_path}"
163
+
164
+ def generate():
165
+ for chunk in stream_file(file_url, TOKEN, proxies):
166
+ yield chunk
167
+
168
+ return Response(generate(), content_type='video/mp4')
169
 
170
  if __name__ == '__main__':
171
  app.run(debug=True, host="0.0.0.0", port=7860)
hf_scraper.py → hf_scrapper.py RENAMED
@@ -16,10 +16,22 @@ def get_system_proxies():
16
  print(f"Error getting system proxies: {e}")
17
  return {}
18
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  def download_file(file_url, token, output_path, proxies):
20
  print(f"Downloading file from URL: {file_url} with proxies: {proxies}")
21
  try:
22
- response = requests.get(file_url, headers={'Authorization': f'Bearer {token}'}, proxies=proxies, stream=True, verify=False)
23
  response.raise_for_status()
24
  with open(output_path, 'wb') as f:
25
  for chunk in response.iter_content(chunk_size=8192):
@@ -36,7 +48,7 @@ def get_file_structure(repo, token, path="", proxies=None):
36
  headers = {'Authorization': f'Bearer {token}'}
37
  print(f"Fetching file structure from URL: {api_url} with proxies: {proxies}")
38
  try:
39
- response = requests.get(api_url, headers=headers, proxies=proxies, verify=False)
40
  response.raise_for_status()
41
  return response.json()
42
  except RequestException as e:
 
16
  print(f"Error getting system proxies: {e}")
17
  return {}
18
 
19
+ def stream_file(file_url, token, proxies):
20
+ print(f"Streaming file from URL: {file_url} with proxies: {proxies}")
21
+ try:
22
+ response = requests.get(file_url, headers={'Authorization': f'Bearer {token}'}, proxies=proxies, stream=True)
23
+ response.raise_for_status()
24
+ for chunk in response.iter_content(chunk_size=8192):
25
+ if chunk:
26
+ yield chunk
27
+ except RequestException as e:
28
+ print(f"Error streaming file: {e}")
29
+ yield b'' # Return empty bytes to indicate an error
30
+
31
  def download_file(file_url, token, output_path, proxies):
32
  print(f"Downloading file from URL: {file_url} with proxies: {proxies}")
33
  try:
34
+ response = requests.get(file_url, headers={'Authorization': f'Bearer {token}'}, proxies=proxies, stream=True)
35
  response.raise_for_status()
36
  with open(output_path, 'wb') as f:
37
  for chunk in response.iter_content(chunk_size=8192):
 
48
  headers = {'Authorization': f'Bearer {token}'}
49
  print(f"Fetching file structure from URL: {api_url} with proxies: {proxies}")
50
  try:
51
+ response = requests.get(api_url, headers=headers, proxies=proxies)
52
  response.raise_for_status()
53
  return response.json()
54
  except RequestException as e:
indexer.py CHANGED
@@ -1,5 +1,5 @@
1
  import json
2
- from hf_scraper import get_system_proxies, get_file_structure, write_file_structure_to_json
3
  from dotenv import load_dotenv
4
  import os
5
 
 
1
  import json
2
+ from hf_scrapper import get_system_proxies, get_file_structure, write_file_structure_to_json
3
  from dotenv import load_dotenv
4
  import os
5
 
templates/index.html CHANGED
@@ -1,8 +1,6 @@
1
  <!DOCTYPE html>
2
- <html lang="en">
3
  <head>
4
- <meta charset="UTF-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
  <title>Media Library</title>
7
  <style>
8
  body {
@@ -79,24 +77,18 @@
79
  <div class="content">
80
  <div class="section" id="films">
81
  <h2>Films</h2>
82
- <div class="grid" id="films-grid">
83
- <!-- Film cards will be inserted here -->
84
- </div>
85
  </div>
86
  <div class="section" id="tv">
87
  <h2>TV Shows</h2>
88
- <div class="grid" id="tv-grid">
89
- <!-- TV show cards will be inserted here -->
90
- </div>
91
  </div>
92
  </div>
93
  <script>
94
  async function fetchData(endpoint) {
95
  try {
96
  const response = await fetch(endpoint);
97
- if (!response.ok) {
98
- throw new Error('Network response was not ok');
99
- }
100
  return await response.json();
101
  } catch (error) {
102
  console.error('Fetch error:', error);
@@ -104,64 +96,49 @@
104
  }
105
  }
106
 
107
- async function fetchImage(title) {
108
  try {
109
- const response = await fetch(`/get_image?title=${encodeURIComponent(title)}`);
110
  if (response.ok) {
111
- return response.url;
112
- } else {
113
- console.error('Image fetch error:', response.statusText);
114
- return `https://via.placeholder.com/200x300?text=${title}`;
 
115
  }
116
  } catch (error) {
117
- console.error('Image fetch error:', error);
118
- return `https://via.placeholder.com/200x300?text=${title}`;
119
  }
 
120
  }
121
 
122
- async function createCard(item) {
123
  const card = document.createElement('div');
124
  card.className = 'card';
125
-
126
  const title = item.path.split('/').pop();
127
- const imgSrc = await fetchImage(title);
128
-
129
- const img = document.createElement('img');
130
- img.src = imgSrc;
131
- img.alt = title;
132
- card.appendChild(img);
133
-
134
- const titleElement = document.createElement('h3');
135
- titleElement.textContent = title;
136
- card.appendChild(titleElement);
137
-
138
- if (item.contents) {
139
- const p = document.createElement('p');
140
- p.textContent = `Contains ${item.contents.length} items`;
141
- card.appendChild(p);
142
- } else {
143
- const link = document.createElement('a');
144
- link.href = `/play/${encodeURIComponent(item.path)}`;
145
- link.textContent = 'Play';
146
- card.appendChild(link);
147
- }
148
-
149
  return card;
150
  }
151
 
152
- async function loadSection(endpoint, containerId) {
 
153
  const data = await fetchData(endpoint);
154
- const container = document.getElementById(containerId);
155
- container.innerHTML = ''; // Clear previous content
156
- for (const item of data) {
157
- container.appendChild(await createCard(item));
158
- }
159
  }
160
 
161
- document.addEventListener('DOMContentLoaded', () => {
162
- loadSection('/films', 'films-grid');
163
- loadSection('/tv', 'tv-grid');
164
- });
165
  </script>
166
  </body>
167
  </html>
 
1
  <!DOCTYPE html>
2
+ <html>
3
  <head>
 
 
4
  <title>Media Library</title>
5
  <style>
6
  body {
 
77
  <div class="content">
78
  <div class="section" id="films">
79
  <h2>Films</h2>
80
+ <div class="grid" id="films-grid"></div>
 
 
81
  </div>
82
  <div class="section" id="tv">
83
  <h2>TV Shows</h2>
84
+ <div class="grid" id="tv-grid"></div>
 
 
85
  </div>
86
  </div>
87
  <script>
88
  async function fetchData(endpoint) {
89
  try {
90
  const response = await fetch(endpoint);
91
+ if (!response.ok) throw new Error('Network response was not ok');
 
 
92
  return await response.json();
93
  } catch (error) {
94
  console.error('Fetch error:', error);
 
96
  }
97
  }
98
 
99
+ async function fetchMetadata(title) {
100
  try {
101
+ const response = await fetch(`/get_metadata?title=${encodeURIComponent(title)}`);
102
  if (response.ok) {
103
+ const data = await response.json();
104
+ if (data.data && data.data.length > 0) {
105
+ const thumbnailUrl = data.data[0].thumbnail;
106
+ return thumbnailUrl;
107
+ }
108
  }
109
  } catch (error) {
110
+ console.error('Metadata fetch error:', error);
 
111
  }
112
+ return null;
113
  }
114
 
115
+ function createCard(item, mediaType) {
116
  const card = document.createElement('div');
117
  card.className = 'card';
 
118
  const title = item.path.split('/').pop();
119
+ card.innerHTML = `
120
+ <img src="https://via.placeholder.com/340x500.png" alt="${title}">
121
+ <h3>${title}</h3>
122
+ <a href="/player?path=${encodeURIComponent(item.path)}&type=${mediaType}">Play</a>
123
+ `;
124
+ fetchMetadata(title).then(thumbnailUrl => {
125
+ if (thumbnailUrl !== null) {
126
+ card.querySelector('img').src = thumbnailUrl;
127
+ }
128
+ });
 
 
 
 
 
 
 
 
 
 
 
 
129
  return card;
130
  }
131
 
132
+ async function populateGrid(endpoint, gridId, mediaType) {
133
+ const grid = document.getElementById(gridId);
134
  const data = await fetchData(endpoint);
135
+ data.forEach(item => {
136
+ grid.appendChild(createCard(item, mediaType));
137
+ });
 
 
138
  }
139
 
140
+ populateGrid('/films', 'films-grid', 'movie');
141
+ populateGrid('/tv', 'tv-grid', 'series');
 
 
142
  </script>
143
  </body>
144
  </html>
templates/player.html ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Media Player</title>
5
+ <style>
6
+ body {
7
+ font-family: Arial, sans-serif;
8
+ background-color: #141414;
9
+ color: #fff;
10
+ margin: 0;
11
+ padding: 0;
12
+ display: flex;
13
+ align-items: center;
14
+ justify-content: center;
15
+ height: 100vh;
16
+ }
17
+ .player-container {
18
+ text-align: center;
19
+ }
20
+ video {
21
+ width: 80%;
22
+ height: auto;
23
+ margin-top: 20px;
24
+ }
25
+ </style>
26
+ </head>
27
+ <body>
28
+ <div class="player-container">
29
+ <h1 id="title"></h1>
30
+ <video id="videoPlayer" controls></video>
31
+ </div>
32
+ <script>
33
+ function getQueryParams() {
34
+ const params = {};
35
+ window.location.search.substring(1).split("&").forEach(pair => {
36
+ const [key, value] = pair.split("=");
37
+ params[decodeURIComponent(key)] = decodeURIComponent(value);
38
+ });
39
+ return params;
40
+ }
41
+
42
+ async function fetchMetadata(title) {
43
+ try {
44
+ const response = await fetch(`/get_metadata?title=${encodeURIComponent(title)}`);
45
+ if (response.ok) {
46
+ return await response.json();
47
+ }
48
+ } catch (error) {
49
+ console.error('Metadata fetch error:', error);
50
+ }
51
+ return null;
52
+ }
53
+
54
+ async function initializePlayer() {
55
+ const params = getQueryParams();
56
+ const { path, type } = params;
57
+ const title = path.split('/').pop();
58
+
59
+ document.getElementById('title').textContent = title;
60
+
61
+ const metadata = await fetchMetadata(title);
62
+ if (metadata && metadata.data && metadata.data.length > 0) {
63
+ console.log(`Loaded metadata for ${title}:`, metadata);
64
+ } else {
65
+ console.log(`No metadata found for ${title}`);
66
+ }
67
+
68
+ const videoPlayer = document.getElementById('videoPlayer');
69
+ const videoUrl = `https://huggingface.co/${REPO}/resolve/main/${path}`;
70
+ videoPlayer.src = videoUrl;
71
+ console.log(`Playing video from URL: ${videoUrl}`);
72
+ }
73
+
74
+ initializePlayer();
75
+ </script>
76
+ </body>
77
+ </html>