2
0
mirror of https://github.com/inventree/inventree-website.git synced 2025-04-27 21:16:45 +00:00
inventree-website/.github/get_stats.py
Matthias Mair 92c445f1ad
Fix stats section hitting API limit (#100)
Fixes #95

Use GH token to not hit the API limit
2023-01-19 22:23:05 +01:00

67 lines
2.1 KiB
Python

from enum import Enum
import json
import os
from pathlib import Path
from urllib.request import urlopen, Request
file_name = Path('_data/general/stats.yml')
github_project = "inventree/inventree"
crowdin_projet_id = 452300
class ReturnMode(Enum):
data = 0
header = 1
def get_data(url, key=None, default=0, mode: ReturnMode=ReturnMode.data, auth=None):
"""Fetches data from remote endpoint"""
httprequest = Request(url, headers={"Accept": "application/json"})
if auth:
httprequest.add_header("Authorization", f"Bearer {auth}")
with urlopen(httprequest) as response:
if mode == ReturnMode.data:
data = response.read().decode()
data = json.loads(data)
elif mode == ReturnMode.header:
data = dict(response.headers.items())
if key:
data = data.get(key, default)
return data
docker = get_data(f'https://hub.docker.com/v2/repositories/{github_project}', 'pull_count')
gh_data = get_data(f'https://api.github.com/repos/{github_project}', auth=os.environ.get('GITHUB_TOKEN'))
stars = gh_data.get('stargazers_count', 0)
forks = gh_data.get('forks_count', 0)
# See https://stackoverflow.com/a/60458265/17860466 # to enabble anon add `&anon=true`
link = get_data(f'https://api.github.com/repos/{github_project}/contributors?per_page=1', mode=ReturnMode.header, auth=os.environ.get('GITHUB_TOKEN')).get('Link')
contributors = link.split('page=')[-1].split('>')[0]
# Crowdin
crowdin_data = get_data(f'https://api.crowdin.com/api/v2/projects/{crowdin_projet_id}?limit=1000', 'data', auth=os.environ.get('CROWDIN_TOKEN'))
languages = len(crowdin_data.get('targetLanguageIds', []))
#translators = get_data(f'https://api.crowdin.com/api/v2/projects/{crowdin_projet_id}/members', 'data', auth=crowdin_token)
# Write data
file_name.write_text(
f"""stats:
- name: Docker pulls
number: {docker}
icon: fa-brands fa-docker
- name: GitHub Stars
number: {stars}
icon: fa-solid fa-star
- name: Forks
number: {forks}
icon: fa-solid fa-code-branch
- name: Contributors
number: {contributors}
icon: fa-solid fa-hands-helping
- name: Languages
number: {languages}
icon: fa-solid fa fa-language
"""
)