forked from CodeforLeipzig/wo-ist-markt-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (36 loc) · 1.47 KB
/
index.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
const nunjucks = require('nunjucks')
const fs = require('fs')
var request = require("request");
const { scrape } = require('./scraper.js')
//const url = 'C:/Users/Joerg/Desktop/markt.html'
const url = 'https://www.leipzig.de/freizeit-kultur-und-tourismus/einkaufen-und-ausgehen/maerkte/wochenmaerkte/'
request({
uri: url,
}, function(error, response, body) {
scrape(body)
nunjucks.configure({ autoescape: true });
const template = fs.readFileSync('./city_template.njk', 'utf-8')
const config = require('./config.json', 'utf-8')
const locations = require('./locations.json', 'utf-8')
const newMarkets = []
config.markets.forEach(market => {
const matchingLocs = locations.locations.filter(loc => loc.name == market.name)
if (matchingLocs.length > 0) {
for (index in matchingLocs) {
matchingLoc = matchingLocs[index];
const newMarket = {}
newMarket.location = matchingLoc.location
newMarket.name = matchingLoc.title
newMarket.coordinates = matchingLoc.coordinates
newMarket.title = market.title
newMarket.openingHours = market.openingHours
newMarkets.push(newMarket)
}
} else {
newMarkets.push(market)
}
})
config.markets = newMarkets
const rendered = nunjucks.renderString(template, config);
fs.writeFileSync('./leipzig.json', rendered, 'utf-8')
});