---
# System prepended metadata

title: Query 51

---

To update the `README.md` files in multiple GitHub repositories by adding GIFs, you can use the GitHub API along with the requests library in Python. Below is a Python script that takes repository names and GIF URLs as input and updates the `README.md` files in those repositories [1]:
```python
import requests
import base64
import getpass

# GitHub username and Personal Access Token
github_username = input("GitHub Username: ")
github_pat = getpass.getpass("GitHub Personal Access Token: ")

# Function to update the README.md file in a repository
def update_readme(repo_owner, repo_name, gif_url):
    # Create the GIF markdown
    gif_markdown = f"![GIF]({gif_url})"
    
    # Fetch the existing README.md content
    readme_url = f'https://api.github.com/repos/{repo_owner}/{repo_name}/contents/README.md'
    response = requests.get(readme_url, auth=(github_username, github_pat))
    
    if response.status_code == 200:
        readme_content = response.json()['content']
        readme_content_decoded = base64.b64decode(readme_content).decode('utf-8')
        
        # Add the GIF markdown to the README content
        new_readme_content = readme_content_decoded + '\n' + gif_markdown
        
        # Update the README.md file
        update_url = f'https://api.github.com/repos/{repo_owner}/{repo_name}/contents/README.md'
        update_data = {
            "message": "Add GIF to README",
            "content": base64.b64encode(new_readme_content.encode('utf-8')).decode('utf-8'),
            "sha": response.json()['sha']
        }
        
        update_response = requests.put(update_url, auth=(github_username, github_pat), json=update_data)
        
        if update_response.status_code == 200:
            print(f"Updated README.md in {repo_owner}/{repo_name} with the GIF.")
        else:
            print(f"Failed to update README.md in {repo_owner}/{repo_name}.")
            print(f"Status Code: {update_response.status_code}")
            print(f"Response: {update_response.text}")
    else:
        print(f"Failed to fetch README.md in {repo_owner}/{repo_name}.")
        print(f"Status Code: {response.status_code}")
        print(f"Response: {response.text}")

# Example usage:
repo_owner = "your_username"
repo_name = "your_repository"
gif_url = "URL_of_your_gif.gif"
update_readme(repo_owner, repo_name, gif_url)
```
In this script:
* You will need to install the requests library if you haven't already (pip install requests).
* Replace `your_username`, `your_repository`, and `URL_of_your_gif.gif` with the actual GitHub username, repository name, and GIF URL you want to use.
* The script first fetches the existing content of the README.md file, then appends the GIF markdown, and finally updates the README.md file in the specified repository.
* You need to provide your GitHub username and a Personal Access Token with appropriate repository access when prompted. Make sure to keep your PAT secure and not hardcode it directly into the script.
* You can call the update_readme function with different repository names and GIF URLs to update multiple repositories.