forked from wilsonfreitas/awesome-quant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.py
More file actions
295 lines (250 loc) · 9.36 KB
/
parse.py
File metadata and controls
295 lines (250 loc) · 9.36 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import json
import os
import re
from html.parser import HTMLParser
from threading import Thread
from urllib.request import urlopen
import pandas as pd
from github import Auth, Github
# using an access token
auth = Auth.Token(os.environ["GITHUB_ACCESS_TOKEN"])
g = Github(auth=auth)
def extract_repo(url):
reu = re.compile(r"^https://github.com/([\w-]+/[-\w\.]+)$")
m = reu.match(url)
if m:
return m.group(1)
else:
return ""
def extract_github_url(description):
"""Extract GitHub URL from description if present."""
github_pattern = re.compile(r"\[GitHub\]\((https://github\.com/[\w-]+/[-\w\.]+)\)")
m = github_pattern.search(description)
if m:
return m.group(1)
return ""
def get_cran_info(url):
"""Fetch Published date and GitHub URL from a CRAN package page.
Returns (published_date, github_url) — either may be empty string.
"""
try:
m = re.search(r"package=(\w+)", url) or re.search(
r"/packages/(\w+)", url
)
if not m:
return "", ""
pkg = m.group(1)
page_url = f"https://cran.r-project.org/web/packages/{pkg}/index.html"
with urlopen(page_url, timeout=10) as resp:
page_html = resp.read().decode("utf-8", errors="replace")
class CranParser(HTMLParser):
def __init__(self):
super().__init__()
self._in_td = False
self._found_published = False
self._in_github_span = False
self.date = ""
self.github_url = ""
def handle_starttag(self, tag, attrs):
if tag == "td":
self._in_td = True
# GitHub links are wrapped in <span class="GitHub">
if tag == "span":
classes = dict(attrs).get("class", "")
if "GitHub" in classes:
self._in_github_span = True
# Also check <a href> for github.com links
if tag == "a" and not self.github_url:
href = dict(attrs).get("href", "")
if "github.com" in href and "/issues" not in href:
self.github_url = href
def handle_endtag(self, tag):
if tag == "td":
self._in_td = False
if tag == "span":
self._in_github_span = False
def handle_data(self, data):
if self._in_td:
if data.strip() == "Published:":
self._found_published = True
elif self._found_published and not self.date:
d = data.strip()
if re.match(r"\d{4}-\d{2}-\d{2}", d):
self.date = d
parser = CranParser()
parser.feed(page_html)
# Clean trailing slashes or .git from GitHub URL
gh = parser.github_url.rstrip("/")
if gh.endswith(".git"):
gh = gh[:-4]
return parser.date, gh
except Exception as e:
print(f"CRAN ERROR {url}: {e}")
return "", ""
def get_pypi_last_updated(url):
"""Fetch the last release date from PyPI JSON API."""
try:
# Extract package name from URL like https://pypi.org/project/tushare/
m = re.search(r"pypi\.org/project/([\w.-]+)", url) or re.search(
r"pypi\.python\.org/pypi/([\w.-]+)", url
)
if not m:
return ""
pkg = m.group(1).rstrip("/")
api_url = f"https://pypi.org/pypi/{pkg}/json"
with urlopen(api_url, timeout=10) as resp:
data = json.loads(resp.read().decode("utf-8"))
releases = data.get("releases", {})
if not releases:
return ""
# Find the latest release with an upload time
for version in sorted(releases.keys(), reverse=True):
release_data = releases[version]
if release_data:
upload_time = release_data[0].get("upload_time_iso_8601")
if upload_time:
return upload_time.split("T")[0]
return ""
except Exception as e:
print(f"PYPI ERROR {url}: {e}")
return ""
def slugify(text):
"""Convert text to lowercase hyphen-separated slug."""
text = text.lower().strip()
text = re.sub(r"[&/]+", "-", text)
text = re.sub(r"[^\w\s-]", "", text)
text = re.sub(r"[\s_]+", "-", text)
text = re.sub(r"-+", "-", text)
return text.strip("-")
re_langs = re.compile(r'^((?:`[^`]+`\s*)+)-\s*(.*)$')
def extract_languages(description: str) -> tuple[list[str], str]:
"""Extract inline language tags from description.
Returns (languages, clean_description).
E.g. "`Python` `Rust` - High-performance..." -> (["Python", "Rust"], "High-performance...")
"""
m = re_langs.match(description)
if m:
lang_str = m.group(1)
clean_desc = m.group(2)
langs = re.findall(r'`([^`]+)`', lang_str)
return langs, clean_desc
return [], description
def get_repo_info(repo):
"""Fetch last commit date and star count from GitHub."""
try:
if repo:
r = g.get_repo(repo)
cs = r.get_commits()
last_commit = cs[0].commit.author.date.strftime("%Y-%m-%d")
stars = r.stargazers_count
return last_commit, stars
else:
return "", 0
except Exception:
print("ERROR " + repo)
return "error", 0
class Project(Thread):
def __init__(self, match, language, category, section_path):
super().__init__()
self._match = match
self.regs = None
self._language = language
self._category = category
self._section_path = section_path
self.languages = []
self.clean_description = ""
def run(self):
m = self._match
primary_url = m.group(2)
# Use clean_description if it was set by the parser, otherwise extract from match
if self.clean_description:
description = self.clean_description
else:
description = m.group(3)
# Check if primary URL is GitHub
is_github = "github.com" in primary_url
# If not GitHub, check if there's a GitHub link in the description
github_url = ""
if not is_github:
github_url = extract_github_url(description)
else:
github_url = primary_url
is_cran = "cran.r-project.org" in primary_url
is_pypi = "pypi.org" in primary_url or "pypi.python.org" in primary_url
is_commercial = self._language == "Commercial & Proprietary Services"
# For CRAN projects, scrape the CRAN page for GitHub URL and published date
cran_date = ""
if is_cran:
cran_date, cran_github = get_cran_info(primary_url)
if cran_github and not github_url:
github_url = cran_github
repo = extract_repo(github_url)
print(repo or primary_url)
last_commit, stars = get_repo_info(repo)
# Fallback: use CRAN/PyPI dates when no GitHub data
if not last_commit or last_commit == "error":
if is_cran and cran_date:
last_commit = cran_date
elif is_pypi:
pypi_date = get_pypi_last_updated(primary_url)
if pypi_date:
last_commit = pypi_date
# Build section slug from category or language
section_slug = slugify(self._category or self._language)
self.regs = dict(
project=m.group(1),
language=self._language,
languages=",".join(self.languages),
category=self._category,
section=self._section_path,
section_slug=section_slug,
last_commit=last_commit,
stars=stars,
url=primary_url,
description=description,
github=is_github or bool(github_url),
cran=is_cran,
pypi=is_pypi,
commercial=is_commercial,
repo=repo,
)
projects = []
with open("README.md", "r", encoding="utf8") as f:
ret = re.compile(r"^(#+) (.*)$")
rex = re.compile(r"^\s*- \[(.*)\]\((.*)\) - (.*)$")
re_badge = re.compile(r"\s*!\[[^\]]*\]\([^)]*\)\s*")
m_titles = []
last_head_level = 0
current_category = ""
for line in f:
line = re_badge.sub(" ", line)
m = rex.match(line)
if m:
raw_desc = m.group(3).strip()
# Extract language tags from description
languages, clean_description = extract_languages(raw_desc)
primary_language = languages[0] if languages else ""
p = Project(
m,
primary_language,
current_category,
current_category,
)
p.languages = languages
p.clean_description = clean_description
p.start()
projects.append(p)
else:
m = ret.match(line)
if m:
hrs = m.group(1)
title = m.group(2).strip()
if len(hrs) == 2 and title != "Contents":
current_category = title
while True:
checks = [not p.is_alive() for p in projects]
if all(checks):
break
projects = [p.regs for p in projects]
df = pd.DataFrame(projects)
df.to_csv("site/projects.csv", index=False)