72 lines
No EOL
2.1 KiB
Python
72 lines
No EOL
2.1 KiB
Python
import logging
|
|
import threading
|
|
import time
|
|
import requests
|
|
from datetime import datetime
|
|
|
|
from fastapi import FastAPI
|
|
from starlette.middleware.cors import CORSMiddleware
|
|
|
|
from app.routes import router
|
|
from utils import declension
|
|
from app.models import Project
|
|
|
|
app = FastAPI()
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=['*'],
|
|
allow_credentials=True,
|
|
allow_methods=['*'],
|
|
allow_headers=['*'],
|
|
)
|
|
app.include_router(router)
|
|
|
|
repos = []
|
|
code_stats = {}
|
|
|
|
|
|
@app.on_event("startup")
|
|
def start():
|
|
threading.Thread(target=load_projects).start()
|
|
threading.Thread(target=load_code_stats).start()
|
|
|
|
|
|
def load_projects():
|
|
while True:
|
|
try:
|
|
resp = requests.get("https://git.mootfrost.dev/api/v1/repos/search",
|
|
params={'limit': 64,
|
|
'sort': 'updated',
|
|
'order': 'desc'}).json()
|
|
global repos
|
|
repos.clear()
|
|
for repo in resp['data']:
|
|
repos.append(Project(repo["name"], repo["description"], repo["html_url"]))
|
|
logging.info(f'{datetime.now()}: Projects updated')
|
|
except Exception as e:
|
|
logging.error(f'{datetime.now()}: Error while updating projects: {e}')
|
|
time.sleep(7200)
|
|
|
|
|
|
def load_code_stats():
|
|
while True:
|
|
resp = requests.get('https://waka.mootfrost.dev/api/compat/wakatime/v1/users/Mootfrost/stats/last_7_days').json()
|
|
global code_stats
|
|
code_stats.clear()
|
|
|
|
for el in resp['data']['languages']:
|
|
hours = el['hours']
|
|
minutes = el['minutes']
|
|
|
|
code_stats[el['name']] = ''
|
|
if hours > 0:
|
|
code_stats[el['name']] = declension(hours, 'час', 'часа', 'часов') + ' '
|
|
if minutes > 0:
|
|
code_stats[el['name']] += declension(minutes, 'минуту', 'минуты', 'минут') + ' '
|
|
if code_stats[el['name']] == '':
|
|
code_stats.pop(el['name'])
|
|
time.sleep(7200)
|
|
|
|
|
|
__all__ = ['code_stats', 'repos', 'app'] |