# Path: trackma/utils.py
# def estimate_aired_episodes(show):
#     """ Estimate how many episodes have passed since airing """
# 
#     if show['status'] == Status.FINISHED:
#         return show['total']
#     elif show['status'] == Status.NOTYET:
#         return 0
#     # It's airing, so we make an estimate based on available information
#     elif show['status'] == Status.AIRING:
#         if 'next_ep_number' in show:  # Do we have the upcoming episode number?
#             return show['next_ep_number']-1
#         elif show['start_date']:  # Do we know when it started? Let's just assume 1 episode = 1 week
#             days = (datetime.datetime.now() - show['start_date']).days
#             if days <= 0:
#                 return 0
# 
#             eps = days // 7 + 1
#             if show['total'] and eps > show['total']:
#                 return show['total']
#             return eps
#     return 0


import time
import os
from threading import Thread
from asyncio import (
    new_event_loop as new_loop,
    set_event_loop as set_loop)
from pypresence.client import Client
from pypresence.exceptions import InvalidID, InvalidPipe
from trackma.utils import estimate_aired_episodes
    def _reconnect(self):
        if not self._enabled:
            try:
                self._rpc = Client(self._client_id)
                self._rpc.start()
                self._enabled = True

            except self._errors:
                self._enabled = False


rpc = DiscordRPC(daemon=True)


def init(engine):
    """
    Initialize this hook.
    """
    rpc.start()
    rpc.present(engine)


def tracker_state(engine, status):
    """
    Update status in thread.
    """
    if status["state"] == 1:
        show = status["show"][0]
        title = show["titles"][0]
        episode = status["show"][-1]

# Path: trackma/utils.py
# def estimate_aired_episodes(show):
#     """ Estimate how many episodes have passed since airing """
# 
#     if show['status'] == Status.FINISHED:
#         return show['total']
#     elif show['status'] == Status.NOTYET:
#         return 0
#     # It's airing, so we make an estimate based on available information
#     elif show['status'] == Status.AIRING:
#         if 'next_ep_number' in show:  # Do we have the upcoming episode number?
#             return show['next_ep_number']-1
#         elif show['start_date']:  # Do we know when it started? Let's just assume 1 episode = 1 week
#             days = (datetime.datetime.now() - show['start_date']).days
#             if days <= 0:
#                 return 0
# 
#             eps = days // 7 + 1
#             if show['total'] and eps > show['total']:
#                 return show['total']
#             return eps
#     return 0


# Path: hooks/presence.py
import time
import os
from threading import Thread
from asyncio import (
    new_event_loop as new_loop,
    set_event_loop as set_loop)
from pypresence.client import Client
from pypresence.exceptions import InvalidID, InvalidPipe
from trackma.utils import estimate_aired_episodes

    def _reconnect(self):
        if not self._enabled:
            try:
                self._rpc = Client(self._client_id)
                self._rpc.start()
                self._enabled = True

            except self._errors:
                self._enabled = False


rpc = DiscordRPC(daemon=True)


def init(engine):
    """
    Initialize this hook.
    """
    rpc.start()
    rpc.present(engine)


def tracker_state(engine, status):
    """
    Update status in thread.
    """
    if status["state"] == 1:
        show = status["show"][0]
        title = show["titles"][0]
        episode = status["show"][-1]

        total = show["total"] or estimate_aired_episodes(