You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.4 KiB
Python

#!/usr/bin/env python3
import configparser
import datetime
import distutils.util
import json
import os
import pathlib
import sys
import time
import dateutil.parser
import mysql.connector
import pytz
import requests
from tables import TABLE_STATEMENT
from util import int_or_none
## Configuration
config = configparser.ConfigParser()
config.read_file(open('config.ini'))
MYSQL_HOST: str = os.environ.get('MYSQL_HOST') or config['mysql'].get('host')
MYSQL_USER: str = os.environ.get('MYSQL_USER') or config['mysql'].get('user')
MYSQL_PASS: str = os.environ.get('MYSQL_PASS') or config['mysql'].get('pass')
MYSQL_DB: str = os.environ.get('MYSQL_DB') or config['mysql'].get('db')
FILE_OUTPUT: bool = distutils.util.strtobool(os.environ.get('FETCH_ENABLE_FILE_OUTPUT') or config['fetch'].getboolean('enable_file_output') or "no")
OUTPUT_PATH: str = os.environ.get('FETCH_OUTPUT_PATH') or config['fetch'].get('output_path')
FAH_TEAM: int = int_or_none(os.environ.get('FETCH_TEAM_ID')) or config['fetch'].getint('team_id')
""" Interval in minutes. If unset, script runs once only. """
FETCH_INTERVAL: int = int_or_none(os.environ.get('FETCH_INTERVAL')) or config['fetch'].getint('interval', fallback=None)
def init_db(cursor: mysql.connector.cursor.MySQLCursor) -> None:
# cursor.execute(TABLE_STATEMENT)
cursor.execute('SET SESSION time_zone="Etc/UTC";')
## Business
def business():
http_data = requests.get('https://stats.foldingathome.org/api/team/{}'.format(FAH_TEAM))
data = http_data.json()
time = datetime.datetime.utcnow()
if FILE_OUTPUT:
fn_dir = os.path.join(OUTPUT_PATH, str(FAH_TEAM))
if not os.path.exists(fn_dir):
os.makedirs(fn_dir)
fn = os.path.join(fn_dir, time.isoformat())
with open(fn, 'w') as f:
f.write(http_data.text)
conn: mysql.connector.MySQLConnection = mysql.connector.connect(
host=MYSQL_HOST,
user=MYSQL_USER,
password=MYSQL_PASS,
db=MYSQL_DB
)
try:
cursor = conn.cursor(prepared=True)
init_db(cursor)
team_sql = 'INSERT IGNORE INTO `team` (`team_id`, `name`) VALUES (?, ?)'
team_data = (data['team'], data['name'])
cursor.execute(team_sql, team_data)
cursor.execute(
'''INSERT INTO team_stats (wus, team_rank, total_teams,
active_50days, last_update, team_id, credit, fetched_at)
VALUES (?,?,?,?,?,?,?,?)''',
(data['wus'], data['rank'], data['total_teams'],
data['active_50'], data['last'], data['team'], data['credit'], time)
)
donor_data = []
donor_stats = []
for donor in data['donors']:
donor_data.append((donor['id'], donor['name']))
donor_stats.append((
donor['id'], donor['wus'], donor.get('rank'), donor['credit'],
donor['team'], time
))
cursor.executemany(
'INSERT IGNORE INTO donor (user_id, name) VALUES (?,?)',
donor_data
)
cursor.executemany(
'INSERT INTO donor_stats (donor_id, wus, donor_rank, credit,'
'team_id, fetched_at) VALUES (?,?,?,?,?,?)',
donor_stats
)
conn.commit()
cursor.close()
conn.close()
except:
raise
def main():
if FETCH_INTERVAL is None:
business()
else:
while True:
business()
time.sleep(FETCH_INTERVAL * 60)
main()