def generate_playlist(pl_id: int, db: Database): # Adding header resp = "#EXTM3U\n" # Getting objects from database themes = db.run('themes.get') # Getting channels from database channels = [] # Filling gaps with None type for db_channel in db.select('channels', '*', "ORDER BY id"): while len(channels) <= db_channel[0]: channels.append(None) channels[db_channel[0]] = Channel(*db_channel) if pl_id == 0: # Default playlist request = [('1,2,3', 0, True)] else: # Getting playlist from database request = db.select('playlists', ['channels', 'quality', 'del_channels'], f"WHERE id = {pl_id}") # Playlist could be empty if len(request) < 1: return None # Getting info about playlist channels_id, quality, remove = request[0] # Converting channels ids from str to list channels_id = list(map(int, channels_id.split(','))) # Adding main channels for ch_id in channels_id: # Getting channel from list channel = channels[ch_id] # Removing channel from list to avoid duplicating channels[ch_id] = None if channel is not None: resp += channel_to_m3u8(channel, themes, quality, False) # Adding other channels for channel in channels: if channel is not None: resp += channel_to_m3u8(channel, themes, quality, remove) return resp
def channels_get(db: Database): """ Function for getting channels from database :return: List with channels """ resp = db.select('channels', '*', "ORDER BY id") channels = [] for db_channel in resp: channels.append(Channel(*db_channel)) return channels
def themes_get(db: Database): """ Function for getting themes from database :return: List with themes, where index = theme id """ resp = db.select('themes', '*') themes = [] for theme in resp: while len(themes) < theme[0] + 1: themes.append(None) themes[theme[0]] = theme[1] return themes
def sources_get(db: Database): """ Function for getting sources from database :return: List with dicts of sources """ resp = db.select('sources', '*') sources = [] for source in resp: sources.append({ 'id': source[0], 'url': source[1], 'last_online': source[2], 'count': source[3] }) return sources