-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisc-database.html
89 lines (79 loc) · 2.91 KB
/
disc-database.html
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
<html><body>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
function collect(error, callback) {
var results = []
, urlTemplate = 'http://localhost:3000/disc-database-page-#{page}.html'
, i, done = 0
, url
, pages = 15, page = 0
for (i = 0; i < pages; i++) {
page = i+1
url = urlTemplate.replace(/#{page}/gi, page)
console.log('requesting ', url);
$.get(url, null, function(content)
{
var parsed = parseContent(content);
results = results.concat(parsed);
console.log('parsed page ', page, parsed)
if (++done >= pages) callback(results);
});
}
console.log('collection done')
}
function parseContent(content) {
var products = []
var $content = $(content)
var rows = $content.find('table[width="99%"]').find('td[align="center"] a[href^="http://www.discgolfcenter.com/main_displayProduct.php"]').closest('tr')
for (var i = 0; i < rows.length; i++) {
var $row = $(rows[i])
var name = $row.find('td:nth-child(2) a[href^="http://www.discgolfcenter.com/main_displayProduct.php"]').text().trim()
var manufacturer = $($row.find('td:nth-child(2) table table a')[2]).text().trim()
var type = $($row.find('td:nth-child(2) table table a')[3]).text().trim()
var plastic = $.map($row.find('td:nth-child(3) tr'), function(item) { return {'name':$(item).find(':nth-child(1) a').text().trim(), 'price': $(item).find(':nth-child(3)').text().trim() }; })
var characteristics = $row.find('td:nth-child(5) td[valign="middle"]')
var flight = {}
flight.difficulty = $(characteristics[0]).text().trim()
flight.speed = $(characteristics[1]).text().trim()
flight.glide = $(characteristics[2]).text().trim()
flight.turn = $(characteristics[3]).text().trim()
flight.fade = $(characteristics[4]).text().trim()
products.push({
name: name,
manufacturer: manufacturer,
type: type,
plastic: plastic,
flight: flight
})
}
return products;
}
function syntaxHighlight(json) {
if (typeof json != 'string') {
json = JSON.stringify(json, undefined, 2);
}
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}
console.log(collect(
function(){ console.log('error'); },
function(results){
$(document.body).html('<pre>'+syntaxHighlight(results)+'</pre>')
}
))
</script>
</body></html>