diff --git a/Dockers/iptv-filter/Dockerfile b/Dockers/iptv-filter/Dockerfile index 907ca7c..befbf18 100644 --- a/Dockers/iptv-filter/Dockerfile +++ b/Dockers/iptv-filter/Dockerfile @@ -1,7 +1,13 @@ -FROM php:7.2-apache +FROM python:3.9-slim -COPY . /var/www/html -COPY vhost.conf /etc/apache2/sites-available/000-default.conf +WORKDIR /app -EXPOSE 80 -RUN chown -R www-data:www-data /var/www/html && a2enmod rewrite \ No newline at end of file +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +COPY main.py . +COPY version . + +EXPOSE 8000 + +CMD ["python", "main.py"] diff --git a/Dockers/iptv-filter/README.md b/Dockers/iptv-filter/README.md deleted file mode 100644 index 895eb51..0000000 --- a/Dockers/iptv-filter/README.md +++ /dev/null @@ -1,21 +0,0 @@ -# IPTV M3U filter - -A simple web server that will download an M3U file, at most once a day, and filter that file down to selected groups. - -## Docker - -To build and run the docker image use: - - docker build -t iptv-filter . - docker run -itd --name iptv-filter -p 81:80 iptv-filter - -You can connect to the container with: - - docker exec -it iptv-filter /bin/bash - -## Docker - Raspberry Pi - -To build the image on a Raspberry Pi, use: - - docker build -f Dockerfile.rpi -t iptv-filter . - docker run -itd --name iptv-filter -p 81:80 iptv-filter diff --git a/Dockers/iptv-filter/iptv-boxsets.php b/Dockers/iptv-filter/iptv-boxsets.php deleted file mode 100644 index 7fec8c0..0000000 --- a/Dockers/iptv-filter/iptv-boxsets.php +++ /dev/null @@ -1,8 +0,0 @@ - /tmp/iptv-boxsets.m3u'); - -header("Content-Description: File Transfer"); -header("Content-Type: application/octet-stream"); -header("Content-Disposition: attachment; filename=\"iptv-boxsets.m3u\""); -readfile("/tmp/iptv-boxsets.m3u"); -?> \ No newline at end of file diff --git a/Dockers/iptv-filter/iptv-boxsets.sh b/Dockers/iptv-filter/iptv-boxsets.sh deleted file mode 100755 index 5a7e7f9..0000000 --- a/Dockers/iptv-filter/iptv-boxsets.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash - -m3u_src="http://iptv.example.com/iptv.m3u" -m3u_file=/tmp/iptv-`date +%Y%m%d`.m3u - -if [ ! -f "$m3u_file" ]; then - # Delete files downloaded over seven days ago. - find /tmp/iptv-* -mtime +7 -exec rm {} \; - - # Download the m3u files. - curl -s $m3u_src --output $m3u_file -fi - -echo "#EXTM3U" -grep -E "^#EXTINF.*group\-title=\"\|EN\| SERIES" -A 1 $m3u_file | sed "/^--$/d" -grep -E "^#EXTINF.*group\-title=\"\|EN.+MOVIES" -A 1 $m3u_file | sed "/^--$/d" - -exit 0 \ No newline at end of file diff --git a/Dockers/iptv-filter/iptv-tv.php b/Dockers/iptv-filter/iptv-tv.php deleted file mode 100644 index 50e2e03..0000000 --- a/Dockers/iptv-filter/iptv-tv.php +++ /dev/null @@ -1,8 +0,0 @@ - /tmp/iptv-tv.m3u'); - -header("Content-Description: File Transfer"); -header("Content-Type: application/octet-stream"); -header("Content-Disposition: attachment; filename=\"iptv-tv.m3u\""); -readfile("/tmp/iptv-tv.m3u"); -?> \ No newline at end of file diff --git a/Dockers/iptv-filter/iptv-tv.sh b/Dockers/iptv-filter/iptv-tv.sh deleted file mode 100755 index 84d0a51..0000000 --- a/Dockers/iptv-filter/iptv-tv.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/bash - -m3u_src="http://iptv.example.com/iptv.m3u" -m3u_file=/tmp/iptv-`date +%Y%m%d`.m3u - -if [ ! -f "$m3u_file" ]; then - # Delete files downloaded over seven days ago. - find /tmp/iptv-* -mtime +7 -exec rm {} \; - - # Download the m3u files. - curl -s $m3u_src --output $m3u_file -fi - -echo "#EXTM3U" -grep -E '^#EXTINF.*group\-title=\"IRE' -A 1 $m3u_file | sed "/^--$/d" -grep -E '^#EXTINF.*group\-title=\"UK\|' -A 1 $m3u_file | sed "/^--$/d" -grep -E '^#EXTINF.*group\-title=\"USA' -A 1 $m3u_file | sed "/^--$/d" -grep -E '^#EXTINF.*group\-title=\"CA' -A 1 $m3u_file | sed "/^--$/d" -grep -E '^#EXTINF.*group\-title=\"SP' -A 1 $m3u_file | sed "/^--$/d" - -exit 0 \ No newline at end of file diff --git a/Dockers/iptv-filter/main.py b/Dockers/iptv-filter/main.py new file mode 100644 index 0000000..ff783a9 --- /dev/null +++ b/Dockers/iptv-filter/main.py @@ -0,0 +1,79 @@ +import os +import requests +import schedule +import time +from datetime import datetime +from flask import Flask, Response +from threading import Thread + +# Configuration +SOURCE_URL = os.environ.get('SOURCE_URL', 'http://example.com/source.m3u8') +PORT = int(os.environ.get('PORT', 8000)) +LOCAL_FILE_PATH = 'playlist.m3u8' + +app = Flask(__name__) + +def download_m3u8(): + """Download the M3U8 file from the source URL""" + try: + print(f"{datetime.now()} - Downloading M3U8 from {SOURCE_URL}") + response = requests.get(SOURCE_URL, timeout=30) + + if response.status_code == 200: + with open(LOCAL_FILE_PATH, 'wb') as f: + f.write(response.content) + print(f"{datetime.now()} - M3U8 file downloaded successfully") + return True + else: + print(f"{datetime.now()} - Failed to download M3U8. Status code: {response.status_code}") + return False + except Exception as e: + print(f"{datetime.now()} - Error downloading M3U8: {str(e)}") + return False + +@app.route('/playlist.m3u8') +def serve_playlist(): + """Serve the M3U8 file""" + try: + with open(LOCAL_FILE_PATH, 'rb') as f: + content = f.read() + return Response(content, mimetype='application/vnd.apple.mpegurl') + except Exception as e: + return str(e), 500 + +@app.route('/health') +def health_check(): + """Health check endpoint""" + if os.path.exists(LOCAL_FILE_PATH): + return "OK", 200 + else: + return "Playlist not available", 503 + +def schedule_downloads(): + """Schedule the M3U8 download to run at midnight every day""" + schedule.every().day.at("00:00").do(download_m3u8) + + while True: + schedule.run_pending() + time.sleep(60) # Check every minute + +def main(): + """Main function to start the application""" + # Initial download + if not download_m3u8(): + print("Initial download failed. Retrying in 30 seconds...") + time.sleep(30) + if not download_m3u8(): + print("Second attempt failed. Starting server anyway...") + + # Start the scheduler in a separate thread + scheduler_thread = Thread(target=schedule_downloads) + scheduler_thread.daemon = True + scheduler_thread.start() + + # Start the Flask app + print(f"{datetime.now()} - Starting web server on port {PORT}") + app.run(host='0.0.0.0', port=PORT) + +if __name__ == '__main__': + main() diff --git a/Dockers/iptv-filter/requirements.txt b/Dockers/iptv-filter/requirements.txt new file mode 100644 index 0000000..89e5967 --- /dev/null +++ b/Dockers/iptv-filter/requirements.txt @@ -0,0 +1,3 @@ +flask==2.0.1 +requests==2.26.0 +schedule==1.1.0 \ No newline at end of file diff --git a/Dockers/iptv-filter/vhost.conf b/Dockers/iptv-filter/vhost.conf deleted file mode 100644 index 8fcb6ab..0000000 --- a/Dockers/iptv-filter/vhost.conf +++ /dev/null @@ -1,9 +0,0 @@ - - DocumentRoot /var/www/html - - - AllowOverride all - Require all granted - DirectoryIndex iptv-tv.php - - \ No newline at end of file