Exemplo n.º 1
0
def sources_save(db: Database, **kwargs):
    """
    function for saving sources
    :key sources: List with sources
    """
    for src in kwargs['sources']:
        db.insert_or_update('sources', **src)
Exemplo n.º 2
0
def tasks_executed(db: Database, **kwargs):
    """
    Function for changing task's executing time
    :key tid: Task id
    """
    db.insert_or_update(
        'tasks',
        id=kwargs['tid'],
        last_execution=datetime.today().date().strftime("%Y-%m-%d"),
        crashed=kwargs['crashed'])
Exemplo n.º 3
0
def channels_save(db: Database, **kwargs):
    """
    Function for saving channels to database
    :key channels: List with channels, should be list
    """
    done = 0
    for ch in kwargs['channels']:
        # TODO: Remake with single request
        done += 1
        print(f"\r\t- Saving channels... \t{done}/{len(kwargs['channels'])}",
              end='')
        db.insert_or_update('channels', **ch.get_dict())
    print(f"\r\t- Saving channels... Done!")
Exemplo n.º 4
0
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
Exemplo n.º 5
0
def themes_add(db: Database, **kwargs):
    """
    Function that add new theme to database
    :key theme: name of theme, should be str
    :return: id of added theme
    """
    return db.insert_or_update('themes', name=kwargs['theme'], _return_id=True)
Exemplo n.º 6
0
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
Exemplo n.º 7
0
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
Exemplo n.º 8
0
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
Exemplo n.º 9
0
from DBHelper import Database
from Tasks import TASKS

# Custom functions for database
from Utils.db_functions import FUNCS

if __name__ == '__main__':
    # Init database
    DB = Database('Config/Database.json', functions=FUNCS)

    for task in TASKS:
        task.add_database(DB)
        task.run()
Exemplo n.º 10
0
def themes_add_default(db: Database):
    themes = list(THEMES['themes'].keys())
    for name in themes:
        if name != '':
            db.insert_or_update('themes', id=themes.index(name), name=name)
Exemplo n.º 11
0
from os import environ as env
from flask import Flask, Response, render_template, request, redirect
from DBHelper import Database
from Utils.db_functions import FUNCS
from Utils.generator import generate_playlist

app = Flask(__name__, static_folder='Web/files', template_folder='Web/templates')

db = Database('Config/Database.json', functions=FUNCS)
db.end()


# --- Playlists --- #
@app.route('/p/<pl_id>')
def get_playlist(pl_id):
    try:
        pl_id = int(pl_id)
    except ValueError:
        return "Bad id", 400

    db.begin()
    playlist = generate_playlist(pl_id, db)
    db.end()

    if playlist is not None:
        return Response(playlist, mimetype='text/m3u8',
                        headers={'Content-disposition': 'attachment; filename=playlist.m3u8'})
    else:
        return "Not found", 404