mirror of
https://github.com/comatory/fb2iCal
synced 2025-06-05 22:09:25 +02:00
add html parsing & file sending
This commit is contained in:
24
lib/index.js
24
lib/index.js
@@ -4,6 +4,7 @@ const path = require('path')
|
||||
|
||||
const crawl = require('./crawler')
|
||||
const parseHTML = require('./parser')
|
||||
const generateICS = require('./ics')
|
||||
|
||||
const port = process.env.PORT || 3000
|
||||
const app = express()
|
||||
@@ -17,19 +18,36 @@ app.get('/', (req, res) => {
|
||||
})
|
||||
|
||||
app.get('*', (req, res) => {
|
||||
res.render('404')
|
||||
res.status(400).render('404')
|
||||
})
|
||||
|
||||
app.post('/download', async (req, res) => {
|
||||
const { url } = req.body
|
||||
|
||||
if (!/facebook/.test(url)) {
|
||||
return res
|
||||
.status(500)
|
||||
.render(
|
||||
'error',
|
||||
{ error: 'Not Facebook URL!' }
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
const html = await crawl(url)
|
||||
const data = parseHTML(html)
|
||||
console.log(data)
|
||||
const ics = await generateICS(data)
|
||||
|
||||
if (ics) {
|
||||
return res
|
||||
.contentType('text/calendar')
|
||||
.send(200, new Buffer(ics, 'utf8'))
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return res.render('error', { error: err.toString() })
|
||||
return res
|
||||
.status(500)
|
||||
.render('error', { error: err.toString() })
|
||||
}
|
||||
|
||||
return res.render('download', { url })
|
||||
|
Reference in New Issue
Block a user