-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_utils.py
59 lines (48 loc) · 1.71 KB
/
github_utils.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
47
48
49
50
51
52
53
54
55
56
57
58
59
import requests
import numpy as np
from tempfile import TemporaryDirectory
import os
def get_raw_link_from_gist(gist_url: str) -> str:
gist_url = gist_url.rstrip("/")
parts = gist_url.split("/")
username = parts[-2]
gist_id = parts[-1]
api_url = f"https://api.github.com/gists/{gist_id}"
response = requests.get(api_url)
if response.status_code == 200:
try:
gist_data = response.json()
files = gist_data["files"]
first_file = next(iter(files.values()))
return first_file["raw_url"]
except (KeyError, ValueError):
pass
return None
def get_raw_link_from_file(file_link: str) -> str:
raw_link = file_link.replace("blob/", "").replace(
"github.com", "raw.githubusercontent.com"
)
return raw_link
def get_code_from_notebook(notebook_code: str) -> str:
"""Converts ipynb file into python file, return python code"""
with TemporaryDirectory() as tmp_dir:
n = "device"
with open(f"{tmp_dir}/{n}.ipynb", "w") as fnotebook:
fnotebook.write(notebook_code)
os.system(f"jupyter nbconvert --to script {tmp_dir}/{n}.ipynb")
with open(f"{tmp_dir}/{n}.py", "r") as fpy:
pycode = fpy.read()
return pycode
def get_raw_code_for_device(link: str) -> str:
if link is np.nan or not link.startswith("http") or "github.com" not in link:
return ""
if "gist" in link:
raw_link = get_raw_link_from_gist(link)
else:
raw_link = get_raw_link_from_file(link)
res = requests.get(raw_link)
if res.status_code != 200:
return ""
if link.endswith(".ipynb"):
return get_code_from_notebook(res.text)
return res.text