Update epg-grabber/getEpgs.py
This commit is contained in:
@@ -1,76 +1,75 @@
|
|||||||
import os
|
import os
|
||||||
import gzip
|
import gzip
|
||||||
import xml.etree.ElementTree as ET
|
import xml.etree.ElementTree as ET
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
save_as_gz = True # Set to True to save an additional .gz version
|
save_as_gz = True # Set to True to save an additional .gz version
|
||||||
|
|
||||||
tvg_ids_file = os.path.join(os.path.dirname(__file__), 'tvg-ids.txt')
|
tvg_ids_file = os.path.join(os.path.dirname(__file__), 'tvg-ids.txt')
|
||||||
output_file = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'epg.xml')
|
output_file = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'epg.xml')
|
||||||
output_file_gz = output_file + '.gz'
|
output_file_gz = output_file + '.gz'
|
||||||
|
|
||||||
def fetch_and_extract_xml(url):
|
def fetch_and_extract_xml(url):
|
||||||
response = requests.get(url)
|
response = requests.get(url)
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
print(f"Failed to fetch {url}")
|
print(f"Failed to fetch {url}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if url.endswith('.gz'):
|
if url.endswith('.gz'):
|
||||||
try:
|
try:
|
||||||
decompressed_data = gzip.decompress(response.content)
|
decompressed_data = gzip.decompress(response.content)
|
||||||
return ET.fromstring(decompressed_data)
|
return ET.fromstring(decompressed_data)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Failed to decompress and parse XML from {url}: {e}")
|
print(f"Failed to decompress and parse XML from {url}: {e}")
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
return ET.fromstring(response.content)
|
return ET.fromstring(response.content)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Failed to parse XML from {url}: {e}")
|
print(f"Failed to parse XML from {url}: {e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def filter_and_build_epg(urls):
|
def filter_and_build_epg(urls):
|
||||||
with open(tvg_ids_file, 'r') as file:
|
with open(tvg_ids_file, 'r') as file:
|
||||||
valid_tvg_ids = set(line.strip() for line in file)
|
valid_tvg_ids = set(line.strip() for line in file)
|
||||||
|
|
||||||
root = ET.Element('tv')
|
root = ET.Element('tv')
|
||||||
|
|
||||||
for url in urls:
|
for url in urls:
|
||||||
epg_data = fetch_and_extract_xml(url)
|
epg_data = fetch_and_extract_xml(url)
|
||||||
if epg_data is None:
|
if epg_data is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
for channel in epg_data.findall('channel'):
|
for channel in epg_data.findall('channel'):
|
||||||
tvg_id = channel.get('id')
|
tvg_id = channel.get('id')
|
||||||
if tvg_id in valid_tvg_ids:
|
if tvg_id in valid_tvg_ids:
|
||||||
root.append(channel)
|
root.append(channel)
|
||||||
|
|
||||||
for programme in epg_data.findall('programme'):
|
for programme in epg_data.findall('programme'):
|
||||||
tvg_id = programme.get('channel')
|
tvg_id = programme.get('channel')
|
||||||
if tvg_id in valid_tvg_ids:
|
if tvg_id in valid_tvg_ids:
|
||||||
root.append(programme)
|
root.append(programme)
|
||||||
|
|
||||||
tree = ET.ElementTree(root)
|
tree = ET.ElementTree(root)
|
||||||
tree.write(output_file, encoding='utf-8', xml_declaration=True)
|
tree.write(output_file, encoding='utf-8', xml_declaration=True)
|
||||||
print(f"New EPG saved to {output_file}")
|
print(f"New EPG saved to {output_file}")
|
||||||
|
|
||||||
if save_as_gz:
|
if save_as_gz:
|
||||||
with gzip.open(output_file_gz, 'wb') as f:
|
with gzip.open(output_file_gz, 'wb') as f:
|
||||||
tree.write(f, encoding='utf-8', xml_declaration=True)
|
tree.write(f, encoding='utf-8', xml_declaration=True)
|
||||||
print(f"New EPG saved to {output_file_gz}")
|
print(f"New EPG saved to {output_file_gz}")
|
||||||
|
|
||||||
def copy_to_server():
|
def copy_to_server():
|
||||||
os.system('scp -o StrictHostKeyChecking=no '+output_file_gz+' brock@192.168.1.10:/var/www/iptv.caldwell.local/epg.xml.gz')
|
os.system('scp -o StrictHostKeyChecking=no '+output_file_gz+' brock@192.168.1.10:/var/www/iptv.caldwell.local/epg.xml.gz')
|
||||||
os.system('ssh -o StrictHostKeyChecking=no brock@192.168.1.10 -t ')
|
os.system('ssh -o StrictHostKeyChecking=no brock@192.168.1.10 -t ')
|
||||||
|
|
||||||
urls = [
|
urls = [
|
||||||
'https://www.dropbox.com/scl/fi/i7kgakvjubjwiscuab467/m3u4u-103216-595281-EPG.xml?rlkey=ykvw6gyhx93erycl19sa6xft5&st=tlrrz59n&dl=1',
|
'https://www.dropbox.com/scl/fi/i7kgakvjubjwiscuab467/m3u4u-103216-595281-EPG.xml?rlkey=ykvw6gyhx93erycl19sa6xft5&st=tlrrz59n&dl=1',
|
||||||
'https://epgshare01.online/epgshare01/epg_ripper_US1.xml.gz',
|
'https://epgshare01.online/epgshare01/epg_ripper_US1.xml.gz',
|
||||||
'https://epgshare01.online/epgshare01/epg_ripper_US_LOCALS2.xml.gz',
|
'https://epgshare01.online/epgshare01/epg_ripper_US_LOCALS2.xml.gz',
|
||||||
'https://epgshare01.online/epgshare01/epg_ripper_CA1.xml.gz',
|
'https://epgshare01.online/epgshare01/epg_ripper_CA1.xml.gz',
|
||||||
'https://epgshare01.online/epgshare01/epg_ripper_PLEX1.xml.gz',
|
'https://epgshare01.online/epgshare01/epg_ripper_PLEX1.xml.gz',
|
||||||
]
|
]
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
filter_and_build_epg(urls)
|
filter_and_build_epg(urls)
|
||||||
copy_to_server()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user