| import os |
| import requests |
| import gzip |
| import shutil |
| import logging |
| from flask import Flask, send_file, jsonify |
| import xml.etree.ElementTree as ET |
| from datetime import datetime |
| import pytz |
|
|
| |
| logging.basicConfig(level=logging.INFO) |
| logger = logging.getLogger(__name__) |
|
|
| app = Flask(__name__) |
|
|
| |
| extract_to_dir = 'extracted' |
| if not os.path.exists(extract_to_dir): |
| os.makedirs(extract_to_dir, exist_ok=True) |
|
|
| |
| xml_tree = None |
|
|
| |
| def download_file(url, output_path): |
| logger.info(f"Downloading from {url}...") |
| try: |
| response = requests.get(url) |
| if response.status_code == 200: |
| with open(output_path, 'wb') as f: |
| f.write(response.content) |
| logger.info(f"Downloaded file to {output_path} with size {len(response.content)} bytes") |
| else: |
| logger.error(f"Failed to download file. Status code: {response.status_code}") |
| except Exception as e: |
| logger.error(f"An error occurred while downloading the file: {e}") |
|
|
| |
| def extract_gz_file(gz_path, extract_to_dir): |
| logger.info(f"Extracting {gz_path} to {extract_to_dir}...") |
| if not os.path.exists(gz_path): |
| logger.error(f"GZ file does not exist: {gz_path}") |
| return |
| try: |
| extracted_file_path = os.path.join(extract_to_dir, 'it_generic_full_lite.xml') |
| with gzip.open(gz_path, 'rb') as f_in: |
| with open(extracted_file_path, 'wb') as f_out: |
| shutil.copyfileobj(f_in, f_out) |
| logger.info(f"Extracted GZ file to {extracted_file_path}") |
| except Exception as e: |
| logger.error(f"An error occurred while extracting the file: {e}") |
|
|
| |
| def parse_xml(): |
| global xml_tree |
| xml_file = os.path.join(extract_to_dir, 'it_generic_full_lite.xml') |
| if not os.path.exists(xml_file): |
| logger.error("XML file does not exist for program data") |
| return None |
|
|
| try: |
| xml_tree = ET.parse(xml_file) |
| logger.info("Parsed XML file and cached the tree") |
| except Exception as e: |
| logger.error(f"Failed to parse XML file: {e}") |
| xml_tree = None |
|
|
| |
| def get_current_program(channel_id): |
| global xml_tree |
| if xml_tree is None: |
| parse_xml() |
| |
| if xml_tree is None: |
| logger.error("XML tree is not available") |
| return None |
|
|
| root = xml_tree.getroot() |
|
|
| |
| italy_tz = pytz.timezone('Europe/Rome') |
|
|
| |
| current_time = datetime.now(italy_tz) |
| logger.info(f"Current Italy time: {current_time}") |
|
|
| for programme in root.findall('programme'): |
| if programme.get('channel') == channel_id: |
| |
| start_time = datetime.strptime(programme.get('start')[:14], "%Y%m%d%H%M%S") |
| start_time = italy_tz.localize(start_time) |
|
|
| stop_time = datetime.strptime(programme.get('stop')[:14], "%Y%m%d%H%M%S") |
| stop_time = italy_tz.localize(stop_time) |
|
|
| logger.info(f"Checking program: {programme.find('title').text} ({start_time} - {stop_time})") |
|
|
| |
| if start_time <= current_time < stop_time: |
| title = programme.find('title').text if programme.find('title') is not None else "No title" |
| desc = programme.find('desc').text if programme.find('desc') is not None else "No description" |
| return {"title": title, "description": desc} |
| logger.info(f"No current program found for {channel_id}.") |
| return None |
|
|
| |
| @app.route('/wltv', methods=['GET']) |
| def serve_extracted_file(): |
| file_name = 'it_generic_full_lite.xml' |
| file_path = os.path.join(extract_to_dir, file_name) |
| |
| if os.path.exists(file_path): |
| logger.info(f"Serving file from {file_path}") |
| return send_file(file_path, as_attachment=False, mimetype='text/xml') |
| else: |
| logger.error("File not found") |
| return "File not found", 404 |
|
|
| |
| @app.route('/logs', methods=['GET']) |
| def get_logs(): |
| try: |
| with open('app.log', 'r') as log_file: |
| logs = log_file.read() |
| return logs, 200 |
| except Exception as e: |
| logger.error(f"Failed to read logs: {e}") |
| return jsonify({"error": "Failed to read logs"}), 500 |
|
|
| |
| @app.route('/update', methods=['POST']) |
| def update_files(): |
| file_url = 'http://epg-guide.com/it.gz' |
| file_path = '/tmp/it_generic_full_lite.gz' |
|
|
| |
| download_file(file_url, file_path) |
| extract_gz_file(file_path, extract_to_dir) |
| |
| |
| global xml_tree |
| xml_tree = None |
|
|
| return "Files updated", 200 |
|
|
| |
| @app.route('/<channel_id>/now', methods=['GET']) |
| def current_program(channel_id): |
| program_info = get_current_program(channel_id) |
| if program_info: |
| return jsonify(program_info) |
| else: |
| return "No current program found for this channel.", 404 |
|
|
| if __name__ == '__main__': |
| file_url = 'http://epg-guide.com/it.gz' |
| file_path = '/tmp/it_generic_full_lite.gz' |
|
|
| |
| download_file(file_url, file_path) |
| extract_gz_file(file_path, extract_to_dir) |
| |
| |
| parse_xml() |
|
|
| |
| app.run(host='0.0.0.0', port=8080) |
|
|