Fetch LeetCode Daily Challenge #7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Fetch LeetCode Daily Challenge | |
| on: | |
| schedule: | |
| - cron: '0 0 * * *' # Runs daily at midnight UTC (adjust for IST, e.g., '30 3 * * *' for ~9 AM IST) | |
| workflow_dispatch: # Allows manual trigger | |
| jobs: | |
| fetch-daily: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repo | |
| uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.x' | |
| - name: Install dependencies | |
| run: pip install requests | |
| - name: Fetch and save daily challenge | |
| env: | |
| REPO_PAT: ${{ secrets.REPO_PAT }} | |
| run: | | |
| import requests | |
| import os | |
| import datetime | |
| from pathlib import Path | |
| # Fetch daily challenge (using a public LeetCode API endpoint) | |
| api_url = "https://leetcode-api-pied.vercel.app/daily" | |
| response = requests.get(api_url) | |
| data = response.json() | |
| # Extract problem details | |
| today = datetime.date.today().isoformat() # e.g., '2025-09-02' | |
| month_folder = today[:7] # e.g., '2025-09' | |
| title = data.get('title', 'Unknown') | |
| difficulty = data.get('difficulty', 'Unknown') | |
| link = data.get('link', 'https://leetcode.com/problems/' + data.get('titleSlug', '')) | |
| # Problem content | |
| content = f"# {title}\n\n**Difficulty:** {difficulty}\n**Link:** {link}\n\n## Description\n{data.get('content', 'Fetch failed.')}\n" | |
| # Create directory and file | |
| base_dir = Path('leetcode-daily-challenges') / month_folder | |
| base_dir.mkdir(parents=True, exist_ok=True) | |
| file_path = base_dir / f"{today}.md" | |
| with open(file_path, 'w') as f: | |
| f.write(content) | |
| # Commit changes | |
| os.system('git config --global user.name "GitHub Actions"') | |
| os.system('git config --global user.email "actions@github.com"') | |
| os.system('git add .') | |
| os.system(f'git commit -m "Add daily challenge for {today}" || echo "No changes"') | |
| os.system('git push') | |
| - name: Push changes | |
| uses: ad-m/github-push-action@v0.8.0 | |
| with: | |
| github_token: ${{ secrets.REPO_PAT }} | |
| branch: main |