Last active
January 3, 2022 18:04
-
-
Save cevatbarisyilmaz/6f643b92cc68c593f5a19a92f81e3aa8 to your computer and use it in GitHub Desktop.
Streaming Availability API, Node.js Usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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