#!/usr/bin/env python3 """ GitHub Trending - Weekly hot projects Fetches recently created popular repositories on GitHub """ import os import sys import requests import json # Add parent dir to path for shared modules sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # Load environment from bashrc def load_env(): bashrc = os.path.expanduser("~/.bashrc") if os.path.exists(bashrc): with open(bashrc) as f: for line in f: line = line.strip() if line.startswith("export ") and "=" in line: key = line.replace("export ", "", 1).split("=")[0].strip() value = line.split("=", 1)[1].strip().strip('"').strip("'") os.environ[key] = value load_env() def get_feishu_token(): """Get Feishu token""" app_id = os.environ.get("CRONJOB_TEAM_WORKFLOW_APP_ID") app_secret = os.environ.get("CRONJOB_TEAM_WORKFLOW_APP_SECRET") if not app_id or not app_secret: print("Error: Missing CRONJOB_TEAM_WORKFLOW_APP_ID or CRONJOB_TEAM_WORKFLOW_APP_SECRET") return None try: resp = requests.post( "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal", headers={"Content-Type": "application/json"}, json={"app_id": app_id, "app_secret": app_secret}, timeout=10 ) data = resp.json() if data.get("code") == 0: return data.get("tenant_access_token") except Exception as e: print(f"Error getting token: {e}") return None def get_github_trending(): """Get trending repositories from GitHub API""" from datetime import datetime, timedelta # Get repos created in last 7 days with most stars week_ago = (datetime.now() - timedelta(days=7)).strftime("%Y-%m-%d") resp = requests.get( "https://api.github.com/search/repositories", params={ "q": f"created:>{week_ago}", "sort": "stars", "order": "desc", "per_page": 10 }, headers={"User-Agent": "Mozilla/5.0"}, timeout=15 ) projects = [] if resp.status_code == 200: data = resp.json() for item in data.get("items", [])[:10]: projects.append({ "name": item["full_name"], "stars": item["stargazers_count"], "desc": (item.get("description") or "")[:80], "url": item["html_url"], "lang": item.get("language", "") }) return projects def format_message(projects): """Format message - short version""" lines = ["🔥 GitHub Trending\n"] for p in projects: lines.append(f"• {p['name']}") if p['desc']: lines.append(f" {p['desc']}") lines.append(f" {p['url']}\n") return "\n".join(lines) def send_feishu_message(token, receive_id, message): """Send message to Feishu""" receive_id_type = "open_id" if receive_id.startswith("ou_") else "chat_id" payload = { "receive_id": receive_id, "msg_type": "text", "content": json.dumps({"text": message}) } resp = requests.post( f"https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type={receive_id_type}", headers={ "Authorization": f"Bearer {token}", "Content-Type": "application/json" }, json=payload, timeout=30 ) return resp.json() def main(): print("=== GitHub Trending ===") # Get Feishu token token = get_feishu_token() if not token: print("Error: Cannot get Feishu token") return # Get trending print("Fetching GitHub trending...") projects = get_github_trending() print(f"Found {len(projects)} projects") # Format message message = format_message(projects) # Send to user (private chat) receive_id = "oc_2c586abb5204eca2ddf6ddf5de338bf6" print(f"Sending to {receive_id}...") result = send_feishu_message(token, receive_id, message) print(f"Result: {result}") print("Done!") if __name__ == "__main__": main()