This guide will walk you through creating a project that automatically publishes articles using Gemini, GitHub Actions, Python, and Unsplash.
- Basic knowledge of Python
- GitHub account
- Unsplash API key
-
Create a new repository on GitHub:
- Go to GitHub and create a new repository.
- Clone the repository to your local machine.
-
Initialize a Python environment:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install necessary packages:
pip install requests pyyaml
-
Get an API key:
- Sign up on Unsplash and create a new application to get your API key.
-
Store the API key:
- Create a file named
config.yaml
and add your API key:unsplash_api_key: YOUR_UNSPLASH_API_KEY
- Create a file named
- Create a script to fetch images and generate articles:
- Create a file named
generate_article.py
and add the following code:import requests import yaml with open('config.yaml', 'r') as file: config = yaml.safe_load(file) def fetch_image(): url = "https://api.unsplash.com/photos/random" headers = {"Authorization": f"Client-ID {config['unsplash_api_key']}"} response = requests.get(url, headers=headers) return response.json() def generate_article(): image = fetch_image() article = f"![{image['description']}]({image['urls']['regular']})\n\n" article += f"Photo by [{image['user']['name']}]({image['user']['links']['html']}) on Unsplash" with open('article.md', 'w') as file: file.write(article) if __name__ == "__main__": generate_article()
- Create a file named
- Create a GitHub Actions workflow:
- Create a directory
.github/workflows
and a filemain.yml
inside it:name: Generate and Publish Article on: schedule: - cron: '0 0 * * *' # Runs daily at midnight push: branches: - main jobs: build: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2 with: python-version: '3.x' - name: Install dependencies run: | python -m venv venv source venv/bin/activate pip install requests pyyaml - name: Run script run: | source venv/bin/activate python generate_article.py - name: Commit and push changes run: | git config --global user.name 'github-actions[bot]' git config --global user.email 'github-actions[bot]@users.noreply.github.com' git add article.md git commit -m 'Automated article update' git push
- Create a directory
- Commit your changes:
git add . git commit -m "Initial commit" git push origin main
Your project is now set up to automatically generate and publish articles daily using GitHub Actions, Python, and Unsplash.