it's now a functional backend
parent
81166a5d49
commit
b7fe73c25d
@ -1,2 +1,3 @@
|
||||
PORT=3001
|
||||
NIX_CHROMIUM_PATH=<PATH/TO/CHROMIUM/INSIDE/NIX/STORE>
|
||||
ON_NIX="true"
|
||||
|
@ -1,41 +1,18 @@
|
||||
import 'dotenv/config'
|
||||
import puppeteer from 'puppeteer'
|
||||
import express from 'express'
|
||||
const app = express()
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
import fetchILMeteo from './scrapers/iLMeteo.js'
|
||||
import fetch3Bmeteo from './scrapers/3Bmeteo.js'
|
||||
import fetchAeronauticaMilitare from './scrapers/AeronauticaMilitare.js'
|
||||
import fetchOpenMeteo from './scrapers/OpenMeteo.js'
|
||||
import WeatherScraper from './weatherScraper.js'
|
||||
|
||||
const NIX_OPS = {
|
||||
executablePath: process.env.NIX_CHROMIUM_PATH,
|
||||
}
|
||||
const opts = process.env.ON_NIX ? NIX_OPS : {}
|
||||
const scraper = new WeatherScraper()
|
||||
await scraper.init()
|
||||
setInterval(() => scraper.fetch(), 1000 * 60 * 5)
|
||||
|
||||
const run = async () => {
|
||||
const browser = await puppeteer.launch(opts)
|
||||
app.get('/', (req, res) => {
|
||||
res.json(scraper.data)
|
||||
})
|
||||
|
||||
const [
|
||||
// Comment out unwanted fields
|
||||
iLMeteo,
|
||||
treBmeteo,
|
||||
openMeteo,
|
||||
aeronauticaMilitare,
|
||||
] = await Promise.all([
|
||||
fetchILMeteo(browser),
|
||||
fetch3Bmeteo(browser),
|
||||
fetchOpenMeteo(),
|
||||
fetchAeronauticaMilitare(browser),
|
||||
])
|
||||
|
||||
await browser.close()
|
||||
|
||||
return {
|
||||
iLMeteo,
|
||||
treBmeteo,
|
||||
openMeteo,
|
||||
aeronauticaMilitare,
|
||||
}
|
||||
}
|
||||
|
||||
const result = await run()
|
||||
console.dir(result, { depth: null })
|
||||
app.listen(port, () => {
|
||||
console.log(`Weather Scraper listening on port ${port}`)
|
||||
})
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,57 @@
|
||||
import 'dotenv/config'
|
||||
import puppeteer from 'puppeteer'
|
||||
|
||||
import fetchILMeteo from './scrapers/iLMeteo.js'
|
||||
import fetch3Bmeteo from './scrapers/3Bmeteo.js'
|
||||
import fetchAeronauticaMilitare from './scrapers/AeronauticaMilitare.js'
|
||||
import fetchOpenMeteo from './scrapers/OpenMeteo.js'
|
||||
|
||||
export default class WeatherScraper {
|
||||
constructor() {
|
||||
this.lock = false
|
||||
this.data = {}
|
||||
}
|
||||
async init(opts) {
|
||||
const NIX_OPS = {
|
||||
executablePath: process.env.NIX_CHROMIUM_PATH,
|
||||
}
|
||||
const OPTS = process.env.ON_NIX ? NIX_OPS : {}
|
||||
|
||||
this.browser = await puppeteer.launch(OPTS)
|
||||
await this.fetch()
|
||||
}
|
||||
async fetch() {
|
||||
const timestamp = new Date().getTime()
|
||||
|
||||
if (this.lock) {
|
||||
console.log(`[${timestamp}] skipped (locked)`)
|
||||
return
|
||||
}
|
||||
this.lock = true
|
||||
|
||||
console.log(`[${timestamp}] updating`)
|
||||
const [
|
||||
// Comment out unwanted fields
|
||||
iLMeteo,
|
||||
treBmeteo,
|
||||
openMeteo,
|
||||
aeronauticaMilitare,
|
||||
] = await Promise.all([
|
||||
fetchILMeteo(this.browser),
|
||||
fetch3Bmeteo(this.browser),
|
||||
fetchOpenMeteo(),
|
||||
fetchAeronauticaMilitare(this.browser),
|
||||
])
|
||||
|
||||
console.log(`[${timestamp}] updated`)
|
||||
this.data = {
|
||||
timestamp,
|
||||
iLMeteo,
|
||||
treBmeteo,
|
||||
openMeteo,
|
||||
aeronauticaMilitare,
|
||||
}
|
||||
|
||||
this.lock = false
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue