-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
112 lines (102 loc) · 3.15 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
import fs from "node:fs";
import readline from "node:readline";
import crypto from "node:crypto";
import querystring from "node:querystring";
import { URL } from "node:url";
import OpenCC from "opencc";
process.loadEnvFile();
const { CONSUMER_KEY, CONSUMER_SECRET, TOKEN, TOKEN_SECRET } = process.env;
if (!CONSUMER_KEY || !CONSUMER_SECRET) {
console.log("Please set CONSUMER_KEY and CONSUMER_SECRET in .env");
process.exit();
}
const oAuthFetch = (endpoint, param) => {
const timeStamp = Math.ceil(new Date().valueOf() / 1000).toString();
const nonce = crypto.randomInt(1000000, 9999999).toString();
const url = new URL(endpoint, "https://www.plurk.com");
const searchParams = new URLSearchParams(
Object.assign(
{
oauth_consumer_key: CONSUMER_KEY,
oauth_nonce: nonce,
oauth_signature_method: "HMAC-SHA1",
oauth_timestamp: timeStamp,
oauth_version: "1.0",
},
TOKEN && TOKEN_SECRET
? {
oauth_token: TOKEN,
oauth_token_secret: TOKEN_SECRET,
}
: {},
param,
),
);
searchParams.sort();
searchParams.append(
"oauth_signature",
crypto
.createHmac("sha1", `${CONSUMER_SECRET}&${TOKEN_SECRET || param?.oauth_token_secret || ""}`)
.update(
[
"GET",
encodeURIComponent(url),
encodeURIComponent(searchParams.toString().replace(/\+/g, "%20")),
].join("&"),
)
.digest("base64"),
);
url.search = searchParams.toString().replace(/\+/g, "%20");
return fetch(url);
};
if (!TOKEN || !TOKEN_SECRET) {
let { oauth_token, oauth_token_secret } = querystring.parse(
await oAuthFetch("/OAuth/request_token", {}).then((e) => e.text()),
);
console.log(
`Please authorize this app in https://www.plurk.com/OAuth/authorize?oauth_token=${oauth_token}`,
);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const oauth_verifier = await new Promise((resolve) =>
rl.question("Enter verification code: ", resolve),
);
rl.close();
({ oauth_token, oauth_token_secret } = querystring.parse(
await oAuthFetch("/OAuth/access_token", {
oauth_token,
oauth_token_secret,
oauth_verifier,
}).then((e) => e.text()),
));
console.log("Saving token in .env...");
fs.writeFileSync(
".env",
[
fs.readFileSync(".env", "utf8"),
`TOKEN=${oauth_token}`,
`TOKEN_SECRET=${oauth_token_secret}`,
].join("\n"),
);
const me = await oAuthFetch("/APP/Users/me", {
oauth_token,
oauth_token_secret,
}).then((e) => e.json());
console.log(`Welcome ${me.display_name}`);
process.exit();
}
const hitokoto = await fetch("https://v1.hitokoto.cn").then((res) => res.json());
const plurk = await oAuthFetch("/APP/Timeline/plurkAdd", {
content: new OpenCC("s2t.json").convertSync(`${hitokoto.hitokoto} [emo76]\n -- ${hitokoto.from}`),
qualifier: ":",
}).then((e) => e.json());
console.log(plurk);
console.log(
await oAuthFetch("/APP/Responses/responseAdd", {
plurk_id: plurk.plurk_id,
content: `https://hitokoto.cn/?id=${hitokoto.id}`,
qualifier: ":",
}).then((e) => e.json()),
);