Spaces:
Paused
Paused
Update wltv_server.py
Browse files- wltv_server.py +34 -93
wltv_server.py
CHANGED
|
@@ -2,10 +2,9 @@ import os
|
|
| 2 |
import requests
|
| 3 |
import gzip
|
| 4 |
import shutil
|
| 5 |
-
import logging
|
| 6 |
-
from flask import Flask, send_file, jsonify
|
| 7 |
import xml.etree.ElementTree as ET
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
# Set up logging
|
| 11 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -18,6 +17,9 @@ extract_to_dir = 'extracted'
|
|
| 18 |
if not os.path.exists(extract_to_dir):
|
| 19 |
os.makedirs(extract_to_dir, exist_ok=True)
|
| 20 |
|
|
|
|
|
|
|
|
|
|
| 21 |
# Function to download the file
|
| 22 |
def download_file(url, output_path):
|
| 23 |
logger.info(f"Downloading from {url}...")
|
|
@@ -41,95 +43,45 @@ def extract_gz_file(gz_path, extract_to_dir):
|
|
| 41 |
logger.error(f"GZ file does not exist: {gz_path}")
|
| 42 |
return
|
| 43 |
try:
|
| 44 |
-
extracted_file_path = os.path.join(extract_to_dir, 'it_wltv_full.xml')
|
| 45 |
with gzip.open(gz_path, 'rb') as f_in:
|
| 46 |
with open(extracted_file_path, 'wb') as f_out:
|
| 47 |
shutil.copyfileobj(f_in, f_out)
|
| 48 |
logger.info(f"Extracted GZ file to {extracted_file_path}")
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
logger.error(f"An error occurred while extracting the file: {e}")
|
| 51 |
|
| 52 |
-
# Function to
|
| 53 |
-
def
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
return None
|
| 58 |
-
|
| 59 |
try:
|
| 60 |
-
tree = ET.parse(
|
| 61 |
root = tree.getroot()
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
logger.info(f"No current program found for channel {channel_id}.")
|
| 75 |
-
return None
|
| 76 |
-
except Exception as e:
|
| 77 |
-
logger.error(f"Failed to parse XML file: {e}")
|
| 78 |
-
return None
|
| 79 |
-
|
| 80 |
-
# Function to dynamically create routes for each channel
|
| 81 |
-
def create_channel_routes():
|
| 82 |
-
xml_file = os.path.join(extract_to_dir, 'it_wltv_full.xml')
|
| 83 |
-
if not os.path.exists(xml_file):
|
| 84 |
-
logger.error("XML file does not exist to create dynamic routes")
|
| 85 |
-
return
|
| 86 |
-
|
| 87 |
-
try:
|
| 88 |
-
tree = ET.parse(xml_file)
|
| 89 |
-
root = tree.getroot()
|
| 90 |
-
|
| 91 |
-
# Find all channels in the XML
|
| 92 |
-
channels = {channel.get('id'): channel.find('display-name').text.replace(" ", "").lower()
|
| 93 |
-
for channel in root.findall('channel')}
|
| 94 |
-
|
| 95 |
-
for channel_id, route_name in channels.items():
|
| 96 |
-
# Create a dynamic route for each channel
|
| 97 |
-
create_route_for_channel(channel_id, route_name)
|
| 98 |
-
|
| 99 |
except Exception as e:
|
| 100 |
-
logger.error(f"
|
| 101 |
|
| 102 |
-
#
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
else:
|
| 110 |
-
return f"No current program found for {route_name}.", 404
|
| 111 |
-
|
| 112 |
-
# Flask route to serve the extracted file
|
| 113 |
-
@app.route('/wltv', methods=['GET'])
|
| 114 |
-
def serve_extracted_file():
|
| 115 |
-
try:
|
| 116 |
-
extracted_files = os.listdir(extract_to_dir)
|
| 117 |
-
logger.info(f"Files in 'extracted' directory: {extracted_files}")
|
| 118 |
-
for file_name in extracted_files:
|
| 119 |
-
logger.info(f"File: {file_name}, Extension: {os.path.splitext(file_name)[1]}")
|
| 120 |
-
except Exception as e:
|
| 121 |
-
logger.error(f"Error listing files in 'extracted' directory: {e}")
|
| 122 |
-
return "Error listing files", 500
|
| 123 |
-
|
| 124 |
-
file_name = 'it_wltv_full.xml' # Updated file name with extension
|
| 125 |
-
file_path = os.path.join(extract_to_dir, file_name)
|
| 126 |
-
|
| 127 |
-
if os.path.exists(file_path):
|
| 128 |
-
logger.info(f"Serving file from {file_path}")
|
| 129 |
-
return send_file(file_path, as_attachment=False, mimetype='text/plain')
|
| 130 |
-
else:
|
| 131 |
-
logger.error("File not found")
|
| 132 |
-
return "File not found", 404
|
| 133 |
|
| 134 |
# Route to fetch logs
|
| 135 |
@app.route('/logs', methods=['GET'])
|
|
@@ -152,18 +104,7 @@ def update_files():
|
|
| 152 |
download_file(file_url, file_path)
|
| 153 |
extract_gz_file(file_path, extract_to_dir)
|
| 154 |
|
| 155 |
-
return "Files updated
|
| 156 |
|
| 157 |
if __name__ == '__main__':
|
| 158 |
-
file_url = 'http://epg-guide.com/wltv.gz'
|
| 159 |
-
file_path = '/tmp/it_wltv_full.gz'
|
| 160 |
-
|
| 161 |
-
# Download and extract the gz file initially
|
| 162 |
-
download_file(file_url, file_path)
|
| 163 |
-
extract_gz_file(file_path, extract_to_dir)
|
| 164 |
-
|
| 165 |
-
# Create dynamic routes based on the extracted XML data
|
| 166 |
-
create_channel_routes()
|
| 167 |
-
|
| 168 |
-
# Start the Flask web server
|
| 169 |
app.run(host='0.0.0.0', port=8080)
|
|
|
|
| 2 |
import requests
|
| 3 |
import gzip
|
| 4 |
import shutil
|
|
|
|
|
|
|
| 5 |
import xml.etree.ElementTree as ET
|
| 6 |
+
import logging
|
| 7 |
+
from flask import Flask, send_file, jsonify, abort
|
| 8 |
|
| 9 |
# Set up logging
|
| 10 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 17 |
if not os.path.exists(extract_to_dir):
|
| 18 |
os.makedirs(extract_to_dir, exist_ok=True)
|
| 19 |
|
| 20 |
+
# In-memory storage for channel data
|
| 21 |
+
channels = {}
|
| 22 |
+
|
| 23 |
# Function to download the file
|
| 24 |
def download_file(url, output_path):
|
| 25 |
logger.info(f"Downloading from {url}...")
|
|
|
|
| 43 |
logger.error(f"GZ file does not exist: {gz_path}")
|
| 44 |
return
|
| 45 |
try:
|
| 46 |
+
extracted_file_path = os.path.join(extract_to_dir, 'it_wltv_full.xml')
|
| 47 |
with gzip.open(gz_path, 'rb') as f_in:
|
| 48 |
with open(extracted_file_path, 'wb') as f_out:
|
| 49 |
shutil.copyfileobj(f_in, f_out)
|
| 50 |
logger.info(f"Extracted GZ file to {extracted_file_path}")
|
| 51 |
+
parse_xml(extracted_file_path) # Update the in-memory data
|
| 52 |
except Exception as e:
|
| 53 |
logger.error(f"An error occurred while extracting the file: {e}")
|
| 54 |
|
| 55 |
+
# Function to parse the XML file and update in-memory data
|
| 56 |
+
def parse_xml(xml_path):
|
| 57 |
+
logger.info(f"Parsing XML file: {xml_path}")
|
| 58 |
+
global channels
|
| 59 |
+
channels = {} # Clear existing data
|
|
|
|
|
|
|
| 60 |
try:
|
| 61 |
+
tree = ET.parse(xml_path)
|
| 62 |
root = tree.getroot()
|
| 63 |
+
|
| 64 |
+
for channel in root.findall('channel'):
|
| 65 |
+
channel_id = channel.get('id')
|
| 66 |
+
display_name = channel.find('display-name').text if channel.find('display-name') is not None else 'Unknown'
|
| 67 |
+
icon_url = channel.find('icon').get('src') if channel.find('icon') is not None else 'No icon'
|
| 68 |
+
channels[channel_id] = {
|
| 69 |
+
'title': display_name,
|
| 70 |
+
'icon': icon_url,
|
| 71 |
+
'now': 'No current program information available'
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
logger.info(f"Updated channel data: {channels}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
except Exception as e:
|
| 76 |
+
logger.error(f"An error occurred while parsing the XML file: {e}")
|
| 77 |
|
| 78 |
+
# Route to fetch channel info
|
| 79 |
+
@app.route('/<channel_id>/now', methods=['GET'])
|
| 80 |
+
def get_channel_info(channel_id):
|
| 81 |
+
channel = channels.get(channel_id)
|
| 82 |
+
if not channel:
|
| 83 |
+
abort(404, description="Channel not found")
|
| 84 |
+
return jsonify(channel)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
# Route to fetch logs
|
| 87 |
@app.route('/logs', methods=['GET'])
|
|
|
|
| 104 |
download_file(file_url, file_path)
|
| 105 |
extract_gz_file(file_path, extract_to_dir)
|
| 106 |
|
| 107 |
+
return "Files updated and in-memory data refreshed", 200
|
| 108 |
|
| 109 |
if __name__ == '__main__':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
app.run(host='0.0.0.0', port=8080)
|