-
Notifications
You must be signed in to change notification settings - Fork 0
/
Storage.gs
78 lines (75 loc) · 2.41 KB
/
Storage.gs
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
class DB {
constructor() {
this._db = PropertiesService.getUserProperties();
return new Proxy(this, {
get: (target, prop) => this._db.getProperty(prop),
set: (target, prop, value) => this._db.setProperty(prop, value)
})
}
}
class PlaylistLookupTable {
constructor(table) {
this._arrTable = table.getRange(1, 1, table.getLastRow(), table.getLastColumn()).getValues();
this._headers = this._arrTable.splice(0, 1)[0]
this._indexes = {}
this._headers.forEach((h, i) => this._indexes[h] = i)
this._ids = {}
this._arrTable.forEach((item, index) => {
item.rowIndex = index + 2;
this._ids[item[this._indexes['ID']]] = item
})
}
_asObject(row) {
if (!row) return
let obj = Object.create(null)
for (let i = 0, l = row.length; i<l; i++) {
obj[this._headers[i]] = row[i]
}
obj.rowIndex = row.rowIndex
return obj
}
find(key, value) {
let index = this._indexes[key]
if (!index) return
return this._asObject(this._arrTable.find(item => item[index] === value));
}
getByID(id) {
return this._asObject(this._ids[id]);
}
}
const defaultConfig = {
saveMyPlaylists: true,
saveLikedVideosPlaylist: false,
useFirebaseNotifications: false,
firebaseNotifyKey: '',
firebaseDeviceToken: '',
useTelegramNotifications: false,
telegramBotToken: '',
telegramSendToID: '',
tryToRestoreVideosAutomatically: false,
replaceOldVideosInPlaylist: false,
regionRestrictionCountryCode: '',
excludePlaylistIdsCommaSeparated: '',
includePlaylistIdsCommaSeparated: ''
}
class UserConfigDB {
constructor(spreadsheet) {
let sheet = spreadsheet.getSheets()[0];
if (sheet.getSheetName() != '[CONFIG]') {
sheet.setName('[CONFIG]')
sheet.appendRow(['KEY', 'VALUE']);
sheet.getRange(1, 1, 1, 2).setFontWeight("bold");
let config = typeof customConfig === 'undefined' ? defaultConfig : {...defaultConfig, ...customConfig}
for (let key in config) {
// true and false values get localized and can lead to unexpected input
sheet.appendRow([key, (typeof config[key] === "boolean")? config[key] ? 'yes' : 'no' : config[key]])
}
sheet.autoResizeColumns(1, 2);
return config
} else {
const range = sheet.getRange(2, 1, 14, 2).getValues()
range.forEach(el => {el[1] = String(el[1]).match(/yes|no/) ? el[1] === 'yes' : el[1]})
return Object.fromEntries(range)
}
}
}