Skip to content

Instantly share code, notes, and snippets.

@cevatbarisyilmaz
Last active January 3, 2022 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cevatbarisyilmaz/6f643b92cc68c593f5a19a92f81e3aa8 to your computer and use it in GitHub Desktop.
Save cevatbarisyilmaz/6f643b92cc68c593f5a19a92f81e3aa8 to your computer and use it in GitHub Desktop.
Streaming Availability API, Node.js Usage
const http = require("https");
const API_KEY = "ENTER YOU API KEY HERE";
const SLEEP_BETWEEN_REQUESTS = 1000; // In milliseconds
search("us", "netflix", "movie", doStuffWithResults) // Change this to different country, service and media type values ("movie" and "series")
function doStuffWithResult(result, country, service, type) {
// This function will be called for each show. Do whatever you want to do with each show here
console.log(result.imdbID, result.title, country, service, type, result.streamingInfo[service][country].link, result.originalLanguage);
}
function doStuffWithResults(results, country, service, type) {
for(let i = 0; i < results.length; i++) {
doStuffWithResult(results[i], country, service, type);
}
}
function search(country, service, type, useResult) {
let currentPage = 1;
let useRequestResult;
useRequestResult = function(data) {
useResult(data.results, country, service, type)
if(data.total_pages > currentPage) {
currentPage++;
setTimeout(function() {request(country, service, type, currentPage, useRequestResult)}, SLEEP_BETWEEN_REQUESTS);
}
}
request(country, service, type, currentPage, useRequestResult)
}
function request(country, service, type, page, useResult) {
const options = {
"method": "GET",
"hostname": "streaming-availability.p.rapidapi.com",
"port": null,
"path": "/search/basic?country=" + country + "&service=" + service + "&type=" + type + "&page=" + page,
"headers": {
"x-rapidapi-key": API_KEY,
"x-rapidapi-host": "streaming-availability.p.rapidapi.com",
"useQueryString": true
}
};
const req = http.request(options, function (res) {
const chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
const body = Buffer.concat(chunks);
const data = JSON.parse(body);
useResult(data)
});
});
req.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment