Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download release assets #2918

Open
nicola-lunghi opened this issue Mar 12, 2024 · 4 comments
Open

Download release assets #2918

nicola-lunghi opened this issue Mar 12, 2024 · 4 comments

Comments

@nicola-lunghi
Copy link

The documentation says that I can download a release assets with:

To download the asset's binary content, set the Accept header of the request to [application/octet-stream](https://docs.github.com/rest/overview/media-types). The API will either redirect the client to the location, or stream it directly if possible. API clients should handle both a 200 or 302 response.

Can I do it with this library?

@xvega
Copy link
Contributor

xvega commented Mar 12, 2024

The documentation says that I can download a release assets with:

To download the asset's binary content, set the Accept header of the request to [application/octet-stream](https://docs.github.com/rest/overview/media-types). The API will either redirect the client to the location, or stream it directly if possible. API clients should handle both a 200 or 302 response.

Can I do it with this library?

I don't think this library provides a direct method to download assets, but you could use the asset's URL with a HTTP library like requests to download the asset.

def browser_download_url(self) -> str:

This is an idea/pseudocode that might help you get started (haven't tested this)

from github import Github
import requests

g = Github("your token")
repo = g.get_repo("your repo")

# Get the specific release or terate over releases
release = repo.get_release("tag_name")

# Loop through assets in the release
for asset in release.get_assets():
    response = requests.get(asset.browser_download_url, allow_redirects=True)
    # whatever you need to do with the binary 

@nicola-lunghi
Copy link
Author

nicola-lunghi commented Mar 12, 2024 via email

@nicola-lunghi
Copy link
Author

nicola-lunghi commented Mar 12, 2024 via email

@nicola-lunghi
Copy link
Author

nicola-lunghi commented Mar 28, 2024

Hi @EnricoMi if I submit a PR would you accept this? there's a good reason why this is not included? there's a similar API also for downloading job artifacts.
Also where I can add this functionality?

    request_url = f"{__gh_base_url__}/repos/{artifact_data['repo_name']}/releases/assets/{artifact_data['id']}"
    headers = {
        "Accept": "application/octet-stream",
        "Authorization": f"Bearer {token}",
    }

    with open(destfile, "wb") as f:
        r = requests.get(request_url, headers=headers, stream=True, timeout=5)
        total_length = r.headers.get("content-length")

        if total_length is None:  # no content length header
            f.write(r.content)
        else:
            for chunk in r.iter_content(chunk_size=4096):
                f.write(chunk)

the missing functionality is to be able to set this header

      "Accept": "application/octet-stream",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants