def mc_policy_eval(policy, env, num_episodes, discount_rate=1.0):
    """
    First visit monte carlo policy evaluation
    input:
    - policy: map observation to action
    - env: environment
    - num_episodes: # of episodes to run
    - discount_rate: gamma
    output:
    - V: state value function according to policy
    """
    state_counter = defaultdict(int)
    V = defaultdict(float)

    for t in range(num_episodes):
        episode = get_episode(policy, env)
        visited_states = set()

        for i, (state, _action, _reward) in enumerate(episode):
            # only allow first visit to contribute to state value
            if state not in visited_states:
                visited_states.add(state)
                state_counter[state] += 1
                G = get_disounted_reward(
                    list(map(lambda x: x[2], episode[i:])), discount_rate)

                # incremental mean
                V[state] += (G - V[state]) / state_counter[state]

    return V
Ejemplo n.º 2
0
def main():
    #TODO: add support for mysql
    for subgroup in d.available_subgroups:
        try:
            cursor.execute("SELECT * FROM %s;" %(subgroup['db_name']))
            anime_list = cursor.fetchall()
        except Exception as e:
            logging.error(e)
        
        for anime in anime_list:
            #Gets rss feed with params
            search_param = ' '.join(anime)

            i = 1
            while True:
                feed_url = ('%s?page=rss&term=%s&user=%s&offset=%d' %(nyaa_url, search_param, subgroup['nyaa_id'], i)).replace(' ', '+')
                i = i + i
            
                feed = feedparser.parse(feed_url)
                if not feed['entries']:
                    break
                #pprint(feed)
                
                for feed_entry in feed['entries']:
                    feed_title = feed_entry['title']
                    parsed_title = utils.get_title(feed_title, subgroup['regex']['title'])
                    if parsed_title == anime[0]:
                        parsed_episode = utils.get_episode(feed_title, subgroup['regex']['episode'])
                        if parsed_episode:
                            cursor.execute("SELECT 1 FROM downloaded WHERE title='%s' AND episode='%s' AND subgroup='%s';" %(anime[0].replace("'", "''"), parsed_episode, subgroup['subgroup']))
                            if not cursor.fetchone():
                                dl_location = cfg.dl_location + anime[0]
                                if not os.path.exists(dl_location):
                                    os.mkdir(dl_location)

                                download_queue.append({'torrent': download_torrent(anime[0], feed_entry['link']), 'info': {'title': anime[0], 'episode': parsed_episode, 'quality': anime[1], 'subgroup': subgroup['subgroup']}})
                                cursor.execute("INSERT INTO downloaded VALUES('%s', '%s', '%s', '%s', '%s');" %(anime[0].replace("'", "''"), parsed_episode, datetime.datetime.now().isoformat(), subgroup['subgroup'], 'Downloading'))
                                connection.commit()

    # spawn child thread here to monitor downloads.
    while len(download_queue):
        for torrent in download_queue:
            torrent_obj = tc.get_torrent(torrent['torrent'])
            if torrent_obj.status == 'seeding':
                logging.info('%s completed.' %(torrent_obj.name))
                cursor.execute("UPDATE downloaded SET status='Completed' WHERE title='%s' AND episode='%s' AND subgroup='%s';" %(torrent['info']['title'].replace("'", "''"), torrent['info']['episode'], torrent['info']['subgroup']))
                connection.commit()
                download_queue.remove(torrent)
            sleep(1)
    connection.commit()
    connection.close()
Ejemplo n.º 3
0
def fetcher():
    for subgroup in d.available_subgroups:
        try:
            cursor.execute("SELECT * FROM %s;" %(subgroup['db_name']))
            search_list = cursor.fetchall()
        except Exception as e:
            logging.error(e)

        for item in search_list:
            search_param = ' '.join(item)
            i = 1
            while True:
                feed_url = ('%s?page=rss&term=%s&user=%s&offset=%d' %(nyaa_url, search_param, subgroup['nyaa_id'], i)).replace(' ', '+')
                i = i + 1
                feed = feedparser.parse(feed_url)
                if not feed['entries']:
                    break
                for feed_entry in feed['entries']:
                    feed_title = feed_entry['title']
                    parsed_title = utils.get_title(feed_title, subgroup['regex']['title'])
                    if parsed_title == item[0]:
                        parsed_episode = utils.get_episode(feed_title, subgroup['regex']['episode'])
                        if parsed_episode:
                            cursor.execute("SELECT 1 FROM downloaded WHERE title='%s' AND episode='%s' AND subgroup = '%s';" %(item[0].replace("'", "''"), parsed_episode, subgroup['subgroup']))
                            if not cursor.fetchone():
                                dl_location = cfg.dl_location + item[0]
                                if not os.path.exists(dl_location):
                                    try:
                                        os.mkdir(dl_location)
                                    except Exception as e:
                                        logging.error(e)
                                payload.append({'title': item[0], 'link': feed_entry['link'], 'episode': parsed_episode, 'subgroup': subgroup['subgroup'], 'quality': item[1]})

            
        #Send payload to WebAPI
        r = requests.post('http://%s:%s/api/addtorrent' %(API_URL, API_PORT), headers={'content-type':'application/json'}, data=json.dumps(payload))
Ejemplo n.º 4
0
 def get(self, args):
     return utils.get_episode(args['link'], args['provider'],
                              args['parent'])
Ejemplo n.º 5
0
def episode(showId, episodeId):
    result = utils.get_episode(showId, episodeId)
    sectionTemplate = "./templates/episode.tpl"
    return template(sectionTemplate, version=utils.getVersion(), result=result)