Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Add endpoint for fetching rss and id3 tags #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ typings/

# next.js build output
.next

files
78 changes: 78 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const express = require('express');
const superagent = require('superagent');
const checksum = require('checksum');
const fs = require('fs');
const nodeID3 = require('node-id3')
const filenamify = require('filenamify');
let Parser = require('rss-parser');
let parser = new Parser();

const applyMiddlewares = require('./middlewares')

const createApp = () => {
const app = express()
app.post('/rss', (req, res) => {
var url = req.query.url;
return parser.parseURL(req.query.url)
.then(async feed => {
let result = [];
let itemsToFetch = 2 // feed.items.length; Takes to long to fetch all
for (var i = 0; i < itemsToFetch; i++) {
const item = feed.items[i];
const stream = fs.createWriteStream('./files/' + item.title)

result.push(new Promise((resolve, reject) => {
stream.on('finish', function() {
const sum = checksum('./files/' + item.title)
console.log('Item number: ' + i + ' done');
resolve({
title: item.title,
checksum: sum,
url: item.enclosure.url
})
})

stream.on("error", (err) => {
console.log("error in request", err)
reject(err);
})
superagent.get(item.enclosure.url).pipe(stream);
}))
}

return Promise.all(result);
})
.then(result => res.send(result))

});

app.get('/id3', (req, res) => {
const url = req.query.url;
const fileName = filenamify(url, {replacement: '_'});

const stream = fs.createWriteStream('./files/' + fileName)

stream.on('finish', function() {
const tags = nodeID3.read('./files/' + fileName);
delete tags.raw;
res.send(tags)
})

stream.on("error", (err) => {
console.log("error in request", err)
throw err;
})

superagent.get(url).pipe(stream);
});

applyMiddlewares(app)

app.run = () => {
app.listen(2500, () => console.log('Server running on PORT:' + 2500));
}

return app
}

module.exports = createApp
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const createApp = require('./app')

const app = createApp()
app.run()
4 changes: 4 additions & 0 deletions middlewares/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = (error, req, res, next) => {
// console.log(error)
res.status(error.status || 500).json({ message: error.message })
}
11 changes: 11 additions & 0 deletions middlewares/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const logger = require('./logger')
const errorHandler = require('./errors')

module.exports = app => {
app.use(errorHandler)
// Default 404 error handler
app.use((req, res, next) => {
res.status(404).send('Route not found')
})
logger(app)
}
10 changes: 10 additions & 0 deletions middlewares/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const morgan = require('morgan')
const fs = require('fs')

module.exports = app => {
app.use(
morgan('common', {
stream: fs.createWriteStream('./app.log', { flags: 'a' }),
})
)
}
Loading