LoremPizza commited on
Commit
7206896
·
verified ·
1 Parent(s): 01bba9c

Update wltv_server.py

Browse files
Files changed (1) hide show
  1. wltv_server.py +28 -24
wltv_server.py CHANGED
@@ -2,21 +2,16 @@ import os
2
  import requests
3
  import zipfile
4
  import logging
5
- from flask import Flask, send_file, Response, jsonify
6
- from io import StringIO # For Python 3
7
 
8
  # Set up logging
9
- log_stream = StringIO()
10
- logging.basicConfig(level=logging.INFO, stream=log_stream, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
11
  logger = logging.getLogger(__name__)
12
 
13
  app = Flask(__name__)
14
 
15
  # Ensure the extracted directory exists
16
  extract_to_dir = 'extracted'
17
- zip_path = 'it_wltv_full.zip'
18
- zip_url = 'http://api.epg-guide.com/wltv.dz'
19
-
20
  if not os.path.exists(extract_to_dir):
21
  os.makedirs(extract_to_dir, exist_ok=True)
22
 
@@ -34,6 +29,9 @@ def download_zip_file(url, output_path):
34
  # Step 2: Extract the file from the zip file
35
  def extract_file(zip_path, extract_to_dir):
36
  logger.info(f"Extracting {zip_path} to {extract_to_dir}...")
 
 
 
37
  try:
38
  with zipfile.ZipFile(zip_path, 'r') as zip_ref:
39
  zip_ref.extractall(extract_to_dir)
@@ -47,15 +45,31 @@ def extract_file(zip_path, extract_to_dir):
47
  except Exception as e:
48
  logger.error(f"An error occurred while extracting the file: {e}")
49
 
50
- # Step 3: Set up a Flask route to expose the extracted file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  @app.route('/wltv', methods=['GET'])
52
  def serve_extracted_file():
53
- # Log contents of extracted directory for debugging
54
  try:
55
  extracted_files = os.listdir(extract_to_dir)
56
  logger.info(f"Files in 'extracted' directory: {extracted_files}")
57
- for file_name in extracted_files:
58
- logger.info(f"File: {file_name}, Extension: {os.path.splitext(file_name)[1]}")
59
  except Exception as e:
60
  logger.error(f"Error listing files in 'extracted' directory: {e}")
61
  return "Error listing files", 500
@@ -70,19 +84,9 @@ def serve_extracted_file():
70
  logger.error("File not found")
71
  return "File not found", 404
72
 
73
- # New route to serve the logs
74
- @app.route('/logs', methods=['GET'])
75
- def serve_logs():
76
- log_contents = log_stream.getvalue()
77
- return Response(log_contents, mimetype='text/plain')
78
-
79
- # Route to manually trigger the download and extraction process
80
- @app.route('/update', methods=['POST'])
81
- def update_files():
82
- download_zip_file(zip_url, zip_path)
83
- extract_file(zip_path, extract_to_dir)
84
- return jsonify({'status': 'Update complete'}), 200
85
-
86
  if __name__ == '__main__':
 
 
 
87
  # Start the Flask web server
88
  app.run(host='0.0.0.0', port=8080)
 
2
  import requests
3
  import zipfile
4
  import logging
5
+ from flask import Flask, send_file, jsonify
 
6
 
7
  # Set up logging
8
+ logging.basicConfig(level=logging.INFO)
 
9
  logger = logging.getLogger(__name__)
10
 
11
  app = Flask(__name__)
12
 
13
  # Ensure the extracted directory exists
14
  extract_to_dir = 'extracted'
 
 
 
15
  if not os.path.exists(extract_to_dir):
16
  os.makedirs(extract_to_dir, exist_ok=True)
17
 
 
29
  # Step 2: Extract the file from the zip file
30
  def extract_file(zip_path, extract_to_dir):
31
  logger.info(f"Extracting {zip_path} to {extract_to_dir}...")
32
+ if not os.path.exists(zip_path):
33
+ logger.error(f"Zip file does not exist: {zip_path}")
34
+ return
35
  try:
36
  with zipfile.ZipFile(zip_path, 'r') as zip_ref:
37
  zip_ref.extractall(extract_to_dir)
 
45
  except Exception as e:
46
  logger.error(f"An error occurred while extracting the file: {e}")
47
 
48
+ # Route to manually trigger the download and extraction process
49
+ @app.route('/update', methods=['POST'])
50
+ def update_files():
51
+ download_zip_file(zip_url, zip_path)
52
+ extract_file(zip_path, extract_to_dir)
53
+ return jsonify({'status': 'Update complete'}), 200
54
+
55
+ # Route to view logs
56
+ @app.route('/logs', methods=['GET'])
57
+ def get_logs():
58
+ # Read logs from the log file
59
+ try:
60
+ with open('app.log', 'r') as f:
61
+ logs = f.read()
62
+ return jsonify({'logs': logs}), 200
63
+ except Exception as e:
64
+ logger.error(f"Failed to read logs: {e}")
65
+ return jsonify({'error': 'Failed to read logs'}), 500
66
+
67
+ # Route to serve the extracted file
68
  @app.route('/wltv', methods=['GET'])
69
  def serve_extracted_file():
 
70
  try:
71
  extracted_files = os.listdir(extract_to_dir)
72
  logger.info(f"Files in 'extracted' directory: {extracted_files}")
 
 
73
  except Exception as e:
74
  logger.error(f"Error listing files in 'extracted' directory: {e}")
75
  return "Error listing files", 500
 
84
  logger.error("File not found")
85
  return "File not found", 404
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  if __name__ == '__main__':
88
+ zip_url = 'http://epg-guide.com/wltv.dz' # Updated URL
89
+ zip_path = 'it_wltv_full.zip'
90
+
91
  # Start the Flask web server
92
  app.run(host='0.0.0.0', port=8080)