LoremPizza commited on
Commit
cbff499
·
verified ·
1 Parent(s): 987d612

Update wltv_server.py

Browse files
Files changed (1) hide show
  1. wltv_server.py +38 -28
wltv_server.py CHANGED
@@ -5,7 +5,7 @@ import logging
5
  from flask import Flask, send_file, jsonify
6
 
7
  # Set up logging
8
- logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
9
  logger = logging.getLogger(__name__)
10
 
11
  app = Flask(__name__)
@@ -15,9 +15,7 @@ extract_to_dir = 'extracted'
15
  if not os.path.exists(extract_to_dir):
16
  os.makedirs(extract_to_dir, exist_ok=True)
17
 
18
- # Update URL to the correct location
19
- zip_url = 'http://epg-guide.com/wltv.gz'
20
- zip_path = 'it_wltv_full.zip'
21
 
22
  # Step 1: Download the zip file
23
  def download_zip_file(url, output_path):
@@ -30,6 +28,8 @@ def download_zip_file(url, output_path):
30
  logger.info(f"Downloaded zip file to {output_path} with size {len(response.content)} bytes")
31
  else:
32
  logger.error(f"Failed to download file. Status code: {response.status_code}")
 
 
33
  except Exception as e:
34
  logger.error(f"An error occurred while downloading the file: {e}")
35
 
@@ -52,34 +52,14 @@ def extract_file(zip_path, extract_to_dir):
52
  except Exception as e:
53
  logger.error(f"An error occurred while extracting the file: {e}")
54
 
55
- # Route to manually trigger the download and extraction process
56
- @app.route('/update', methods=['POST'])
57
- def update_files():
58
- download_zip_file(zip_url, zip_path)
59
- extract_file(zip_path, extract_to_dir)
60
- return jsonify({'status': 'Update complete'}), 200
61
-
62
- # Route to view logs
63
- @app.route('/logs', methods=['GET'])
64
- def get_logs():
65
- try:
66
- if os.path.exists('app.log'):
67
- with open('app.log', 'r') as f:
68
- logs = f.read()
69
- return jsonify({'logs': logs}), 200
70
- else:
71
- logger.error("Log file does not exist")
72
- return jsonify({'error': 'Log file does not exist'}), 500
73
- except Exception as e:
74
- logger.error(f"Failed to read logs: {e}")
75
- return jsonify({'error': 'Failed to read logs'}), 500
76
-
77
- # Route to serve the extracted file
78
  @app.route('/wltv', methods=['GET'])
79
  def serve_extracted_file():
80
  try:
81
  extracted_files = os.listdir(extract_to_dir)
82
  logger.info(f"Files in 'extracted' directory: {extracted_files}")
 
 
83
  except Exception as e:
84
  logger.error(f"Error listing files in 'extracted' directory: {e}")
85
  return "Error listing files", 500
@@ -94,6 +74,36 @@ def serve_extracted_file():
94
  logger.error("File not found")
95
  return "File not found", 404
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  if __name__ == '__main__':
 
 
 
 
 
 
 
98
  # Start the Flask web server
99
- app.run(host='0.0.0.0', port=8080)
 
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__)
 
15
  if not os.path.exists(extract_to_dir):
16
  os.makedirs(extract_to_dir, exist_ok=True)
17
 
18
+ log_storage = []
 
 
19
 
20
  # Step 1: Download the zip file
21
  def download_zip_file(url, output_path):
 
28
  logger.info(f"Downloaded zip file to {output_path} with size {len(response.content)} bytes")
29
  else:
30
  logger.error(f"Failed to download file. Status code: {response.status_code}")
31
+ except PermissionError as e:
32
+ logger.error(f"Permission error occurred while downloading the file: {e}")
33
  except Exception as e:
34
  logger.error(f"An error occurred while downloading the file: {e}")
35
 
 
52
  except Exception as e:
53
  logger.error(f"An error occurred while extracting the file: {e}")
54
 
55
+ # Step 3: Set up a Flask route to expose the extracted file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  @app.route('/wltv', methods=['GET'])
57
  def serve_extracted_file():
58
  try:
59
  extracted_files = os.listdir(extract_to_dir)
60
  logger.info(f"Files in 'extracted' directory: {extracted_files}")
61
+ for file_name in extracted_files:
62
+ logger.info(f"File: {file_name}, Extension: {os.path.splitext(file_name)[1]}")
63
  except Exception as e:
64
  logger.error(f"Error listing files in 'extracted' directory: {e}")
65
  return "Error listing files", 500
 
74
  logger.error("File not found")
75
  return "File not found", 404
76
 
77
+ # Route to fetch logs
78
+ @app.route('/logs', methods=['GET'])
79
+ def get_logs():
80
+ try:
81
+ with open('app.log', 'r') as log_file:
82
+ logs = log_file.read()
83
+ return logs, 200
84
+ except Exception as e:
85
+ logger.error(f"Failed to read logs: {e}")
86
+ return jsonify({"error": "Failed to read logs"}), 500
87
+
88
+ # Route to update (download and extract again)
89
+ @app.route('/update', methods=['POST'])
90
+ def update_files():
91
+ zip_url = 'http://epg-guide.com/wltv.gz'
92
+ zip_path = '/tmp/it_wltv_full.zip'
93
+
94
+ # Download and extract the zip file
95
+ download_zip_file(zip_url, zip_path)
96
+ extract_file(zip_path, extract_to_dir)
97
+
98
+ return "Files updated", 200
99
+
100
  if __name__ == '__main__':
101
+ zip_url = 'http://epg-guide.com/wltv.gz'
102
+ zip_path = '/tmp/it_wltv_full.zip'
103
+
104
+ # Download and extract the zip file initially
105
+ download_zip_file(zip_url, zip_path)
106
+ extract_file(zip_path, extract_to_dir)
107
+
108
  # Start the Flask web server
109
+ app.run(host='0.0.0.0', port=8080)