Repository navigation
Expand file tree
/
Copy pathgithub_issue_scraper.py
More file actions
151 lines (120 loc) · 3.95 KB
/
Copy pathgithub_issue_scraper.py
File metadata and controls
151 lines (120 loc) · 3.95 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# pip install requests bs4
import requests
import re
from urllib.parse import urlparse
from bs4 import BeautifulSoup
from urllib.parse import urlparse
import pandas as pd
URL_PATTERN = re.compile(r"https?://[^\s<>\[\]()\"']+")
REPO = "OpenOptimizationOrg/OPL"
LABEL = "new benchmark / problem"
EMAIL_RE = re.compile(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b")
def get_issues(repo, label, token=None):
"""
Get all issues from a GitHub repository with a specific label.
repo: GitHub repository in the format "owner/repo"
label: label to filter issues by
token: optional GitHub personal access token
"""
url = f"https://api.github.com/repos/{repo}/issues"
headers = {"Accept": "application/vnd.github+json"}
if token:
headers["Authorization"] = f"Bearer {token}"
issues = []
page = 1
while True:
params = {
"labels": label,
"state": "all",
"per_page": 100,
"page": page,
}
response = requests.get(url, headers=headers, params=params)
response.raise_for_status()
batch = response.json()
if not batch:
break
issues.extend(batch)
page += 1
return issues
def extract_links(text: str) -> list[str]:
if not text:
return []
links = URL_PATTERN.findall(
text
) # Remove punctuation commonly attached to URLs in prose
return [link.rstrip(".,;:!?") for link in links]
def extract_emails(text: str) -> list[str]:
if not text:
return []
mails = EMAIL_RE.findall(text)
return [mail.rstrip(".,;:!?") for mail in mails]
def arxiv_info(arxiv_link: str) -> dict:
# Identify arxiv id from link (could be pdf format or /abs format)
arxiv_id = None
if "/abs/" in arxiv_link:
arxiv_id = arxiv_link.split("/abs/")[-1]
elif "/pdf/" in arxiv_link:
arxiv_id = arxiv_link.split("/pdf/")[-1].replace(".pdf", "")
html_url = f"https://arxiv.org/html/{arxiv_id}"
response = requests.get(html_url, timeout=15)
if response.status_code != 200:
return {}
response.raise_for_status()
# print(response.text)
soup = BeautifulSoup(response.text, "html.parser")
text = soup.get_text(" ", strip=True)
emails = extract_emails(text)
title = soup.title.string if soup.title else "No Title Found"
info = {
"emails": emails,
"title": title,
}
return info
def find_paper(citation: str) -> dict:
if not citation:
return {}
url = "https://api.openalex.org/works"
params = {
"search": citation,
"per-page": 10,
}
r = requests.get(url, params=params)
r.raise_for_status()
results = r.json()["results"]
if not results:
return {}
# Verify that the result matches enough
# TODO: check for doi or arxiv id to check matches
res = results[0]
if res["relevance_score"] < 0.5:
return {}
else:
return {
"authorships": res.get("authorships", []),
"doi": res.get("doi", ""),
"title": res.get("title", ""),
}
if __name__ == "__main__":
issues = get_issues(
repo=REPO,
label=LABEL,
)
data = []
for issue in issues[0:3]: # Limiting to first 5 issues for testing
issue_data = {"issue_number": issue["number"], "title": issue["title"]}
links = extract_links(issue.get("body", ""))
issue_data["links"] = links
for l in links:
if "arxiv.org" in l:
arxiv_info_dict = arxiv_info(l)
issue_data["emails"] = arxiv_info_dict.get("emails", [])
issue_data["title"] = arxiv_info_dict.get("title", "")
paper_data = ""
if issue_data.get("title"):
paper_data = issue_data.get("title", "")
else:
issue.get("body", "")
paper_data = find_paper(paper_data)
issue_data.update(paper_data)
print(issue_data)