app.js
4.87 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const request = require('request');
/*
const fs = require('fs');
const path = require('path');
const HTTPS = require('https');
const domain = "www.osstestdw.ml"
const sslport = 23023;
*/
var express = require('express')
var app = express()
app.use(express.static(__dirname + '/views'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
// api키 변경 필요
const api_key = 'e2133d897fmsh64fafbbb5226011p1b0391jsn51403f111f2c'
//팀 정보
var teamStats = []
var teams;
var playerStats = []
//선수 분석
const playerStatOptions = {
method: 'GET',
url: 'https://api-football-beta.p.rapidapi.com/players',
qs: {season: '2020', league: '39', team: '33'},
headers: {
'x-rapidapi-key': api_key,
'x-rapidapi-host': 'api-football-beta.p.rapidapi.com',
useQueryString: true
}
};
//팀 분석
var teamStatOptions = {
method: 'GET',
url: 'https://api-football-beta.p.rapidapi.com/teams/statistics',
qs: {team: '33', season: '2019', league: '39'},
headers: {
'x-rapidapi-key': api_key,
'x-rapidapi-host': 'api-football-beta.p.rapidapi.com',
useQueryString: true
}
};
// 전체 팀
const teamsOptions = {
method: 'GET',
url: 'https://api-football-beta.p.rapidapi.com/teams',
qs: {league: '39', season: '2020'},
headers: {
'x-rapidapi-key': api_key,
'x-rapidapi-host': 'api-football-beta.p.rapidapi.com',
useQueryString: true
}
};
//팀 순위
const standingsOptions = {
method: 'GET',
url: 'https://api-football-beta.p.rapidapi.com/standings',
qs: {season: '2020', league: '39'},
headers: {
'x-rapidapi-key': api_key,
'x-rapidapi-host': 'api-football-beta.p.rapidapi.com',
useQueryString: true
}
};
// top 20
const topScorersOptions = {
method: 'GET',
url: 'https://api-football-beta.p.rapidapi.com/players/topscorers',
qs: {season: '2020', league: '39'},
headers: {
'x-rapidapi-key': api_key,
'x-rapidapi-host': 'api-football-beta.p.rapidapi.com',
useQueryString: true
}
};
app.get('/TopScorers', function(req, res){
request(topScorersOptions, function(error, response, body){
if (error) throw new Error(error);
var resData = JSON.parse(body)
res.render('PersonalStanding.ejs', {resData})
})
})
app.get('/TeamStanding', function(req, res){
request(standingsOptions, function(error, response, body){
if (error) throw new Error(error);
var teamStanding = JSON.parse(body)
res.render('TeamStanding.ejs', {teamStanding})
})
})
app.get('/TeamStat', function(req, res){
res.render('TeamStat.ejs', {teams})
})
app.get('/TeamStat/:id', function(req, res){
var teamId = req.params.id;
teamStatOptions.qs.team = teamId;
request(teamStatOptions, function(error, response, body){
if (error) throw new Error(error);
var teamStat = JSON.parse(body)
var players = [];
for(var i = 0 ; i < playerStats.length; i++){
if(playerStats[i].statistics[0].team.id == teamId){
players.push(playerStats[i]);
}
}
res.render('TeamStatDetail.ejs', {teamStat, players})
})
})
app.get('/PlayerStat', function(req, res){
res.render('PersonalStat.ejs', {playerStats})
})
app.get('/PlayerStat/:id', function(req, res){
var playerStat;
for(var i = 0 ; i< playerStats.length; i++){
if((playerStats[i].player.id + "") == req.params.id){
playerStat = playerStats[i];
break;
}
}
res.render('PersonalStatDetail.ejs', {playerStat})
})
app.get('/', function(req, res){
if(playerStats.length == 0){
request(teamsOptions, function (error, response, body) {
if (error) throw new Error(error);
teams = JSON.parse(body)
for(var i = 0 ; i < 20; i++){
playerStatOptions.qs.team = teams.response[i].team.id;
request(playerStatOptions, function(error, response, body){
if (error) throw new Error(error);
var temp = JSON.parse(body)
console.log(temp)
for(var j = 0 ; j < temp.response.length; j++){
playerStats.push(temp.response[j]);
}
})
}
res.render('index.ejs');
});
}
else
res.render('index.ejs');
})
var server = app.listen(23023, function(){
var host = server.address().address
var port = server.address().port;
console.log('App listening at http://%s:%s', host, port)
})
/*
try {
const option = {
ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
};
HTTPS.createServer(option, app).listen(sslport, () => {
console.log(`[HTTPS] Server is started on port ${sslport}`);
});
} catch (error) {
console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
console.log(error);
}*/