-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.gs
More file actions
124 lines (116 loc) · 4.32 KB
/
main.gs
File metadata and controls
124 lines (116 loc) · 4.32 KB
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
/*
Copyright 2021 YanhuiJessica
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var shinningLatest, shinningLastDate;
var infinityLatest, infinityLastDate;
var OPENAI_API_KEY = '<openai-api-key>';
var OPENAI_BASE_URL = '<openai-base-url>';
var RSS_BASE_URL = '<rss-base-url>';
function send(payload) {
if (payload) {
let data = {
"method": "post",
"payload": payload,
"muteHttpExceptions": true
}
response = UrlFetchApp.fetch("https://api.telegram.org/bot<bot-token>/", data);
Logger.log(response.getContentText());
}
}
function getItems(uid) {
let feed = UrlFetchApp.fetch(RSS_BASE_URL + uid).getContentText();
let doc = XmlService.parse(feed);
let root = doc.getRootElement();
let channel = root.getChild('channel');
let items = channel.getChildren('item');
return items;
}
function textProcess(item, lastDate, isShinningNikki) {
let date_string = item.getChildText('pubDate');
let date = new Date(date_string);
if (lastDate && date <= lastDate) return null;
let description = item.getChildText('description');
let forward = description.indexOf('<blockquote');
if (forward != -1) description = description.slice(0, forward);
let msg = "";
let codereg = new RegExp("<[^>]+>", "g");
description = description.replace(codereg, ' ');
if (description.indexOf("兑换码") != -1) {
let response = UrlFetchApp.fetch(OPENAI_BASE_URL + 'v1/responses', {
'method': 'post',
'headers': {
'authorization': 'Bearer ' + OPENAI_API_KEY,
},
'contentType': 'application/json',
'payload': JSON.stringify({
'model': 'gpt-5-mini',
'input': '请提取以下文本中的兑换码并直接输出对应兑换码(多个兑换码间用英文逗号分隔),若无则查看是否有其它获取方式并直接输出对应获取方法(忽略直播间掉落的兑换码预告),若无获取方式请输出空字符串:\n' + description,
'reasoning': {
'effort': 'low'
},
'text': {
'verbosity': 'low'
}
})
});
msg = JSON.parse(response.getContentText())['output'][1]['content'][0]['text'];
}
if (isShinningNikki && date > shinningLatest) {
PropertiesService.getScriptProperties().setProperty('shinningLastDate', date_string);
shinningLatest = date;
}
else if (!isShinningNikki && date > infinityLatest) {
PropertiesService.getScriptProperties().setProperty('infinityLastDate', date_string);
infinityLatest = date;
}
return msg.replace(/\n/g, '').replace(/,/g, '\n').replace(/"/g, '');
}
function main() {
// shinning nikki
shinningLastDate = new Date(PropertiesService.getScriptProperties().getProperty('shinningLastDate'));
shinningLatest = shinningLastDate;
let targets = ['6498105282', '7840676854', '7521490767'];
let items = getItems(targets[0]);
for (let i = 1; i < targets.length; i ++) {
items = items.concat(getItems(targets[i]));
}
let shinningMap = new Map();
for (let i in items) {
let msg = textProcess(items[i], shinningLastDate, true);
if(msg && !shinningMap.has(msg) && shinningMap.set(msg, 1)) {
send({
"method": "sendMessage",
"chat_id": "@shinning_nikki_weibo_code",
"text": msg,
});
}
}
// infinity nikki
infinityLastDate = new Date(PropertiesService.getScriptProperties().getProperty('infinityLastDate'));
infinityLatest = infinityLastDate;
targets = ['7801655101', '7915828567', '7915670982'];
items = getItems(targets[0]);
for (let i = 1; i < targets.length; i ++) {
items = items.concat(getItems(targets[i]));
}
let infinityMap = new Map();
for (let i in items) {
let msg = textProcess(items[i], infinityLastDate, false);
if(msg && !infinityMap.has(msg) && infinityMap.set(msg, 1)) {
send({
"method": "sendMessage",
"chat_id": "@infinity_nikki_weibo_code",
"text": msg,
});
}
}
}