-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice.js
41 lines (35 loc) · 1.18 KB
/
service.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
// Node dependencies
const express = require('express');
const bodyParser = require('body-parser');
const methodOverride = require('method-override');
// Local dependencies
const scraper = require('./scraper');
const store = require('./storage/store');
// configuration
var app = express();
var storeObj = new store.Store();
var scrapeTarget = "Hamburg";
var scraperInstance = new scraper.ImmoweltScraper(scrapeTarget, storeObj);
app.use(bodyParser.urlencoded({'extended':'true'}));
app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
app.use(methodOverride());
// routes
app.get('/api/entries', function(req, res) {
//get all entries
data = storeObj.getEntries();
res.json(data);
});
app.get('/api/entry/:entryId', function(req, res) {
//get all entries
data = storeObj.getEntry(req.params['entryId']);
res.json(data);
});
// start server
var isInitialScrape = storeObj.getEntries().length == 0;
scraperInstance.start(isInitialScrape);
if (!isInitialScrape) {
setInterval(function() { scraperInstance.start(); }, (60 * 1000) * 10) // restart every 10 minutes
}
app.listen(1234);
console.log("Scraper service running on http://localhost:1234");