1
0
mirror of https://github.com/comatory/fb2iCal synced 2024-12-28 17:30:51 +01:00
Facebook-Events-iCal-Converter/lib/index.js

68 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-10-15 09:22:08 +02:00
const express = require('express')
const bodyParser = require('body-parser')
const path = require('path')
2019-10-20 19:30:32 +02:00
const favicon = require('serve-favicon')
2019-10-15 09:22:08 +02:00
const crawl = require('./crawler')
const parseHTML = require('./parser')
2019-10-15 22:45:03 +02:00
const generateICS = require('./ics')
2019-10-17 22:36:29 +02:00
const { genericErrorHandler, checkFBURL } = require('./middlewares')
2019-10-15 09:22:08 +02:00
2019-10-15 22:55:12 +02:00
const port = process.env.PORT
2019-10-15 09:22:08 +02:00
const app = express()
app.set('view engine', 'ejs')
app.set('views', path.join(__dirname, 'views'))
2019-10-20 19:30:32 +02:00
2019-10-20 20:32:25 +02:00
app.use(express.static(path.join(__dirname, 'public')))
2019-10-20 20:47:19 +02:00
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
2019-10-15 09:22:08 +02:00
app.use(bodyParser())
app.get('/', (req, res) => {
res.render('index')
})
2019-10-17 22:36:29 +02:00
app.get('/error', (req, res) => {
const error = req.error || req.query.error || ''
2019-10-17 22:36:29 +02:00
res
.status(500)
.render('error', { error })
})
app.get('/download', (req, res) => {
res
.status(200)
.render('download')
})
// NOTE: Capture all unkown URLs
app.get('*', (req, res) => {
res.status(400).render('404')
2019-10-17 22:36:29 +02:00
})
app.use('/download', checkFBURL)
2019-10-15 09:22:08 +02:00
app.post('/download', async (req, res) => {
const { url } = req.body
try {
const html = await crawl(url)
const data = parseHTML(html)
2019-10-15 22:45:03 +02:00
const ics = await generateICS(data)
if (ics) {
return res
.contentType('text/calendar')
.send(200, new Buffer(ics, 'utf8'))
}
2019-10-15 09:22:08 +02:00
} catch (err) {
2019-10-17 22:36:29 +02:00
return next(err)
2019-10-15 09:22:08 +02:00
}
})
2019-10-17 22:36:29 +02:00
app.use(genericErrorHandler)
2019-10-15 09:22:08 +02:00
app.listen(port, () => {
console.log(`App running on port ${port}`)
})