Optimize request usage
Closed this issue ยท 4 comments
Optimized request usage!
Currently we are using request
twice internally to get xkcd image url
and title
i.e
//Get num
request('http://xkcd.com/info.0.json',function (error, response, body) {
//Get url and title
request(xkcd_url, function (error, response, body) {});
});
I think we can do it in one request call, of course a little bit of scrapping though ๐
http://c.xkcd.com/random/comic/
=> 302
redirection => to randomly show a comic.
As request
module follows 302
redirection we can leverage this feature.
var request = require('request');
var cheerio = require('cheerio');
var targetUrl = "http://c.xkcd.com/random/comic/";
request(targetUrl, function(error, response, body){
if(!error && response.statusCode == 200){
$ = cheerio.load(body);
var str = $("#middleContainer").text();
var re = /http:\/\/xkcd\.com\/([0-9]+)\//;
var pageUrl = str.match(re)[0];// xkcd page url
var title = $("#comic img").attr("title");// xkcd image title
var imgSrc = $("#comic img").attr("src");// xkcd image
}
});
Let me know if it make sense to do this change ๐ ? Would love to raise a pull request if required.
Demo preview xkcd-json
@samarpanda Instead of scraping the webpage, it's always better to stick with the JSON API.
The code is taking the current comic count from http://xkcd.com/info.0.json
and then generating a random number within range and then fetching that comic again.
I can't think of avoiding two requests using this API, would be great if we you find one such API.
@hemanth ๐ make sense. Will check if i can. Another thought was to have some kind of caching for a day. So we do the comic count request once a day and not for every request of xkcd-imgs
. Not sure how to implement a cache for a npm module or is it a good practise?
@samarpanda node-cache is the first thing I can recall, it has a TTL
as well.
Caching wouldn't make it all that random, so abandoning the idea, feel free to reopen.