async def event_listener():
    """
    This makes a websocket connection to Kodi and listens to all events raised, for each event raised,
    a JSON object is formatted as string adn is stored as a line in a log file.
    The log file naming convention is yyyy-mm-dd.txt adn the new file is created sunday 00:00 of every week

    Our websocket connection was based of this example:
    https://pypi.org/project/websockets/

    :return: None
    """
    async with websockets.connect(parameters.KODI_WEBSOCKET_URL,
                                  ping_interval=None) as websocket:
        print('Kodi connected')
        await websocket.send(
            '{"jsonrpc":"2.0","method":"JSONRPC.NotifyAll","params": { "sender": "tom", "message": "x" }}'
        )
        log = None
        kodi = KodiResource()

        async for message in websocket:
            event = await websocket.recv()
            now = datetime.datetime.now()

            event_data = kodi.player_get_item()

            row = '{"date": "' + now.strftime(
                "%Y-%m-%d %H:%M:%S"
            ) + '", "event": ' + event + ', "item": ' + json.dumps(
                event_data) + '}'

            # calculate name of the file, this row should be saved to,
            # sunday 00:00 of every week
            week_start = now - datetime.timedelta(days=now.isoweekday() % 7)
            file_name = week_start.strftime("%Y-%m-%d") + ".txt"
            print(file_name, row)

            if log is None or file_name != log.name:
                if log:
                    #  the currently open file is no the log we want to save to, so close it
                    log.close()
                # open the new file in 'append' mode
                log = open(parameters.EVENT_LOG_PATH + file_name, 'a')

            log.write(row + '\n')
            # flush contents of python file writing buffer to file,
            # makes sure what we've written is immediately inserted
            log.flush()
Exemple #2
0
def watched_recording():
    """
    When a recording that is being played has already been seen by the user, this method will delete the recording.
    Steps taken:
    1) Ask Kodi what is being played
    2) Find and delete recording using TVHeadend

    :return: None
    """
    kodi = KodiResource()
    tv = TvHeadEndResource()
    item_dict = kodi.player_get_item()

    title = item_dict['title']
    plot = item_dict['plot']

    if title == "":
        print("Current Kodi player doesn't have a title")
    else:
        tv.find_and_delete_recordings(title, plot=plot)

        print(title, plot)
    return title
Exemple #3
0
def dislike_show():
    """
    Marks a shows as 'disliked'. This method takes several steps:
    1) asks Kodi what is being watched
    2) searches the Shows table for the same show
    3) updates the Show record with disliked = 1
    4) delete all recordings of the show
    5) delete any timers associated with the show

    :return:
            None
    """
    connection = mysql.connector.connect(user=parameters.DB_USER,
                                         database=parameters.DB_NAME)
    cursor = connection.cursor()

    kodi = KodiResource()
    item_dict = kodi.player_get_item()

    update_shows_with_dislikes = ("UPDATE shows "
                                  "SET disliked = 1 "
                                  "WHERE title = %s")

    title = item_dict['title']

    cursor.execute(update_shows_with_dislikes, (title, ))
    connection.commit()

    tv = TvHeadEndResource()
    tv.find_and_delete_recordings(title)
    print(title)

    timer = TimerManager()
    timer_ids = timer.find_timer_ids(title)
    if len(timer_ids) > 0:
        print("deleting : ", title)
        timer.delete_timers(timer_ids)
Exemple #4
0
async def event_listener():
    """
    This makes a websocket connection to Kodi and listens to all events raised, for each event raised,
    a JSON object is formatted as string adn is stored as a line in a log file.
    The log file naming convention is yyyy-mm-dd.txt adn the new file is created sunday 00:00 of every week

    Our websocket connection was based of this example:
    https://pypi.org/project/websockets/

    :return: None
    """
    async with websockets.connect(parameters.KODI_WEBSOCKET_URL,
                                  ping_interval=None) as websocket:
        print('Kodi connected')
        await websocket.send(
            '{"jsonrpc":"2.0","method":"JSONRPC.NotifyAll","params": { "sender": "tom", "message": "x" }}'
        )
        log = None
        kodi = KodiResource()

        async for message in websocket:
            if message == '':
                print('Skipping empty event')
                continue

            now = datetime.datetime.now()
            event = json.loads(message)

            event_data = event['params']['data'] or {}
            if 'item' in event_data:
                event_item = event_data['item'] or {}
            else:
                event_item = {}

            if 'channeltype' in event_item and event_item[
                    'channeltype'] == 'tv':
                data = kodi.pvr_get_channel_details(event_item['id'], [
                    "thumbnail", "channeltype", "hidden", "locked", "channel",
                    "lastplayed", "broadcastnow", "broadcastnext", "uniqueid",
                    "icon", "channelnumber", "subchannelnumber", "isrecording"
                ])
            else:
                data = {}

            item = kodi.player_get_item(quiet=True) or {}

            row = json.dumps({
                "date": now.strftime("%Y-%m-%d %H:%M:%S"),
                "event": event,
                "data": data,
                "item": item
            })

            # calculate name of the file, this row should be saved to,
            # sunday 00:00 of every week
            week_start = now - datetime.timedelta(days=now.isoweekday() % 7)
            file_name = week_start.strftime("%Y-%m-%d") + ".txt"
            print(file_name, row)

            if log is None or file_name != log.name:
                if log:
                    #  the currently open file is no the log we want to save to, so close it
                    log.close()
                # open the new file in 'append' mode
                log = open(parameters.EVENT_LOG_PATH + file_name, 'a')

            log.write(row + '\n')
            # flush contents of python file writing buffer to file,
            # makes sure what we've written is immediately inserted
            log.flush()