-
Notifications
You must be signed in to change notification settings - Fork 0
/
scores1.qml
299 lines (268 loc) · 9.75 KB
/
scores1.qml
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import QtQuick 2.9
import org.kde.plasma.core 2.1
Rectangle {
id:scoresMain
width:420
height:185
color:"black"
// docs https://gist.github.com/akeaswaran/b48b02f1c94f873c6655e7129910fc3b
//property var url1:"https://site.api.espn.com/apis/site/v2/sports/baseball/mlb/teams/18"
//property var url1:"https://site.api.espn.com/apis/site/v2/sports/baseball/mlb/scoreboard"
property var url1:"https://site.api.espn.com/apis/site/v2/sports/basketball/nba/scoreboard"
property var key:0
//property var favTeam:"Houston Astros"
property var favTeam:"Houston Rockets"
property var gameStatusState:""
property var gameStatusDetail:""
property var gameStatusDescription:""
property var gamePeriod:""
property var gameDate:""
property var gameHeadline:""
property var gamePlayoffRecord:""
property var gameAltHeadline:""
property var gameboxScoresURL:""
property var homeTeamName:""
property var homeTeamAbrev:""
property var homeTeamLogo:""
property var homeTeamScore:""
property var homeTeamRecord:""
property var awayTeamName:""
property var awayTeamAbrev:""
property var awayTeamLogo:""
property var awayTeamScore:""
property var awayTeamRecord:""
Component.onCompleted: {
getData(url1);
}
function getOrdinal(n) { // assigns superfix to date
var s=["th","st","nd","rd"],
v=n%100;
return (s[(v-20)%10]||s[v]||s[0]);
}
function getData(url){ // get json data sources
var xhr = new XMLHttpRequest;
var scores={};
xhr.open("GET", url); // set Method and File true=asynchronous
xhr.onreadystatechange = function () {
if(xhr.readyState == 4){ // if request_status == DONE
if (xhr.status == 200) {
var response = xhr.responseText;
scores=JSON.parse(response);
processGameData(scores);
response=null;
xhr=null;
}
}
return
}
xhr.send(); // begin the request
}
function processGameData (scores) {
for (var i = 0; i < Object.keys(scores.events).length; i++) {
if (scores.events[i].competitions[0].competitors[0].team.displayName===favTeam) {
key=i
break;
}
else if (scores.events[i].competitions[0].competitors[1].team.displayName===favTeam) {
key=i
break;
}
key=0; // else no home team game, pick first available game.
}
gameStatusState=scores.events[key].competitions[0].status.type.state;
gameStatusDetail=scores.events[key].competitions[0].status.type.detail;
gameStatusDescription=scores.events[key].competitions[0].status.type.description;
gamePeriod=scores.events[key].competitions[0].status.period;
gameDate=scores.events[key].competitions[0].date;
gameboxScoresURL=scores.events[key].links[0].href;
//sevents[0].competitions[0].series.title
gamePlayoffRecord=scores.events[key].competitions[0].hasOwnProperty("series") ? scores.events[key].competitions[0].series.summary:""
gameAltHeadline=(typeof scores.events[key].competitions[0].notes[0]!="undefined") ? scores.events[key].competitions[0].notes[0].headline : ""
gameHeadline=scores.events[key].competitions[0].hasOwnProperty("headlines") ? scores.events[key].competitions[0].headlines[0].shortLinkText : gameAltHeadline+" "+gamePlayoffRecord
//gameHeadline=scores.events[key].competitions[0].hasOwnProperty("headlines") ? scores.events[key].competitions[0].headlines[0].shortLinkText : ""
homeTeamName=scores.events[key].competitions[0].competitors[0].team.displayName;
homeTeamAbrev=scores.events[key].competitions[0].competitors[0].team.abbreviation;
homeTeamLogo=scores.events[key].competitions[0].competitors[0].team.logo;
homeTeamScore=scores.events[key].competitions[0].competitors[0].score;
homeTeamRecord=scores.events[key].competitions[0].competitors[0].records[0].summary;
awayTeamName=scores.events[key].competitions[0].competitors[1].team.displayName;
awayTeamAbrev=scores.events[key].competitions[0].competitors[1].team.abbreviation;
awayTeamLogo=scores.events[key].competitions[0].competitors[1].team.logo;
awayTeamScore=scores.events[key].competitions[0].competitors[1].score;
awayTeamRecord=scores.events[key].competitions[0].competitors[1].records[0].summary;
scores=null;
return
}
function gameState() {
if (gameStatusState == "pre") {
return (Qt.formatDateTime(new Date(gameDate),"h:mm ap"));
}
else if (gameStatusState == "in") {
if (gameStatusDescription!="In Progress") {
return (gameStatusDetail);
}
return (gamePeriod+getOrdinal(gamePeriod));
}
else {
return (gameStatusDetail);
}
}
function gameInterval () {
if (gameStatusState == "pre") {
if (Math.abs((Qt.formatDateTime(checkTimer.data["Local"]["DateTime"],"hhmm")-Qt.formatDateTime(new Date(gameDate),"hhmm"))) < 2) {
// less than 5 mins to game start time
return 20*60*1000;
}
return 2*60*60*1000;
}
if (gameStatusState == "in") {
if (gameStatusDetail=="Delayed") {
return 1*60*60*1000;
}
return 20*60*1000;
}
if (gameStatusState == "post") {
return 8*60*60*1000;
}
return 2*60*60*1000;// default 2 hours if nothing matched
}
function refreshGameData () {
getData(url1);
return
}
Rectangle {
id:rect1
width:scoresMain.width*.99
height:85
antialiasing : true
color:"transparent"
border.color:"transparent"
radius:8
anchors.top:scoresMain.top
anchors.left:scoresMain.left
anchors.leftMargin:2
MouseArea {
id: mouseArea1a
anchors.fill: parent
cursorShape: Qt.PointingHandCursor
hoverEnabled:true
acceptedButtons: Qt.LeftButton | Qt.MiddleButton
onEntered:parent.border.color=Theme.viewHoverColor
onExited:parent.border.color="transparent"
onClicked:(mouse.button==Qt.LeftButton) ? Qt.openUrlExternally(gameboxScoresURL) : refreshGameData()
}
Image{
id:atl
source:awayTeamLogo
height:48
width:48
anchors.left:rect1.left
anchors.top:rect1.top
anchors.leftMargin:15
anchors.rightMargin:15
anchors.topMargin:10
}
Text {
id:ateam
text:awayTeamAbrev
color:"white"
font.pointSize:16
anchors.topMargin:10
anchors.leftMargin:15
anchors.rightMargin:15
anchors.top:rect1.top
anchors.left:atl.right
Text {
id:atr
text:"("+awayTeamRecord+")"
color:"gray"
font.pointSize:10
antialiasing : true
anchors.horizontalCenter:parent
anchors.top:parent.bottom
}
}
Text {
text:awayTeamScore
color:"white"
font.pointSize:16
anchors.topMargin:10
anchors.top:rect1.top
anchors.left:ateam.right
anchors.leftMargin:15
}
Text {
text:gameState()
color:"white"
anchors.topMargin:15
anchors.top:rect1.top
anchors.horizontalCenter:rect1.horizontalCenter
Text {
text:Qt.formatDateTime(new Date(gameDate),"M/dd/yy")
color:"gray"
font.pointSize:10
anchors.top:parent.bottom
anchors.horizontalCenter:parent.horizontalCenter
}
}
Image{
id:htl
source:homeTeamLogo
height:48
width:48
anchors.right:hta.left
anchors.top:rect1.top
anchors.rightMargin:15
anchors.topMargin:10
}
Text {
id:hta
text:homeTeamAbrev
color:"white"
font.pointSize:16
anchors.topMargin:10
anchors.leftMargin:15
anchors.rightMargin:15
anchors.top:rect1.top
anchors.right:hts.left
Text {
text:"("+homeTeamRecord+")"
color:"gray"
font.pointSize:10
anchors.top:parent.bottom
anchors.horizontalCenter:parent.horizontalCenter
}
}
Text {
id:hts
text:homeTeamScore
color:"white"
font.pointSize:16
anchors.topMargin:10
anchors.leftMargin:15
anchors.rightMargin:15
anchors.top:rect1.top
anchors.right:rect1.right
}
Text {
anchors.bottom:rect1.bottom
anchors.left:rect1.left
anchors.bottomMargin:2
text:gameHeadline
color:Theme.textColor
font.pointSize:10
antialiasing : true
leftPadding:5
width:rect1.width*.97
elide: Text.ElideRight
wrapMode: Text.NoWrap
}
}
property var gameTimer: Timer {
interval: gameInterval()
running:true
repeat:true
triggeredOnStart:false
onTriggered:getData(url1)
}
}