There are some good solutions already, but if you use requests just follow Github's API.
The endpoint for all content is
GET /repos/:owner/:repo/contents/:path
But keep in mind that the default behavior of Github's API is to encode the content using base64.
In your case you would do the following:
#!/usr/bin/env python3
import base64
import requests
url = 'https://api.github.com/repos/{user}/{repo_name}/contents/{path_to_file}'
req = requests.get(url)
if req.status_code == requests.codes.ok:
req = req.json() # the response is a JSON
# req is now a dict with keys: name, encoding, url, size ...
# and content. But it is encoded with base64.
content = base64.b64decode(req['content'])
else:
print('Content was not found.')
Answer from dasdachs on Stack OverflowThere are some good solutions already, but if you use requests just follow Github's API.
The endpoint for all content is
GET /repos/:owner/:repo/contents/:path
But keep in mind that the default behavior of Github's API is to encode the content using base64.
In your case you would do the following:
#!/usr/bin/env python3
import base64
import requests
url = 'https://api.github.com/repos/{user}/{repo_name}/contents/{path_to_file}'
req = requests.get(url)
if req.status_code == requests.codes.ok:
req = req.json() # the response is a JSON
# req is now a dict with keys: name, encoding, url, size ...
# and content. But it is encoded with base64.
content = base64.b64decode(req['content'])
else:
print('Content was not found.')
You can access a text version by changing the beginning of your link to
https://raw.githubusercontent.com/
python - Read content of file which is in GitHub using PyGitHub - Stack Overflow
A Python script which can fetch the contents of a GitHub repo using it's URL
I created a Python script to view a Github repository's file size!
Python Module that Extracts the Browsers History
I'm a little confused, Should I delete this module from Pypi Index?
More on reddit.comVideos
» pip install github-contents
You can use this code to see the content of the file:
from github import Github
github = Github('user', 'password')
user = github.get_user()
repository = user.get_repo('Demo')
file_content = repository.get_contents('sample.txt')
print(file_content.decoded_content.decode())
If you need to see more attributes like decoded_content, just type this:
print(help(file_content))
You can use the get_contents API
Here is an example:
from github import Github
github = Github('user', 'password')
user = github.get_user()
repository = user.get_repo('Demo')
file_content = repository.get_contents('sample.txt')
GitHub's API provides basic data about a repository and its contents but does not offer detailed contents of each file unless you specify it in the path while sending the request.
I was working on a project that required extracting code from public repositories using their URLs, and we couldn't find a reliable method to retrieve the repository contents in JSON format.
So i decided to make a script for it. When you run the script it will ask for a url and then it will return the file structure of that repo and the contents in each file. Planning to make it into a package for py and js.