-
Notifications
You must be signed in to change notification settings - Fork 2
/
weekly.py
55 lines (51 loc) · 1.92 KB
/
weekly.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from loadsongs import load_song_lists
from app.models import WeeklyPost
from app import db, current_app, raw_songdata
import datetime
import random
import json
import app
import os
import send_webhook
songpairs, songtypes = load_song_lists()
def get_current_weekly():
with open('weekly.json', 'r') as f:
weeklylist = json.load(f)
return [weeklylist['song'], weeklylist['length']]
def create_json(item):
scoredict = {
item.id: {
'song': item.song,
'difficulty': item.difficulty,
'lettergrade': item.lettergrade,
'score': item.score,
'stagepass': item.stagepass,
'platform': item.platform,
'ranked': item.ranked,
'author': item.author.username
}
}
rootdir = os.path.join(current_app.root_path, f'static/archived_weekly')
datedir = os.path.join(current_app.root_path, f'static/archived_weekly', datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
if not os.path.exists(datedir):
os.makedirs(datedir)
jsonfile = os.path.join(rootdir, datedir, f'{item.id}.json')
with open(jsonfile, 'w+') as f:
json.dump(scoredict, f, indent=2)
def randomize_weekly(app):
songs = load_song_lists()
with open('weekly.json', 'r') as f:
weeklyjson = json.load(f)
newsong = random.choice(songpairs)[0]
weeklyjson['song'] = newsong.decode('utf-8')
weeklyjson['length'] = raw_songdata[newsong.decode('utf-8')]['length']
weeklyjson['diffs'] = raw_songdata[newsong.decode('utf-8')]['difficulties']
with open('weekly.json', 'w') as f:
json.dump(weeklyjson, f, indent=2)
with app.app_context():
posts = db.session.query(WeeklyPost).all()
for item in posts:
create_json(item)
db.session.query(WeeklyPost).delete()
db.session.commit()
send_webhook.notify(newsong.decode('utf-8'), raw_songdata[newsong.decode('utf-8')]['length'])