|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Auto-update awesome-cli-apps GitHub stars badge in README files.""" |
| 3 | + |
| 4 | +import re |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import requests |
| 9 | + |
| 10 | +# Target repo to track stars for (awesome-cli-apps) |
| 11 | +REPO_OWNER = "agarrharr" |
| 12 | +REPO_NAME = "awesome-cli-apps" |
| 13 | +GITHUB_API_URL = f"https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}" |
| 14 | + |
| 15 | +# Badge pattern to update the "Featured-Awesome CLI Apps XXk" badge |
| 16 | +BADGE_PATTERN = r'(https://img\.shields\.io/badge/Featured-Awesome%20CLI%20Apps%20)([\d.]+k)(%E2%AD%90)' |
| 17 | + |
| 18 | + |
| 19 | +def format_stars(count: int) -> str: |
| 20 | + """Format star count to human readable string.""" |
| 21 | + if count >= 1000: |
| 22 | + return f"{count / 1000:.1f}k".replace(".0k", "k") |
| 23 | + return str(count) |
| 24 | + |
| 25 | + |
| 26 | +def get_github_stars() -> int: |
| 27 | + """Fetch star count from GitHub API.""" |
| 28 | + try: |
| 29 | + response = requests.get(GITHUB_API_URL, timeout=30) |
| 30 | + response.raise_for_status() |
| 31 | + data = response.json() |
| 32 | + return data.get("stargazers_count", 0) |
| 33 | + except Exception as e: |
| 34 | + print(f"Error fetching stars: {e}") |
| 35 | + sys.exit(1) |
| 36 | + |
| 37 | + |
| 38 | +def update_readme_stars_badge(filepath: Path, stars: int) -> bool: |
| 39 | + """Update awesome-cli-apps stars badge in README.""" |
| 40 | + content = filepath.read_text(encoding="utf-8") |
| 41 | + original_content = content |
| 42 | + |
| 43 | + formatted = format_stars(stars) |
| 44 | + |
| 45 | + # Pattern for the awesome-cli-apps featured badge |
| 46 | + # Matches: https://img.shields.io/badge/Featured-Awesome%20CLI%20Apps%20XXk%E2%AD%90-... |
| 47 | + badge_pattern = r'(https://img\.shields\.io/badge/Featured-Awesome%20CLI%20Apps%20)([\d.]+k)(%E2%AD%90)' |
| 48 | + |
| 49 | + def replace_stars(match): |
| 50 | + return match.group(1) + formatted + match.group(3) |
| 51 | + |
| 52 | + content = re.sub(badge_pattern, replace_stars, content) |
| 53 | + |
| 54 | + if content != original_content: |
| 55 | + filepath.write_text(content, encoding="utf-8") |
| 56 | + print(f"Updated awesome-cli-apps stars in {filepath}: {formatted}") |
| 57 | + return True |
| 58 | + else: |
| 59 | + print(f"No changes needed in {filepath}") |
| 60 | + return False |
| 61 | + |
| 62 | + |
| 63 | +def main(): |
| 64 | + """Main entry point.""" |
| 65 | + stars = get_github_stars() |
| 66 | + print(f"Current GitHub stars: {stars} ({format_stars(stars)})") |
| 67 | + |
| 68 | + # Update main README |
| 69 | + readme_path = Path("README.md") |
| 70 | + if readme_path.exists(): |
| 71 | + update_readme_stars_badge(readme_path, stars) |
| 72 | + else: |
| 73 | + print("README.md not found") |
| 74 | + |
| 75 | + # Update docs README if exists |
| 76 | + docs_readme = Path("docs/README.md") |
| 77 | + if docs_readme.exists(): |
| 78 | + update_readme_stars_badge(docs_readme, stars) |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + main() |
0 commit comments