Skip to content

Commit

Permalink
Merge pull request #39 from guerda/feat_genre_heatmap
Browse files Browse the repository at this point in the history
feat: add genre heatmap
  • Loading branch information
guerda authored Jan 14, 2025
2 parents 133d714 + f0590af commit 84923ad
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
40 changes: 39 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ async def get_artist_stats(
artists = beets_statistics.get_artist_stats(limit=100)
count = beets_statistics.get_track_count()
return templates.TemplateResponse(
request=request, name="artists.html", context={"artists": artists, "track_count": count}
request=request,
name="artists.html",
context={"artists": artists, "track_count": count},
)


Expand Down Expand Up @@ -138,3 +140,39 @@ async def get_track_quality(
name="quality.html",
context={"bitrates": bitrates, "track_count": count},
)

@app.get("/genre-decade-heatmap", response_class=HTMLResponse)
async def get_genre_decade_heatmap(
request: Request,
beets_statistics: Annotated[BeetsStatistics, Depends(get_beets_statistics)]
):
results = beets_statistics.get_genre_decade_heatmap()

min_decade = 9999
max_decade = 0
max_genre_count = 0
heatmap = {}

for result in results:
min_decade = min(min_decade, result['decade'])
max_decade = max(max_decade, result['decade'])
max_genre_count = max(max_genre_count, result['count'])

if result['genre'] not in heatmap:
heatmap[result['genre']] = {}

heatmap[result['genre']][result['decade']] = result['count']

# Fill out sparse table
for genre in heatmap:
for decade in range(min_decade, max_decade+1, 10):
if decade not in heatmap[genre]:
heatmap[genre][decade] = 0

print(heatmap)

return templates.TemplateResponse(
request=request,
name="genre-decade-heatmap.html",
context={"heatmap": heatmap, "decades": range(min_decade, max_decade+1, 10), "max_genre_count": max_genre_count},
)
20 changes: 20 additions & 0 deletions beetsstatistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,26 @@ def get_track_quality(self, limit: int = 0):
except sqlite3.Error as e:
raise DBQueryError from e

def get_genre_decade_heatmap(self):
try:
cursor = self.get_db_connection().cursor()
query = """select
genre,
YEAR / 10 * 10 as decade,
count(1) as count
from albums
where genre != '' and year > 0
group by 1,2
order by 1,2 asc"""
res = cursor.execute(query)
results = res.fetchall()
cursor.close()

return results
except sqlite3.Error as e:
raise DBQueryError from e



if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="beets-statistics")
Expand Down
35 changes: 35 additions & 0 deletions templates/genre-decade-heatmap.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Albums - Beets Statistics</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="{{ url_for('static', path='main.css') }}" />
</head>
<body>
{% include 'navigation.html' %}
<content>
<h2>Genre Decade Heatmap</h2>
<table>
<tr>
<th>Genre</th>
{% for decade in decades %}
<th>{{ decade }}
{% endfor %}
</tr>
{% for genre in heatmap %}
<tr>
<td>{{ genre }}</td>
{% for decade in heatmap[genre]|sort %}
<td><div style="background-color: rgba(0,200,50, {{ heatmap[genre][decade] / max_genre_count }}); padding: 0.15em; margin: 0.1em; border-radius: 3px; width: 4em; display:inline;"
title="{{decade}} {{heatmap[genre][decade]}} {{genre}};">
{{ heatmap[genre][decade] }}</div></td>
{% endfor %}
</tr>
{% endfor %}
</table>
</content>
{% include 'footer.html' %}
</body>
</html>
1 change: 1 addition & 0 deletions templates/navigation.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ <h1>Beets Statistics</h1>
<li><a href="/artists">Artists</a></li>
<li><a href="/decades">Decades</a></li>
<li><a href="/quality">Quality</a></li>
<li><a href="/genre-decade-heatmap">Genre Heatmap</a></li>
</ul>
</navigation>

0 comments on commit 84923ad

Please sign in to comment.