-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
99 lines (87 loc) · 2.84 KB
/
index.ts
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
import axios from 'axios';
import { JSDOM } from 'jsdom';
import dayjs from 'dayjs';
import { Question } from './type';
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
import initMDBClient from '@elevenback/mdb-client';
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.tz.setDefault('Asia/Tokyo');
const environments: { [key: string]: string } = {
APP_COOKIE: `${process.env.APP_COOKIE}`.replace(/\n/g, ''),
APP_IFTTT_EVENT_NAME: `${process.env.APP_IFTTT_EVENT_NAME}`.replace(/\n/g, ''),
APP_IFTTT_SERVICE_KEY: `${process.env.APP_IFTTT_SERVICE_KEY}`.replace(/\n/g, ''),
APP_MINDB_ORIGIN: `${process.env.APP_MINDB_ORIGIN}`.replace(/\n/g, ''),
APP_MINDB_ATOM_ID: `${process.env.APP_MINDB_ATOM_ID}`.replace(/\n/g, ''),
APP_MINDB_TOKEN: `${process.env.APP_MINDB_TOKEN}`.replace(/\n/g, ''),
};
const mdbClient = initMDBClient({
apiOrigin: environments.APP_MINDB_ORIGIN,
token: environments.APP_MINDB_TOKEN,
})
const peingApiClient = axios.create({
withCredentials: true,
headers: {
cookie: `${environments.APP_COOKIE || ''}`,
},
});
if (!Object.values(environments).every((v) => !!v)) {
console.error('Missing Environment');
process.exit(1);
}
async function getLastUpdatedAt(): Promise<string | null> {
const data = await mdbClient.getAtomValue(environments.APP_MINDB_ATOM_ID);
return data || '';
}
async function saveLastUpdatedAt(lastUpdatedAt: string) {
await mdbClient.updateAtomValue(environments.APP_MINDB_ATOM_ID, lastUpdatedAt);
return;
}
async function run() {
try {
const response = await peingApiClient.get('https://peing.net/ja/box');
const DOM = new JSDOM(response.data);
const questionElement = DOM.window.document.body
.querySelector('[data-questions]')
?.getAttribute('data-questions');
if (!questionElement) {
console.error('Missing question list element.');
return;
}
const questions: Question[] = JSON.parse(
decodeURIComponent(questionElement),
);
const [question] = questions;
const lastUpdatedAt = await getLastUpdatedAt();
if (dayjs(question.created_at).diff(lastUpdatedAt) < 0) {
console.log('Missing questions');
return;
}
await saveLastUpdatedAt(dayjs().format());
await axios
.post(
`https://maker.ifttt.com/trigger/${environments.APP_IFTTT_EVENT_NAME}/with/key/${environments.APP_IFTTT_SERVICE_KEY}`,
{
value1: dayjs().format('YYYY/MM/DD HH:mm'),
value2: question.body,
value3: question.eye_catch.url,
},
{
headers: {
'Content-Type': 'application/json',
},
},
)
.then((r) => {
console.log(r);
})
.catch((e) => {
console.log(e.response.data.errors);
});
process.exit(0);
} catch (error) {
console.error('An error occurred:', error);
}
}
run();