forked from flow2000/news
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
234 lines (224 loc) · 8.41 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
//时间位移,0表示今天,1表示昨天,以此类推
let offset = 0
//获取新闻API,可根据部署域名更换
let NEWS_API = "https://news.aqcoder.cn/60s"
//壁纸API
let BINGAPI = "https://news.aqcoder.cn/bing"
//微博热搜API
let WEIBOAPI = "https://news.aqcoder.cn/weibo"
//B站热搜API
let BILIAPI = "https://news.aqcoder.cn/bili"
//js入口
get_day_news(offset);
//获取新闻
function get_day_news(offset) {
NProgress.start();
// 获取壁纸
axios.get(`${BINGAPI}`)
.then(function (response) {
bing_list = response.data.data
//加载壁纸
document.getElementById('bing').src = bing_list[offset].url
})
.catch(function (error) {
Notiflix.Notify.failure(`获取壁纸数据失败\uD83D\uDE1E,请点击跳转至问题反馈`, function () {
window.open("https://github.com/flow2000/news/issues/new")
});
NProgress.done()
console.log(error);
});
axios.get(`${NEWS_API}?offset=${offset}`)
.then(function (response) {
load_day_news(response.data)
//保存当前时间(12月24日,农历腊月初二,星期六!)
if (offset === 0) {
localStorage.setItem('current_time', response.data.data.time)
}
})
.catch(function (error) {
Notiflix.Notify.failure(`获取壁纸数据失败\uD83D\uDE1E,请点击跳转至问题反馈`, function () {
window.open("https://github.com/flow2000/news/issues/new")
});
NProgress.done()
console.log(error);
});
}
//加载新闻
function load_day_news(data) {
//切换按钮文字
btn_text = document.getElementsByClassName("switch_btn")[0].innerText
try {
NProgress.done();
if (data['data']) {
data = data['data'];
// 加载标题
if (data['time'].includes('月')) {
document.getElementById('date').innerHTML = data['time'];
} else {
document.getElementById('date').innerHTML = '暂无数据';
}
// 显示通知
try {
const date_now = str_to_date(data['time']);
Notiflix.Notify.success(`${date_now} 更新成功`, {
showOnlyTheLastOne: false,
});
} catch (error) {
Notiflix.Notify.success(`更新失败`, {
showOnlyTheLastOne: false,
});
}
// 加载weiyu
if (data['weiyu'].includes('【微语】')) {
document.getElementById('weiyu').innerHTML = data['weiyu'].replace("【微语】", '');
} else {
load_yiyan()
}
// 清空原有的新闻
document.getElementById('news').innerHTML = '';
for (let i = 0; i < data['news'].length; i++) {
// 将其变成 li 并插入ol
const li = document.createElement('li');
var n = data['news'][i];
if (n[1] === '、') {
n = n.substring(2);
} else if (n[2] === '、') {
n = n.substring(3);
}
if (btn_text !== '切换至微博热搜') {
li.innerHTML = `<a href=${data['urls'][i]} target='_blank'>${n}</a>`;
} else {
li.innerHTML = n;
}
// 插入新的 li
document.getElementById('news').appendChild(li);
}
} else {
Notiflix.Notify.failure(`加载新闻失败 \uD83D\uDE1E`);
}
}
catch (error) {
Notiflix.Notify.failure(`加载新闻失败 \uD83D\uDE1E`);
}
}
// 打开新窗口
function bing_click() {
window.open(document.getElementById('bing').src.split('_1920x1080.jpg')[0] + '_UHD.jpg');
}
//后一天
function after() {
if (offset === 0) {
Notiflix.Notify.success('当前已经是最新的了');
} else {
offset -= 1;
direction = 'before';
get_day_news(offset);
}
}
//前一天
function before() {
if (offset === 4) {
Notiflix.Notify.warning('之后没有了');
} else {
offset += 1;
direction = 'after';
get_day_news(offset);
}
}
//切换新闻
function change_origin() {
current_time = localStorage.getItem("current_time")
btn_text = document.getElementsByClassName("switch_btn")[0].innerText
if (btn_text === '切换至微博热搜') {
NProgress.start();
axios.get(WEIBOAPI)
.then(function (response) {
var data = response.data
var news_data = {}
var news_title = []
var news_url = []
for (let i = 0; i < data['data'].length; i++) {
news_title.push(data['data'][i].title)
news_url.push(data['data'][i].url)
}
news_data['news'] = news_title
news_data['urls'] = news_url
news_data['time'] = current_time
news_data['topic'] = '微博热搜'
news_data['weiyu'] = ''
data['data'] = news_data
load_day_news(data)
})
.catch(function (error) {
Notiflix.Notify.failure(`微博热搜获取失败\uD83D\uDE1E,请点击跳转至问题反馈`, function () {
window.open("https://github.com/flow2000/news/issues/new")
});
NProgress.done()
console.log(error);
});
document.getElementById("news_title").innerText = '微博热搜'
document.getElementsByClassName("before_btn")[0].style.display = "none";
document.getElementsByClassName("after_btn")[0].style.display = "none";
document.getElementsByClassName("switch_btn")[0].innerText = '切换至B站热搜'
} else if (btn_text === '切换至B站热搜') {
axios.get(BILIAPI)
.then(function (response) {
var data = response.data
var news_data = {}
var news_title = []
var news_url = []
for (let i = 0; i < data['data'].length; i++) {
news_title.push(data['data'][i].title)
news_url.push(data['data'][i].url)
}
news_data['news'] = news_title
news_data['urls'] = news_url
news_data['time'] = current_time
news_data['topic'] = 'B站热搜'
news_data['weiyu'] = ''
data['data'] = news_data
load_day_news(data)
})
.catch(function (error) {
Notiflix.Notify.failure(`B站热搜获取失败\uD83D\uDE1E,请点击跳转至问题反馈`, function () {
window.open("https://github.com/flow2000/news/issues/new")
});
NProgress.done()
console.log(error);
});
document.getElementById("news_title").innerText = 'B站热搜'
document.getElementsByClassName("before_btn")[0].style.display = "none";
document.getElementsByClassName("after_btn")[0].style.display = "none";
document.getElementsByClassName("switch_btn")[0].innerText = '切换至每日早报'
} else if (btn_text === '切换至每日早报') {
location.reload()
}
}
//加载一言
function load_yiyan() {
axios.get('https://v1.hitokoto.cn/?c=k')
.then(function (response) {
data = response.data
document.getElementById('weiyu').innerHTML = response.data['hitokoto'];
})
.catch(function (error) {
Notiflix.Notify.failure(`获取一言失败 \uD83D\uDE1E`);
console.log(error);
});
}
//字符串格式的日期格式化'yyyy-mm-dd'或者'mm-dd'
function str_to_date(str) {
const dateRegex = /(\d{4})年?(\d{1,2})月(\d{1,2})日?/;
const matches = str.match(dateRegex);
if (matches) {
return `${matches[1]}-${matches[2]}-${matches[3]}`;
} else {
// 如果没有年份,则默认为今年
const year = new Date().getFullYear();
const matches = str.match(/(\d{1,2})月(\d{1,2})日?/);
if (matches) {
return `${year}-${matches[1]}-${matches[2]}`;
}
}
return str;
}