60 lines
2.1 KiB
Python
60 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Fetch live GitHub repository stats via API and write to file.
|
|
|
|
Evidence: This script makes a real HTTP request to GitHub's public API.
|
|
"""
|
|
import json
|
|
import urllib.request
|
|
import urllib.error
|
|
from datetime import datetime
|
|
|
|
def fetch_repo_stats(owner: str, repo: str) -> dict:
|
|
"""Fetch repository stats from GitHub API."""
|
|
url = f"https://api.github.com/repos/{owner}/{repo}"
|
|
|
|
try:
|
|
req = urllib.request.Request(url, headers={'User-Agent': 'spark-snippet'})
|
|
with urllib.request.urlopen(req, timeout=10) as response:
|
|
data = json.loads(response.read().decode('utf-8'))
|
|
return {
|
|
'name': data.get('name', repo),
|
|
'full_name': data.get('full_name', f'{owner}/{repo}'),
|
|
'description': data.get('description', ''),
|
|
'stars': data.get('stargazers_count', 0),
|
|
'forks': data.get('forks_count', 0),
|
|
'open_issues': data.get('open_issues_count', 0),
|
|
'html_url': data.get('html_url', ''),
|
|
'updated_at': data.get('updated_at', ''),
|
|
}
|
|
except urllib.error.HTTPError as e:
|
|
return {'error': f"HTTP {e.code}: {e.reason}"}
|
|
except urllib.error.URLError as e:
|
|
return {'error': f"URL Error: {e.reason}"}
|
|
|
|
def main():
|
|
# Test with a real, stable repository
|
|
stats = fetch_repo_stats('python', 'cpython')
|
|
|
|
output_path = '/root/.hermes/wizard-outputs/spark/github_stats.json'
|
|
|
|
# Add metadata
|
|
result = {
|
|
'fetched_at': datetime.utcnow().isoformat() + 'Z',
|
|
'artifact': '/root/.hermes/wizard-outputs/spark/github_stats.json',
|
|
'data': stats
|
|
}
|
|
|
|
with open(output_path, 'w') as f:
|
|
json.dump(result, f, indent=2)
|
|
|
|
print(f"Written to: {output_path}")
|
|
print(f"Status: {'ERROR: ' + stats.get('error', '') if 'error' in stats else 'SUCCESS'}")
|
|
if 'error' not in stats:
|
|
print(f"Stars: {stats['stars']}")
|
|
print(f"Forks: {stats['forks']}")
|
|
print(f"Open Issues: {stats['open_issues']}")
|
|
|
|
return result
|
|
|
|
if __name__ == '__main__':
|
|
main() |