Exemple #1
0
def stop_show():
    """
    Tells Kodi to stop playing
    :return: None
    """
    kodi = KodiResource()
    kodi.player_stop()
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 #3
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 #4
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)
 def __init__(self, offset_filename):
     super(RecordBroadcast, self).__init__(offset_filename)
     self.db = DatabaseResource()
     self.kodi = KodiResource()
Exemple #6
0
def play_something():
    """
    Finds a recording to play and tells kodi to play it, the steps are;
    1) Get a list of all recordings from Kodi
    2) Grab the title of the first recording
    3) Get the last season/episode watched from the database
    4) Find the next episode to watch in the recordings list
    5) Flay the net episode

    :param title: Optionally a specific title to watch
    :return: None
    """
    kodi = KodiResource()
    tv = TvHeadEndResource()
    record_dict = kodi.pvr_get_recordings()

    if len(record_dict) == 0:
        return

    title = record_dict[0]['label']

    connection = mysql.connector.connect(user=parameters.DB_USER,
                                         database=parameters.DB_NAME)
    cursor = connection.cursor()

    select_season_info = (
        "select s.show_id, e.season, max(e.episode) last_episode "
        "from shows s "
        "inner join episodes e on s.show_id = e.show_id "
        "where s.title = %s "
        "group by s.show_id, e.season "
        "order by e.season desc "
        "limit 1; ")

    cursor.execute(select_season_info, (title, ))
    next_episode = 0
    for (show_id, season, last_episode) in cursor:
        next_episode = last_episode + 1
        print("seen", season, last_episode, "looking for", season,
              next_episode)

    resume_current = False
    player_recording = None
    recording_ids = []
    print("Check for new episode...")
    for record in record_dict:
        # print(record)
        if record['label'].lower() == title.lower():
            # print(record['label'], record['recordingid'])
            recording_ids.append(record['recordingid'])
    record_details_dict = kodi.pvr_get_recording_details_batch(recording_ids)

    for line in record_details_dict:
        episode_dict = series_info(line['plot'])

        if episode_dict['episode'] < next_episode - 2:
            tv.find_and_delete_recordings(line['label'], plot=line['plot'])

        if episode_dict['episode'] == next_episode - 1 and line['resume'][
                'position'] > 0:
            print("Resume current episode", next_episode - 1)
            player_recording = {
                "id": line['recordingid'],
                "title": line['label']
            }
            resume_current = True

        if episode_dict['episode'] == next_episode:
            print("Found next episode", next_episode)
            player_recording = {
                "id": line['recordingid'],
                "title": line['label']
            }

    if not player_recording:
        print("No unwatched future episodes")

    if player_recording:
        print('Playing', player_recording['title'])
        if resume_current:
            # Kodi asks user if they want to start from beginning or to resume, by default it highlights the resume
            # option, therefore we want send a post to "select" current choice. This is my hack for getting around
            # Kodi's api limitation in that there is no command to skip the option. The delay is because our player.open
            # post doesnt give a response until the set programme is being played, so we create a timer delay ,rather
            # than a time.sleep, for the select request to run despite there no request
            # after given seconds for Kodi to catch up
            t = Timer(2.0, kodi.input_select)
            t.start()
            print("play")
            kodi.player_open(player_recording['id'])

        else:
            kodi.player_open(player_recording['id'])
        return player_recording['title']
    return "nothing"
Exemple #7
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()
Exemple #8
0
 def __init__(self, offset_filename):
     super(RecordSimilarBroadcasts, self).__init__(offset_filename)
     self.db = DatabaseResource()
     self.kodi = KodiResource()
     self.tvdb = TheTvDbResource()