-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetcher.py
46 lines (37 loc) · 3.89 KB
/
fetcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import os
import sys
import yaml
import base64
import requests
import subprocess
print(sys.argv)
def grab_file(filelink, token):
headers = {"Authorization": f"Bearer {token}"}
r = requests.get(url=filelink, headers=headers)
#print(r.content.decode('ascii'))
fyaml = r.content.decode()
fyaml = yaml.safe_load(fyaml)
if fyaml["name"].endswith("png"):
imgstring = fyaml["content"]
imgstring = imgstring.replace('\n', '')
imgbytes = bytes(imgstring, "ascii")
with open(fyaml["name"], "wb") as writefile:
writefile.write(base64.decodebytes(imgbytes))
else:
with open(fyaml["name"], 'w') as writefile:
writefile.write(base64.b64decode(fyaml['content']).decode('utf8'))
name = sys.argv[1]
dirlink = sys.argv[2]
token = os.getenv("API_ACCESS_TOKEN")
headers = {"Authorization": f"Bearer {token}"}
r = requests.get(url=dirlink, headers=headers)
ryaml = r.content.decode('ascii')
#ryaml = '''[{"name":"DemoCombinatorics.ipynb","path":"combinatorics/DemoCombinatorics.ipynb","sha":"e79937261893b94eef9cee5d9e0795b85279af5b","size":27805,"url":"https://api.github.com/repos/oscar-system/notebooks/contents/combinatorics/DemoCombinatorics.ipynb?ref=master","html_url":"https://github.com/oscar-system/notebooks/blob/master/combinatorics/DemoCombinatorics.ipynb","git_url":"https://api.github.com/repos/oscar-system/notebooks/git/blobs/e79937261893b94eef9cee5d9e0795b85279af5b","download_url":"https://raw.githubusercontent.com/oscar-system/notebooks/master/combinatorics/DemoCombinatorics.ipynb?token=ABY3USEGBIFPBWWJHFN2IITGOLEF2","type":"file","_links":{"self":"https://api.github.com/repos/oscar-system/notebooks/contents/combinatorics/DemoCombinatorics.ipynb?ref=master","git":"https://api.github.com/repos/oscar-system/notebooks/git/blobs/e79937261893b94eef9cee5d9e0795b85279af5b","html":"https://github.com/oscar-system/notebooks/blob/master/combinatorics/DemoCombinatorics.ipynb"}},{"name":"Project.toml","path":"combinatorics/Project.toml","sha":"88e9a0d4093595cf7dc10557f0b04ac067731781","size":54,"url":"https://api.github.com/repos/oscar-system/notebooks/contents/combinatorics/Project.toml?ref=master","html_url":"https://github.com/oscar-system/notebooks/blob/master/combinatorics/Project.toml","git_url":"https://api.github.com/repos/oscar-system/notebooks/git/blobs/88e9a0d4093595cf7dc10557f0b04ac067731781","download_url":"https://raw.githubusercontent.com/oscar-system/notebooks/master/combinatorics/Project.toml?token=ABY3USAI256CTT43YONR5FLGOLEF2","type":"file","_links":{"self":"https://api.github.com/repos/oscar-system/notebooks/contents/combinatorics/Project.toml?ref=master","git":"https://api.github.com/repos/oscar-system/notebooks/git/blobs/88e9a0d4093595cf7dc10557f0b04ac067731781","html":"https://github.com/oscar-system/notebooks/blob/master/combinatorics/Project.toml"}},{"name":"README.md","path":"combinatorics/README.md","sha":"60bb57738309e60966978a9c8bad2979fe12838b","size":57,"url":"https://api.github.com/repos/oscar-system/notebooks/contents/combinatorics/README.md?ref=master","html_url":"https://github.com/oscar-system/notebooks/blob/master/combinatorics/README.md","git_url":"https://api.github.com/repos/oscar-system/notebooks/git/blobs/60bb57738309e60966978a9c8bad2979fe12838b","download_url":"https://raw.githubusercontent.com/oscar-system/notebooks/master/combinatorics/README.md?token=ABY3USD2UTXMJFOYCQXZSTTGOLEF2","type":"file","_links":{"self":"https://api.github.com/repos/oscar-system/notebooks/contents/combinatorics/README.md?ref=master","git":"https://api.github.com/repos/oscar-system/notebooks/git/blobs/60bb57738309e60966978a9c8bad2979fe12838b","html":"https://github.com/oscar-system/notebooks/blob/master/combinatorics/README.md"}}]'''
ryaml = yaml.safe_load(ryaml)
#print(ryaml[0]["url"])
nbfilename = ''
for i in ryaml:
if "ipynb" in i["name"]:
nbfilename = i["name"]
grab_file(i["url"], token)
print(nbfilename)