-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathckornega.py
82 lines (63 loc) · 2.39 KB
/
ckornega.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import json, re
import requests
from urlextract import URLExtract
import sys, gzip
utid: str = 'ckornega'
baseURL: dict = { 'model':'https://huggingface.co/', 'data': 'https://huggingface.co/datasets/', 'source': 'https://' }
post: str = '/raw/main/README.md'
postGHMaster: str = 'blob/master/README.md'
postGHMain: str = 'blob/main/README.md'
extU: URLExtract = URLExtract()
DOIpattern: str = r'\b(10\.\d{4,9}\/[-._;()/:A-Z0-9]+)\b/i'
#r1\b(10[.][0-9]{4,}(?:[.][0-9]+)*/(?:(?!["&\'<>])[[:graph:]])+)\b'
BIBpattern: str = r'@\w+{.*?[\}]{2}' # Discovery
def extractURLs(c: str) -> list:
return extU.find_urls(c)
def extractDOIs(c: str) -> list:
return re.findall(DOIpattern, c)
def extractBIBS(c: str) -> list:
return re.findall(BIBpattern, c, re.DOTALL)
fo = gzip.open(f"output/{utid}.json.gz", 'w')
def run(tp: str):
post0 = post if tp != 'source' else postGHMaster
with open(f"input/{utid}_{tp}", 'r', encoding='utf8', buffering=16384) as f:
for line in f:
line = line.strip()
add: str = ""
if tp == 'source':
npapers, line = line.split(';');
add = "/" if line[-1] != "/" else ""
url = baseURL[tp] + line + add
url = url + post0
try:
r = requests.get(url)
if r.status_code == 404:
if tp != 'source': continue # failure
url = baseURL[tp] + line + add
url = url + postGHMain
try:
r = requests.get(url)
if r.status_code == 404: continue # failure
except: continue
except: continue
content = r.text;
urls = extractURLs(content)
dois = extractDOIs(content)
bibs = extractBIBS(content.replace('\n', '').replace('\\n', ''))
# bibs = [bib[:250] for bib in bibs]
res = {
'ID': line,
'type': tp,
'url': url,
'content': content,
'links': urls,
'dois': dois,
'bibs': bibs
}
out = json.dumps(res, ensure_ascii=False)
fo.write((out+"\n").encode())
if __name__ == "__main__":
run('model')
run('data')
run('source')
fo.close()