Last active
August 17, 2022 23:43
-
-
Save StoneyEagle/bec6ea0c950932b3fa9d5ca4373e5b21 to your computer and use it in GitHub Desktop.
Get all Netflix shows and movies
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
//axiosclient | |
import axios from 'axios'; | |
const tmdbApiClient = axios.create(); | |
tmdbApiClient.defaults.baseURL = 'https://api.themoviedb.org/3/'; | |
tmdbApiClient.defaults.timeout = 1000 * 60 * 5; | |
tmdbApiClient.interceptors.request.use( | |
async (conf) => { | |
conf.params = { | |
api_key: process.env.TMDB_API_KEY, | |
// language: i18next.language || process.env.LANGUAGE, | |
...conf.params, | |
}; | |
conf.timeout = 1000 * 60 * 5; | |
return new Promise((resolve) => { | |
conf.headers['Accept'] = 'application/json'; | |
resolve(conf); | |
}); | |
}, | |
function (error) { | |
// console.log(error.response.message); | |
return Promise.reject(error.response.message); | |
} | |
); | |
export default tmdbApiClient; | |
//tmdb.js | |
export async function discoverNetflix(limit = 50) { | |
let data = []; | |
let base = await tmdbApiClient.get(`discover/tv`, { params: { page: 1, with_networks: 213, append_to_response: 'images' } }).catch((err) => console.error(err)); | |
for (let j = 0; j < base.data.results.length; j++) { | |
data.push(base.data.results[j]); | |
} | |
for (let i = 1; i < base.data.total_pages && i < limit && i < 1000; i++) { | |
let res = await tmdbApiClient.get(`discover/tv`, { params: { page: i + 1, with_networks: 213, append_to_response: 'images' } }).catch((err) => console.error(err)); | |
for (let j = 0; j < res?.data.results.length; j++) { | |
data.push(res.data.results[j]); | |
} | |
} | |
base = await tmdbApiClient.get(`discover/movie`, { params: { page: 1, with_networks: 213, append_to_response: 'images' } }).catch((err) => console.error(err)); | |
for (let j = 0; j < base.data.results.length; j++) { | |
data.push(base.data.results[j]); | |
} | |
for (let i = 1; i < base.data.total_pages && i < limit && i < 1000; i++) { | |
let res = await tmdbApiClient.get(`discover/movie`, { params: { page: i + 1, with_networks: 213, append_to_response: 'images' } }).catch((err) => console.error(err)); | |
for (let j = 0; j < res?.data.results.length; j++) { | |
data.push(res.data.results[j]); | |
} | |
} | |
return data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment