-
Notifications
You must be signed in to change notification settings - Fork 0
/
building_scraper.js
75 lines (63 loc) · 2.43 KB
/
building_scraper.js
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
const rp = require('request-promise-native')
const MongoClient = require('mongodb').MongoClient
// Returns a promise to scrape OSU buildings
async function scrapeBuildings (apiKey) {
const response = await rp(`https://api.oregonstate.edu/v1/locations/?apikey=${apiKey}&page%5Bsize%5D=10000`)
return processResponse(response)
}
function processResponse (responseData) {
return JSON.parse(responseData).data.map(b => b.attributes)
}
function parseAbbreviationOrNull (abbreviation) {
if (typeof abbreviation === 'string' && abbreviation != null) {
return abbreviation.length > 0 ? abbreviation.toUpperCase() : null
} else {
return null
}
}
function parseIntOrNull (number) {
return isNaN(parseInt(number, 10)) ? null : parseInt(number, 10)
}
function parseFloatOrNull (number) {
return isNaN(parseFloat(number)) ? null : parseFloat(number)
}
// Strips unnecessary fields from retrieved building data
function transformBuilding (building) {
return {
abbr: parseAbbreviationOrNull(building.abbreviation),
name: building.name || null,
address: building.address || null,
buildingNumber: parseIntOrNull(building.bldgID),
latitude: parseFloatOrNull(building.latitude),
longitude: parseFloatOrNull(building.longitude)
}
}
// Strips unnecessary fields from retrieved building data array
function transformBuildings (buildingArray) {
return buildingArray.map(transformBuilding)
}
if (require.main === module) {
(async function () {
const apiKey = process.env.API_KEY
const mongoUrl = process.env.MONGO_URL || 'mongodb://localhost:27017/test'
if (apiKey == null) throw new Error('No API_KEY specified for OSU Buildings API')
try {
const client = await MongoClient.connect(mongoUrl)
const buildingsCollection = client.db('test').collection('buildings')
const buildings = await scrapeBuildings(apiKey).then(transformBuildings)
await buildingsCollection.deleteMany({})
const insertResult = await buildingsCollection.insertMany(buildings)
console.log(`Sucessfully inserted data for ${insertResult.insertedCount} buildings`)
client.close()
} catch (err) {
console.log(err)
process.exit(1)
}
})()
}
exports.scrapeBuildings = scrapeBuildings
exports.processResponse = processResponse
exports.transformBuilding = transformBuilding
exports.transformBuildings = transformBuildings
exports.parseIntOrNull = parseIntOrNull
exports.parseFloatOrNull = parseFloatOrNull