patternjavascriptMinor
Video downloader app implemented in RxJs
Viewed 0 times
appimplementedvideodownloaderrxjs
Problem
I have used multiple map/flatmap combinations. Could anybody help me with the review?
});
}
function getVideoUrl(data) {
var pattern = /"http(.*)(\.flv|\.mkv|\.mp4)"/;
var matches = pattern.exec(data.body);
if (matches && matches[0]) {
return matches[0]
} else {
return null;
}
}
function postToGorilla(url) {
var params = {
url: url,
method: 'POST',
headers: {
'content-type': 'appli
var Rx = require('rx');
var RxNode = require('rx-node');
var request = require('request');
var _ = require('lodash');
var cheerio = require('cheerio');
exports.getResult = function(req, res) {
var result = '';
fetchContent({
url: req.body.url
}, req.body.season)
.flatMap(getEpisodeRange)
.map(formEpisodeUrl.bind(null, req.body.url, req.body.season))
.flatMap(downloadHtml)
.map(getGorillaUrl)
.flatMap(postToGorilla)
.map(getVideoUrl)
.flatMap(prepareHtml)
.subscribe(function(data) {
result += data;
}, function(error) {
res.send(JSON.stringify(error))
}, function() {
res.send(result);
});
}
function fetchContent(params) {
console.log(params.url);
var args = [].slice.call(arguments, 1)
return Rx.Observable.create(function(observer) {
request(params, function(error, response, body) {
if (error) {
observer.onError();
} else {
observer.onNext({
response: response,
body: body,
args: args
});
}
observer.onCompleted();
})
});
};
function prepareHtml(url) {
return ;
}
function fileName(url, season, episode) {
return Rx.Observable.just(url.split('/'))
.last().map(function(name) {
return name.replace('_', ' ').concat( S${season}E${episode}`);});
}
function getVideoUrl(data) {
var pattern = /"http(.*)(\.flv|\.mkv|\.mp4)"/;
var matches = pattern.exec(data.body);
if (matches && matches[0]) {
return matches[0]
} else {
return null;
}
}
function postToGorilla(url) {
var params = {
url: url,
method: 'POST',
headers: {
'content-type': 'appli
Solution
Overall, I think this is very decent code, some observations:
JSHint
Odd stuff
-
You use the extremely cool
You should stick to 1 approach
-
It seemed odd that the HTML you prepare has no text in the anchor:
Also, do you want to consider at least surrounding the URL with double quotes?
Sugar
-
You use
-
This seems a good use case for a ternary:
could be
I am still not sure why you return
Style
JSHint
- Use JsHint ;)
- You have a number of missing semicolons, and an unnecessary semicolon
return url = new Buffer(url, 'base64').toString('ascii');
- You are not using the function
fileName(url, season, episode){anywhere
Odd stuff
-
You use the extremely cool
fetchContent({ url }); but also the mundane {
response: response,
body: body,
args: args
}You should stick to 1 approach
- Your regex
/"http(.*)(\.flv|\.mkv|\.mp4)"/
- Is not supporting avi, wmv, or mpg file extensions.
- Is not supporting uppercase incoming file names extentions.
- Keeps repeating the
\.for each extension, so I would keep that out of the brackets.
- If you plan for many entries, then I would keep the regex outside of the function, and generate that regex only 1 single time for performance
- You have a stray
console.log(params.url);, get rid of it
-
It seemed odd that the HTML you prepare has no text in the anchor:
return ``;Also, do you want to consider at least surrounding the URL with double quotes?
Sugar
-
You use
req.body 3 times in getResult, I would have had a var params = req.body; as a shortcut -
This seems a good use case for a ternary:
if (matches && matches[0]) {
return
} else {
return null;
}could be
return (matches && matches[0]) ? matches[0] : null;I am still not sure why you return
null actually. PrepareHtml will return `, so perhaps consider
return (matches && matches[0]) ? matches[0] : '';
Anonymous functions kill kittens every day
- The 3 functions within your
subscribe call are anonymous. It is in general terrible to look at a stack trace filled with anonymous functions
- You also have one in
getEpisodeRange
- I do like that you declare your other functions 'the right way'
Comments go a long way
getEpisodeRange` needs comments, it is the most intense code- There are actually no comments at all in this code, and I can parse everything just fine except the above mentioned function
Style
- Formatting looks good to me
- Naming looks good to me
- I did not find obvious repetitions from a DRY perspective
Code Snippets
{
response: response,
body: body,
args: args
}return `<a href=${url}></a>`;if (matches && matches[0]) {
return
} else {
return null;
}return (matches && matches[0]) ? matches[0] : null;return (matches && matches[0]) ? matches[0] : '';Context
StackExchange Code Review Q#133812, answer score: 3
Revisions (0)
No revisions yet.