forked from shoel-uddin/Gamers_Paradise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddGametoDB.js
136 lines (118 loc) · 3.37 KB
/
AddGametoDB.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const { Game } = require("./models");
// const express = require("express");
// const app = express();
//API
//"https://api.rawg.io/api/games?=name%2Cbackground_image%2Crating"
const axios = require("axios");
let url = "https://api.rawg.io/api/games?=name%2Cbackground_image%2Crating";
async function GetFirstPage() {
let data = [];
let obj = {};
let res = await axios.get(url);
// console.log(res);
for (i = 0; i < 20; i++) {
obj = {};
let title = res.data.results[i].name;
let image = res.data.results[i].background_image;
// let title = gameResults;
let jsontitle = JSON.stringify(title);
let jsonimage = JSON.stringify(image);
// console.log(`Game Title: ${jsontitle}`);
// console.log(`Game Image: ${jsonimage}`);
let genres = res.data.results[i].genres;
let genrelist = [];
for (let g of genres) {
let genre = g.name;
genrelist.push(genre);
// console.log(genre);
}
let plist = [];
let platforms = res.data.results[i].platforms;
for (let plat of platforms) {
let system = plat.platform.name;
plist.push(system);
// console.log(system);
}
console.log(jsontitle);
console.log(jsonimage);
obj["title"] = title;
obj["image"] = image;
obj["genre"] = genrelist;
obj["desc"] = "";
obj["rating"] = null;
obj["review"] = "";
obj["platform"] = plist;
obj["createdAt"] = new Date();
obj["updatedAt"] = new Date();
data.push(obj);
}
// console.log(data);
return data;
}
// GetFirstPage();
// let titles = [];
// let images = [];
// let platforms = [];
// let genres = [];
async function GetPages() {
// 10 Pages at a Time as of Right Now
// 24,568 Pages in Total
let data = [];
let obj = {};
for (p = 2; p < 50; p++) {
let url2 = `https://api.rawg.io/api/games?=name%2Cbackground_image%2Crating&page=${p}`;
let res = await axios.get(url2);
// console.log(res);
for (i = 0; i < 20; i++) {
obj = {};
// Game Title
let title = res.data.results[i].name;
title ? title : "";
// titles.push(name);
// console.log(`Game Title: ${name}`);
// Game Image
let image = res.data.results[i].background_image;
image ? image : "";
// images.push(image);
// console.log(image);
// Game Platforms
let plist = [""];
let platforms = res.data.results[i].platforms;
for (let plat of platforms) {
let system = plat.platform.name;
plist.push(system);
// console.log(system);
}
plist == [] ? null : plist;
// Game Genres
let genres = res.data.results[i].genres;
let genrelist = [""];
for (let g of genres) {
let genre = g.name;
genrelist.push(genre);
// console.log(genre);
}
// console.log(genrelist);
// genrelist = genrelist == [] ? genrelist : ;
obj["title"] = title;
obj["image"] = image;
obj["genre"] = genrelist;
obj["desc"] = "";
obj["rating"] = null;
obj["review"] = "";
obj["platform"] = plist;
obj["createdAt"] = new Date();
obj["updatedAt"] = new Date();
data.push(obj);
}
}
return data;
// console.table(data);
}
// GetPages();
// // const titleObj = Object.assign({}, titles);
// console.log(titleObj);
// // console.log(images);
// }
//
module.exports = { GetFirstPage, GetPages };