-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
136 lines (112 loc) · 3.5 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
'use strict'
const fs = require('fs')
const path = require('path')
const express = require('express')
const request = require('request-promise')
const low = require('lowdb')
const storage = require('lowdb/file-sync')
const moment = require('moment-timezone')
const cdnify = require('./upload')
const app = express()
const db = low('db.json', { storage })
const NASA_KEY = '3CZtMuShxwtbGP1bLL6S8DZZVBb7r6NkK8ltECfr' // this is a personal key, please apply for one for yourself for free at https://api.nasa.gov
const CDN_HOST = 'http://7xpbf9.com2.z0.glb.qiniucdn.com/' // this is a Chinese cloud storage provider, you should apply for your own too
const IMAGE_STORAGE_PATH = path.join(__dirname, 'images')
const STATUS_CODE = {
INTERNAL_ERROR: 102
}
// let daysBefore = 0 // 暂时不缓存前一天的图片
let latestImageLocation = ''
const image = db('images').last()
if (image) {
latestImageLocation = image.url
}
try {
fs.accessSync(IMAGE_STORAGE_PATH)
} catch (e) {
fs.mkdirSync(IMAGE_STORAGE_PATH)
}
const fetch = (time) => {
const formattedTime = time.format('YYYY-MM-DD')
const url = `https://api.nasa.gov/planetary/apod?api_key=${NASA_KEY}&date=${formattedTime}`
console.log('fethcing ' + formattedTime)
return new Promise((resolve, reject) => {
request(url)
.then(html => {
const result = JSON.parse(html)
if (result.media_type === 'image') {
resolve(result.url)
} else {
reject(new Error('not image'))
}
})
.catch(err => {
reject(err)
})
})
}
const cache = (time) => {
if (!time) {
time = moment.tz('America/New_York')
}
const picName = `${moment.tz('America/New_York').format('YYYY-MM-DD')}_nasa_apod.jpg`
const pathToSave = path.join(IMAGE_STORAGE_PATH, picName)
let qiniuPicName = '';
if (latestImageLocation) {
qiniuPicName = latestImageLocation[latestImageLocation.length - 1];
}
if (picName !== qiniuPicName) {
fetch(time)
.then(url => {
// daysBefore = 0
request(url)
.pipe(fs.createWriteStream(pathToSave))
.on('finish', () => {
cdnify(picName, pathToSave)
.then(reply => {
console.log('uploaded to qiniu, key: %s, hash: %s, at %s', reply.key, reply.hash, new Date())
latestImageLocation = CDN_HOST + reply.key
db('images')
.chain()
.push({ url: latestImageLocation })
.remove(element => {
return latestImageLocation !== element.url
})
.value()
fs.unlinkSync(path.join(IMAGE_STORAGE_PATH, picName))
})
.catch(err => {
console.error('cdn error: ' + err)
})
})
})
.catch(err => {
console.error('fetch error: ' + err)
// daysBefore++
// cache(moment.tz('America/New_York').subtract(daysBefore, 'days'))
})
}
}
app.get('/apod', (req, res) => {
if (latestImageLocation) {
res.send({
type: 'ImageURL',
data: [latestImageLocation]
})
} else {
res.send({
type: 'Error',
data: [{
code: STATUS_CODE.INTERNAL_ERROR,
message: '没有获取到 NASA 太空图片'
}]
})
}
})
app.listen(3000, () => {
console.log('app listening on port 3000')
})
setInterval(() => {
cache(moment.tz('America/New_York'))
}, moment.duration(4, 'hours').as('milliseconds'))
cache(moment.tz('America/New_York'))