From 3a06f9c56f17a0c3cbc8e1df84948c88c12513e8 Mon Sep 17 00:00:00 2001 From: "Casper V. Kristensen" Date: Fri, 15 Mar 2019 01:17:45 +0100 Subject: [PATCH] Refactor nested function out of generate_post for better readability. --- dailyreleases/generation.py | 79 ++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 37 deletions(-) diff --git a/dailyreleases/generation.py b/dailyreleases/generation.py index 50ec79f..c474e11 100644 --- a/dailyreleases/generation.py +++ b/dailyreleases/generation.py @@ -14,28 +14,54 @@ from .parsing import ParsedReleases, Release, ReleaseType logger = logging.getLogger(__name__) -def generate_post(parsed_releases: ParsedReleases) -> str: +def popularity(release: Release): + # The popularity of a game is defined by the number of reviews it has on Steam, however: + # - We rank RIPs lower than non-RIPs so the same game released as both will sort the non-RIP first. + # - Releases with highlights (e.g. PROPER/DENUVO) are always ranked highest. + is_rip = "RIP" in [tag.upper() for tag in release.tags] + return len(release.highlights), release.num_reviews, not is_rip + + +def row(release: Release): + # The rows in the table containing updates will use the full rls_name as the name, while tables + # containing game and DLC releases will show tags and highlights, as well as the stylized game_name. + if release.type == ReleaseType.UPDATE: + name = f"[{release.rls_name}]({release.nfo_link})" + else: + tags = " ({})".format(" ".join(release.tags)) if release.tags else "" + highlights = " **- {}**".format(", ".join(release.highlights)) if release.highlights else "" + name = "[{}{}]({}){}".format(util.markdown_escape(release.game_name), + tags, + release.nfo_link, + highlights) + + stores = ", ".join(f"[{name}]({link})" for name, link in release.store_links.items()) + + if release.score == -1: + reviews = "-" + else: + num_reviews_humanized = util.humanize(release.num_reviews, precision=1, prefix="dec", suffix="") + reviews = f"{release.score:.0%} ({num_reviews_humanized})" + + return name, release.group, stores, reviews + + +def generate_post(releases: ParsedReleases) -> str: post = [] - for platform, platform_releases in parsed_releases.items(): + for platform, platform_releases in releases.items(): + # Skip if platform doesn't contain any releases in any of the three release type categories if sum(len(pr) for pr in platform_releases.values()) == 0: continue post.append(f"# {platform}") - for release_type, releases in platform_releases.items(): - if not releases: + for type, type_releases in platform_releases.items(): + if not type_releases: continue # Releases in the tables are grouped by release group, and the groups are ordered according to the most # popular game within the group. Games are sorted by popularity internally in the groups as well. - def popularity(release: Release): - # The popularity of a game is defined by the number of reviews it has on Steam, however: - # - We rank RIPs lower than non-RIPs so the same game released as both will sort the non-RIP first. - # - Releases with highlights (e.g. PROPER/DENUVO) are always ranked highest. - is_rip = "RIP" in [tag.upper() for tag in release.tags] - return len(release.highlights), release.num_reviews, not is_rip - - group_order = defaultdict(lambda: (-1, False)) - for release in releases: + group_order = defaultdict(lambda: (0, -1, False)) + for release in type_releases: group_order[release.group] = max(group_order[release.group], popularity(release)) def order(release: Release): @@ -43,32 +69,11 @@ def generate_post(parsed_releases: ParsedReleases) -> str: release.group, # ensure grouping if two groups share group_order popularity(release)) - def row(release: Release): - # The rows in the table containing updates will use the full rls_name as the name, while tables - # containing game and DLC releases will show tags and highlights, as well as the stylized game_name. - if release_type == ReleaseType.UPDATE: - name = f"[{release.rls_name}]({release.nfo_link})" - else: - tags = " ({})".format(" ".join(release.tags)) if release.tags else "" - highlights = " **- {}**".format(", ".join(release.highlights)) if release.highlights else "" - name = "[{}{}]({}){}".format(util.markdown_escape(release.game_name), - tags, - release.nfo_link, - highlights) + type_releases.sort(key=order, reverse=True) - if release.score == -1: - reviews = "-" - else: - num_reviews_humanized = util.humanize(release.num_reviews, precision=1, prefix="dec", suffix="") - reviews = f"{release.score:.0%} ({num_reviews_humanized})" - - stores = ", ".join(f"[{name}]({link})" for name, link in release.store_links.items()) - - return name, release.group, stores, reviews - - post.append(f"| {release_type} | Group | Store | Score (Reviews) |") + post.append(f"| {type} | Group | Store | Score (Reviews) |") post.append("|:-|:-|:-|:-|") - post.extend("| {} | {} | {} | {} |".format(*row(rls)) for rls in sorted(releases, key=order, reverse=True)) + post.extend("| {} | {} | {} | {} |".format(*row(rls)) for rls in type_releases) post.append("") post.append(" ")