Skip to content Skip to sidebar Skip to footer

Bash Or Python: How To Download A Single Specified File From Github?

I need to write a script which periodically downloads a specific file from my Github account. I've seen this Github page which seems to be what I am looking for, but I have not bee

Solution 1:

It depends on the filetype. For non-binaries, you can get the url of the file by clicking on the "raw" button next to "edit" when you have the file open in github. Then, you can just just use curl or wget to download it.

Here is a picture to make things clear:

enter image description here

Then copy the url:

enter image description here

Solution 2:

Assuming the file is synced, I'd write a bash script that went something like this:

#!/bin/bash# file: ./getFromGit# cd into git-synced directory and update the filecd /directory/path/
git checkout origin/branch /path/to/file/in/dirStructure

Then

$ chmod u+x ./getFromGit$ cp getFromGit /usr/local/bin # or wherever your executables like git are

Now from any directory you like, you can call getFromGit and it will get the file you want.

See this tutorial from Jason Rudolph for more info on checking out a single file from a given branch with git.

If it's not synced, I agree with @jh314: just use wget

Solution 3:

This should work (untested, but should give you an idea):

import time
import subprocess
import sys
import shlex

if __name__ == "__main__":
    cmd = ("curl -L"if sys.platform == "darwin"else"wget")
    url = ......
    cmd += " {0} -o {1}".format(url, url.split("/")[-1])
    p = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

    (stdout, stderr) = p.communicate()

    ifstderr:
        raise Exception(stderr)

    time.sleep(...)

    # Call another instance of this script
    subprocess.Popen(['python', sys.argv[0]])

It will call the script again after the time has elapsed, and exit. As long as the command finishes without an error, it will keep calling itself again.

Post a Comment for "Bash Or Python: How To Download A Single Specified File From Github?"