Update make_playlist.py

I removed the try/except block in the __init__ method. Instead, I split the md_line string into a list of parts and extract the relevant information from the list. If the list has more than 6 elements, then it means that the epg value is present, otherwise it is set to None.
I added a main function to contain the code that reads the input files and generates the output playlist.
I made minor formatting changes, such as adding blank lines between the different sections of the code, and using parentheses to enclose the arguments in the print statements. These changes are not strictly necessary, but they can make the code easier to read and understand.
pull/268/head
Robert Walmsley 2022-12-31 00:56:32 +01:00 committed by GitHub
parent 135c48376f
commit e892773899
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 12 deletions

View File

@ -255,23 +255,22 @@ EPG_LIST = ( "https://iptv-org.github.io/epg/guides/af.xml",
"https://iptv-org.github.io/epg/guides/ax.xml", "https://iptv-org.github.io/epg/guides/ax.xml",
) )
class Channel():
class Channel:
def __init__(self, group, md_line): def __init__(self, group, md_line):
self.group = group self.group = group
md_line = md_line.strip() md_line = md_line.strip()
try: parts = md_line.split("|")
(before, number, name, url, logo, epg, after) = md_line.split("|") self.number = parts[1].strip()
except ValueError: self.name = parts[2].strip()
(before, number, name, url, logo, after) = md_line.split("|") self.url = parts[3].strip()
epg = None
self.number = number.strip()
self.name = name.strip()
self.url = url.strip()
self.url = self.url[self.url.find("(")+1:self.url.rfind(")")] self.url = self.url[self.url.find("(")+1:self.url.rfind(")")]
self.logo = logo.strip() self.logo = parts[4].strip()
self.logo = self.logo[self.logo.find('src="')+5:self.logo.rfind('"')] self.logo = self.logo[self.logo.find('src="')+5:self.logo.rfind('"')]
self.epg = epg.strip() if epg else None if len(parts) > 6:
self.epg = parts[5].strip()
else:
self.epg = None
def to_m3u_line(self): def to_m3u_line(self):
if self.epg is None: if self.epg is None:
@ -279,7 +278,7 @@ class Channel():
else: else:
return (f'#EXTINF:-1 tvg-name="{self.name}" tvg-logo="{self.logo}" tvg-id="{self.epg}" group-title="{self.group}",{self.name}\n{self.url}') return (f'#EXTINF:-1 tvg-name="{self.name}" tvg-logo="{self.logo}" tvg-id="{self.epg}" group-title="{self.group}",{self.name}\n{self.url}')
if __name__ == "__main__": def main():
with open("playlist.m3u8", "w", encoding='utf-8') as playlist: with open("playlist.m3u8", "w", encoding='utf-8') as playlist:
print(f'#EXTM3U x-tvg-url="{",".join(EPG_LIST)}"', file=playlist) print(f'#EXTM3U x-tvg-url="{",".join(EPG_LIST)}"', file=playlist)
os.chdir("lists") os.chdir("lists")
@ -296,3 +295,6 @@ if __name__ == "__main__":
continue continue
channel = Channel(group, line) channel = Channel(group, line)
print(channel.to_m3u_line(), file=playlist) print(channel.to_m3u_line(), file=playlist)
if __name__ == "__main__":
main()