-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathNASA.js
123 lines (119 loc) · 2.88 KB
/
NASA.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
/**
* Author: evilbutcher
* Github: https://github.com/evilbutcher
**/
const $ = require("Config");
const ERR = MYERR();
!(async () => {
if (init() == true) {
var imglink = await getinfo();
var flag = Math.floor(Math.random() * 10);
if ($.headers.statusCode == 200) {
if (flag >= 0 && flag < 2) {
console.log(`${flag} 展示备用图片1`);
var cover = $.thisimglink;
} else if (flag >= 2 && flag < 4) {
console.log(`${flag} 展示备用图片2`);
cover = $.thisimglink2;
} else {
console.log(`${flag} 展示NASA图片`);
cover = imglink[0];
var title = imglink[1];
console.log(title);
}
} else {
if (flag >= 0 && flag < 5) {
console.log(`${flag} 展示备用图片1`);
cover = $.thisimglink;
} else {
console.log(`${flag} 展示备用图片2`);
cover = $.thisimglink2;
}
}
$widget.setTimeline(ctx => {
return {
type: "zstack",
views: [
{
type: "image",
props: {
uri: cover,
resizable: true,
scaledToFill: true,
clipped: true,
widgetURL: cover
}
},
{
type: "text",
props: {
text: title,
font: {
name: "Cochin-Italic",
size: 20
},
color: $color("white"),
lineLimit: 1
}
}
]
};
});
}
})().catch(err => {
if (err instanceof ERR.TokenError) {
$push.schedule({
title: "NASA - Config配置错误❌",
body: err.message
});
} else if (err instanceof ERR.RequestError) {
console.log("NASA - 网络错误❌" + err.message);
} else {
$push.schedule({
title: "NASA - 出现错误❌",
body: JSON.stringify(err, Object.getOwnPropertyNames(err))
});
}
});
function init() {
try {
$.thisapikey = $.nasaapi();
$.thisimglink = $.imglink();
$.thisimglink2 = $.imglink2();
return true;
} catch (err) {
throw new ERR.TokenError("配置文件中未找到NASA API或备用图片地址");
}
}
function MYERR() {
class TokenError extends Error {
constructor(message) {
super(message);
this.name = "TokenError";
}
}
class RequestError extends Error {
constructor(message) {
super(message);
this.name = "RequestError";
}
}
return {
TokenError,
RequestError
};
}
async function getinfo() {
const url = `https://api.nasa.gov/planetary/apod?api_key=${$.thisapikey}`;
var resp = await $http.get({
url: url,
timeout: 8
});
$.headers = resp.response;
if ($.headers == null) {
throw new ERR.RequestError("网络超时获取失败");
}
var cover = resp.data.url;
var title = resp.data.title;
return [cover, title];
}