Spaces:
Paused
Paused
Update wltv_server.py
Browse files- wltv_server.py +27 -32
wltv_server.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
-
import
|
|
|
|
| 4 |
import logging
|
| 5 |
from flask import Flask, send_file, jsonify
|
| 6 |
|
|
@@ -15,17 +16,15 @@ extract_to_dir = 'extracted'
|
|
| 15 |
if not os.path.exists(extract_to_dir):
|
| 16 |
os.makedirs(extract_to_dir, exist_ok=True)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# Step 1: Download the zip file
|
| 21 |
-
def download_zip_file(url, output_path):
|
| 22 |
logger.info(f"Downloading from {url}...")
|
| 23 |
try:
|
| 24 |
response = requests.get(url)
|
| 25 |
if response.status_code == 200:
|
| 26 |
with open(output_path, 'wb') as f:
|
| 27 |
f.write(response.content)
|
| 28 |
-
logger.info(f"Downloaded
|
| 29 |
else:
|
| 30 |
logger.error(f"Failed to download file. Status code: {response.status_code}")
|
| 31 |
except PermissionError as e:
|
|
@@ -33,26 +32,22 @@ def download_zip_file(url, output_path):
|
|
| 33 |
except Exception as e:
|
| 34 |
logger.error(f"An error occurred while downloading the file: {e}")
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
def
|
| 38 |
-
logger.info(f"Extracting {
|
| 39 |
-
if not os.path.exists(
|
| 40 |
-
logger.error(f"
|
| 41 |
return
|
| 42 |
try:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
for file in extracted_files:
|
| 49 |
-
logger.info(f"File: {file}, Extension: {os.path.splitext(file)[1]}")
|
| 50 |
-
except zipfile.BadZipFile:
|
| 51 |
-
logger.error("The downloaded file is not a zip file or is corrupted.")
|
| 52 |
except Exception as e:
|
| 53 |
logger.error(f"An error occurred while extracting the file: {e}")
|
| 54 |
|
| 55 |
-
#
|
| 56 |
@app.route('/wltv', methods=['GET'])
|
| 57 |
def serve_extracted_file():
|
| 58 |
try:
|
|
@@ -88,22 +83,22 @@ def get_logs():
|
|
| 88 |
# Route to update (download and extract again)
|
| 89 |
@app.route('/update', methods=['POST'])
|
| 90 |
def update_files():
|
| 91 |
-
|
| 92 |
-
|
| 93 |
|
| 94 |
-
# Download and extract the
|
| 95 |
-
|
| 96 |
-
|
| 97 |
|
| 98 |
return "Files updated", 200
|
| 99 |
|
| 100 |
if __name__ == '__main__':
|
| 101 |
-
|
| 102 |
-
|
| 103 |
|
| 104 |
-
# Download and extract the
|
| 105 |
-
|
| 106 |
-
|
| 107 |
|
| 108 |
# Start the Flask web server
|
| 109 |
-
app.run(host='0.0.0.0', port=8080)
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
+
import gzip
|
| 4 |
+
import shutil
|
| 5 |
import logging
|
| 6 |
from flask import Flask, send_file, jsonify
|
| 7 |
|
|
|
|
| 16 |
if not os.path.exists(extract_to_dir):
|
| 17 |
os.makedirs(extract_to_dir, exist_ok=True)
|
| 18 |
|
| 19 |
+
# Function to download the file
|
| 20 |
+
def download_file(url, output_path):
|
|
|
|
|
|
|
| 21 |
logger.info(f"Downloading from {url}...")
|
| 22 |
try:
|
| 23 |
response = requests.get(url)
|
| 24 |
if response.status_code == 200:
|
| 25 |
with open(output_path, 'wb') as f:
|
| 26 |
f.write(response.content)
|
| 27 |
+
logger.info(f"Downloaded file to {output_path} with size {len(response.content)} bytes")
|
| 28 |
else:
|
| 29 |
logger.error(f"Failed to download file. Status code: {response.status_code}")
|
| 30 |
except PermissionError as e:
|
|
|
|
| 32 |
except Exception as e:
|
| 33 |
logger.error(f"An error occurred while downloading the file: {e}")
|
| 34 |
|
| 35 |
+
# Function to extract .gz files
|
| 36 |
+
def extract_gz_file(gz_path, extract_to_dir):
|
| 37 |
+
logger.info(f"Extracting {gz_path} to {extract_to_dir}...")
|
| 38 |
+
if not os.path.exists(gz_path):
|
| 39 |
+
logger.error(f"GZ file does not exist: {gz_path}")
|
| 40 |
return
|
| 41 |
try:
|
| 42 |
+
extracted_file_path = os.path.join(extract_to_dir, 'it_wltv_full')
|
| 43 |
+
with gzip.open(gz_path, 'rb') as f_in:
|
| 44 |
+
with open(extracted_file_path, 'wb') as f_out:
|
| 45 |
+
shutil.copyfileobj(f_in, f_out)
|
| 46 |
+
logger.info(f"Extracted GZ file to {extracted_file_path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
logger.error(f"An error occurred while extracting the file: {e}")
|
| 49 |
|
| 50 |
+
# Flask route to serve the extracted file
|
| 51 |
@app.route('/wltv', methods=['GET'])
|
| 52 |
def serve_extracted_file():
|
| 53 |
try:
|
|
|
|
| 83 |
# Route to update (download and extract again)
|
| 84 |
@app.route('/update', methods=['POST'])
|
| 85 |
def update_files():
|
| 86 |
+
file_url = 'http://epg-guide.com/wltv.gz'
|
| 87 |
+
file_path = '/tmp/it_wltv_full.gz'
|
| 88 |
|
| 89 |
+
# Download and extract the gz file
|
| 90 |
+
download_file(file_url, file_path)
|
| 91 |
+
extract_gz_file(file_path, extract_to_dir)
|
| 92 |
|
| 93 |
return "Files updated", 200
|
| 94 |
|
| 95 |
if __name__ == '__main__':
|
| 96 |
+
file_url = 'http://epg-guide.com/wltv.gz'
|
| 97 |
+
file_path = '/tmp/it_wltv_full.gz'
|
| 98 |
|
| 99 |
+
# Download and extract the gz file initially
|
| 100 |
+
download_file(file_url, file_path)
|
| 101 |
+
extract_gz_file(file_path, extract_to_dir)
|
| 102 |
|
| 103 |
# Start the Flask web server
|
| 104 |
+
app.run(host='0.0.0.0', port=8080)
|