LoremPizza commited on
Commit
6e22549
·
verified ·
1 Parent(s): f3b6343

Update wltv_server.py

Browse files
Files changed (1) hide show
  1. wltv_server.py +57 -12
wltv_server.py CHANGED
@@ -49,8 +49,8 @@ def extract_gz_file(gz_path, extract_to_dir):
49
  except Exception as e:
50
  logger.error(f"An error occurred while extracting the file: {e}")
51
 
52
- # Function to get the current program for a channel
53
- def get_current_program(channel_id):
54
  xml_file = os.path.join(extract_to_dir, 'it_wltv_full.xml')
55
  if not os.path.exists(xml_file):
56
  logger.error("XML file does not exist for program data")
@@ -61,30 +61,75 @@ def get_current_program(channel_id):
61
  root = tree.getroot()
62
 
63
  # Get the current time
64
- current_time = datetime.now().strftime("%Y%m%d%H%M%S")
65
 
66
  for programme in root.findall('programme'):
67
  if programme.get('channel') == channel_id:
68
  start_time = datetime.strptime(programme.get('start')[:14], "%Y%m%d%H%M%S")
69
  stop_time = datetime.strptime(programme.get('stop')[:14], "%Y%m%d%H%M%S")
70
- if start_time <= datetime.now() < stop_time:
71
- title = programme.find('title').text if programme.find('title') is not None else "No title"
72
- desc = programme.find('desc').text if programme.find('desc') is not None else "No description"
73
  return {"title": title, "description": desc}
74
- logger.info(f"No current program found for {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
- # Route to serve the current program on any channel
81
- @app.route('/<channel_id>/now', methods=['GET'])
82
- def current_program(channel_id):
83
- program_info = get_current_program(channel_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  if program_info:
85
  return jsonify(program_info)
86
  else:
87
- return "No current program found.", 404
88
 
89
  if __name__ == '__main__':
90
  file_url = 'http://epg-guide.com/wltv.gz'
 
49
  except Exception as e:
50
  logger.error(f"An error occurred while extracting the file: {e}")
51
 
52
+ # Function to get the current program for Sky TG24
53
+ def get_current_program(channel_id="skytg24"):
54
  xml_file = os.path.join(extract_to_dir, 'it_wltv_full.xml')
55
  if not os.path.exists(xml_file):
56
  logger.error("XML file does not exist for program data")
 
61
  root = tree.getroot()
62
 
63
  # Get the current time
64
+ current_time = datetime.now()
65
 
66
  for programme in root.findall('programme'):
67
  if programme.get('channel') == channel_id:
68
  start_time = datetime.strptime(programme.get('start')[:14], "%Y%m%d%H%M%S")
69
  stop_time = datetime.strptime(programme.get('stop')[:14], "%Y%m%d%H%M%S")
70
+ if start_time <= current_time < stop_time:
71
+ title = programme.find('title').text
72
+ desc = programme.find('desc').text
73
  return {"title": title, "description": desc}
74
+ logger.info("No current program found for Sky TG24.")
75
  return None
76
  except Exception as e:
77
  logger.error(f"Failed to parse XML file: {e}")
78
  return None
79
 
80
+ # Flask route to serve the extracted file
81
+ @app.route('/wltv', methods=['GET'])
82
+ def serve_extracted_file():
83
+ try:
84
+ extracted_files = os.listdir(extract_to_dir)
85
+ logger.info(f"Files in 'extracted' directory: {extracted_files}")
86
+ for file_name in extracted_files:
87
+ logger.info(f"File: {file_name}, Extension: {os.path.splitext(file_name)[1]}")
88
+ except Exception as e:
89
+ logger.error(f"Error listing files in 'extracted' directory: {e}")
90
+ return "Error listing files", 500
91
+
92
+ file_name = 'it_wltv_full.xml' # Updated file name with extension
93
+ file_path = os.path.join(extract_to_dir, file_name)
94
+
95
+ if os.path.exists(file_path):
96
+ logger.info(f"Serving file from {file_path}")
97
+ return send_file(file_path, as_attachment=False, mimetype='text/plain')
98
+ else:
99
+ logger.error("File not found")
100
+ return "File not found", 404
101
+
102
+ # Route to fetch logs
103
+ @app.route('/logs', methods=['GET'])
104
+ def get_logs():
105
+ try:
106
+ with open('app.log', 'r') as log_file:
107
+ logs = log_file.read()
108
+ return logs, 200
109
+ except Exception as e:
110
+ logger.error(f"Failed to read logs: {e}")
111
+ return jsonify({"error": "Failed to read logs"}), 500
112
+
113
+ # Route to update (download and extract again)
114
+ @app.route('/update', methods=['POST'])
115
+ def update_files():
116
+ file_url = 'http://epg-guide.com/wltv.gz'
117
+ file_path = '/tmp/it_wltv_full.gz'
118
+
119
+ # Download and extract the gz file
120
+ download_file(file_url, file_path)
121
+ extract_gz_file(file_path, extract_to_dir)
122
+
123
+ return "Files updated", 200
124
+
125
+ # Route to serve the current program on Sky TG24
126
+ @app.route('/skytg24/now', methods=['GET'])
127
+ def current_skytg24_program():
128
+ program_info = get_current_program("skytg24")
129
  if program_info:
130
  return jsonify(program_info)
131
  else:
132
+ return "No current program found for Sky TG24.", 404
133
 
134
  if __name__ == '__main__':
135
  file_url = 'http://epg-guide.com/wltv.gz'