This commit is contained in:
Eduard Prigoana 2025-02-05 16:55:42 +02:00
commit e4f83ca037
2 changed files with 27 additions and 17 deletions

View file

@ -1,10 +1,13 @@
import requests import requests
from flask import Flask, Response from flask import Flask, Response
from flask_cors import CORS
import threading
app = Flask(__name__) app = Flask(__name__)
CORS(app) # Enable CORS for all routes
# List of M3U file URLs # List of M3U file URLs
urls = [ URLS = [
"https://iptv-org.github.io/iptv/index.country.m3u", "https://iptv-org.github.io/iptv/index.country.m3u",
"https://forge.fsky.io/frost/repo/raw/branch/main/tv.m3u", "https://forge.fsky.io/frost/repo/raw/branch/main/tv.m3u",
"https://raw.githubusercontent.com/Free-TV/IPTV/refs/heads/master/playlist.m3u8", "https://raw.githubusercontent.com/Free-TV/IPTV/refs/heads/master/playlist.m3u8",
@ -16,13 +19,19 @@ urls = [
"https://raw.githubusercontent.com/reklamalinir/freeadultiptv/refs/heads/master/live_adult_channels.m3u" "https://raw.githubusercontent.com/reklamalinir/freeadultiptv/refs/heads/master/live_adult_channels.m3u"
] ]
combined_m3u = None # Variable to store the combined M3U content # Cached M3U content and a lock for thread safety
combined_m3u = None
lock = threading.Lock()
def fetch_m3u(url): def fetch_m3u(url):
"""Fetch M3U content from a given URL.""" """Fetch M3U content from a given URL with error handling."""
response = requests.get(url) try:
response.raise_for_status() # Raise an error for bad responses response = requests.get(url, timeout=5) # Set timeout to prevent long waits
return response.text response.raise_for_status() # Raise an error for bad responses
return response.text
except requests.RequestException as e:
print(f"Error fetching {url}: {e}")
return ""
def combine_m3u(urls): def combine_m3u(urls):
"""Combine M3U content from multiple URLs and remove duplicates.""" """Combine M3U content from multiple URLs and remove duplicates."""
@ -37,29 +46,29 @@ def combine_m3u(urls):
if line.startswith('#EXTINF:'): if line.startswith('#EXTINF:'):
current_entry = line current_entry = line
elif line and current_entry: elif line and current_entry:
# Use the stream link as the key to ensure uniqueness
stream_link = line.strip() stream_link = line.strip()
if stream_link not in unique_streams: if stream_link not in unique_streams:
unique_streams[stream_link] = current_entry + '\n' + stream_link unique_streams[stream_link] = f"{current_entry}\n{stream_link}"
current_entry = None # Reset current entry after adding current_entry = None # Reset after adding
# Return the combined M3U content
return '#EXTM3U\n' + '\n'.join(unique_streams.values()) return '#EXTM3U\n' + '\n'.join(unique_streams.values())
@app.route('/all.m3u') @app.route('/all.m3u')
def serve_m3u(): def serve_m3u():
"""Serve the combined M3U file.""" """Serve the cached combined M3U file, generating it if necessary."""
global combined_m3u global combined_m3u
if combined_m3u is None: # Generate if not already done with lock:
combined_m3u = combine_m3u(urls) if combined_m3u is None: # Generate if not already done
combined_m3u = combine_m3u(URLS)
return Response(combined_m3u, mimetype='application/vnd.apple.mpegurl') return Response(combined_m3u, mimetype='application/vnd.apple.mpegurl')
@app.route('/generate') @app.route('/generate')
def generate_m3u(): def generate_m3u():
"""Regenerate the M3U file from the sources.""" """Force regeneration of the M3U file from the sources."""
global combined_m3u global combined_m3u
combined_m3u = combine_m3u(urls) # Regenerate the M3U content with lock:
combined_m3u = combine_m3u(URLS) # Regenerate the M3U content
return Response("M3U playlist regenerated!", mimetype='text/plain') return Response("M3U playlist regenerated!", mimetype='text/plain')
if __name__ == '__main__': if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000) app.run(host='0.0.0.0', port=5000, threaded=True)

View file

@ -1,2 +1,3 @@
Flask flask
flask-cors
requests requests