|
| 1 | +#!/usr/bin/python3 |
| 2 | +import configparser |
| 3 | +import gi |
| 4 | +import os |
| 5 | +import requests |
| 6 | +import shutil |
| 7 | +import string |
| 8 | +import threading |
| 9 | +import re |
| 10 | +from gi.repository import GObject |
| 11 | +from random import choice |
| 12 | + |
| 13 | +# M3U parsing regex |
| 14 | +PARAMS = re.compile(r'(\S+)="(.*?)"') |
| 15 | +EXTINF = re.compile(r'^#EXTINF:(?P<duration>-?\d+?) ?(?P<params>.*),(?P<title>.*?)$') |
| 16 | +PROVIDERS_PATH = os.path.expanduser("~/.hypnotix/providers") |
| 17 | + |
| 18 | +# Used as a decorator to run things in the background |
| 19 | +def _async(func): |
| 20 | + def wrapper(*args, **kwargs): |
| 21 | + thread = threading.Thread(target=func, args=args, kwargs=kwargs) |
| 22 | + thread.daemon = True |
| 23 | + thread.start() |
| 24 | + return thread |
| 25 | + return wrapper |
| 26 | + |
| 27 | +# Used as a decorator to run things in the main loop, from another thread |
| 28 | +def idle(func): |
| 29 | + def wrapper(*args): |
| 30 | + GObject.idle_add(func, *args) |
| 31 | + return wrapper |
| 32 | + |
| 33 | +def slugify(string): |
| 34 | + """ |
| 35 | + Normalizes string, converts to lowercase, removes non-alpha characters, |
| 36 | + and converts spaces to hyphens. |
| 37 | + """ |
| 38 | + return "".join(x.lower() for x in string if x.isalnum()) |
| 39 | + |
| 40 | +class Provider(): |
| 41 | + def __init__(self, name, url): |
| 42 | + self.name = name |
| 43 | + self.path = os.path.join(PROVIDERS_PATH, slugify(name)) |
| 44 | + self.url = url |
| 45 | + self.groups = [] |
| 46 | + self.channels = [] |
| 47 | + |
| 48 | +class Group(): |
| 49 | + def __init__(self, name): |
| 50 | + self.name = name |
| 51 | + self.channels = [] |
| 52 | + |
| 53 | +class Channel(): |
| 54 | + def __init__(self, info): |
| 55 | + self.info = info |
| 56 | + self.id = None |
| 57 | + self.name = None |
| 58 | + self.logo = None |
| 59 | + self.group_title = None |
| 60 | + self.title = None |
| 61 | + self.url = None |
| 62 | + match = EXTINF.fullmatch(info) |
| 63 | + if match != None: |
| 64 | + res = match.groupdict() |
| 65 | + if 'params' in res: |
| 66 | + params = dict(PARAMS.findall(res['params'])) |
| 67 | + if "tvg-name" in params and params['tvg-name'].strip() != "": |
| 68 | + self.name = params['tvg-name'].strip() |
| 69 | + if "tvg-logo" in params and params['tvg-logo'].strip() != "": |
| 70 | + self.logo = params['tvg-logo'].strip() |
| 71 | + if "group-title" in params and params['group-title'].strip() != "": |
| 72 | + self.group_title = params['group-title'].strip() |
| 73 | + if 'title' in res: |
| 74 | + self.title = res['title'] |
| 75 | + |
| 76 | +class Manager(): |
| 77 | + |
| 78 | + def __init__(self): |
| 79 | + os.system("mkdir -p '%s'" % PROVIDERS_PATH) |
| 80 | + |
| 81 | + def download_playlist(self, provider): |
| 82 | + success = False |
| 83 | + try: |
| 84 | + response = requests.get(provider.url, timeout=10) |
| 85 | + if response.status_code == 200: |
| 86 | + print("Download success") |
| 87 | + try: |
| 88 | + source = response.content.decode("UTF-8") |
| 89 | + except UnicodeDecodeError as e: |
| 90 | + source = response.content.decode("latin1") |
| 91 | + if (source.count("#EXTM3U") > 0 and source.count("#EXTINF") > 0): |
| 92 | + print("Content looks legit") |
| 93 | + with open(provider.path, "w") as file: |
| 94 | + file.write(source) |
| 95 | + success = True |
| 96 | + except Exception as e: |
| 97 | + print(e) |
| 98 | + finally: |
| 99 | + return success |
| 100 | + |
| 101 | + def load_channels(self, provider): |
| 102 | + with open(provider.path, "r") as file: |
| 103 | + channel = None |
| 104 | + group = None |
| 105 | + groups = {} |
| 106 | + for line in file: |
| 107 | + line = line.strip() |
| 108 | + if line.startswith("#EXTM3U"): |
| 109 | + continue |
| 110 | + if line.startswith("#EXTINF"): |
| 111 | + channel = Channel(line) |
| 112 | + continue |
| 113 | + if "://" in line: |
| 114 | + if channel == None: |
| 115 | + continue |
| 116 | + if channel.url != None: |
| 117 | + # We already found the URL, skip the line |
| 118 | + continue |
| 119 | + if channel.name == None or "***" in channel.name: |
| 120 | + continue |
| 121 | + channel.url = line |
| 122 | + provider.channels.append(channel) |
| 123 | + if channel.group_title != None and channel.group_title.strip() != "": |
| 124 | + if group == None or group.name != channel.group_title: |
| 125 | + if channel.group_title in groups.keys(): |
| 126 | + group = groups[channel.group_title] |
| 127 | + else: |
| 128 | + group = Group(channel.group_title) |
| 129 | + provider.groups.append(group) |
| 130 | + groups[channel.group_title] = group |
| 131 | + group.channels.append(channel) |
| 132 | + |
0 commit comments