forked from MarvNC/yomitan-dictionaries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdrive-updater.js
189 lines (170 loc) Β· 5.77 KB
/
gdrive-updater.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
const getProperty = (propertyName) => {
const propertyValue = PropertiesService.getScriptProperties().getProperty(propertyName);
if (!propertyValue) {
throw new Error(`${propertyName} not set`);
}
return propertyValue;
};
const japaneseFolderId = getProperty('japaneseFolderId');
const mandarinFolderId = getProperty('mandarinFolderId');
const cantoneseFolderId = getProperty('cantoneseFolderId');
const githubAccessToken = getProperty('githubAccessToken');
function downloadAllRepos() {
for (const repo of repos) {
downloadFromGithub(repo);
}
}
/**
* @typedef {Object} GithubRepoDictionary
* @property {string} url
* @property {string} folderId
* @property {RegExp} includedNameRegex
* @property {RegExp} removeNameRegex
* @property {string} fileNamePrefix
* @property {boolean} [addDate]
*/
/**
* @type {GithubRepoDictionary[]}
*/
const repos = [
{
url: 'https://api.github.com/repos/stephenmk/stephenmk.github.io/releases/latest',
folderId: japaneseFolderId,
includedNameRegex: /yomi/,
removeNameRegex: /jitendex/,
fileNamePrefix: '[JA-EN] ',
},
{
url: 'https://api.github.com/repos/themoeway/jmdict-yomitan/releases/latest',
folderId: japaneseFolderId,
includedNameRegex: /JMnedict/,
removeNameRegex: /JMnedict/,
fileNamePrefix: '[JA-JA Names] ',
addDate: true,
},
{
url: 'https://api.github.com/repos/themoeway/jmdict-yomitan/releases/latest',
folderId: japaneseFolderId,
includedNameRegex: /KANJIDIC_english/,
removeNameRegex: /KANJIDIC_english/,
fileNamePrefix: '[Kanji] ',
addDate: true,
},
{
url: 'https://api.github.com/repos/MarvNC/cc-cedict-yomitan/releases/latest',
folderId: mandarinFolderId,
includedNameRegex: /CC\-CEDICT(?!\.Hanzi)/,
removeNameRegex: /CC\-CEDICT(?!\.Hanzi)/,
fileNamePrefix: '[ZH-EN] ',
addDate: true,
},
{
url: 'https://api.github.com/repos/MarvNC/cc-cedict-yomitan/releases/latest',
folderId: mandarinFolderId,
includedNameRegex: /CC\-CEDICT\.Hanzi/,
removeNameRegex: /CC\-CEDICT\.Hanzi/,
fileNamePrefix: '[Hanzi] ',
addDate: true,
},
{
url: 'https://api.github.com/repos/MarvNC/wordshk-yomitan/releases/latest',
folderId: cantoneseFolderId,
includedNameRegex: /Words\.hk\.[\d-]+.zip$/,
removeNameRegex: /Words\.hk\.[\d-]+.zip$/,
fileNamePrefix: '[YUE-EN & YUE] ',
addDate: false,
},
{
url: 'https://api.github.com/repos/MarvNC/wordshk-yomitan/releases/latest',
folderId: cantoneseFolderId,
includedNameRegex: /Words\.hk\.Honzi.[\d-]+.zip$/,
removeNameRegex: /Words\.hk\.Honzi.[\d-]+.zip$/,
fileNamePrefix: '[Honzi] ',
addDate: false,
},
// {
// url: 'https://api.github.com/repos/MarvNC/pixiv-yomitan/releases/latest',
// folderId: japaneseFolderId,
// includedNameRegex: /^Pixiv_[\d\-]+\.zip$/,
// removeNameRegex: /^Pixiv_[\d\-]+\.zip$/,
// fileNamePrefix: '[JA-JA Encyclopedia] ',
// addDate: false,
// },
];
// Function to download a release repo dictionary from GitHub and save it to Google Drive
/**
* @param {GithubRepoDictionary} githubRepo
*/
function downloadFromGithub(githubRepo) {
const headers = {
Authorization: 'token ' + githubAccessToken,
};
const options = {
headers: headers,
};
const releaseInfo = UrlFetchApp.fetch(githubRepo.url, options).getContentText();
const releaseData = JSON.parse(releaseInfo);
const assets = releaseData.assets;
let asset;
// Find the asset containing the includedNameRegex in its name and download it
for (let i = 0; i < assets.length; i++) {
if (assets[i].name.match(githubRepo.includedNameRegex)) {
asset = assets[i];
break;
}
}
// If asset is found, download it and save it to Google Drive
if (asset?.browser_download_url && asset.browser_download_url !== '') {
const response = UrlFetchApp.fetch(asset.browser_download_url);
const fileBlob = response.getBlob();
// Remove existing files for this dictionary
removeFilesWithSubstring(githubRepo.folderId, githubRepo.removeNameRegex);
const folder = DriveApp.getFolderById(githubRepo.folderId);
const createdFile = folder.createFile(fileBlob);
// Prepend file with to follow naming convention
let fileName = createdFile.getName();
// add prefix
fileName = githubRepo.fileNamePrefix + fileName;
// Add date to file name if specified
if (githubRepo.addDate) {
const date = asset.created_at.split('T')[0];
// Suffix before file extension
fileName = fileName.replace(/(\.[\w\d_-]+)$/i, ` (${date})$1`);
}
createdFile.setName(fileName);
Logger.log(`Downloaded ${createdFile.getName()} to Google Drive.`);
} else {
Logger.log(`No asset containing ${githubRepo.includedNameRegex} found in the latest release.`);
}
}
/**
* Remove existing files from a folder that match a regex
* Uses the Google Drive API to delete files, so files will bypass the trash folder
* @param {string} folderId
* @param {RegExp} regexToRemove
*/
function removeFilesWithSubstring(folderId, regexToRemove) {
const folder = DriveApp.getFolderById(folderId);
const files = folder.getFiles();
while (files.hasNext()) {
const file = files.next();
if (file.getName().match(regexToRemove)) {
// Get the access token
const accessToken = ScriptApp.getOAuthToken();
// Define the URL
const url = `https://www.googleapis.com/drive/v3/files/${file.getId()}`;
// Make the request
const response = UrlFetchApp.fetch(url, {
method: 'delete',
headers: {
Authorization: `Bearer ${accessToken}`,
},
muteHttpExceptions: true,
});
// Log the response for debugging
Logger.log(
`Deleted ${file.getName()} from Google Drive. Response code: ${response.getResponseCode()}`
);
}
}
}