1
0
mirror of https://github.com/krateng/maloja.git synced 2023-08-10 21:12:55 +03:00

Added support for regular tasks and implemented it for medals

This commit is contained in:
Krateng 2019-04-03 17:16:27 +02:00
parent c67aced296
commit 4557a208e9
2 changed files with 125 additions and 21 deletions

View File

@ -788,26 +788,10 @@ def build_db():
# load cached images
#loadCache()
#medals
global MEDALS
firstyear = datetime.datetime.utcfromtimestamp(STAMPS[0]).year
currentyear = datetime.datetime.utcnow().year
for year in range(firstyear,currentyear):
charts = get_charts_artists(within=[year])
scr = -1
rank = 0
for a in charts:
if a["scrobbles"] != scr: rank = charts.index(a) + 1
if rank > 3: break
artist = a["artist"]
if rank == 1: MEDALS.setdefault(artist,{}).setdefault("gold",[]).append(year)
if rank == 2: MEDALS.setdefault(artist,{}).setdefault("silver",[]).append(year)
if rank == 3: MEDALS.setdefault(artist,{}).setdefault("bronze",[]).append(year)
#start regular tasks
startpulse()
scr = a["scrobbles"]
log("Database fully built!")

View File

@ -1,7 +1,7 @@
import re
import os
import hashlib
from threading import Thread
from threading import Thread, Timer
import pickle
import urllib
import datetime
@ -12,6 +12,21 @@ from doreah import caching
from doreah.logging import log
#####
## RULESTATE VALIDATION
#####
def checksumTSV(folder):
sums = ""
@ -74,7 +89,25 @@ def consistentRulestate(folder,checksums):
### Media info
#####
## IMAGES
#####
def apirequest(artists=None,artist=None,title=None):
@ -421,3 +454,90 @@ def resolveImage(artist=None,track=None):
return getTrackImage(track["artists"],track["title"])
elif artist is not None:
return getArtistImage(artist)
#####
## PULSE MAINTENANCE
#####
def startpulse():
# execute all actions for startup
# they will themselves trigger their next pass
yearly()
monthly()
daily()
def yearly():
#medals
from database import MEDALS, STAMPS, get_charts_artists
MEDALS.clear()
firstyear = datetime.datetime.utcfromtimestamp(STAMPS[0]).year
currentyear = datetime.datetime.utcnow().year
for year in range(firstyear,currentyear):
charts = get_charts_artists(within=[year])
scr = -1
rank = 0
for a in charts:
if a["scrobbles"] != scr: rank = charts.index(a) + 1
if rank > 3: break
artist = a["artist"]
if rank == 1: MEDALS.setdefault(artist,{}).setdefault("gold",[]).append(year)
if rank == 2: MEDALS.setdefault(artist,{}).setdefault("silver",[]).append(year)
if rank == 3: MEDALS.setdefault(artist,{}).setdefault("bronze",[]).append(year)
scr = a["scrobbles"]
# schedule for next year
now = datetime.datetime.utcnow()
nextyear = datetime.datetime(now.year+1,1,1)
wait = nextyear.timestamp() - now.timestamp()
Timer(wait,yearly).start()
def monthly():
log("New month!",module="debug")
# schedule for next month
now = datetime.datetime.utcnow()
nextmonth = datetime.datetime(now.year,now.month + 1,1) if now.month != 12 else datetime.datetime(now.year+1,1,1)
wait = nextmonth.timestamp() - now.timestamp()
Timer(wait,monthly).start()
def daily():
log("New day!",module="debug")
# schedule for tomorrow
now = datetime.datetime.utcnow()
nextday = datetime.datetime(now.year,now.month,now.day) + datetime.timedelta(days=1)
wait = nextday.timestamp() - now.timestamp()
Timer(wait,daily).start()