-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper
executable file
·177 lines (156 loc) · 4.11 KB
/
scraper
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
#!/usr/bin/env python3
import urllib.request
import ssl
from html.parser import HTMLParser
import json
ssl._create_default_https_context = ssl._create_unverified_context
DOWNLOADS = 'url'
KEYMAP = {
'npm': 'npm',
'NODE_MODULE_VERSION': 'node-module-version',
'V8': 'v8',
'Version': 'version',
'Date': 'date',
'LTS': 'lts',
DOWNLOADS: 'url'
}
# All branches
BRANCHES = [
{
"name": "All",
"pattern": ""
},
{
"name": "Node.js 15.x",
"pattern": "15."
},
{
"name": "Node.js 14.x LTS",
"pattern": "14."
},
{
"name": "Node.js 13.x",
"pattern": "13."
},
{
"name": "Node.js 12.x LTS",
"pattern": "12."
},
{
"name": "Node.js 11.x",
"pattern": "11."
},
{
"name": "Node.js 10.x LTS",
"pattern": "10."
},
{
"name": "Node.js 9.x",
"pattern": "9."
},
{
"name": "Node.js 8.x LTS",
"pattern": "8."
},
{
"name": "Node.js 7.x",
"pattern": "7."
},
{
"name": "Node.js 6.x LTS",
"pattern": "6."
},
{
"name": "Node.js 5.x",
"pattern": "5."
},
{
"name": "Node.js 4.x",
"pattern": "4."
},
{
"name": "Node.js 0.12.x",
"pattern": "0.12."
},
{
"name": "Node.js 0.10.x",
"pattern": "0.10."
}
]
result_list = []
def generate_download_links(obj, prefix):
(name, version) = obj[KEYMAP['Version']].split(' ')
if name == 'io.js':
return None
obj[KEYMAP['Version']] = version
obj[DOWNLOADS] = {
'shasum256': '%sSHASUMS256.txt' % prefix,
'darwin': '%snode-v%s-darwin-x64.tar.xz' % (prefix, version),
'win': '%snode-v%s-win-x64.7z' % (prefix, version),
'linux': '%snode-v%s-linux-x64.tar.xz' % (prefix, version)
}
return obj
class NodeVersionsParser(HTMLParser):
in_table = False
in_list = False
tmp_obj = None
tmp_key = None
def handle_starttag(self, tag, attrs):
# Go into the <table class="download-table">
if tag == 'table':
for key, value in attrs:
if key == 'class':
self.in_table = value.startswith('download-table')
break
# Go into the <tbody>, the list of all node versions
if self.in_table and tag == 'tbody':
self.in_list = True
if self.in_list:
# Start a new item
if tag == 'tr':
self.tmp_obj = {}
if tag == 'td':
key = attrs[0][1]
# Download link
if key == 'download-table-last':
self.tmp_key = DOWNLOADS
else:
self.tmp_key = key
# Generate download links
if tag == 'a' and self.tmp_key == DOWNLOADS:
self.tmp_obj = generate_download_links(self.tmp_obj, attrs[0][1])
self.tmp_key = None
def handle_endtag(self, tag):
global result_list
if tag == 'table':
self.in_table = False
if tag == 'tbody':
self.in_list = False
if self.in_list:
# End of an item, append to the list
if tag == 'tr':
if self.tmp_obj is not None:
result_list.append(self.tmp_obj)
self.tmp_obj = None
if tag == 'td':
self.tmp_key = None
def handle_data(self, data):
if self.tmp_obj is not None and self.tmp_key:
self.tmp_obj[KEYMAP[self.tmp_key]] = data.strip()
output = {
'branches': BRANCHES,
'versions': result_list
}
parser = NodeVersionsParser()
req = urllib.request.Request(
'https://nodejs.org/en/download/releases/',
None,
{
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:79.0) Gecko/20100101 Firefox/79.0'
}
)
res = urllib.request.urlopen(req)
parser.feed(res.read().decode('utf-8'))
file = open('data.json', 'w')
file.write(json.dumps(output, indent=4, separators=(',', ': ')))
file.close()