Skip to content

Instantly share code, notes, and snippets.

@juanmirod
Created April 23, 2023 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanmirod/8b6044b0071bdcc5cb0bbcf933b7a576 to your computer and use it in GitHub Desktop.
Save juanmirod/8b6044b0071bdcc5cb0bbcf933b7a576 to your computer and use it in GitHub Desktop.
I ask ChatGPT to help me create a script to upload files to gist

You: how can I upload a markdown file as a gist using python?

JARVIS:

To upload a markdown file as a gist using Python, you can use the GitHub API through the requests module. Here's an example code:

import requests

url = 'https://api.github.com/gists'
headers = {'Authorization': 'token <your_access_token>'}

with open('path_to_your_markdown_file.md', 'r') as file:
    data = {
        "description": "<description_of_your_gist>",
        "public": True,
        "files": {
            "file.md": {
                "content": file.read()
            }
        }
    }

response = requests.post(url, headers=headers, json=data)
gist_url = response.json()['html_url']

print(f"Gist created at {gist_url}")

Replace <your_access_token> with a valid access token that has the necessary permissions to create gists. You can generate a personal access token in your GitHub account settings.

Replace <description_of_your_gist> with a brief summary of what your gist does.

Replace path_to_your_markdown_file.md with the actual path to your markdown file.

The code sends a POST request to the Gist API, passing a JSON payload containing the description, visibility, and content of the gist file. Once the gist is created, the script prints the URL of the newly created gist.

You: q

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment