forked from vincep5/MMM-MyStandings
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnode_helper.js
More file actions
122 lines (110 loc) · 3.83 KB
/
Copy pathnode_helper.js
File metadata and controls
122 lines (110 loc) · 3.83 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
const Log = require('logger')
const NodeHelper = require('node_helper')
const fs = require('node:fs')
const path = require('node:path')
module.exports = NodeHelper.create({
start: function () {
Log.log('Starting node_helper for: ' + this.name)
},
getDirectoryTree(dirPath) {
const result = []
const files = fs.readdirSync(dirPath, { withFileTypes: true })
files.forEach((file) => {
const filePath = path.join(dirPath, file.name)
if (file.name.endsWith('.svg') || file.name.endsWith('.png')) {
result.push({ name: file.name })
}
else if (file.isDirectory()) {
const children = this.getDirectoryTree(filePath)
if (children.length > 0) {
result.push({ name: file.name, children })
}
}
})
return result
},
async getData(notification, payload) {
try {
const response = await fetch(payload.url, {
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) MagicMirrorModule',
'Accept': 'application/json, text/plain, */*',
},
})
Log.debug(`[MMM-MyStandings] ${payload.url} fetched`)
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const data = await response.json()
this.sendSocketNotification(notification, {
result: data,
uniqueID: payload.uniqueID,
})
}
catch (error) {
Log.error(`[MMM-MyStandings] Could not load data: ${error}`)
}
},
async getSNETData(notification, payload) {
var queryYear = new Date().getFullYear()
var standings = []
while (standings.length === 0 && queryYear > 2020) {
try {
const response = await fetch(payload.url + queryYear)
Log.debug(`[MMM-MyStandings] ${payload.url} fetched`)
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const data = await response.json()
if (data['data']['teams']) {
standings = data['data']['teams']
}
if (standings.length === 0) {
queryYear = queryYear - 1
}
}
catch (error) {
Log.error(`[MMM-MyStandings] Could not load data: ${error}`)
}
}
this.sendSocketNotification(`STANDINGS_RESULT_SNET-${queryYear}_${notification.split('-')[1]}`, {
result: standings,
uniqueID: payload.uniqueID,
})
},
// Subclass socketNotificationReceived received.
socketNotificationReceived: function (notification, payload) {
if (notification == 'MMM-MYSTANDINGS-GET-LOCAL-LOGOS') {
this.localLogos = {}
var fsTree = this.getDirectoryTree('./modules/MMM-MyStandings/logos')
fsTree.forEach((league) => {
if (league.children) {
var logoFiles = []
league.children.forEach((file) => {
logoFiles.push(file.name)
})
this.localLogos[league.name] = logoFiles
}
})
this.localLogosCustom = {}
fsTree = this.getDirectoryTree('./modules/MMM-MyStandings/logos_custom')
fsTree.forEach((league) => {
if (league.children) {
var logoFiles = []
league.children.forEach((file) => {
logoFiles.push(file.name)
})
this.localLogosCustom[league.name] = logoFiles
}
})
this.sendSocketNotification('MMM-MYSTANDINGS-LOCAL-LOGO-LIST', { uniqueID: payload.uniqueID, logos: this.localLogos, logosCustom: this.localLogosCustom })
}
else if (payload.url.includes('stats-api.sportsnet.ca/web_standings')) {
this.getSNETData(notification, payload)
}
else if (notification.startsWith('STANDINGS_RESULT')) {
this.getData(notification, payload)
}
},
})