Skip to main content

ghuc

PyPI Python 3.5, 3.6, 3.7 License: MIT CircleCI status

ghuc (derived from githubusercontent and pronounced gee·huck) is a command line tool for uploading images/documents to GitHub as issue attachments. Images are then available at https://user-images.githubusercontent.com, and documents are available as https://github.com/<user>/<repo>/files/.... It automates the traditional flow of navigating to a repo -> opening an issue -> uploading an image -> copying the URL, which is very cumbersome. With the constant deterioration of Imgur, one-stop upload to user-images.githubusercontent.com is the next best thing for the occasional image embeds in docs or comment section/forum posts.

ghuc is partially powered by Selenium WebDriver. Tested and supported on macOS, Linux, and Windows.

Please respect GitHub's ToS and do NOT abuse this tool.

Contents

Installation

pip install ghuc

Isolated installation through pipx is recommended.

Non-Python dependencies

Since ghuc uses Selenium WebDriver, one of the following browser and driver combos is required:

Optional dependencies

  • libmagic is needed for accurate MIME type detection; otherwise, MIME types are guessed based on file extensions.

Usage

$ ghuc -h
usage: ghuc [-h] [-r REPOSITORY_ID] [-x PROXY] [-q] [--debug] [--gui]
            [--container] [--version]
            PATH [PATH ...]

Uploads images/documents to GitHub as issue attachments. See
https://github.com/zmwangx/ghuc for detailed documentation.

positional arguments:
  PATH

optional arguments:
  -h, --help            show this help message and exit
  -r REPOSITORY_ID, --repository-id REPOSITORY_ID
                        id of repository to upload from (defaults to 1)
  -x PROXY, --proxy PROXY
                        HTTP or SOCKS proxy
  -q, --quiet           set logging level to ERROR
  --debug               set logging level to DEBUG
  --gui                 disable headless mode when running browser sessions
                        through Selenium WebDriver
  --container           add extra browser options to work around problems in
                        containers
  --version             show program's version number and exit

Notes:

  • Not all file types are supported by GitHub. At the moment of writing the following, I quote, are supported:

    GIF, JPEG, JPG, PNG, DOCX (seriously?), GZ, LOG, PDF, PPTX, TXT, XLSX, and ZIP.

  • The first time you use ghuc, you'll be prompted for your GitHub credentials to log in (see How it works; you may use environment variables documented below to bypass interactive prompts). Your cookies will be cached but your credentials are never stored. Subsequent runs may phone GitHub for a new token once in a while, but ideally you should not need to log in again.

  • --repository-id: determines what repo shows up the URL of uploaded documents. E.g., with the default 1, the URL may be https://github.com/mojombo/grit/files/3027504/random.pdf; when set to 36502, one may get https://github.com/git/git/files/3027505/random.pdf instead. This option is cosmetic as long as a repository exists by the id. This option has no effect on image uploads as far as I can tell.

  • --proxy: HTTP and SOCKS (4/4a/5/5h) proxies are supported, i.e., the following protocol prefixes are recognized: http://, https://, socks4://, socks4a://, socks5://, socks5h://. If no protocol is specified, http:// is assumed. The https_proxy environment variable is also honored.

  • --gui and --container: these are mostly development/testing options; end users don't need to touch them. --container in particular may not be secure for end user systems.

Environment variables

  • GITHUB_USERNAME, GITHUB_PASSWORD and GITHUB_TOTP_SECRET: interactive prompts for credentials are suppressed when these are provided. GITHUB_TOTP_SECRET is needed only if you use TOTP for two-factor authentication. If you only use text messages for 2FA (highly discouraged), the login flow might work but there's no guarantee (since I don't have a setup like this; contribution welcome). If you only use FIDO U2F for 2FA, you're out of luck.
  • https_proxy: see --proxy.

How it works

Uploading files as issue attachments requires two levels of authentication: GitHub session cookies and an uploadPolicyAuthenticityToken (CSRF token of sort). uploadPolicyAuthenticityToken can be found on any GitHub page with a comment box while logged in, and each one is valid for quite a while (without dedicated testing it's hard to say how long, but the validity period is at least more than a day).

ghuc logs into GitHub through Selenium WebDriver and caches session cookies as well as the token. It then performs all uploads with the cached values and doesn't touch the browser anymore (so normal uploads should be pretty fast) until the token is stale, at which point it attempts to restore the previous browser session and fetch a new token.

FAQ

  • Why not Puppeteer?

    Puppeteer is way more flexible than Selenium WebDriver from a developer's point of view, but having to download a bundled, ~100MB, non-auto-updating copy of Chromium for every install that's already outdated the next time you use it is really user-hostile. I'm also not a fan of the Node/npm ecosystem. Finally, synchronous is good for this project.

TODO

  • Respect proxy option in selenium invocations (currently browsers use system proxy settings);
  • Page loads: wait for DOMContentLoaded instead of load (selenium default, wastes a lot time on slow connections);
  • Handle authentication failures and allow retries (currently we assume all credentials are correct).

License

Copyright © 2019 Zhiming Wang i@zhimingwang.org. The MIT License.

Metadata

Release files for ghuc 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ghuc 0.1
File Size Uploaded
ghuc-0.1.tar.gz 9.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ghuc 0.1
File Interpreter ABI Platform
ghuc-0.1-py3-none-any.whl Python 3 none any Details

Total release size: 19.1 kB

Release files / ghuc-0.1.tar.gz

Download URL ghuc-0.1.tar.gz
Size 9.1 kB
Tags Source
SHA-256 checksum
How to use checksums
8101a7d90a2363ad0d04fed7347100b8367d5a05162332a3f84a8974d0ae53ae
BLAKE2b-256 checksum
How to use checksums
92fc45e6df93f290eeff5050d163655df1bdabadfab1563061f57b0dd6057ff6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.1

Release files / ghuc-0.1-py3-none-any.whl

Download URL ghuc-0.1-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cfcf73eec066d6f366cb7ed76973e4a9a167df0613920491c1ce096c19d2e19c
BLAKE2b-256 checksum
How to use checksums
0b91fc56ee95be640effef0f83f5ac4a038ec46fd9733ba97d69d28f1731c1d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.1

Release history Release notifications | RSS feed

This release

0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page