I lately wanted to determine methods to write an updater script for a undertaking I used to be engaged on. The appliance is launched on an inside GitHub web page with compressed recordsdata and an executable. I wanted a option to examine the most recent launch artifacts in GitHub and obtain them.
Let’s learn how all this works!
Getting Set Up
You will have to obtain and set up a few packages to make this all work. Particularly, you’ll need the next:
You possibly can set up each of those utilizing pip. Open up your terminal and run the next command:
python -m pip set up PyGithub requests
As soon as this finishes, it’s best to have every little thing you could get the most recent GitHub launch property.
Downloading the Newest Launch Belongings
The one different merchandise you’ll need to make this work is a GitHub private entry token. You will have to create a kind of. Relying in your use case, you could wish to create what quantities to a bot account to make your token final just a little longer.
The following step is to put in writing some code. Open up your favourite Python IDE and create a brand new file. Then add the next 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"] = "utility/octet-stream" session = requests.Session() auth = Auth.Token(token) # Token will be None if the repo is public g = Github(auth=auth) # Use this one in case you have an inside GitHub occasion: #g = Github(auth=auth, base_url="https://YOUR_COMPANY_URL/api/v3") repo = g.get_repo("consumer/repo") # Substitute with the correct consumer and repo combo for launch in repo.get_releases(): # Releases are returned with the most recent first print(launch) break for asset in launch.get_assets(): print(asset.identify) vacation spot = Path(r"C:Temp") / asset.identify response = session.get(asset.url, stream=True, headers=headers) with open(vacation spot, "wb") as f: for chunk in response.iter_content(1024*1024): f.write(chunk) print(f"Downloaded asset to {vacation spot}")
The primary half of this code is your imports and boilerplate for making a GitHub authentication token and a requests Session object. In case you work for an organization and have an inside GitHub occasion, see the commented-out code and use that as an alternative on your GitHub authentication.
The following step is to get the GitHub repository and loop over its releases. By default, the iterable will return the gadgets with the most recent first and the oldest final. So that you get away of the loop on the primary launch discovered to get the most recent.
At this level, you loop over the property within the launch. In my case, I wished to search out an asset that was an executable and obtain it, however this code downloads all of the property.
Wrapping Up
It is a fairly quick instance, but it surely demonstrates one of many many issues you are able to do with the useful PyGitHub bundle. It’s best to test it out if you could script different duties in GitHub.
Pleased coding!