mirror of
https://github.com/krateng/maloja.git
synced 2025-04-15 08:20:32 +03:00
115 lines
2.8 KiB
Python
115 lines
2.8 KiB
Python
from ..__pkginfo__ import version
|
|
from ..malojatime import ranges, thisweek, thisyear
|
|
from ..globalconf import malojaconfig
|
|
|
|
from doreah.regular import yearly, daily
|
|
from doreah.logging import log
|
|
|
|
import datetime
|
|
import json
|
|
import urllib
|
|
import itertools
|
|
|
|
|
|
|
|
get_track = lambda x:(frozenset(x["track"]["artists"]),x["track"]["title"])
|
|
get_artist = lambda x:x["artist"]
|
|
|
|
def group_by_attribute(sequence,attribute):
|
|
grouped = itertools.groupby(sequence,key=lambda x:x[attribute])
|
|
for attrvalue,members in grouped:
|
|
yield attrvalue,list(members)
|
|
|
|
def collect_rankings(chart,identify,collection,iteration=None,count=True):
|
|
grouped = group_by_attribute(chart,"rank")
|
|
for rank, members in grouped:
|
|
if not count and rank not in rankmedals: break
|
|
if count and rank != 1: break
|
|
|
|
for m in members:
|
|
# get the actual object that we're counting
|
|
entity = identify(m)
|
|
|
|
# count no1 spots
|
|
if count:
|
|
collection[entity] = collection.setdefault(entity,0) + 1
|
|
|
|
# collect instances of top3 spots
|
|
else:
|
|
medal = rankmedals[rank]
|
|
collection.setdefault(entity,{}).setdefault(medal,[]).append(iteration)
|
|
|
|
|
|
rankmedals = {
|
|
1:'gold',
|
|
2:'silver',
|
|
3:'bronze'
|
|
}
|
|
|
|
@yearly
|
|
def update_medals():
|
|
|
|
|
|
from ..database import MEDALS_ARTISTS, MEDALS_TRACKS, STAMPS, get_charts_artists, get_charts_tracks
|
|
|
|
|
|
MEDALS_ARTISTS.clear()
|
|
MEDALS_TRACKS.clear()
|
|
|
|
for year in ranges(step="year"):
|
|
if year == thisyear(): break
|
|
|
|
charts_artists = get_charts_artists(timerange=year)
|
|
charts_tracks = get_charts_tracks(timerange=year)
|
|
|
|
collect_rankings(charts_artists,get_artist,MEDALS_ARTISTS,iteration=year,count=False)
|
|
collect_rankings(charts_tracks,get_track,MEDALS_TRACKS,iteration=year,count=False)
|
|
|
|
|
|
@daily
|
|
def update_weekly():
|
|
|
|
from ..database import WEEKLY_TOPTRACKS, WEEKLY_TOPARTISTS, get_charts_artists, get_charts_tracks
|
|
|
|
|
|
WEEKLY_TOPARTISTS.clear()
|
|
WEEKLY_TOPTRACKS.clear()
|
|
|
|
for week in ranges(step="week"):
|
|
if week == thisweek(): break
|
|
|
|
charts_artists = get_charts_artists(timerange=week)
|
|
charts_tracks = get_charts_tracks(timerange=week)
|
|
|
|
collect_rankings(charts_artists,get_artist,WEEKLY_TOPARTISTS)
|
|
collect_rankings(charts_tracks,get_track,WEEKLY_TOPTRACKS)
|
|
|
|
|
|
@daily
|
|
def send_stats():
|
|
if malojaconfig["SEND_STATS"]:
|
|
|
|
log("Sending daily stats report...")
|
|
|
|
from ..database import ARTISTS, TRACKS, SCROBBLES
|
|
|
|
keys = {
|
|
"url":"https://myrcella.krateng.ch/malojastats",
|
|
"method":"POST",
|
|
"headers":{"Content-Type": "application/json"},
|
|
"data":json.dumps({
|
|
"name":malojaconfig["NAME"],
|
|
"url":malojaconfig["PUBLIC_URL"],
|
|
"version":".".join(str(d) for d in version),
|
|
"artists":len(ARTISTS),
|
|
"tracks":len(TRACKS),
|
|
"scrobbles":len(SCROBBLES)
|
|
}).encode("utf-8")
|
|
}
|
|
try:
|
|
req = urllib.request.Request(**keys)
|
|
response = urllib.request.urlopen(req)
|
|
log("Sent daily report!")
|
|
except:
|
|
log("Could not send daily report!")
|