Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/simonw/github-contents
Python class for reading and writing data to a GitHub repository
https://github.com/simonw/github-contents
github githubapi
Last synced: 4 months ago
JSON representation
Python class for reading and writing data to a GitHub repository
- Host: GitHub
- URL: https://github.com/simonw/github-contents
- Owner: simonw
- Created: 2019-06-09T19:10:59.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-02-24T18:34:49.000Z (almost 4 years ago)
- Last Synced: 2024-05-01T23:17:31.999Z (10 months ago)
- Topics: github, githubapi
- Language: Python
- Homepage:
- Size: 26.4 KB
- Stars: 20
- Watchers: 2
- Forks: 3
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# github-contents
[](https://pypi.org/project/github-contents/)
[](https://github.com/simonw/github-contents/releases)
[](https://github.com/simonw/github-contents/actions?query=workflow%3ATest)
[](https://github.com/simonw/github-contents/blob/main/LICENSE)Read and write both small and large files to Github.
The regular [GitHub Contents API](https://developer.github.com/v3/repos/contents/) can't handle files larger than 1MB - this class knows how to spot that problem and switch to the large-file-supporting low level [Git Data API](https://developer.github.com/v3/git/) instead.
Note that file contents is passed and returned as bytestrings, not regular strings.
## Installation
pip install github-contents
## Usage
You will need a GitHub OAuth token with full repository access.
The easiest way to create one of these is using [https://github.com/settings/tokens](https://github.com/settings/tokens)
```python
from github_contents import GithubContents# For repo simonw/disaster-data:
github = GithubContents(
"simonw",
"disaster-data",
token=GITHUB_OAUTH_TOKEN,
branch="main"
)
```
To read a file:
```python
content_in_bytes, sha = github.read(path_within_repo)
```
To write a file:
```python
content_sha, commit_sha = github.write(
filepath=path_within_repo,
content_bytes=contents_in_bytes,
sha=previous_sha, # Optional
commit_message=commit_message,
committer={
"name": COMMITTER_NAME,
"email": COMMITTER_EMAIL,
},
)
```