-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
294 lines (241 loc) · 7.23 KB
/
index.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* global hexo */
/* eslint prefer-promise-reject-errors: 0*/
'use strict';
var https = require('https');
var Promise = require('bluebird');
var hexoUtil = require('hexo-util');
var tagUtil = require('./flickrTagUtil');
var APIKey = hexo.config.flickr_api_key || false;
// use hexo-fs
var fs = require('hexo-fs');
var cacheJson = [];
// option
var cacheFilePath = hexo.config.flickr_cache_file_path || false;
var cachePeriod = hexo.config.flickr_cache_expires || false;
var enabledCache = !(!cacheFilePath && !cachePeriod);
if (!cachePeriod)cachePeriod = Number(cachePeriod);
// load cache file
if (enabledCache && fs.existsSync(cacheFilePath)) {
cacheJson = fs.readFileSync(cacheFilePath);
cacheJson = JSON.parse(cacheJson);
}
// return used element
function filterElement(flJson) {
var returnJson = {'photo': {}};
returnJson.photo.title = flJson.photo.title;
returnJson.photo.description = flJson.photo.description;
returnJson.photo.farm = flJson.photo.farm;
returnJson.photo.id = flJson.photo.id;
returnJson.photo.media = flJson.photo.media;
returnJson.photo.secret = flJson.photo.secret;
returnJson.photo.server = flJson.photo.server;
returnJson.photo.urls = flJson.photo.urls;
return returnJson;
}
// push flickr photos
function pushFlickrJson(flickrJson) {
if (!enabledCache) return;
var isMatch = false;
var filterJson = filterElement(flickrJson);
for (var i = 0; i < cacheJson.length; i++) {
if (cacheJson[i].fl.photo.id === flickrJson.photo.id) {
cacheJson[i].fl = filterJson;
isMatch = true;
}
}
if (!isMatch)cacheJson.push({'fl': filterJson, 'expires': 0 });
}
// get cache flickr photos json
function getFlickrCacheJson(photoId) {
if (!enabledCache) return null;
var d = new Date();
for (var i = 0; i < cacheJson.length; i++) {
if (cacheJson[i].fl.photo.id === photoId) {
if (cacheJson[i].expires > d.getTime()) {
return cacheJson[i].fl;
}
break;
}
}
return null;
}
// get image size
function getImageSize(flickrJson, photo_size) {
var sizeInfo = {'width': 0, 'height': 0};
var sizeTable = {
's': 'Square',
'q': 'Large Square',
't': 'Thumbnail',
'm': 'Small',
'n': 'Small 320',
'-': 'Medium',
'z': 'Medium 640',
'c': 'Medium 800',
'b': 'Large',
'o': 'Original'
};
if (flickrJson && flickrJson.sizes.size) {
for (var i = 0; i < flickrJson.sizes.size.length; i++) {
if (flickrJson.sizes.size[i].label === sizeTable[photo_size]) {
sizeInfo.width = flickrJson.sizes.size[i].width;
sizeInfo.height = flickrJson.sizes.size[i].height;
}
}
}
return sizeInfo;
}
// push flickr photos
function pushImageSizeAndExpress_flickrJson(imageSize, photo_id) {
if (!enabledCache) return null;
var d = new Date();
var expiresTime = d.getTime() + cachePeriod;
for (var i = 0; i < cacheJson.length; i++) {
if (cacheJson[i].fl.photo.id === photo_id) {
cacheJson[i].fl.photo.imgSize = imageSize;
cacheJson[i].expires = expiresTime;
}
}
}
//
function addTagHeight(imgAttr, imgSize) {
var returnImgAttr = imgAttr;
returnImgAttr.width = imgSize.width;
returnImgAttr['data-height'] = imgSize.height;
return returnImgAttr;
}
/**
* promise Flickr API request
* @param {Array} tagArgs Tag args ex: ['15905712665', 'z']
* @resolve {Object} image attrs
*/
var promiseRequest = function(tagArgs) {
if (!APIKey) {
throw new Error('flickr_api_key configuration is required');
}
var tag = tagUtil.convertAttr(tagArgs);
return new Promise(function(resolve, reject) {
var flJson = getFlickrCacheJson(tag.id);
if (!flJson) {
// console.log("[api access getInfo]photoId= " + tag.id);
var url = 'https://api.flickr.com/services/rest/?method=flickr.photos.getInfo'
+ '&api_key=' + APIKey
+ '&photo_id=' + tag.id
+ '&format=json'
+ '&nojsoncallback=1';
https.get(url, function(res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
var json = JSON.parse(data);
if (json.stat === 'ok') {
pushFlickrJson(json);
resolve(tagUtil.imgFormat(tag, json));
} else {
return reject('Flickr Tag Error: ' + tag.id + ' ' + json.message);
}
});
}).on('error', function(e) {
return reject('Fetch Flickr API error: ' + e);
});
} else {
// console.log("[cache getInfo]photoId= " + tag.id);
return resolve(tagUtil.imgFormat(tag, flJson));
}
});
};
/**
* promise Flickr API request
* @param {Array} tagArgs Tag args ex: ['15905712665', 'z']
* @resolve {Object} image attrs
*/
var promiseRequest_imageSize = function(tagArgs, returnImgAttr) {
if (!APIKey) {
throw new Error('flickr_api_key configuration is required');
}
var tag = tagUtil.convertAttr(tagArgs);
return new Promise(function(resolve, reject) {
var flJson = getFlickrCacheJson(tag.id);
if (!flJson || !flJson.photo.imgSize) {
var url = 'https://api.flickr.com/services/rest/?method=flickr.photos.getSizes'
+ '&api_key=' + APIKey
+ '&photo_id=' + tag.id
+ '&format=json'
+ '&nojsoncallback=1';
https.get(url, function(res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
var json = JSON.parse(data);
if (json.stat === 'ok') {
var imgSize = getImageSize(json, tag.size);
pushImageSizeAndExpress_flickrJson(imgSize, tag.id);
resolve(addTagHeight(returnImgAttr, imgSize));
} else {
return reject('Flickr Tag Error: ' + tag.id + ' ' + json.message);
}
});
}).on('error', function(e) {
return reject('Fetch Flickr API error: ' + e);
});
} else {
return resolve(addTagHeight(tagUtil.imgFormat(tag, flJson), flJson.photo.imgSize));
}
});
};
/**
* Flickr tag
*
* Syntax:
* ```
* {% flickr [class1,class2,classN] photo_id [size] %}
* ```
*/
hexo.extend.tag.register('flickr', function(args, content) {
return promiseRequest(args).then(function(imgAttr) {
return promiseRequest_imageSize(args, imgAttr).then(function(imgAttr_internal) {
return hexoUtil.htmlTag('img', imgAttr_internal);
}, function(err) {
hexo.log.error(err);
});
}, function(err) {
hexo.log.error(err);
});
}, {async: true});
// write cache file
hexo.extend.filter.register('after_generate', function() {
if (enabledCache) {
fs.writeFileSync(cacheFilePath, JSON.stringify(cacheJson));
}
});
/**
* For gallery post
*
* Syntax:
* ```
* photos:
* - flickr photo_id [size]
* - flickr photo_id [size]
* ```
*/
hexo.extend.filter.register('pre', function(data) {
if (!data.photos) return data;
return Promise.map(data.photos, function(photo) {
var photoTag = photo.split(' ');
if (photoTag[0] !== 'flickr') {
return photo;
}
var tagArgs = photoTag.slice(1);
return promiseRequest(tagArgs).then(function(imgAttr) {
return imgAttr.src;
}, function(err) {
hexo.log.error(err);
});
}).then(function(results) {
data.photos = results;
return data;
});
});