forked from OstlerDev/PopcornTV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.js
executable file
·264 lines (246 loc) · 8.53 KB
/
settings.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
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
var fs = require('fs');
var logger = require('./logger');
/*
Change setting for UDID in aTVSettings.json, this is called from the settings page.
*/
function changeSetting(UDID, setting, newSetting){
if (setting == 'tvendpoint'){
setTVEndpoint(newSetting);
return;
}
var data = fs.readFileSync(__dirname + '/aTVSettings.json'), settings;
try {
settings = JSON.parse(data);
logger.Debug('Changing setting ' + setting + ' from ' + settings[UDID][setting] + ' to ' + newSetting + ' - UDID: ' + UDID);
settings[UDID][setting] = newSetting;
fs.writeFileSync(__dirname + '/aTVSettings.json', JSON.stringify(settings, null, 4));
} catch (err) {
logger.error('There is an error changing a setting, please post this on the Github page')
logger.error(err);
process.exit();
}
}
/*
Load and return settings for UDID
*/
function loadSettings(UDID){
if (fs.existsSync(__dirname + '/aTVSettings.json')) {
logger.Debug('Loading Settings');
var data = fs.readFileSync(__dirname + '/aTVSettings.json'), config;
try {
settings = JSON.parse(data);
if (settings[UDID] == undefined) {
logger.Debug("Adding TV " + UDID);
return addTV(UDID);
} else {
settings[UDID].tvendpoint = getTVEndpoint();
return settings[UDID];
}
} catch (err) {
logger.error('There is an error loading settings, please post this on the Github page')
logger.error(err);
}
} else {
logger.notice('No Settings file... Creating one now...');
return createFile(UDID);
}
}
/*
If the file is null then we need to create it so we call this function, it adds the first TV UDID and basic settings
*/
function createFile(UDID){
var settings = {};
settings[UDID] = {
quality: '720p',
fanart: 'On',
subtitle: 'Off',
keep: 'Off',
subSize: '100'
};
var data = JSON.stringify(settings, null, 4);
fs.writeFileSync(__dirname + '/aTVSettings.json', data);
return settings;
}
/*
Add a TV to the settings file, used because we need to allow multiple Apple TV's per file.
*/
function addTV(UDID){
logger.Debug('=== Settings ===');
logger.Debug('TV not in settings file, adding it. UDID: ' + UDID);
var data = fs.readFileSync(__dirname + '/aTVSettings.json'), settings;
try {
settings = JSON.parse(data);
settings[UDID] = {
quality: '720p',
fanart: 'On',
subtitle: 'Off',
keep: 'Off',
subSize: '100'
};
fs.writeFileSync(__dirname + '/aTVSettings.json', JSON.stringify(settings, null, 4));
return settings[UDID];
} catch (err) {
logger.error('There is an error adding a TV, please post this on the Github page')
logger.error(err);
}
}
/*
Add a favorite to the settings file for the UDID
*/
function addFavorite(type, id, UDID){
try {
var data = fs.readFileSync(__dirname + '/aTVSettings.json'), settings;
try {
settings = JSON.parse(data);
favorites = settings[UDID].favorites || [];
favorites.push({type: type, id: id});
settings[UDID].favorites = favorites;
fs.writeFileSync(__dirname + '/aTVSettings.json', JSON.stringify(settings, null, 4));
} catch (err) {
logger.error('There is an error adding to favorite, please post this on the Github page')
logger.error(err);
process.exit();
}
} catch(e){
logger.warning('Settings file does not exist, creating...');
createFile(UDID);
}
}
/*
Remove a favorite from the settings file for the UDID
*/
function removeFavorite(type, id, UDID){
try {
var data = fs.readFileSync(__dirname + '/aTVSettings.json'), settings;
try {
settings = JSON.parse(data);
favorites = settings[UDID].favorites || [];
for (var i = 0; i < favorites.length; i++) {
if (favorites[i].type == type && favorites[i].id == id){
favorites.splice(i, 1);
}
};
settings[UDID].favorites = favorites;
fs.writeFileSync(__dirname + '/aTVSettings.json', JSON.stringify(settings, null, 4));
} catch (err) {
logger.error('There is an error removing from favorites, please post this on the Github page')
logger.error(err);
process.exit();
}
} catch(e){
logger.warning('Settings file does not exist, creating...');
createFile(UDID);
}
}
/*
Check to see if the type and ID is a favorite, this is used to generate the scrobble XML and figure out which one is needed to be used.
*/
function checkFavorite(type, id, UDID){
try{
var data = fs.readFileSync(__dirname + '/aTVSettings.json'), settings;
try {
settings = JSON.parse(data);
favorites = settings[UDID].favorites || [];
var isFavorite = false;
favorites.forEach(function(favorite){
if (favorite.type == type && favorite.id == id){
isFavorite = true;
}
})
return isFavorite;
} catch (err) {
logger.error('There is an error checking favorites, please post this on the Github page')
logger.error(err);
process.exit();
}
} catch(e){
logger.warning('Settings file does not exist, creating...');
createFile(UDID);
}
}
/*
Check setting for UDID and return it.
*/
function checkSetting(setting, UDID){
try{
var data = fs.readFileSync(__dirname + '/aTVSettings.json'), settings;
try {
settings = JSON.parse(data);
setting = settings[UDID][setting] || [];
return setting;
} catch (err) {
settings = addTV(UDID);
return settings[UDID][setting];
}
} catch(e){
logger.Debug(e);
logger.warning('Settings file does not exist, creating...');
createFile(UDID);
}
}
/*
Get all Favorites for the UDID, this is used in the favorites.xml generation
*/
function getFavorites(UDID){
try{
var data = fs.readFileSync(__dirname + '/aTVSettings.json'), settings;
try {
settings = JSON.parse(data);
if (settings[UDID] == undefined){
addTV(UDID);
return [];
} else {
favorites = settings[UDID].favorites || [];
return favorites;
}
} catch (err) {
logger.error('There is an error getting favorites, please post this on the Github page')
logger.error(err);
process.exit();
}
} catch(e){
logger.warning('Settings file does not exist, creating...');
createFile(UDID);
return undefined;
}
}
function getTVEndpoint(){
try{
var data = fs.readFileSync(__dirname + '/config.json'), settings, endpoint;
try {
settings = JSON.parse(data);
endpoint = settings.tvendpoint || 'eztvapi.co.za';
return endpoint;
} catch (err) {
logger.error('There is an error checking the TV API Endpoint, please post this on the Github page')
logger.error(err);
process.exit();
}
} catch(e){
logger.warning('Config file does not exist, please report this error on the github page');
logger.error(e);
}
}
function setTVEndpoint(newEndpoint){
var data = fs.readFileSync(__dirname + '/config.json'), settings, endpoint;
try {
settings = JSON.parse(data);
endpoint = settings.tvendpoint || 'eztvapi.co.za';
logger.Debug('Changing TV API Endpoint to ' + newEndpoint + ' from ' + endpoint);
settings.tvendpoint = newEndpoint;
fs.writeFileSync(__dirname + '/config.json', JSON.stringify(settings, null, 4));
} catch (err) {
logger.error('There is an error changing the TV API Endpoint, please post this on the Github page')
logger.error(err);
process.exit();
}
}
exports.loadSettings = loadSettings;
exports.checkSetting = checkSetting;
exports.changeSetting = changeSetting;
exports.addFavorite = addFavorite;
exports.removeFavorite = removeFavorite;
exports.checkFavorite = checkFavorite;
exports.getFavorites = getFavorites;
exports.getTVEndpoint = getTVEndpoint;
exports.setTVEndpoint = setTVEndpoint;