forked from visioncan/hexo-tag-flickr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
195 lines (161 loc) · 4.83 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
/* global hexo */
/* eslint prefer-promise-reject-errors: 0*/
'use strict';
const https = require('https');
const Promise = require('bluebird');
const hexoUtil = require('hexo-util');
const tagUtil = require('./flickrTagUtil');
const APIKey = hexo.config.flickr.api_key || false;
const DefaultSize = hexo.config.flickr.default_size || '-'; // default size is medium
const LinkToSource = hexo.config.flickr.linkto_source || true;
// Enable srcset
const UseSrcset = hexo.config.flickr.use_srcset || true;
// use hexo-fs
const fs = require('hexo-fs');
let cacheJson = [];
// option
const cacheFilePath = hexo.config.flickr.cache_file_path || false;
// default cache period is 24h, to comply with flickr TOS
let cachePeriod = hexo.config.flickr.cache_expires || '86400000';
if (cachePeriod) cachePeriod = Number(cachePeriod);
const enabledCache = cacheFilePath && cachePeriod;
// load cache file
if (cacheFilePath && fs.existsSync(cacheFilePath)) {
cacheJson = fs.readFileSync(cacheFilePath);
cacheJson = JSON.parse(cacheJson);
}
// get cache flickr photos json
function getFlickrCacheJson(photoId) {
if (!enabledCache) return null;
const d = new Date();
for (let i = 0; i < cacheJson.length; i++) {
if (cacheJson[i].fl2.id === photoId) {
if (cacheJson[i].expires > d.getTime()) {
return cacheJson[i].fl2;
}
break;
}
}
return null;
}
// push flickr photos
function pushImageSizeAndExpress_flickrJson(image, photo_id) {
if (!enabledCache) return null;
const d = new Date();
const expiresTime = d.getTime() + cachePeriod;
let isMatch = false;
for (let i = 0; i < cacheJson.length; i++) {
if (cacheJson[i].fl2.id === photo_id) {
cacheJson[i].fl2.img = image;
cacheJson[i].expires = expiresTime;
isMatch = true;
}
}
if (!isMatch)cacheJson.push({'fl2': {'id': photo_id, 'img': image}, 'expires': expiresTime });
}
/**
* promise Flickr API request
* @param {Array} tagArgs Tag args ex: ['15905712665', 'z']
* @resolve {Object} image attrs
*/
const promiseRequest = function(tagArgs) {
if (!APIKey) {
throw new Error('flickr.api_key configuration is required');
}
const tag = tagUtil.convertAttr(tagArgs, DefaultSize, UseSrcset);
return new Promise((resolve, reject) => {
const flJson = getFlickrCacheJson(tag.id);
if (!flJson || !flJson.photo.imgSize) {
const 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, res => {
let data = '';
res.on('data', chunk => {
data += chunk;
});
res.on('end', () => {
const json = JSON.parse(data);
if (json.stat === 'ok') {
const img = tagUtil.getImage(json, tag.size);
pushImageSizeAndExpress_flickrJson(img, tag.id);
resolve(tagUtil.imgFormat(tag, img));
} else {
return reject('Flickr Tag Error: ' + tag.id + ' ' + json.message);
}
});
}).on('error', e => {
return reject('Fetch Flickr API error: ' + e);
});
} else {
return resolve(tagUtil.imgFormat(tag, flJson));
}
});
};
/**
* Flickr tag
*
* Syntax:
* ```
* {% flickr [class1,class2,classN] photo_id [size] %}
* ```
*/
hexo.extend.tag.register('flickr', (args, _) => {
return promiseRequest(args).then(imgAttr_internal => {
const photoId = imgAttr_internal.photoId;
delete imgAttr_internal.photoId;
const img = hexoUtil.htmlTag('img', imgAttr_internal);
if (LinkToSource) {
return '<a href=\'//flic.kr/p/' + tagUtil.toBase58(photoId) + '/sizes/l\' target=\'_blank\' rel=\'noopener noreferrer\'>' + img + '</a>';
}
return img;
}, err => {
hexo.log.error(err);
});
}, {async: true});
// write cache file
hexo.extend.filter.register('after_generate', () => {
if (enabledCache) {
fs.writeFileSync(cacheFilePath, JSON.stringify(cacheJson));
}
});
hexo.extend.filter.register('after_clean', () => {
if (enabledCache) {
return fs.exists(cacheFilePath).then(exist => {
if (!exist) return;
return fs.unlink(cacheFilePath).then(() => {
hexo.log.debug('Deleted flickr cache.');
});
});
}
});
/**
* For gallery post
*
* Syntax:
* ```
* photos:
* - flickr photo_id [size]
* - flickr photo_id [size]
* ```
*/
hexo.extend.filter.register('pre', data => {
if (!data.photos) return data;
return Promise.map(data.photos, photo => {
const photoTag = photo.split(' ');
if (photoTag[0] !== 'flickr') {
return photo;
}
const tagArgs = photoTag.slice(1);
return promiseRequest(tagArgs).then(imgAttr => {
return imgAttr.src;
}, err => {
hexo.log.error(err);
});
}).then(results => {
data.photos = results;
return data;
});
});