ccc_progress_tracker_bot/main.py

117 lines
3.1 KiB
Python
Raw Permalink Normal View History

2023-08-21 01:54:32 +02:00
#!/usr/bin/env python
from datetime import date
2023-08-21 01:54:32 +02:00
import json
import os
import requests as r
import secret
from time import time
import traceback
API_CALL = 'https://suya.place/api/v1/statuses'
API_HEADERS = { 'Authorization': 'Bearer %s' % (secret.TOKEN) } # API key here!
EXCEPTION_MESSAGE_FORMAT = '@a1ba\n%s' # send an exception text to my direct messages
KOTCRAB_API = 'https://kotcrab.com/progress/proxy/ccc?x=%d'
def get_stats():
data = r.get(KOTCRAB_API % int(time() * 1000)).json()
return data[0]['units'], data[1]['units']
def post(visibility, status):
resp = r.post(API_CALL, headers = API_HEADERS, json = {
'visibility': visibility,
'status' : status
})
return resp.status_code == 200
def parse_stats(stats, ref):
data = {}
# this stuff isn't documented by Kotcrab so let's be careful here
if stats.keys() != ref.keys():
raise Exception
for k in stats.keys():
if ref[k]['name'] != stats[k]['name']:
raise Exception
all_pages = len(ref[k]['data'])
edited_pages = int(float(stats[k]['edited']) * 0.01 * all_pages)
data[stats[k]['name']] = { 'edited': edited_pages, 'all': all_pages }
return data
def dump_and_compare_data(new):
s = json.dumps(new, indent = '\t')
with open('last.json.new', 'w') as fd:
fd.write(s)
try:
with open('last.json', 'r') as fd:
s = fd.read()
old = json.loads(s)
for k in new:
if k not in old:
new[k]['update'] = True
continue
new[k]['update'] = old[k] != new[k]
except OSError:
for k in new:
new[k]['update'] = False
os.rename('last.json.new', 'last.json')
def days_until_release(product, release_date):
days_until = (release_date - date.today()).days
if days_until > 0:
return '%d days until %s release' % (days_until, product)
2023-10-01 17:29:58 +02:00
elif days_until_ccc == 0:
return '%s is going to be released TODAY! 🎉' % (product)
2023-10-01 17:29:58 +02:00
elif days_until_ccc > -14:
return '%s was released few days ago, did you played it yet?' % (product)
def compose_text(data):
# text = 'Today\'s CCC Editing Progress: \n'
# text = 'CCC editing is finished! 🎉'
text = ''
text += days_until_release('Fate/Extra CCC translation', date(2023, 12, 25))
text += '\n\n'
# text += 'Now counting until Steam release of WITCH ON THE HOLY NIGHT as well too. You can wishlist the game here: https://store.steampowered.com/app/2052410/WITCH_ON_THE_HOLY_NIGHT/\n'
text += days_until_release('WITCH ON THE HOLY NIGHT(Steam)', date(2023, 12, 13))
return text
2023-10-01 17:29:58 +02:00
# for k in data:
# d = data[k]
# mark = ''
# if d['update']:
# if d['edited'] == d['all']:
# mark = '🎉' # my font can't render this emoji lul
# else:
# mark = '⚠️'
#
# percent = d['edited'] / float(d['all']) * 100.0
#
# text += '%s: %.2f%% (%d/%d) %s\n' % (k, percent, d['edited'], d['all'], mark)
2023-08-21 01:54:32 +02:00
def main():
# stats, ref = get_stats()
# stats = parse_stats(stats, ref)
# dump_and_compare_data(stats)
2023-08-21 01:54:32 +02:00
text = compose_text(None)
2023-08-21 01:54:32 +02:00
print(text)
# bots are forced to be unlisted on my instance anyway
post('unlisted', text)
if __name__ == "__main__":
try:
main()
except Exception as e:
t = traceback.format_exc()
print(t)
post('direct', EXCEPTION_MESSAGE_FORMAT % t)