40 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from fastapi import APIRouter, HTTPException
import requests
import xml.etree.ElementTree as ET
from datetime import datetime, timedelta
class RSSService:
@staticmethod
def get_rss_by_username(username: str):
url = f"https://git.numerum.team/{username}.rss"
response = requests.get(url)
if response.status_code != 200:
raise HTTPException(status_code=400, detail="Не удалось загрузить RSS")
root = ET.fromstring(response.text)
items = root.findall(".//item")
activity_map = {}
for item in items:
pub_date_elem = item.find("pubDate")
if pub_date_elem is not None:
pub_date_str = pub_date_elem.text.strip()
pub_date = datetime.strptime(pub_date_str, "%a, %d %b %Y %H:%M:%S %z")
date_str = pub_date.strftime("%Y-%m-%d")
activity_map[date_str] = activity_map.get(date_str, 0) + 1
today = datetime.now(pub_date.tzinfo)
activity_days = 365
result = []
for i in range(activity_days - 1, -1, -1):
day = today - timedelta(days=i)
date_str = day.strftime("%Y-%m-%d")
count = activity_map.get(date_str, 0)
result.append({"date": date_str, "count": count})
return result