Created
June 7, 2021 18:29
-
-
Save mugan86/e3eb675666bff533841cefc9804fa811 to your computer and use it in GitHub Desktop.
Extractor de series y películas de API The MovieDB
This file contains hidden or 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
import axios from "axios"; | |
import express from "express"; | |
const URL = 'http://localhost:4008/graphql'; | |
export const TV = (page: number = 1, primaryYear = -1) => ` | |
{ | |
discoverTv (page: ${page}, primaryYear: ${primaryYear}) { | |
tvShows { | |
id | |
name | |
overview | |
firstAirDate | |
posterPath | |
} | |
} | |
} | |
`; | |
export const MOVIE = (page = 1, primaryYear = -1) => ` | |
{ | |
discoverMovie(page: ${page}, primaryYear: ${primaryYear}) { | |
movies { | |
id | |
overview | |
title | |
releaseDate | |
posterPath | |
} | |
} | |
} | |
`; | |
const app = express(); | |
const MongoClient = require("mongodb").MongoClient; | |
let db: any; | |
MongoClient.connect( | |
"mongodb://localhost:27017/warehouse-management", | |
{ useNewUrlParser: true, useUnifiedTopology: true }, | |
(err: any, client: any) => { | |
if (err) return console.log(err); | |
db = client.db(); // whatever your database name is | |
app.listen(3000, () => { | |
console.log("listening on 3000"); | |
}); | |
} | |
); | |
function getApi(query: string) { | |
return axios.post(URL, { | |
query | |
}); | |
} | |
function getRandomPrice(min = 6.99, max = 20.99) { | |
return +(Math.random() * (min - max) + max).toFixed(2) | |
} | |
app.get("/", (_, res) => { | |
res.send("Hello World"); | |
}); | |
app.get("/series/:year", async (req, res_) => { | |
console.log(req.params); | |
let startYear = 2002;//+req.params.year; | |
let array: any = []; | |
for (let year = startYear; year <= 2004; year ++) { | |
for (let i = 1; i <= 600; i++) { | |
try { | |
await getApi(TV(i, year)) | |
.then(res => { | |
const tags = res.data.data.discoverTv.tvShows; | |
console.log(tags.length); | |
const tagsComplete: any = []; | |
for (let j = 0; j < tags.length; j++) { | |
let selectTag = tags[j]; | |
selectTag = { | |
id: `TV_SHOW_${selectTag.id}`, | |
name: selectTag.name, | |
description: selectTag.overview, | |
stock: 10000, | |
price: getRandomPrice(), | |
active: true, | |
currency: "EUR", | |
type: "TV_SHOW", | |
posterPath: selectTag.posterPath, | |
releaseDate: selectTag.firstAirDate | |
} | |
if (selectTag.description !== '') { | |
tagsComplete.push(selectTag); | |
} | |
} | |
array = [...array, ...tagsComplete]; | |
console.log(array); | |
setTimeout(function() { | |
console.log("Esperamos medio segundo..."); | |
}, 500); | |
}) | |
.catch(error => { | |
console.error(error); | |
i = 1001; | |
}); | |
} catch (e) { | |
i = 1001; | |
} | |
} | |
} | |
db.collection("products") | |
.insertMany(array) | |
.then((_: any) => { | |
console.log("products ok!"); | |
res_.json(array); | |
}) | |
.catch((_: any) => { | |
res_.send("products not add"); | |
}); | |
res_.json(array); | |
}); | |
app.get("/movies/:year", async (req, res_) => { | |
console.log(req.params); | |
let startYear = +req.params.year; | |
let array: any = []; | |
for (let year = startYear; year <= 2021; year ++) { | |
for (let i = 1; i <= 1000; i++) { | |
try { | |
await getApi(MOVIE(i, year)) | |
.then(res => { | |
const tags = res.data.data.discoverMovie.movies; | |
console.log(tags.length); | |
const tagsComplete: any = []; | |
for (let j = 0; j < tags.length; j++) { | |
let selectTag = tags[j]; | |
selectTag = { | |
id: `MOVIE_${selectTag.id}`, | |
name: selectTag.title, | |
description: selectTag.overview, | |
stock: 10000, | |
price: getRandomPrice(), | |
active: true, | |
currency: "EUR", | |
type: "MOVIE", | |
posterPath: selectTag.posterPath, | |
releaseDate: selectTag.releaseDate | |
} | |
if (selectTag.description !== '') { | |
tagsComplete.push(selectTag); | |
} | |
} | |
array = [...array, ...tagsComplete]; | |
console.log(array); | |
setTimeout(function() { | |
console.log("Esperamos medio segundo..."); | |
}, 500); | |
}) | |
.catch(error => { | |
console.error(error); | |
i = 1001; | |
}); | |
} catch (e) { | |
i = 1001; | |
} | |
} | |
} | |
db.collection("products") | |
.insertMany(array) | |
.then((_: any) => { | |
console.log("products ok!"); | |
res_.json(array); | |
}) | |
.catch((_: any) => { | |
res_.send("products not add"); | |
}); | |
res_.json(array); | |
}); |
This file contains hidden or 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
{ | |
"name": "o-db-vg-s", | |
"version": "1.0.0", | |
"description": "Para añadir la información de la API Rawg en la BBDD de nuestro proyecto de tienda", | |
"main": "build/index.js", | |
"author": "Anartz Mugika Ledo <[email protected]>", | |
"license": "MIT", | |
"private": false, | |
"scripts": { | |
"start": "nodemon \"src/index.ts\" --exec \"ts-node\" src/index.ts -e ts,json" | |
}, | |
"dependencies": { | |
"axios": "^0.19.2", | |
"chalk": "^3.0.0", | |
"express": "^4.17.1", | |
"mongodb": "^3.5.3" | |
}, | |
"devDependencies": { | |
"@types/express": "^4.17.2", | |
"@types/mongodb": "^3.3.16", | |
"nodemon": "^2.0.2", | |
"ts-node": "^8.6.2", | |
"typescript": "^3.7.5" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment