This repository was archived by the owner on Oct 25, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathMain.gs
More file actions
160 lines (121 loc) · 3.84 KB
/
Copy pathMain.gs
File metadata and controls
160 lines (121 loc) · 3.84 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
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
var SPRED_SHEET_ID = '19Gc6rE4Oo1CcrahJb5c45t1Gel-EGSvkBcNWtf3f_Bg';
var DOWNLOAD_URL_BASE = 'http://www.opera.com/download/get/';
var DOWNLOAD_URL = {
stable: 'http://www.opera.com/download/get/?partner=www&opsys=Windows&product=Opera',
beta: 'http://www.opera.com/download/get/?partner=www&opsys=Windows&product=Opera%20beta',
developer: 'http://www.opera.com/download/get/?partner=www&opsys=Windows&product=Opera%20Developer'
};
var DOWNLOAD_LINK_REGEX = /=\"(\/download[^\"]+)\"/;
var VERSION_REGEX = /\/([0-9.]+)\/win/;
/**
* GET /?channel='stable'|'beta'|'developer'&format='json'|'xml'
*/
function doGet(e) {
return entryPoint(e, 'get');
}
function entryPoint(e, method) {
var channel = e.parameter.channel || 'stable';
var format = e.parameter.format || 'json';
var info = getOperaInfo(channel);
info.channel = channel;
// エラーログを記載
if (info.errMsg) {
insertRow([new Date(), info.errMsg]);
}
return render(info, format);
}
function getOperaInfo(channel) {
var downloadInfo = getOperaDownloadInfo(channel);
if (!downloadInfo.url) {
return downloadInfo;
}
return getOperaVersionInfo(downloadInfo.url, 0);
}
function getOperaDownloadInfo(channel) {
var url = DOWNLOAD_URL[channel];
if (!url) {
return { errMsg: 'Can\'t find download URL' };
}
var response = UrlFetchApp.fetch(url);
var code = response.getResponseCode();
var content = response.getContentText();
// HTTP ステータスコードが 200 である場合、成功
if (code !== 200) {
return { errMsg: 'Can\'t fetch Opera website' }; // 失敗
}
// ダウンロード先 URL を探す
var matches = content.match(DOWNLOAD_LINK_REGEX);
if (!matches) {
return { errMsg: 'Can\'t find a download link' }; // 失敗
}
var url = matches[1];
url = url.replace(/&/g, '&');
url = url.replace(/\/\//g, '/');
return { url: DOWNLOAD_URL_BASE + url };
}
function getOperaVersionInfo(url, retry) {
var params = {
followRedirects: false
};
if (!url) {
return { errMsg: 'Can\'t find download URL' };
}
var response = UrlFetchApp.fetch(url, params);
var headers = response.getHeaders();
var code = response.getResponseCode();
var location = headers.Location;
// HTTP ステータスコードが 3xx である場合、成功
if (String(code).charAt(0) !== '3') {
return { errMsg: 'Can\'t fetch a download page' }; // 失敗
}
// 転送先 (ダウンロード URL) が見つからない場合
if (!location) {
return { errMsg: 'Can\'t find *.exe url' }; // 失敗
}
// バージョンを取得
var matches = location.match(VERSION_REGEX);
if (!matches || !matches[1]) {
if (retry < 3) {
return getOperaVersionInfo(location, retry + 1);
}
return { errMsg: 'Can\'t find Opera version' };
}
var version = matches[1];
return {
version: version,
downloadUrl: location
};
}
function insertRow(rowContents) {
try {
var ss = SpreadsheetApp.openById(SPRED_SHEET_ID);
var sheet = ss.getActiveSheet();
sheet.appendRow(rowContents);
return null;
}
catch (e) {
return e;
}
}
function render(data, format) {
if (!('errMsg' in data)) {
data.errMsg = null;
}
data.isSucceeded = !data.errMsg;
if (format === 'xml') {
return createXmlOutput(data);
}
return createJsonOutput(data);
}
function createJsonOutput(data) {
var mime = ContentService.MimeType.JSON;
var output = ContentService.createTextOutput(JSON.stringify(data));
output.setMimeType(mime);
return output;
}
function createXmlOutput(data) {
var mime = ContentService.MimeType.XML;
var output = ContentService.createTextOutput(xmlStringify(data));
output.setMimeType(mime);
return output;
}