Closed phatpham9 closed 4 years ago
Hey, sorry, I missed this issue before.
Unfortunately, I won't be opening the backend source code because it is around ~50 lines of code that consists of a couple of functions that contain security tokens and stuff like that, so there is no value in that. However, if you are looking for parsers, I have already open-sourced them:
You can install them with NPM and expose them via a simple HTTP server. In the end, assuming you have a method to run HTTP GET
requests, you can deploy your API with literally 3 lines of code: get the HTML, pass it to the parser, return the response.
In fact, I tried to gather the pieces to a single file and remove the details, and below is what the API looks like, as you can see the majority of the code is boilerplate, and the core API handler is a single method call, and it is not even the best way to write it probably. I haven't tested the below, sharing just to give you all an idea.
const https = require('https');
const ghParser = require('gh-trending-parser');
function httpsGet(url, successCallback, errorCallback) {
var req = https.get(url, function (res) {
var output = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
output += chunk;
});
res.on('end', () => {
successCallback(output);
});
});
req.on('error', (err) => {
errorCallback(err);
});
}
function response(statusCode, data) {
return {
statusCode,
headers: {
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify({
success: statusCode === 200,
data,
}),
}
}
module.exports.github = (event, context, callback) => {
httpsGet(
'https://github.com/trending',
output => callback(null, response(200, ghParser.parse(output))),
err => callback(null, response(500, err.message))
);
};
What's the verdict? :)