Reputation: 289
i have a gist(csv format) which updates on a daily basis and contains n revisions. Each revision data is different from one another.
I need to know the difference between each revision so i used gist api to retrieve the revisions which can be saved in csv
My requirement:
Struck here how to download the file. I tried with urlib, request pypackages but i couldn't figure where i am behind.Thanks
gist_ids = 'abc'
def main():
gh = github3.login(
token=os.environ.get('my_token'),
url=' ')
my_gist = gh.gist(gist_ids)
for gist_commit in my_gist.commits():
resp_str= gist_commit.get_gist().as_json()
resp_json = json.loads(resp_str)
resp_url = resp_json['files']['example.csv']['raw_url']
print resp_url
if __name__ == '__main__':
main()
O/p: I have 4 revisions so it displayed 4 urls https://github.com/gist/eec70654d178a8e1fe497d0bce94e0db/raw/5c058121cc4f289773b7013208ca5c5b0d97ba33/example.csv
https://github.com/gist/eec70654d178a8e1fe497d0bce94e0db/raw/cfb04e18bdf18bf7ab0b708951f62d2095c49f7d/example.csv
https://github.com/gist/eec70654d178a8e1fe497d0bce94e0db/raw/a20174f568129df4348d355eb0d6e378db7fa646/example.csv
https://github.com/gist/eec70654d178a8e1fe497d0bce94e0db/raw/137c57ef411067564341d389571dab2da070c828/example.csv
Upvotes: 0
Views: 221
Reputation: 28757
So you're over-complicating some things. Each of the objects returned by github3.py have the information you want.
I've taken your code below and modified it slightly. To summarize
I removed the usage of as_json()
since there's no point in coercing the data to and from a string. If you wanted a dictionary, you could have used as_dict()
.
Next, I used the gist's commit history and used that to find the file for each revision.
Using the GistHistory object and the actual GistFile object, I construct your filename to save them so it they will look like 5c058121cc4f289773b7013208ca5c5b0d97ba33-example.csv
Finally, I use the GistFile object to actually retrieve the file content and save it to disk
gist_ids = 'abc'
def main():
gh = github3.login(
token=os.environ.get('my_token'),
url=' ')
my_gist = gh.gist(gist_ids)
for gist_commit in my_gist.commits():
gist = gist_commit.get_gist()
gist_file = [file for file in gist.files()
if file.filename == 'example.csv']
filename = '{}-{}'.format(gist_commit.version, gist_file.filename)
with open(filename, 'wb') as fd:
fd.write(gist_file.content())
print filename
Upvotes: 0