94 lines
2.6 KiB
Python
94 lines
2.6 KiB
Python
|
|
import glob
|
|
import re
|
|
import os
|
|
import matplotlib.pyplot as plt
|
|
import sys
|
|
|
|
def is_in_year(year, content):
|
|
return re.search(r"date:\s?\"?" + str(year) + "-", content) is not None
|
|
|
|
def end_of_year(year):
|
|
games = []
|
|
for name in glob.glob('../content/games/**/*.md'):
|
|
if not '/_index.md' in name:
|
|
with open(name) as file:
|
|
content = file.read()
|
|
|
|
if is_in_year(year, content):
|
|
games.append(to_game_entry(content, name))
|
|
|
|
return games
|
|
|
|
def to_game_entry(content, name):
|
|
parts = name.split('/')
|
|
game_name = parts[-1].replace(".md", "")
|
|
game_platform = parts[-2]
|
|
cover = "../static/games/" + game_platform + "/" + game_name + "/" + "cover.jpg"
|
|
|
|
def pry_out(key, defval):
|
|
val = re.search(key + r":\s?(.*)", content)
|
|
if val is None:
|
|
return defval
|
|
if type(defval) is float:
|
|
return float(val.group(1))
|
|
if type(defval) is int:
|
|
return int(val.group(1))
|
|
return val.group(1).replace("\"", "")
|
|
|
|
return { "cover": cover, "hltb": pry_out("howlongtobeat_hrs", 0.0), "name": pry_out("game_name", ""), "platform": game_platform, "score": pry_out("score", 0) }
|
|
|
|
def montage(games_list, year):
|
|
coverlist = list(map(lambda game: game["cover"], games_list))
|
|
file = "collage-" + str(year) + ".jpg"
|
|
|
|
cmd = "montage " + " ".join(coverlist) + " -geometry +0+0 -tile 8x5 -resize 170x200! " + file
|
|
os.system(cmd)
|
|
os.system("open " + file)
|
|
|
|
def print_stats(games_list):
|
|
games_list = sorted(games_list, key=lambda game: game["hltb"])
|
|
total_hours = sum(list(map(lambda game: game["hltb"], games_list)))
|
|
|
|
print(" -- total hours: " + str(total_hours))
|
|
print(" -- average hours: " + str(round(total_hours / len(games_list), 2)))
|
|
print(" -- average a day: " + str(round(total_hours / 355, 2)))
|
|
print()
|
|
|
|
print(" -- longest game: " + str(games_list[-1]["hltb"]) + " hours; " + games_list[-1]["name"])
|
|
print(" -- shortest game: " + str(games_list[0]["hltb"]) + " hours; " + games_list[0]["name"])
|
|
print()
|
|
|
|
per_platform = {}
|
|
for game in games_list:
|
|
if game["platform"] not in per_platform:
|
|
per_platform[game["platform"]] = [game]
|
|
else:
|
|
per_platform[game["platform"]].append(game)
|
|
|
|
for k, v in per_platform.items():
|
|
print(" -- Platform: " + k + " (" + str(len(v)) + "/" + str(len(games_list)) + ")")
|
|
|
|
|
|
def generate_chart(games_list):
|
|
x = list(map(lambda g: g["name"], games_list))
|
|
y = list(map(lambda g: g["score"], games_list))
|
|
|
|
plt.plot(x, y)
|
|
plt.xticks(rotation = 90)
|
|
plt.show()
|
|
|
|
|
|
if len(sys.argv) <= 1:
|
|
print("Please provide a year [e.g. end-of-year-poster.py 2022].")
|
|
exit(-1)
|
|
year = int(sys.argv[1])
|
|
|
|
os.system("rm -rf *.jpg")
|
|
games_list = end_of_year(year)
|
|
|
|
montage(games_list, year)
|
|
print_stats(games_list)
|
|
generate_chart(games_list)
|
|
|