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
import cheerio from "npm:cheerio@1.0.0-rc.12"
export default async function(req: Request): Promise<Response> {
const data = await getLaunches()
return Response.json(data)
}
export async function getLaunches() {
var y10to19 = await loadPage(
"https://en.wikipedia.org/wiki/List_of_Falcon_9_and_Falcon_Heavy_launches_(2010%E2%80%932019)",
)
var y20to22 = await loadPage(
"https://en.wikipedia.org/wiki/List_of_Falcon_9_and_Falcon_Heavy_launches_(2020%E2%80%932022)",
)
var current = await loadPage("https://en.wikipedia.org/wiki/List_of_Falcon_9_and_Falcon_Heavy_launches")
var data = {
pastLaunches: [
...getRows(y10to19, "#Launches", "table.collapsible", parsePastRows),
...getRows(y20to22, "#Launches", "table.collapsible", parsePastRows),
...getRows(current, "#Past_launches", "table.collapsible", parsePastRows),
],
launches: [
...getRows(current, "#Future_launches", "table", parseFutureRows),
],
}
return data
}
async function loadPage(url) {
const response = await fetch(url)
const body = await response.text()
const $ = cheerio.load(body)
return $
}
function getRows($, h2Selector, tableSelector, parseFunc) {
var launchesH2 = $(h2Selector).parent()
var launchesTable = launchesH2.nextAll(tableSelector)
var rows = launchesTable.find("tr")
rows = rows.filter(function(i, el) {
if ($(this).find("th").length > 2) return false // hide header
// if ($(this).find("td").first().attr("colspan") == 6) return false // hide year rows
return true
})
let launches = parseFunc(rows, $)
return launches
}
function parseFutureRows(rows, $) {
var launches = []
var launch: any = {}
rows.each(function(i, el) {
$(this).find("br").replaceWith(" ")
var children = $(this).children()
// console.log(children.length)
if (children.first().attr("rowspan")) {
launch = {}
launch.dateText = removeReferences(children.eq(0).text())
launch.dateText = launch.dateText.replace(/(\d\d:\d\d)/, " $1")
if (launch.dateText.match(/(\d\d:\d\d)/)) launch.date = new Date(launch.dateText.replace("~", "@") + " UTC")
if (isNaN(launch.date)) launch.date = null
launch.type = removeReferences(children.eq(1).text()).replace("♺", "♻️")
launch.site = removeReferences(children.eq(2).text())
launch.payload = removeReferences(children.eq(3).text())
launch.payloadIcon = getPayloadIcon(launch.payload)
launch.orbit = removeReferences(children.eq(4).text())
launch.customer = removeReferences(children.eq(5).text())
}
else if (!children.first().attr("colspan") && children.length == 1) {
launch.type += ", " + removeReferences(children.eq(0).text())
}
else if (children.first().attr("colspan")) {
launch.note = removeReferences(children.eq(0).text())
launch.payloadIcon = launch.payloadIcon || getPayloadIcon(launch.note)
launches.push(launch)
}
})
return launches
}
function parsePastRows(rows, $) {
var launches = []
var launch: any = {}
rows.each(function(i, el) {
$(this).find("br").replaceWith(" ")
var children = $(this).children()
// console.log(children.length)
if (children.first().attr("rowspan")) {
launch = {}
launch.dateText = removeReferences(children.eq(1).text())
launch.dateText = launch.dateText.replace(/(\d\d:\d\d)/, " $1")
if (launch.dateText.match(/(\d\d:\d\d)/)) launch.date = new Date(launch.dateText.replace("~", "@") + " UTC")
if (isNaN(launch.date)) launch.date = null
launch.type = removeReferences(children.eq(2).text()).replace("♺", "♻️")
launch.site = removeReferences(children.eq(3).text())
launch.payload = removeReferences(children.eq(4).text())
launch.payloadIcon = getPayloadIcon(launch.payload)