I recently needed to figure out how to write an updater script for a project I was working on. The application is released on an internal GitHub page with compressed files and an executable. I needed a way to check the latest release artifacts in GitHub and download them.

Let’s find out how all this works!

Getting Set Up

You will need to download and install a couple of packages to make this all work. Specifically, you will need the following:

You can install both of these using pip. Open up your terminal and run the following command:

python -m pip install PyGithub requests

Once this finishes, you should have everything you need to get the latest GitHub release assets.

Downloading the Latest Release Assets

The only other item you will need to make this work is a GitHub personal access token. You will need to create one of those. Depending on your use case, you may want to create what amounts to a bot account to make your token last a little longer.

The next step is to write some code. Open up your favorite Python IDE and create a new file. Then add the following code to it:

import requests

from github import Auth
from github import Github
from pathlib import Path

token =  "YOUR_PERSONAL_ACCESS_TOKEN"

headers = CaseInsensitiveDict()
headers["Authorization"] = f"token {token}"
headers["Accept"] = "application/octet-stream"
session = requests.Session()

auth = Auth.Token(token)  # Token can be None if the repo is public
g = Github(auth=auth)

# Use this one if you have an internal GitHub instance:
#g = Github(auth=auth, base_url="https://YOUR_COMPANY_URL/api/v3")

repo = g.get_repo("user/repo")  # Replace with the proper user and repo combo
for release in repo.get_releases():
    # Releases are returned with the latest first
    print(release)
    break

for asset in release.get_assets():
    print(asset.name)
    destination = Path(r"C:\Temp") / asset.name
    response = session.get(asset.url, stream=True, headers=headers)
    with open(destination, "wb") as f:
        for chunk in response.iter_content(1024*1024):
            f.write(chunk)
    print(f"Downloaded asset to {destination}")

The first half of this code is your imports and boilerplate for creating a GitHub authentication token and a requests Session object. If you work for a company and have an internal GitHub instance, see the commented-out code and use that instead for your GitHub authentication.

The next step is to get the GitHub repository and loop over its releases. By default, the iterable will return the items with the latest first and the oldest last. So you break out of the loop on the first release found to get the latest.

At this point, you loop over the assets in the release. In my case, I wanted to find an asset that was an executable and download it, but this code downloads all the assets.

Wrapping Up

This is a pretty short example, but it demonstrates one of the many things you can do with the handy PyGitHub package. You should check it out if you need to script other tasks in GitHub.

Happy coding!

Share.
Leave A Reply