LoremPizza commited on
Commit
41281fb
·
verified ·
1 Parent(s): 1924ff2

Update wltv_server.py

Browse files
Files changed (1) hide show
  1. wltv_server.py +41 -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 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")
@@ -71,12 +71,44 @@ def get_current_program(channel_id="skytg24"):
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():
@@ -120,16 +152,10 @@ def update_files():
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'
@@ -139,5 +165,8 @@ if __name__ == '__main__':
139
  download_file(file_url, file_path)
140
  extract_gz_file(file_path, extract_to_dir)
141
 
 
 
 
142
  # Start the Flask web server
143
  app.run(host='0.0.0.0', port=8080)
 
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 given 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")
 
71
  title = programme.find('title').text
72
  desc = programme.find('desc').text
73
  return {"title": title, "description": desc}
74
+ logger.info(f"No current program found for channel {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
+ # Function to dynamically create routes for each channel
81
+ def create_channel_routes():
82
+ xml_file = os.path.join(extract_to_dir, 'it_wltv_full.xml')
83
+ if not os.path.exists(xml_file):
84
+ logger.error("XML file does not exist to create dynamic routes")
85
+ return
86
+
87
+ try:
88
+ tree = ET.parse(xml_file)
89
+ root = tree.getroot()
90
+
91
+ # Find all channels in the XML
92
+ channels = {channel.get('id'): channel.find('display-name').text.replace(" ", "").lower()
93
+ for channel in root.findall('channel')}
94
+
95
+ for channel_id, route_name in channels.items():
96
+ # Create a dynamic route for each channel
97
+ create_route_for_channel(channel_id, route_name)
98
+
99
+ except Exception as e:
100
+ logger.error(f"Failed to create dynamic routes from XML file: {e}")
101
+
102
+ # Helper function to create route for a specific channel
103
+ def create_route_for_channel(channel_id, route_name):
104
+ @app.route(f'/{route_name}/now', methods=['GET'])
105
+ def current_program():
106
+ program_info = get_current_program(channel_id)
107
+ if program_info:
108
+ return jsonify(program_info)
109
+ else:
110
+ return f"No current program found for {route_name}.", 404
111
+
112
  # Flask route to serve the extracted file
113
  @app.route('/wltv', methods=['GET'])
114
  def serve_extracted_file():
 
152
  download_file(file_url, file_path)
153
  extract_gz_file(file_path, extract_to_dir)
154
 
155
+ # Create dynamic routes after updating
156
+ create_channel_routes()
157
 
158
+ return "Files updated", 200
 
 
 
 
 
 
 
159
 
160
  if __name__ == '__main__':
161
  file_url = 'http://epg-guide.com/wltv.gz'
 
165
  download_file(file_url, file_path)
166
  extract_gz_file(file_path, extract_to_dir)
167
 
168
+ # Create dynamic routes based on the extracted XML data
169
+ create_channel_routes()
170
+
171
  # Start the Flask web server
172
  app.run(host='0.0.0.0', port=8080)