def _gen_text_discussions(db, formats, show, stream):
	episodes = db.get_episodes(show)
	debug("Num previous episodes: {}".format(len(episodes)))
	if len(episodes) > 0:
		table = []
		for episode in episodes:
			episode = stream.to_display_episode(episode)
			poll_handler = services.get_default_poll_handler()
			poll = db.get_poll(show, episode)
			if poll is None:
				score = None
				poll_link = None
			elif poll.has_score:
				score = poll.score
				poll_link = poll_handler.get_results_link(poll)
			else:
				score = poll_handler.get_score(poll)
				poll_link = poll_handler.get_results_link(poll)
			table.append(safe_format(formats["discussion"], episode=episode.number, link=episode.link, score=score if score else "", poll_link=poll_link if poll_link else "http://localhost")) # Need valid link even when empty

		num_columns = 1 + (len(table) - 1) // 20
		format_head, format_align = formats["discussion_header"], formats["discussion_align"]
		table_head = '|'.join(num_columns * [format_head]) + '\n' + '|'.join(num_columns * [format_align])
		table = ['|'.join(table[i::20]) for i in range(20)]
		return table_head + "\n" + "\n".join(table)
	else:
		return formats["discussion_none"]
def _gen_text_discussions(db, formats, show, stream):
	episodes = db.get_episodes(show)
	debug("Num previous episodes: {}".format(len(episodes)))
	N_LINES = 13
	n_episodes = 4 * N_LINES # maximum 4 columns
	if len(episodes) > n_episodes:
		debug(f'Clipping to most recent {n_episodes} episodes')
		episodes = episodes[-n_episodes:]
	if len(episodes) > 0:
		table = []
		for episode in episodes:
			episode = stream.to_display_episode(episode)
			poll_handler = services.get_default_poll_handler()
			poll = db.get_poll(show, episode)
			if poll is None:
				score = None
				poll_link = None
			elif poll.has_score:
				score = poll.score
				poll_link = poll_handler.get_results_link(poll)
			else:
				score = poll_handler.get_score(poll)
				poll_link = poll_handler.get_results_link(poll)
			score = poll_handler.convert_score_str(score)
			table.append(safe_format(formats["discussion"], episode=episode.number, link=episode.link, score=score, poll_link=poll_link if poll_link else "http://localhost")) # Need valid link even when empty

		num_columns = 1 + (len(table) - 1) // N_LINES
		format_head, format_align = formats["discussion_header"], formats["discussion_align"]
		table_head = '|'.join(num_columns * [format_head]) + '\n' + '|'.join(num_columns * [format_align])
		table = ['|'.join(table[i::N_LINES]) for i in range(N_LINES)]
		return table_head + "\n" + "\n".join(table)
	else:
		return formats["discussion_none"]
def _gen_text_poll(db, config, formats, show, episode):
	handler = services.get_default_poll_handler()
	title = config.post_poll_title.format(show = show.name, episode = episode.number)

	poll = db.get_poll(show, episode)
	if poll is None:
		poll_id = handler.create_poll(title, headers = {'User-Agent': config.useragent}, submit=not config.debug)
		if poll_id:
			site = db.get_poll_site(key=handler.key)
			db.add_poll(show, episode, site, poll_id)
			poll = db.get_poll(show, episode)

	if poll is not None:
		poll_url = handler.get_link(poll)
		poll_results_url = handler.get_results_link(poll)
		return safe_format(formats["poll"], poll_url=poll_url, poll_results_url=poll_results_url)
	else:
		return ""
Exemple #4
0
def _record_poll_scores(config, db, update_db):
    polls = db.get_polls(missing_score=True)
    handler = services.get_default_poll_handler()
    info(f"Record scores for service {handler.key}")

    updated = 0
    for poll in polls:
        if datetime.now() - poll.date > timedelta(days=7):
            score = handler.get_score(poll)
            info(
                f"Updating poll score for show {poll.show_id} / episode {poll.episode} ({score})"
            )
            if score:
                db.update_poll_score(poll, score, commit=update_db)
                updated += 1

    info(
        f"{updated} scores recorded, {len(polls) - updated} scores not updated"
    )