def handleSubscription(self, subscription):
        if not subscription.anime:
            return False
        print('handling', subscription.anime)

        aid = subscription.anime.id
        nyaa = Nyaa(aid)
        group = nyaa.get_groups()[0][0]
        # gotta wait for groups to be available 8D
        if not group:
            return False
        # F**K THE CONFIG
        qualities = ['720p', '480p', '']

        for episode in subscription.anime.episodes:
            if episode.download:
                # if theres an entry for this episode in the download table, skip it
                continue

            if episode.files:
                found = False
                for file in episode.files:
                    if isfile(file.path):
                        # set the found flag is we find a valid file
                        found = True
                    else:
                        # delete any invalid looking rows
                        with session_scope() as session:
                            session.delete(file)
                if found:
                    # if we found a valid file for this episode, skip it
                    continue

            # get all the available torrents for this episode
            available = nyaa.find_episode(episode.epno)
            if len(available):
                # TODO have group/quality preference/order to pick the right one
                # and download the one we want if there was any
                chosen = None
                for quality in qualities:
                    for torrent in available:
                        title = torrent[0].lower()
                        good = group.lower() in title and quality in title
                        if good:
                            chosen = torrent
                            break
                    if chosen:
                        break
                if chosen:
                    Nyaa.download_torrent(chosen, eid=episode.id)
def anime(aid):
    anime = Anime(aid)
    if anime.xml == None:
        return "That AID isn't right"
    indexed_anime = index.Anime.get({"id": aid})
    episodes = indexed_anime.episodes if indexed_anime is not None else []

    # get splash image from tvdb
    splash = indexed_anime.picture
    if not splash:
        for title in indexed_anime.get_names():
            splash = tvdb.get_fanart(title)
            if splash:
                indexed_anime.picture = splash
                # update the row with this picture, don't know if there's an easier way but this shouldn't happen here ideally
                with index.session_scope() as session:
                    session.merge(indexed_anime)
                break

    return render_template("anime.html", anime=indexed_anime, episodes=episodes, splash=splash, poster=None)