mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-07-02 05:17:26 +00:00
Compare commits
117 Commits
v2.1.0
...
v2.5.2-202
Author | SHA1 | Date | |
---|---|---|---|
d89711b0b9 | |||
a3376e96ee | |||
33d18cef6b | |||
7aa00ff98b | |||
e956e36584 | |||
a5fca68ef5 | |||
8edb728fe9 | |||
cc05af8a24 | |||
60fa641962 | |||
3ab373cc70 | |||
461cd246d8 | |||
f040f130d7 | |||
1f402fbbac | |||
f7f3c3beaf | |||
04c1753c22 | |||
d993e9d719 | |||
60e91987d1 | |||
65392fc2fe | |||
d38934a0f4 | |||
7943357d96 | |||
a170ff040b | |||
cb2ebfdb73 | |||
66b22c543a | |||
6bace4b048 | |||
597aac4e95 | |||
181d60285b | |||
c06b8717c3 | |||
b4a28b6735 | |||
40057c3683 | |||
4beb4d78fb | |||
d9edaa681f | |||
7b52abde60 | |||
2cb4fe1dbc | |||
5830327dad | |||
cbd10ff281 | |||
84b8006f89 | |||
d32d891ad7 | |||
aabd35ce71 | |||
a36eb713d7 | |||
f452c1c128 | |||
cb42aadb15 | |||
b969bb29b2 | |||
0d74007a98 | |||
edade24883 | |||
34f57404ca | |||
99b6e14ff7 | |||
c8fe2c3fdd | |||
49a33d4078 | |||
cd1d2316ee | |||
cf08353ed9 | |||
bef6a684b9 | |||
04960889b4 | |||
0dc8fc78b4 | |||
40c5284be2 | |||
46cb47c7d1 | |||
fe213deecb | |||
f279d99fda | |||
a09aef5be2 | |||
766411fa09 | |||
84b544cea9 | |||
2f492f5b03 | |||
15af939c3f | |||
a28cb142b4 | |||
e8fda3214b | |||
fba6d3532c | |||
0075446412 | |||
2388cd419e | |||
7f83b16118 | |||
10cad32efa | |||
22a113ef38 | |||
6824b12e8f | |||
1c556c5b94 | |||
dbc9f4c90b | |||
d3c1975722 | |||
c09b750fe6 | |||
ca479ef1af | |||
c2c49d2355 | |||
22e74e8cd5 | |||
d549c5674d | |||
c6952de84c | |||
410d6c0828 | |||
aaa1bc932b | |||
a1e54e70d9 | |||
16522155e1 | |||
5f796f7da0 | |||
5a52dd9208 | |||
b53aafa81b | |||
28aa086f15 | |||
0eadefa74f | |||
4afa414725 | |||
0f202cb076 | |||
1f3d99ac10 | |||
d33f4028d1 | |||
4020bbfea8 | |||
19605a9401 | |||
21466a49f9 | |||
73ae9a268b | |||
620c3785ad | |||
2df5513727 | |||
8798633c2c | |||
9dfc20a525 | |||
dd88f2acab | |||
36460e4c34 | |||
084be3970a | |||
11005b0f6c | |||
970be847a0 | |||
09fa1bd2e8 | |||
ae27141fea | |||
b328def8f9 | |||
dbfed874c0 | |||
f02b0e9e98 | |||
260cfe3dd0 | |||
c1427379c6 | |||
f844d150e8 | |||
2ec696b3cd | |||
c8c6dbe4fd | |||
4dff477ded |
14
.gitignore
vendored
14
.gitignore
vendored
@ -1,6 +1,16 @@
|
||||
# Ignore test date folder
|
||||
/pluginData/
|
||||
/logs/
|
||||
/.idea/
|
||||
/CGJ_2.iml
|
||||
/cookies.store
|
||||
/target/
|
||||
|
||||
# Ignore Idea files
|
||||
/.idea/
|
||||
/CGJ_2.iml
|
||||
|
||||
# Ignore Visual Studio Code files
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
/.settings/
|
||||
/.vscode/
|
||||
|
10
Dockerfile.sample
Normal file
10
Dockerfile.sample
Normal file
@ -0,0 +1,10 @@
|
||||
FROM openjdk:8u252-jre
|
||||
|
||||
ENV jarFileName=ContentGrabbingJi-exec.jar
|
||||
ENV CGJ_REDIS_URI="127.0.0.1:6379"
|
||||
ENV CGJ_PROXY=""
|
||||
RUN mkdir /data/
|
||||
|
||||
COPY ${jarFileName} /CGJ.jar
|
||||
|
||||
CMD ["java", "-Dcgj.logsPath=/data/logs", "-jar", "/CGJ.jar", "botMode", "-botDataDir=/data"]
|
165
LICENSE
Normal file
165
LICENSE
Normal file
@ -0,0 +1,165 @@
|
||||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
|
||||
This version of the GNU Lesser General Public License incorporates
|
||||
the terms and conditions of version 3 of the GNU General Public
|
||||
License, supplemented by the additional permissions listed below.
|
||||
|
||||
0. Additional Definitions.
|
||||
|
||||
As used herein, "this License" refers to version 3 of the GNU Lesser
|
||||
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
||||
General Public License.
|
||||
|
||||
"The Library" refers to a covered work governed by this License,
|
||||
other than an Application or a Combined Work as defined below.
|
||||
|
||||
An "Application" is any work that makes use of an interface provided
|
||||
by the Library, but which is not otherwise based on the Library.
|
||||
Defining a subclass of a class defined by the Library is deemed a mode
|
||||
of using an interface provided by the Library.
|
||||
|
||||
A "Combined Work" is a work produced by combining or linking an
|
||||
Application with the Library. The particular version of the Library
|
||||
with which the Combined Work was made is also called the "Linked
|
||||
Version".
|
||||
|
||||
The "Minimal Corresponding Source" for a Combined Work means the
|
||||
Corresponding Source for the Combined Work, excluding any source code
|
||||
for portions of the Combined Work that, considered in isolation, are
|
||||
based on the Application, and not on the Linked Version.
|
||||
|
||||
The "Corresponding Application Code" for a Combined Work means the
|
||||
object code and/or source code for the Application, including any data
|
||||
and utility programs needed for reproducing the Combined Work from the
|
||||
Application, but excluding the System Libraries of the Combined Work.
|
||||
|
||||
1. Exception to Section 3 of the GNU GPL.
|
||||
|
||||
You may convey a covered work under sections 3 and 4 of this License
|
||||
without being bound by section 3 of the GNU GPL.
|
||||
|
||||
2. Conveying Modified Versions.
|
||||
|
||||
If you modify a copy of the Library, and, in your modifications, a
|
||||
facility refers to a function or data to be supplied by an Application
|
||||
that uses the facility (other than as an argument passed when the
|
||||
facility is invoked), then you may convey a copy of the modified
|
||||
version:
|
||||
|
||||
a) under this License, provided that you make a good faith effort to
|
||||
ensure that, in the event an Application does not supply the
|
||||
function or data, the facility still operates, and performs
|
||||
whatever part of its purpose remains meaningful, or
|
||||
|
||||
b) under the GNU GPL, with none of the additional permissions of
|
||||
this License applicable to that copy.
|
||||
|
||||
3. Object Code Incorporating Material from Library Header Files.
|
||||
|
||||
The object code form of an Application may incorporate material from
|
||||
a header file that is part of the Library. You may convey such object
|
||||
code under terms of your choice, provided that, if the incorporated
|
||||
material is not limited to numerical parameters, data structure
|
||||
layouts and accessors, or small macros, inline functions and templates
|
||||
(ten or fewer lines in length), you do both of the following:
|
||||
|
||||
a) Give prominent notice with each copy of the object code that the
|
||||
Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the object code with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
4. Combined Works.
|
||||
|
||||
You may convey a Combined Work under terms of your choice that,
|
||||
taken together, effectively do not restrict modification of the
|
||||
portions of the Library contained in the Combined Work and reverse
|
||||
engineering for debugging such modifications, if you also do each of
|
||||
the following:
|
||||
|
||||
a) Give prominent notice with each copy of the Combined Work that
|
||||
the Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
c) For a Combined Work that displays copyright notices during
|
||||
execution, include the copyright notice for the Library among
|
||||
these notices, as well as a reference directing the user to the
|
||||
copies of the GNU GPL and this license document.
|
||||
|
||||
d) Do one of the following:
|
||||
|
||||
0) Convey the Minimal Corresponding Source under the terms of this
|
||||
License, and the Corresponding Application Code in a form
|
||||
suitable for, and under terms that permit, the user to
|
||||
recombine or relink the Application with a modified version of
|
||||
the Linked Version to produce a modified Combined Work, in the
|
||||
manner specified by section 6 of the GNU GPL for conveying
|
||||
Corresponding Source.
|
||||
|
||||
1) Use a suitable shared library mechanism for linking with the
|
||||
Library. A suitable mechanism is one that (a) uses at run time
|
||||
a copy of the Library already present on the user's computer
|
||||
system, and (b) will operate properly with a modified version
|
||||
of the Library that is interface-compatible with the Linked
|
||||
Version.
|
||||
|
||||
e) Provide Installation Information, but only if you would otherwise
|
||||
be required to provide such information under section 6 of the
|
||||
GNU GPL, and only to the extent that such information is
|
||||
necessary to install and execute a modified version of the
|
||||
Combined Work produced by recombining or relinking the
|
||||
Application with a modified version of the Linked Version. (If
|
||||
you use option 4d0, the Installation Information must accompany
|
||||
the Minimal Corresponding Source and Corresponding Application
|
||||
Code. If you use option 4d1, you must provide the Installation
|
||||
Information in the manner specified by section 6 of the GNU GPL
|
||||
for conveying Corresponding Source.)
|
||||
|
||||
5. Combined Libraries.
|
||||
|
||||
You may place library facilities that are a work based on the
|
||||
Library side by side in a single library together with other library
|
||||
facilities that are not Applications and are not covered by this
|
||||
License, and convey such a combined library under terms of your
|
||||
choice, if you do both of the following:
|
||||
|
||||
a) Accompany the combined library with a copy of the same work based
|
||||
on the Library, uncombined with any other library facilities,
|
||||
conveyed under the terms of this License.
|
||||
|
||||
b) Give prominent notice with the combined library that part of it
|
||||
is a work based on the Library, and explaining where to find the
|
||||
accompanying uncombined form of the same work.
|
||||
|
||||
6. Revised Versions of the GNU Lesser General Public License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions
|
||||
of the GNU Lesser General Public License from time to time. Such new
|
||||
versions will be similar in spirit to the present version, but may
|
||||
differ in detail to address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Library as you received it specifies that a certain numbered version
|
||||
of the GNU Lesser General Public License "or any later version"
|
||||
applies to it, you have the option of following the terms and
|
||||
conditions either of that published version or of any later version
|
||||
published by the Free Software Foundation. If the Library as you
|
||||
received it does not specify a version number of the GNU Lesser
|
||||
General Public License, you may choose any version of the GNU Lesser
|
||||
General Public License ever published by the Free Software Foundation.
|
||||
|
||||
If the Library as you received it specifies that a proxy can decide
|
||||
whether future versions of the GNU Lesser General Public License shall
|
||||
apply, that proxy's public statement of acceptance of any version is
|
||||
permanent authorization for you to choose that version for the
|
||||
Library.
|
35
README.md
Normal file
35
README.md
Normal file
@ -0,0 +1,35 @@
|
||||
# ContentGrabbingJi
|
||||
Pixiv爬虫一只,同时也是一个机器人/插件!
|
||||
|
||||
## 支持的机器人平台 ##
|
||||
- [Mirai](https://github.com/mamoe/mirai)
|
||||
- [CoolQ](https://cqp.cc)(基于`SpringCQ`, 不支持多账号使用, 需要使用`CQHttp`插件)
|
||||
|
||||
## Usage ##
|
||||
|
||||
> 注意: 运行色图姬前, 你需要准备一个Pixiv账号的会话Cookie存储文件, 否则色图姬将无法运行.
|
||||
|
||||
### Arguments ###
|
||||
- 通用参数
|
||||
- `proxy` / `CGJ_PROXY`: 设置代理
|
||||
- 格式: `协议://地址:端口`
|
||||
- 示例: `socks5://127.0.0.1:1080`
|
||||
- 机器人参数
|
||||
- `botDataDir` / `CGJ_BOT_DATA_DIR`: 设置`botMode`运行模式下机器人数据存储目录
|
||||
- 格式: `路径`
|
||||
- 示例: `./data`
|
||||
- 默认: `./`
|
||||
- `redisAddress` / `CGJ_REDIS_URI`: Redis服务器地址
|
||||
- 格式: `地址:端口`
|
||||
- 示例: `127.0.0.1:6379`
|
||||
|
||||
### Commands ###
|
||||
- `pluginMode`: CoolQ插件模式
|
||||
- `botMode`: Mirai独立模式
|
||||
- `collectionDownload`: 收藏下载, 以原图画质下载Cookie所属账号的所有收藏作品
|
||||
- `getRecommends`: 将访问主页获得的推荐作品全部以原图画质下载
|
||||
- `getRankingIllust`: 以原图画质下载指定排行榜类型的全部作品
|
||||
- `search`: 搜索指定内容并获取相关作品信息(不下载)
|
||||
|
||||
|
||||
|
18
documents/interfaces/Pixiv接口标准返回格式.md
Normal file
18
documents/interfaces/Pixiv接口标准返回格式.md
Normal file
@ -0,0 +1,18 @@
|
||||
## Pixiv接口标准返回格式 ##
|
||||
Pixiv大部分接口在返回数据时都会遵循以下格式:
|
||||
```json
|
||||
{
|
||||
"error": false,
|
||||
"message": "",
|
||||
"body": {
|
||||
|
||||
}
|
||||
}
|
||||
```
|
||||
大部分是如此(部分接口比较特别, 不是这个格式)
|
||||
|
||||
属性|类型|说明
|
||||
---|---|---
|
||||
error|Boolean|如果接口返回错误信息, 该属性为`true`
|
||||
message|String|如果`error`属性为`true`, 则该属性不为空字符串
|
||||
body|Object/Array|如果`error`不为`true`, 该属性有数据, 否则属性可能不存在
|
16
documents/interfaces/Pixiv搜索接口.md
Normal file
16
documents/interfaces/Pixiv搜索接口.md
Normal file
@ -0,0 +1,16 @@
|
||||
## Pixiv内容搜索接口 ##
|
||||
接口地址:
|
||||
```
|
||||
GET https://www.pixiv.net/ajax/search/{Type}/{Content}?{参数...}
|
||||
```
|
||||
变量名:
|
||||
- `Type`: 内容类型
|
||||
- illustrations(插画)
|
||||
- top(推荐)
|
||||
- manga(漫画)
|
||||
- novels(小说)
|
||||
- `Content`: 搜索内容
|
||||
|
||||
|
||||
参数列表:
|
||||
- `word`
|
127
documents/interfaces/Pixiv首页数据接口.md
Normal file
127
documents/interfaces/Pixiv首页数据接口.md
Normal file
@ -0,0 +1,127 @@
|
||||
## Pixiv首页数据接口 ##
|
||||
|
||||
说明:
|
||||
该接口涉及用户账户隐私, 不要尝试对该接口返回数据做不安全云端存储, 或未经用户允许的发送出去.
|
||||
|
||||
接口地址:
|
||||
```
|
||||
GET https://www.pixiv.net/ajax/top/{type}?mode={mode}&lang={lang}
|
||||
```
|
||||
|
||||
参数:
|
||||
- `type`: 首页类型
|
||||
- `illust`: 插画
|
||||
- `manga`: 漫画
|
||||
- `novel`: 小说
|
||||
- `mode`: 内容类型
|
||||
- `all`: 不限类型
|
||||
- `r18`: 成人内容
|
||||
- `lang`: 语言(只写几个)
|
||||
- `zh`: 中文
|
||||
|
||||
> 是否需要登录: `是`
|
||||
> 是否为Pixiv接口标准返回格式: `是`
|
||||
> 是否需要Referer请求头: `未知`
|
||||
|
||||
请求Url示例:
|
||||
```
|
||||
GET https://www.pixiv.net/ajax/top/illust?mode=all&lang=zh
|
||||
```
|
||||
|
||||
响应示例:
|
||||
```
|
||||
(内容过长, 略)
|
||||
```
|
||||
|
||||
返回内容(Json):
|
||||
- `page`: 网页相关内容
|
||||
- `tags`: (`Object[]`) 热门标签
|
||||
- `tag`: (`String`) 标签名
|
||||
- `count`: (`Number`) 作品数量?
|
||||
- `lev`: (`Number`) 不明?
|
||||
- `ids`: (`Number[]`) 作品数组
|
||||
- `follow`: (`Number[]`) 已关注作者的作品推荐
|
||||
- `mypixiv`: (`?[]`) 不明
|
||||
- `recommend`: (`String[]` -> `Number[]`) 推荐作品的Id?
|
||||
- `recommendByTag`: (`Object[]`) 标签的推荐作品
|
||||
- `tag`: (`String`) 标签名
|
||||
- `ids`: ((`String[]` -> `Number[]`) 作品Id数组
|
||||
- `ranking`: (`Object`) 排行榜前100名数据
|
||||
- `date`: (`String`) 排行榜日期(`yyyyMMdd`)
|
||||
- `items`: (`Object[]`) 排行榜简略数据
|
||||
- `rank`: (`String` -> `Number`) 排行榜名次
|
||||
- `id`: (`String` -> `Number`) 作品Id
|
||||
- `pixivision`: (`Object[]`) Pixiv的推荐文章
|
||||
- `id`: (`String` -> `Number`) 文章Id
|
||||
- `title`: (`String`) 文章标题
|
||||
- `thumbnailUrl`: (`String`) 文章封面图的Url地址
|
||||
- `abtestId`: (`String` -> `Number`) pixivision文章地址的参数`utm_content`的值
|
||||
- `recommendUser`: (`Object[]`) 推荐用户及其作品
|
||||
- `id`: (`Number`) 用户Id
|
||||
- `illustIds`: (`String[]` -> `Number[]`) 插画作品Id
|
||||
- `novelIds`: (`String[]` -> `Number[]`) 小说作品Id
|
||||
- `contestOngoing`: (`Object[]`) 进行中的比赛活动(没找到更多信息了, 先这么说着)
|
||||
- (待完善)
|
||||
- `contestResult`: (`Object[]`) 比赛结果信息
|
||||
- `slug`: (`String`) 比赛代号?
|
||||
- `type`: (`String`) 比赛作品类型?
|
||||
- `name`: (`String`) 比赛名
|
||||
- `url`: (`String`) 结果公布链接
|
||||
- `iconUrl`: (`String`) 图标链接(不明意义的图标)
|
||||
- `editorRecommend`: (`Object[]`) 编辑推荐(小说), 后续可能会删除, 这个应该是配合活动出的数据
|
||||
- (待完善)
|
||||
- `boothFollowItemIds`: (`String[]` -> `Number[]`) 已关注用户的最新商品
|
||||
- `sketchLiveFollowIds`: (`?[]`) 不明?
|
||||
- `sketchLivePopularIds`: (`String[]` -> `Number[]`) 不明?
|
||||
- `myFavoriteTags`: (`?[]`) 关注的标签
|
||||
- `newPost`: (`String[]` -> `Number[]`) 不明?
|
||||
- `thumbnails`: (`Object`) 已关注用户的作品
|
||||
- `illust`: (`Object[]`) 插画作品
|
||||
- `illustId`: (`String` -> `Number`) 作品Id(或者准确来讲是 插画Id?)
|
||||
- `illustTitle`: (`String`) 插画标题
|
||||
- `id`: (`String` -> `Number`) 作品Id
|
||||
- `title`: (`String`) 作品标题
|
||||
- `illustType`: (`Number`) 作品类型
|
||||
- `xRestrict`: (`Number`) 不明?
|
||||
- `restrict`: (`Number`) 不明?
|
||||
- `sl`: (`Number`) 不明?
|
||||
- `url`: (`String`) 作品在主页的封面图Url
|
||||
- `description`: (`String`) 作品说明?
|
||||
- `tags`: (`String[]`) 标签原始名数组(不带翻译的原始名称)
|
||||
- `userId`: (`String` -> `Number`) 用户Id
|
||||
- `userName`: (`String`) 用户名
|
||||
- `width`: (`Number`) 作品宽度
|
||||
- `height`: (`Number`) 作品高度
|
||||
- `pageCount`: (`Number`) 作品页数
|
||||
- `isBookmarkable`: (`Boolean`) 不明?
|
||||
- `bookmarkData`: (`?`) 不明?
|
||||
- `alt`: (`String`) 简略信息
|
||||
- `isAdContainer`: (`Boolean`) 广告标识?
|
||||
- `titleCaptionTranslation`: (`Object`) 不明?
|
||||
- (待完善)
|
||||
- `urls`: (`Object`) 其他封面图尺寸的链接
|
||||
- (略)
|
||||
- `seriesId`: (`?`) 不明?
|
||||
- `seriesTitle`: (`?`) 不明?
|
||||
- `profileImageUrl`: (`String`) 用户头像图链接
|
||||
- `users`: (`Object[]`) 用户信?
|
||||
- `userId`: (`String` -> `Number`) 用户Id
|
||||
- `name`: (`String`) 用户名
|
||||
- `image`: (`String`) 用户头像图链接
|
||||
- `imageBig`: (`String`) 用户大尺寸头像图链接
|
||||
- `premium`: (`Boolean`) 是否为Pixiv高级会员
|
||||
- `isFollowed`: (`Boolean`) 是否关注
|
||||
- `isMypixiv`: (`Boolean`) 不明?
|
||||
- `isBlocking`: (`Boolean`) 是否为黑名单?
|
||||
- `background`: (`?`) 不明?
|
||||
- `partial`: (`Number`) 不明?
|
||||
- `tagTranslation`: 标签翻译名
|
||||
- `key=${标签原始名}`: (`Object`) 标签翻译信息
|
||||
- `${语言代码}`: (`String`) 对应语言代码的翻译, 不一定有
|
||||
- `boothItems`: (`Object[]`) 商品信息
|
||||
- (待完善)
|
||||
- `sketchLives`: (`Object[]`) 不明?
|
||||
- (待完善)
|
||||
- `zoneConfig`: (`Object`) 不明?
|
||||
- (待完善)
|
||||
|
42
pom.xml
42
pom.xml
@ -4,9 +4,9 @@
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>CGJ_2</artifactId>
|
||||
<version>2.1.0</version>
|
||||
<groupId>net.lamgc</groupId>
|
||||
<artifactId>ContentGrabbingJi</artifactId>
|
||||
<version>2.5.2-20200522.1-SNAPSHOT</version>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
@ -19,7 +19,7 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
<mirai.CoreVersion>0.32.0</mirai.CoreVersion>
|
||||
<mirai.CoreVersion>1.0-RC2-1</mirai.CoreVersion>
|
||||
<mirai.JaptVersion>1.1.1</mirai.JaptVersion>
|
||||
<kotlin.version>1.3.71</kotlin.version>
|
||||
<ktor.version>1.3.2</ktor.version>
|
||||
@ -30,14 +30,26 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<configuration>
|
||||
<source>8</source>
|
||||
<target>8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>log4j2-test.xml</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>2.2.6.RELEASE</version>
|
||||
<configuration>
|
||||
<mainClass>net.lamgc.cgj.Main</mainClass>
|
||||
<classifier>exec</classifier>
|
||||
@ -50,20 +62,6 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-maven-plugin</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>compile</id>
|
||||
<phase>process-sources</phase>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
@ -88,7 +86,7 @@
|
||||
<dependency>
|
||||
<groupId>net.lamgc</groupId>
|
||||
<artifactId>java-utils</artifactId>
|
||||
<version>1.1.0_5-SNAPSHOT</version>
|
||||
<version>1.2.0_20200514.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
@ -153,12 +151,12 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.mamoe</groupId>
|
||||
<artifactId>mirai-core-jvm</artifactId>
|
||||
<artifactId>mirai-core</artifactId>
|
||||
<version>${mirai.CoreVersion}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.mamoe</groupId>
|
||||
<artifactId>mirai-core-qqandroid-jvm</artifactId>
|
||||
<artifactId>mirai-core-qqandroid</artifactId>
|
||||
<version>${mirai.CoreVersion}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@ -174,7 +172,7 @@
|
||||
<dependency>
|
||||
<groupId>net.lamgc</groupId>
|
||||
<artifactId>PixivLoginProxyServer</artifactId>
|
||||
<version>1.1.0</version>
|
||||
<version>1.1.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup</groupId>
|
||||
|
@ -7,10 +7,11 @@ import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import net.lamgc.cgj.bot.CQConfig;
|
||||
import net.lamgc.cgj.bot.MiraiMain;
|
||||
import net.lamgc.cgj.pixiv.*;
|
||||
import net.lamgc.cgj.proxy.PixivAccessProxyServer;
|
||||
import net.lamgc.cgj.bot.framework.coolq.CQConfig;
|
||||
import net.lamgc.cgj.bot.framework.mirai.MiraiMain;
|
||||
import net.lamgc.cgj.pixiv.PixivDownload;
|
||||
import net.lamgc.cgj.pixiv.PixivSearchBuilder;
|
||||
import net.lamgc.cgj.pixiv.PixivURL;
|
||||
import net.lamgc.plps.PixivLoginProxyServer;
|
||||
import net.lamgc.utils.base.ArgumentsProperties;
|
||||
import net.lamgc.utils.base.runner.Argument;
|
||||
@ -20,9 +21,7 @@ import org.apache.commons.io.IOUtils;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.CookieStore;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -43,7 +42,7 @@ import java.util.zip.ZipOutputStream;
|
||||
@SpringBootApplication
|
||||
public class Main {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger("Main");
|
||||
private final static Logger log = LoggerFactory.getLogger(Main.class.getName());
|
||||
|
||||
private final static File storeDir = new File("store/");
|
||||
|
||||
@ -52,9 +51,19 @@ public class Main {
|
||||
public static HttpHost proxy;
|
||||
|
||||
public static void main(String[] args) throws IOException, ClassNotFoundException {
|
||||
log.trace("ContentGrabbingJi 正在启动...");
|
||||
log.debug("Args: {}, LogsPath: {}", Arrays.toString(args), System.getProperty("cgj.logsPath"));
|
||||
log.debug("运行目录: {}", System.getProperty("user.dir"));
|
||||
ArgumentsProperties argsProp = new ArgumentsProperties(args);
|
||||
if(argsProp.containsKey("proxy")) {
|
||||
URL proxyUrl = new URL(argsProp.getValue("proxy"));
|
||||
|
||||
|
||||
if(!getSettingToSysProp(argsProp, "proxy", null)) {
|
||||
getEnvSettingToSysProp("CGJ_PROXY", "proxy", null);
|
||||
}
|
||||
|
||||
String proxyAddress = System.getProperty("cgj.proxy");
|
||||
if(!Strings.isNullOrEmpty(proxyAddress)) {
|
||||
URL proxyUrl = new URL(proxyAddress);
|
||||
proxy = new HttpHost(proxyUrl.getHost(), proxyUrl.getPort());
|
||||
log.info("已启用Http协议代理:{}", proxy.toHostString());
|
||||
} else {
|
||||
@ -65,33 +74,27 @@ public class Main {
|
||||
log.error("创建文件夹失败!");
|
||||
}
|
||||
|
||||
// TODO: 需要修改参数名了, 大概改成类似于 workerDir这样的吧
|
||||
if(argsProp.containsKey("cqRootDir")) {
|
||||
log.info("cqRootDir: {}", argsProp.getValue("cqRootDir"));
|
||||
System.setProperty("cgj.cqRootDir", argsProp.getValue("cqRootDir"));
|
||||
} else {
|
||||
log.warn("未设置cqRootDir, 当前运行目录将作为酷Q机器人所在目录.");
|
||||
System.setProperty("cgj.cqRootDir", "./");
|
||||
if(!getSettingToSysProp(argsProp, "botDataDir", "./") &&
|
||||
!getEnvSettingToSysProp("CGJ_BOT_DATA_DIR", "botDataDir", "./")) {
|
||||
log.warn("未设置botDataDir, 当前运行目录将作为酷Q机器人所在目录.");
|
||||
}
|
||||
if(!getSettingToSysProp(argsProp, "redisAddress", "127.0.0.1") &&
|
||||
!getEnvSettingToSysProp("CGJ_REDIS_URI", "redisAddress", "127.0.0.1")) {
|
||||
log.warn("未设置RedisAddress, 将使用默认值连接Redis服务器(127.0.0.1:6379)");
|
||||
}
|
||||
|
||||
if(argsProp.containsKey("redisAddr")) {
|
||||
log.info("redisAddress: {}", argsProp.getValue("redisAddr"));
|
||||
System.setProperty("cgj.redisAddress", argsProp.getValue("redisAddr"));
|
||||
} else {
|
||||
log.info("未设置RedisAddress, 将使用默认值连接Redis服务器(127.0.0.1:6379)");
|
||||
System.setProperty("cgj.redisAddress", "127.0.0.1");
|
||||
}
|
||||
|
||||
File cookieStoreFile = new File("cookies.store");
|
||||
File cookieStoreFile = new File(System.getProperty("cgj.botDataDir"), "cookies.store");
|
||||
if(!cookieStoreFile.exists()) {
|
||||
log.error("未找到cookies.store文件, 是否启动PixivLoginProxyServer? (yes/no)");
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
if(scanner.nextLine().equalsIgnoreCase("yes")) {
|
||||
startPixivLoginProxyServer();
|
||||
} else {
|
||||
System.exit(1);
|
||||
return;
|
||||
log.warn("未找到cookies.store文件, 是否启动PixivLoginProxyServer? (yes/no)");
|
||||
try(Scanner scanner = new Scanner(System.in)) {
|
||||
if(scanner.nextLine().trim().equalsIgnoreCase("yes")) {
|
||||
startPixivLoginProxyServer();
|
||||
} else {
|
||||
System.exit(1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(cookieStoreFile));
|
||||
cookieStore = (CookieStore) ois.readObject();
|
||||
@ -103,17 +106,50 @@ public class Main {
|
||||
ArgumentsRunner.run(Main.class, args);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从ArgumentsProperties获取设置项到System Properties
|
||||
* @param prop ArgumentsProperties对象
|
||||
* @param key 设置项key
|
||||
* @param defaultValue 默认值
|
||||
* @return 如果成功从ArgumentsProperties获得设置项, 返回true, 如未找到(使用了defaultValue或null), 返回false;
|
||||
*/
|
||||
private static boolean getSettingToSysProp(ArgumentsProperties prop, String key, String defaultValue) {
|
||||
if(prop.containsKey(key)) {
|
||||
log.info("{}: {}", key, prop.getValue(key));
|
||||
System.setProperty("cgj." + key, prop.getValue(key));
|
||||
return true;
|
||||
} else {
|
||||
if(defaultValue != null) {
|
||||
System.setProperty("cgj." + key, defaultValue);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean getEnvSettingToSysProp(String envKey, String sysPropKey, String defaultValue) {
|
||||
String env = System.getenv(envKey);
|
||||
if(env != null) {
|
||||
System.setProperty("cgj." + sysPropKey, env);
|
||||
return true;
|
||||
} else if(defaultValue != null) {
|
||||
System.setProperty("cgj." + sysPropKey, defaultValue);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Command
|
||||
public static void botMode(@Argument(name = "args", force = false) String argsStr) {
|
||||
new MiraiMain().init();
|
||||
MiraiMain main = new MiraiMain();
|
||||
main.init();
|
||||
main.close();
|
||||
}
|
||||
|
||||
@Command
|
||||
public static void pluginMode(@Argument(name = "args", force = false) String argsStr) {
|
||||
if(!System.getProperty("cgj.cqRootDir").endsWith("\\") && !System.getProperty("cgj.cqRootDir").endsWith("/")) {
|
||||
System.setProperty("cgj.cqRootDir", System.getProperty("cgj.cqRootDir") + "/");
|
||||
if(!System.getProperty("cgj.botDataDir").endsWith("\\") && !System.getProperty("cgj.botDataDir").endsWith("/")) {
|
||||
System.setProperty("cgj.botDataDir", System.getProperty("cgj.botDataDir") + "/");
|
||||
}
|
||||
log.info("酷Q机器人根目录: {}", System.getProperty("cgj.cqRootDir"));
|
||||
log.info("酷Q机器人根目录: {}", System.getProperty("cgj.botDataDir"));
|
||||
CQConfig.init();
|
||||
Pattern pattern = Pattern.compile("/\\s*(\".+?\"|[^:\\s])+((\\s*:\\s*(\".+?\"|[^\\s])+)|)|(\".+?\"|[^\"\\s])+");
|
||||
Matcher matcher = pattern.matcher(Strings.nullToEmpty(argsStr));
|
||||
@ -353,90 +389,20 @@ public class Main {
|
||||
builder,
|
||||
PixivURL.getPixivRefererLink(illustId)
|
||||
);
|
||||
|
||||
/*log.info("正在下载...");
|
||||
List<String> list = PixivDownload.getIllustAllPageDownload(
|
||||
HttpClientBuilder.create()
|
||||
.setProxy(proxy)
|
||||
.build(),
|
||||
illustId, PixivDownload.PageQuality.ORIGINAL);*/
|
||||
}
|
||||
}
|
||||
/*
|
||||
|
||||
if(searchBuilder.getSearchArea().equals(PixivSearchBuilder.SearchArea.TOP)) {
|
||||
|
||||
} else {
|
||||
JsonArray illustsArray = resultBody
|
||||
.getAsJsonObject(searchBuilder.getSearchArea().jsonKey).getAsJsonArray("data");
|
||||
log.info("已找到与 {} 相关插图信息:", content);
|
||||
int count = 1;
|
||||
for (JsonElement jsonElement : illustsArray) {
|
||||
JsonObject illustObj = jsonElement.getAsJsonObject();
|
||||
//TODO: 防止数据内混入无效内容, 需要检查对象是否有illustId
|
||||
if(!illustObj.has("illustId")) {
|
||||
continue;
|
||||
}
|
||||
int illustId = illustObj.get("illustId").getAsInt();
|
||||
StringBuilder builder = new StringBuilder("[");
|
||||
illustObj.get("tags").getAsJsonArray().forEach(el -> builder.append(el.getAsString()).append(", "));
|
||||
builder.replace(builder.length() - 2, builder.length(), "]");
|
||||
log.info("({} / {})\n\t作品id: {}, \n\t作者名(作者id): {} ({}), \n\t作品标题: {}, \n\t作品Tags: {}, \n\t作品链接: {}",
|
||||
count++,
|
||||
illustsArray.size(),
|
||||
illustId,
|
||||
illustObj.get("userName").getAsString(),
|
||||
illustObj.get("userId").getAsInt(),
|
||||
illustObj.get("illustTitle").getAsString(),
|
||||
builder,
|
||||
PixivURL.getPixivRefererLink(illustId)
|
||||
);
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
@Command(defaultCommand = true)
|
||||
public static void testRun() {
|
||||
/*loadCookieStoreFromFile();
|
||||
|
||||
if(cookieStore == null){
|
||||
startPixivLoginProxyServer();
|
||||
}*/
|
||||
|
||||
//accessPixivToFile();
|
||||
|
||||
//startPixivAccessProxyServer();
|
||||
|
||||
//saveCookieStoreToFile();
|
||||
log.info("这里啥都没有哟w");
|
||||
}
|
||||
|
||||
private static void loadCookieStoreFromFile() throws IOException {
|
||||
log.info("正在加载CookieStore...");
|
||||
File storeFile = new File("./cookies.store");
|
||||
if(!storeFile.exists()){
|
||||
log.info("未找到CookieStore, 跳过加载.");
|
||||
return;
|
||||
}
|
||||
ObjectInputStream stream = new ObjectInputStream(new FileInputStream(storeFile));
|
||||
Object result;
|
||||
try {
|
||||
result = stream.readObject();
|
||||
} catch (ClassNotFoundException e) {
|
||||
log.error("加载出错", e);
|
||||
return;
|
||||
}
|
||||
cookieStore = (CookieStore) result;
|
||||
cookieStore.getCookies().forEach(cookie -> log.debug(cookie.getName() + ": " + cookie.getValue() + ", isExpired: " + cookie.isExpired(new Date())));
|
||||
log.info("CookieStore加载完成.");
|
||||
}
|
||||
|
||||
private static void saveCookieStoreToFile() throws IOException {
|
||||
log.info("正在保存CookieStore...");
|
||||
File outputFile = new File("./cookies.store");
|
||||
if(!outputFile.exists() && !outputFile.delete() && !outputFile.createNewFile()){
|
||||
File outputFile = new File(System.getProperty("cgj.botDataDir"), "cookies.store");
|
||||
if(!outputFile.exists() && !outputFile.createNewFile()){
|
||||
log.error("保存CookieStore失败.");
|
||||
return;
|
||||
}
|
||||
@ -461,96 +427,27 @@ public class Main {
|
||||
proxyServerStartThread.setName("LoginProxyServerThread");
|
||||
proxyServerStartThread.start();
|
||||
//System.console().readLine();
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
log.info("登录完成后, 使用\"done\"命令结束登录过程.");
|
||||
while(true) {
|
||||
if (scanner.nextLine().equalsIgnoreCase("done")) {
|
||||
log.info("关闭PLPS服务器...");
|
||||
proxyServer.close();
|
||||
cookieStore = proxyServer.getCookieStore();
|
||||
try {
|
||||
log.info("正在保存CookieStore...");
|
||||
saveCookieStoreToFile();
|
||||
log.info("CookieStore保存完成.");
|
||||
} catch (IOException e) {
|
||||
log.error("CookieStore保存时发生异常, 本次CookieStore仅可在本次运行使用.", e);
|
||||
try(Scanner scanner = new Scanner(System.in)) {
|
||||
while(true) {
|
||||
if (scanner.nextLine().equalsIgnoreCase("done")) {
|
||||
log.info("关闭PLPS服务器...");
|
||||
proxyServer.close();
|
||||
cookieStore = proxyServer.getCookieStore();
|
||||
try {
|
||||
log.info("正在保存CookieStore...");
|
||||
saveCookieStoreToFile();
|
||||
log.info("CookieStore保存完成.");
|
||||
} catch (IOException e) {
|
||||
log.error("CookieStore保存时发生异常, 本次CookieStore仅可在本次运行使用.", e);
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
log.warn("要结束登录过程, 请使用\"done\"命令.");
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
log.warn("要结束登录过程, 请使用\"done\"命令.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void accessPixivToFile() throws IOException {
|
||||
File cookieStoreFile = new File("./cookie.txt");
|
||||
if (!cookieStoreFile.exists() && !cookieStoreFile.createNewFile()) {
|
||||
log.info("Cookie文件存储失败");
|
||||
}
|
||||
/*log.info("正在写出Cookie, Cookie count: " + cookieStore.getCookies().size());
|
||||
FileWriter cookieWriter = new FileWriter(cookieStoreFile);
|
||||
cookieStore.getCookies().forEach(cookie -> {
|
||||
try {
|
||||
StringBuilder sb = new StringBuilder().append(cookie.getName()).append(" = ").append(cookie.getValue());
|
||||
log.info("正在导出Cookie: " + sb.toString());
|
||||
cookieWriter.append(sb.toString()).append("\n").flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
log.info("Cookie写出完成");*/
|
||||
|
||||
log.info("尝试通过捕获的Cookie访问Pixiv...");
|
||||
HttpClient httpClient = new PixivSession(new HttpHost("127.0.0.1", 1080), cookieStore).getHttpClient();
|
||||
HttpGet request = new HttpGet("https://www.pixiv.net");
|
||||
request.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0");
|
||||
request.addHeader(new BasicHeader("accept-encoding", "gzip, deflate, br"));
|
||||
request.addHeader(new BasicHeader("accept-language", "zh-CN,zh;q=0.9"));
|
||||
StringBuilder cookieBuilder = new StringBuilder();
|
||||
cookieStore.getCookies().forEach(cookie -> {
|
||||
if(cookie.isExpired(new Date())){
|
||||
return;
|
||||
}
|
||||
cookieBuilder.append(cookie.getName()).append("=").append(cookie.getValue()).append("; ");
|
||||
});
|
||||
request.addHeader("cookie", cookieBuilder.toString());
|
||||
|
||||
HttpResponse response = httpClient.execute(request);
|
||||
log.info("正在写入文件...");
|
||||
File outFile = new File("./pixiv.html");
|
||||
if (outFile.createNewFile() && !outFile.exists()) {
|
||||
log.info("文件创建失败!");
|
||||
}else {
|
||||
new FileWriter(outFile).append(EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8)).flush();
|
||||
}
|
||||
|
||||
Pixiv pixiv = new Pixiv(httpClient);
|
||||
pixiv.getRecommend().forEach(illustMap -> {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
illustMap.forEach((key, value) -> builder.append(key).append(": ").append(value).append("\n"));
|
||||
try {
|
||||
builder.append("download Link: ").append(Arrays.toString(pixiv.getAllDownloadLink(Integer.parseInt(illustMap.get(Pixiv.ATTR_ILLUST_ID)))));
|
||||
} catch (IOException e) {
|
||||
log.error("获取下载链接时出错!", e);
|
||||
}
|
||||
log.info(builder.append("\n").toString());
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private static void startPixivAccessProxyServer(){
|
||||
log.info("正在启动访问代理服务器, 将浏览器相关缓存清空后, 使用浏览器进行访问以尝试Cookie正确性.");
|
||||
final PixivAccessProxyServer accessProxyServer = new PixivAccessProxyServer(cookieStore, new ProxyConfig(ProxyType.SOCKS5, "127.0.0.1", 1080));
|
||||
Thread accessProxyServerThread = new Thread(() -> {
|
||||
log.info("正在启动PAPS...");
|
||||
accessProxyServer.start(1007);
|
||||
log.info("PAPS已关闭.");
|
||||
});
|
||||
accessProxyServerThread.setName("AccessProxyServerThread");
|
||||
accessProxyServerThread.start();
|
||||
new Scanner(System.in).nextLine();
|
||||
log.info("关闭PAPS服务器...");
|
||||
accessProxyServer.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,57 +0,0 @@
|
||||
package net.lamgc.cgj.bot;
|
||||
|
||||
import net.lz1998.cq.robot.CoolQ;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
public class AutoArtworksSender {
|
||||
|
||||
private final CoolQ CQ;
|
||||
private final ReceiveType receiveType;
|
||||
private final long targetReceiveId;
|
||||
private Timer timer = new Timer();
|
||||
private TimerTask task = new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
HttpGet request = new HttpGet();
|
||||
|
||||
// https://api.imjad.cn/pixiv/v2/?type=tags
|
||||
}
|
||||
};
|
||||
|
||||
public AutoArtworksSender(CoolQ cq, ReceiveType receiveType, long receiveId) {
|
||||
this.CQ = cq;
|
||||
this.receiveType = receiveType;
|
||||
this.targetReceiveId = receiveId;
|
||||
}
|
||||
|
||||
public void reset(long time) {
|
||||
if(time <= 0) {
|
||||
timer.schedule(task, new Random().nextInt(10 * 60 * 60 * 1000) + 7200000L); //2H ~ 12H
|
||||
} else {
|
||||
timer.schedule(task, time);
|
||||
}
|
||||
}
|
||||
|
||||
public void sendMessage(String message, boolean auto_escape) {
|
||||
switch (receiveType) {
|
||||
case GROUP:
|
||||
CQ.sendGroupMsg(targetReceiveId, message, auto_escape);
|
||||
break;
|
||||
case Discuss:
|
||||
CQ.sendDiscussMsg(targetReceiveId, message, auto_escape);
|
||||
break;
|
||||
case PRIVATE:
|
||||
CQ.sendPrivateMsg(targetReceiveId, message, auto_escape);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public enum ReceiveType {
|
||||
PRIVATE, GROUP, Discuss
|
||||
}
|
||||
|
||||
}
|
32
src/main/java/net/lamgc/cgj/bot/AutoSender.java
Normal file
32
src/main/java/net/lamgc/cgj/bot/AutoSender.java
Normal file
@ -0,0 +1,32 @@
|
||||
package net.lamgc.cgj.bot;
|
||||
|
||||
import net.lamgc.cgj.bot.message.MessageSender;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 自动发送器
|
||||
*/
|
||||
public abstract class AutoSender {
|
||||
|
||||
private MessageSender messageSender;
|
||||
|
||||
/**
|
||||
* 构造一个自动发送器
|
||||
* @param messageSender 自动发送器所使用的消息发送器
|
||||
*/
|
||||
public AutoSender(MessageSender messageSender) {
|
||||
this.messageSender = Objects.requireNonNull(messageSender);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设置等等消息发送器
|
||||
* @return 消息发送器
|
||||
*/
|
||||
MessageSender getMessageSender() {
|
||||
return this.messageSender;
|
||||
}
|
||||
|
||||
public abstract void send();
|
||||
|
||||
}
|
@ -1,85 +1,87 @@
|
||||
package net.lamgc.cgj.bot;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import net.lamgc.cgj.bot.message.MessageSenderBuilder;
|
||||
import net.lamgc.cgj.bot.message.MessageSource;
|
||||
import net.lamgc.cgj.pixiv.PixivDownload;
|
||||
import net.lamgc.cgj.pixiv.PixivURL;
|
||||
import net.lamgc.utils.base.runner.Argument;
|
||||
import net.lamgc.utils.base.runner.Command;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Date;
|
||||
import java.util.Properties;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
public class BotAdminCommandProcess {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(BotAdminCommandProcess.class.getSimpleName());
|
||||
private final static Logger log = LoggerFactory.getLogger(BotAdminCommandProcess.class.getName());
|
||||
|
||||
private final static File globalPropFile = new File("./global.properties");
|
||||
private final static File pushListFile = new File(System.getProperty("cgj.botDataDir"), "pushList.json");
|
||||
|
||||
private final static Hashtable<Long, JsonObject> pushInfoMap = new Hashtable<>();
|
||||
|
||||
private final static Gson gson = new GsonBuilder()
|
||||
.setPrettyPrinting()
|
||||
.create();
|
||||
|
||||
@Command
|
||||
public String clearCache() {
|
||||
public static String cleanCache() {
|
||||
BotCommandProcess.clearCache();
|
||||
return "操作已完成.";
|
||||
}
|
||||
|
||||
@Command
|
||||
public String setGlobalProperty(@Argument(name = "key") String key, @Argument(name = "value") String value, @Argument(name = "save", force = false) boolean saveNow) {
|
||||
String lastValue = BotCommandProcess.globalProp.getProperty(key);
|
||||
BotCommandProcess.globalProp.setProperty(key, Strings.nullToEmpty(value));
|
||||
if(saveNow) {
|
||||
saveGlobalProperties();
|
||||
public static String setProperty(
|
||||
@Argument(name = "group", force = false) long groupId,
|
||||
@Argument(name = "key") String key,
|
||||
@Argument(name = "value") String value
|
||||
) {
|
||||
if(Strings.isNullOrEmpty(key)) {
|
||||
return "未选择配置项key.";
|
||||
}
|
||||
return "全局配置项 " + key + " 现已设置为: " + value + " (设置前的值: " + lastValue + ")";
|
||||
String lastValue = SettingProperties.setProperty(groupId, key, value.equals("null") ? null : value);
|
||||
return (groupId <= 0 ? "已更改全局配置 " : "已更改群组 " + groupId + " 配置 ") +
|
||||
key + " 的值: '" + value + "' (原配置值: '" + lastValue + "')";
|
||||
}
|
||||
|
||||
@Command
|
||||
public String getGlobalProperty(@Argument(name = "key") String key) {
|
||||
return "全局配置项 " + key + " 当前值: " + BotCommandProcess.globalProp.getProperty(key, "(Empty)");
|
||||
public static String getProperty(
|
||||
@Argument(name = "group", force = false) long groupId,
|
||||
@Argument(name = "key") String key
|
||||
) {
|
||||
if(Strings.isNullOrEmpty(key)) {
|
||||
return "未选择配置项key.";
|
||||
}
|
||||
return (groupId <= 0 ? "全局配置 " : "群组 " + groupId + " 配置 ") +
|
||||
key + " 设定值: '" + SettingProperties.getProperty(groupId, key, "(empty)") + "'";
|
||||
}
|
||||
|
||||
@Command
|
||||
public String saveGlobalProperties() {
|
||||
log.info("正在保存全局配置文件...");
|
||||
|
||||
try {
|
||||
if(!globalPropFile.exists()) {
|
||||
if(!globalPropFile.createNewFile()) {
|
||||
log.error("全局配置项文件保存失败!({})", "文件创建失败");
|
||||
return "全局配置项文件保存失败!";
|
||||
}
|
||||
}
|
||||
BotCommandProcess.globalProp.store(new FileOutputStream(globalPropFile), "");
|
||||
log.info("全局配置文件保存成功!");
|
||||
return "保存全局配置文件 - 操作已完成.";
|
||||
} catch (IOException e) {
|
||||
log.error("全局配置项文件保存失败!", e);
|
||||
return "全局配置项文件保存失败!";
|
||||
}
|
||||
public static String saveProperties() {
|
||||
log.info("正在保存配置文件...");
|
||||
SettingProperties.saveProperties();
|
||||
log.info("配置文件保存操作已完成.");
|
||||
return "保存配置 - 操作已完成.";
|
||||
}
|
||||
|
||||
@Command
|
||||
public String loadGlobalProperties(@Argument(name = "reload", force = false) boolean reload) {
|
||||
Properties cache = new Properties();
|
||||
if(!globalPropFile.exists()) {
|
||||
return "未找到全局配置文件, 无法重载";
|
||||
}
|
||||
|
||||
try(Reader reader = new BufferedReader(new FileReader(globalPropFile))) {
|
||||
cache.load(reader);
|
||||
} catch (IOException e) {
|
||||
log.error("重载全局配置文件时发生异常", e);
|
||||
return "加载全局配置文件时发生错误!";
|
||||
}
|
||||
|
||||
public static String loadProperties(@Argument(name = "reload", force = false) boolean reload) {
|
||||
if(reload) {
|
||||
BotCommandProcess.globalProp.clear();
|
||||
SettingProperties.clearProperties();
|
||||
}
|
||||
BotCommandProcess.globalProp.putAll(cache);
|
||||
return "全局配置文件重载完成.";
|
||||
|
||||
SettingProperties.loadProperties();
|
||||
return "操作已完成.";
|
||||
}
|
||||
|
||||
@Command
|
||||
public String runUpdateTask(@Argument(force = false, name = "date") Date queryTime) {
|
||||
public static String runUpdateTask(@Argument(force = false, name = "date") Date queryTime) {
|
||||
try {
|
||||
BotCommandProcess.runUpdateTimer(queryTime);
|
||||
} catch (Exception e) {
|
||||
@ -89,4 +91,252 @@ public class BotAdminCommandProcess {
|
||||
return "操作已完成.";
|
||||
}
|
||||
|
||||
private final static String RANKING_SETTING_TIME_MIN = "time.min";
|
||||
private final static String RANKING_SETTING_TIME_FLOAT = "time.float";
|
||||
private final static String RANKING_SETTING_RANKING_START = "ranking.start";
|
||||
private final static String RANKING_SETTING_RANKING_END = "ranking.end";
|
||||
private final static String RANKING_SETTING_RANKING_MODE = "ranking.mode";
|
||||
private final static String RANKING_SETTING_RANKING_CONTENT_TYPE = "ranking.contentType";
|
||||
private final static String RANKING_SETTING_PAGE_QUALITY = "page.quality";
|
||||
|
||||
@Command
|
||||
public static String addPushGroup(
|
||||
@Argument(name = "$fromGroup") long fromGroup,
|
||||
@Argument(name = "group", force = false, defaultValue = "0") long groupId,
|
||||
@Argument(name = "minTime", force = false, defaultValue = "21600000") long minTime,
|
||||
@Argument(name = "floatTime", force = false, defaultValue = "10800000") int floatTime,
|
||||
@Argument(name = "rankingStart", force = false, defaultValue = "1") int rankingStart,
|
||||
@Argument(name = "rankingStop", force = false, defaultValue = "150") int rankingStop,
|
||||
@Argument(name = "mode", force = false, defaultValue = "DAILY") String rankingMode,
|
||||
@Argument(name = "type", force = false, defaultValue = "ILLUST") String rankingContentType,
|
||||
@Argument(name = "original", force = false, defaultValue = "false") boolean original
|
||||
) {
|
||||
if(minTime <= 0 || floatTime <= 0) {
|
||||
return "时间不能为0或负数!";
|
||||
} else if(rankingStart <= 0 || rankingStop - rankingStart <= 0) {
|
||||
return "排行榜范围选取错误!";
|
||||
}
|
||||
|
||||
PixivURL.RankingContentType type;
|
||||
PixivURL.RankingMode mode;
|
||||
try {
|
||||
type = PixivURL.RankingContentType.valueOf("TYPE_" + rankingContentType.toUpperCase());
|
||||
} catch(IllegalArgumentException e) {
|
||||
return "无效的排行榜类型参数!";
|
||||
}
|
||||
|
||||
try {
|
||||
mode = PixivURL.RankingMode.valueOf("MODE_" + rankingMode.toUpperCase());
|
||||
} catch(IllegalArgumentException e) {
|
||||
return "无效的排行榜模式参数!";
|
||||
}
|
||||
|
||||
if(!type.isSupportedMode(mode)) {
|
||||
return "不兼容的排行榜模式与类型!";
|
||||
}
|
||||
|
||||
long group = groupId <= 0 ? fromGroup : groupId;
|
||||
JsonObject setting = new JsonObject();
|
||||
setting.addProperty(RANKING_SETTING_TIME_MIN, minTime);
|
||||
setting.addProperty(RANKING_SETTING_TIME_FLOAT, floatTime);
|
||||
setting.addProperty(RANKING_SETTING_RANKING_START, rankingStart);
|
||||
setting.addProperty(RANKING_SETTING_RANKING_END, rankingStop);
|
||||
setting.addProperty(RANKING_SETTING_RANKING_MODE, rankingMode);
|
||||
setting.addProperty(RANKING_SETTING_RANKING_CONTENT_TYPE, rankingContentType);
|
||||
setting.addProperty(RANKING_SETTING_PAGE_QUALITY, original ?
|
||||
PixivDownload.PageQuality.ORIGINAL.name() :
|
||||
PixivDownload.PageQuality.REGULAR.name());
|
||||
if(pushInfoMap.containsKey(group)) {
|
||||
log.info("群 {} 已存在Timer, 删除Timer...", group);
|
||||
removePushGroup(fromGroup, groupId);
|
||||
}
|
||||
|
||||
log.info("群组 {} 新推送配置: {}", group, setting);
|
||||
log.info("正在增加Timer...");
|
||||
pushInfoMap.put(group, setting);
|
||||
addPushTimer(group, setting);
|
||||
return "已在 " + group + " 开启定时推送功能。";
|
||||
}
|
||||
|
||||
/**
|
||||
* 重载推送列表
|
||||
*/
|
||||
@Command
|
||||
public static String loadPushList() {
|
||||
pushInfoMap.clear();
|
||||
if(!pushListFile.exists()) {
|
||||
log.warn("推送列表文件不存在, 跳过加载.");
|
||||
return "文件不存在, 跳过加载.";
|
||||
}
|
||||
|
||||
try (Reader reader = new BufferedReader(new FileReader(pushListFile))) {
|
||||
pushInfoMap.putAll(gson.fromJson(reader, new TypeToken<Map<Long, JsonObject>>(){}.getType()));
|
||||
loadAllPushTimer(false);
|
||||
return "列表重载完成";
|
||||
} catch (IOException e) {
|
||||
log.error("重载推送列表时发生错误", e);
|
||||
return "加载时发生异常";
|
||||
}
|
||||
}
|
||||
|
||||
@Command
|
||||
public static String savePushList() {
|
||||
try {
|
||||
if(!pushListFile.exists()) {
|
||||
pushListFile.createNewFile();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("PushList.json文件创建失败", e);
|
||||
return "保存失败!请检查控制台信息.";
|
||||
}
|
||||
|
||||
try (Writer writer = new FileWriter(pushListFile)) {
|
||||
writer.write(gson.toJson(pushInfoMap));
|
||||
return "保存成功.";
|
||||
} catch (IOException e) {
|
||||
log.error("写入PushList.json文件失败!", e);
|
||||
return "保存失败!请检查控制台信息.";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载所有推送Timer
|
||||
* @param flush 是否完全重载, 如为true则加载前会删除所有已加载的Timer
|
||||
*/
|
||||
public static void loadAllPushTimer(boolean flush) {
|
||||
if(flush) {
|
||||
RandomIntervalSendTimer.timerIdSet().forEach(id -> RandomIntervalSendTimer.getTimerById(id).destroy());
|
||||
} else {
|
||||
cleanPushTimer();
|
||||
}
|
||||
pushInfoMap.forEach(BotAdminCommandProcess::addPushTimer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据设置增加Timer
|
||||
* @param id 群组id
|
||||
* @param setting jsonObject设置集
|
||||
*/
|
||||
private static void addPushTimer(long id, JsonObject setting) {
|
||||
try {
|
||||
RandomIntervalSendTimer.getTimerById(id);
|
||||
return;
|
||||
} catch(NoSuchElementException ignored) {
|
||||
}
|
||||
|
||||
int rankingStart = setting.has(RANKING_SETTING_RANKING_START) ? setting.get(RANKING_SETTING_RANKING_START).getAsInt() : 1;
|
||||
int rankingEnd = setting.has(RANKING_SETTING_RANKING_END) ? setting.get(RANKING_SETTING_RANKING_END).getAsInt() : 150;
|
||||
PixivURL.RankingMode rankingMode = PixivURL.RankingMode.MODE_DAILY;
|
||||
PixivURL.RankingContentType rankingContentType = PixivURL.RankingContentType.TYPE_ILLUST;
|
||||
PixivDownload.PageQuality pageQuality = PixivDownload.PageQuality.REGULAR;
|
||||
|
||||
if(rankingStart <= 0 || rankingStart > 500) {
|
||||
log.warn("群组 [{}] - 无效的RankingStart设定值, 将重置为默认设定值(1): {}", id, rankingStart);
|
||||
rankingStart = 1;
|
||||
} else if(rankingEnd > 500 || rankingEnd <= 0) {
|
||||
log.warn("群组 [{}] - 无效的RankingEnd设定值, 将重置为默认设定值(150): {}", id, rankingEnd);
|
||||
rankingEnd = 150;
|
||||
} else if(rankingStart > rankingEnd) {
|
||||
log.warn("群组 [{}] - 无效的排行榜选取范围, 将重置为默认设定值(1 ~ 150): start={}, end={}", id, rankingStart, rankingEnd);
|
||||
rankingStart = 1;
|
||||
rankingEnd = 150;
|
||||
}
|
||||
|
||||
if(setting.has(RANKING_SETTING_RANKING_MODE)) {
|
||||
String value = setting.get(RANKING_SETTING_RANKING_MODE).getAsString().trim().toUpperCase();
|
||||
try {
|
||||
rankingMode = PixivURL.RankingMode.valueOf(value.startsWith("MODE_") ? value : "MODE_" + value);
|
||||
} catch(IllegalArgumentException e) {
|
||||
log.warn("群组ID [{}] - 无效的RankingMode设定值, 将重置为默认值: {}", id, value);
|
||||
}
|
||||
}
|
||||
if(setting.has(RANKING_SETTING_RANKING_CONTENT_TYPE)) {
|
||||
String value = setting.get(RANKING_SETTING_RANKING_CONTENT_TYPE).getAsString().trim().toUpperCase();
|
||||
try {
|
||||
rankingContentType = PixivURL.RankingContentType.valueOf(value.startsWith("TYPE_") ? value : "TYPE_" + value);
|
||||
} catch(IllegalArgumentException e) {
|
||||
log.warn("群组ID [{}] - 无效的RankingContentType设定值: {}", id, value);
|
||||
}
|
||||
}
|
||||
|
||||
if(setting.has(RANKING_SETTING_PAGE_QUALITY)) {
|
||||
String value = setting.get(RANKING_SETTING_PAGE_QUALITY).getAsString().trim().toUpperCase();
|
||||
try {
|
||||
pageQuality = PixivDownload.PageQuality.valueOf(value);
|
||||
} catch(IllegalArgumentException e) {
|
||||
log.warn("群组ID [{}] - 无效的PageQuality设定值: {}", id, value);
|
||||
}
|
||||
}
|
||||
|
||||
AutoSender sender = new RandomRankingArtworksSender(
|
||||
MessageSenderBuilder.getMessageSender(MessageSource.Group, id),
|
||||
rankingStart,
|
||||
rankingEnd,
|
||||
rankingMode, rankingContentType,
|
||||
pageQuality
|
||||
);
|
||||
|
||||
RandomIntervalSendTimer timer = RandomIntervalSendTimer.createTimer(
|
||||
id,
|
||||
sender,
|
||||
setting.get("time.min").getAsLong(),
|
||||
setting.get("time.float").getAsInt(),
|
||||
true, true);
|
||||
log.info("群组 {} 已创建对应Timer: {}", id, Integer.toHexString(timer.hashCode()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除一个推送定时器
|
||||
* @param id 群号
|
||||
* @throws NoSuchElementException 当这个群号没有定时器的时候抛出异常
|
||||
*/
|
||||
@Command
|
||||
public static String removePushGroup(@Argument(name = "$fromGroup") long fromGroup, @Argument(name = "group", force = false) long id) {
|
||||
long group = id <= 0 ? fromGroup : id;
|
||||
RandomIntervalSendTimer.getTimerById(group).destroy();
|
||||
pushInfoMap.remove(group);
|
||||
return "已关闭群 " + group + " 的美图推送功能。";
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据已修改的pushInfoMap将已经被删除的Timer取消
|
||||
*/
|
||||
private static void cleanPushTimer() {
|
||||
RandomIntervalSendTimer.timerIdSet().forEach(id -> {
|
||||
if(!pushInfoMap.containsKey(id)) {
|
||||
RandomIntervalSendTimer.getTimerById(id).destroy();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Command
|
||||
public static String getReportList() {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
|
||||
Set<String> keys = BotCommandProcess.reportStore.keys();
|
||||
StringBuilder msgBuilder = new StringBuilder();
|
||||
msgBuilder.append("当前被报告的作品列表:\n");
|
||||
int count = 1;
|
||||
for(String key : keys) {
|
||||
String illustIdStr = key.substring(key.indexOf(".") + 1);
|
||||
JsonObject report = BotCommandProcess.reportStore.getCache(illustIdStr).getAsJsonObject();
|
||||
log.debug("{} - Report: {}", illustIdStr, report);
|
||||
String reason = report.get("reason").isJsonNull() ? "" : report.get("reason").getAsString();
|
||||
msgBuilder.append(count).append(". 作品Id: ").append(illustIdStr)
|
||||
.append("(").append(dateFormat.format(new Date(report.get("reportTime").getAsLong()))).append("):\n")
|
||||
.append("报告者QQ:").append(report.get("fromQQ").getAsLong()).append("\n")
|
||||
.append("报告所在群:").append(report.get("fromGroup").getAsLong()).append("\n")
|
||||
.append("报告原因:\n").append(reason).append("\n");
|
||||
}
|
||||
return msgBuilder.toString();
|
||||
}
|
||||
|
||||
@Command
|
||||
public static String unBanArtwork(@Argument(name = "id") int illustId) {
|
||||
if(illustId <= 0) {
|
||||
return "无效的作品id!";
|
||||
}
|
||||
boolean removeResult = BotCommandProcess.reportStore.remove(String.valueOf(illustId));
|
||||
return removeResult ? "作品已解封!" : "解封失败!可能该作品并未被封禁。";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -54,6 +54,14 @@ public class BotCode {
|
||||
return new BotCode(keys[0], keys[1], param);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取BotCode所使用的匹配正则表达式
|
||||
* @return 用于匹配BotCode的正则表达式对象
|
||||
*/
|
||||
public static Pattern getCodePattern() {
|
||||
return Pattern.compile(codePattern.pattern());
|
||||
}
|
||||
|
||||
private String platformName;
|
||||
private String functionName;
|
||||
private Hashtable<String, String> parameter = new Hashtable<>();
|
||||
|
@ -2,21 +2,22 @@ package net.lamgc.cgj.bot;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.base.Throwables;
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
import com.google.gson.*;
|
||||
import io.netty.handler.codec.http.HttpHeaderNames;
|
||||
import net.lamgc.cgj.Main;
|
||||
import net.lamgc.cgj.bot.cache.*;
|
||||
import net.lamgc.cgj.bot.event.BotEventHandler;
|
||||
import net.lamgc.cgj.bot.event.BufferMessageEvent;
|
||||
import net.lamgc.cgj.bot.sort.PreLoadDataComparator;
|
||||
import net.lamgc.cgj.pixiv.PixivDownload;
|
||||
import net.lamgc.cgj.pixiv.PixivSearchBuilder;
|
||||
import net.lamgc.cgj.pixiv.PixivURL;
|
||||
import net.lamgc.cgj.pixiv.PixivDownload.PageQuality;
|
||||
import net.lamgc.cgj.pixiv.PixivURL.RankingContentType;
|
||||
import net.lamgc.cgj.pixiv.PixivURL.RankingMode;
|
||||
import net.lamgc.cgj.util.URLs;
|
||||
import net.lamgc.utils.base.runner.Argument;
|
||||
import net.lamgc.utils.base.runner.Command;
|
||||
import net.lamgc.utils.event.EventExecutor;
|
||||
import net.lz1998.cq.utils.CQCode;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
@ -26,84 +27,72 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@SuppressWarnings({"SynchronizationOnLocalVariableOrMethodParameter", "SameParameterValue"})
|
||||
public class BotCommandProcess {
|
||||
|
||||
private final static PixivDownload pixivDownload = new PixivDownload(Main.cookieStore, Main.proxy);
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(BotCommandProcess.class.getSimpleName());
|
||||
private final static Logger log = LoggerFactory.getLogger(BotCommandProcess.class.getName());
|
||||
|
||||
private final static File imageStoreDir = new File(System.getProperty("cgj.cqRootDir"), "data/image/cgj/");
|
||||
public final static Properties globalProp = new Properties();
|
||||
private final static File imageStoreDir = new File(System.getProperty("cgj.botDataDir"), "data/image/cgj/");
|
||||
private final static Gson gson = new GsonBuilder()
|
||||
.serializeNulls()
|
||||
.create();
|
||||
|
||||
private final static Hashtable<String, File> imageCache = new Hashtable<>();
|
||||
private final static CacheStore<JsonElement> illustInfoCache = new JsonRedisCacheStore(BotEventHandler.redisServer, "illustInfo", gson);
|
||||
private final static CacheStore<JsonElement> illustPreLoadDataCache = new HotDataCacheStore<>(
|
||||
new JsonRedisCacheStore(BotEventHandler.redisServer, "illustPreLoadData", gson),
|
||||
new LocalHashCacheStore<>(), 3600000, 900000);
|
||||
private final static CacheStore<JsonElement> searchBodyCache = new JsonRedisCacheStore(BotEventHandler.redisServer, "searchBody", gson);
|
||||
private final static CacheStore<List<JsonObject>> rankingCache = new JsonObjectRedisListCacheStore(BotEventHandler.redisServer, "ranking", gson);
|
||||
private final static CacheStore<List<String>> pagesCache = new RedisPoolCacheStore<List<String>>(BotEventHandler.redisServer, "imagePages") {
|
||||
@Override
|
||||
protected String parse(List<String> dataObj) {
|
||||
return gson.toJson(dataObj);
|
||||
}
|
||||
/* -------------------- 缓存 -------------------- */
|
||||
|
||||
@Override
|
||||
protected List<String> analysis(String dataStr) {
|
||||
return gson.fromJson(dataStr, new TypeToken<List<String>>(){}.getType());
|
||||
}
|
||||
};
|
||||
private final static Hashtable<String, File> imageCache = new Hashtable<>();
|
||||
|
||||
/**
|
||||
* 图片异步缓存执行器
|
||||
* 作品信息缓存 - 不过期
|
||||
*/
|
||||
private final static EventExecutor imageCacheExecutor = new EventExecutor(new ThreadPoolExecutor(
|
||||
Runtime.getRuntime().availableProcessors() >= 2 ? 2 : 1,
|
||||
(int) Math.ceil(Runtime.getRuntime().availableProcessors() / 2F),
|
||||
5L, TimeUnit.SECONDS,
|
||||
new LinkedBlockingQueue<>(128),
|
||||
new ThreadFactoryBuilder()
|
||||
.setNameFormat("imageCacheThread-%d")
|
||||
.build(),
|
||||
new ThreadPoolExecutor.DiscardOldestPolicy()
|
||||
));
|
||||
private final static CacheStore<JsonElement> illustInfoCache =
|
||||
new JsonRedisCacheStore(BotEventHandler.redisServer, "illustInfo", gson);
|
||||
|
||||
/**
|
||||
* 作品信息预加载数据 - 有效期 2 小时, 本地缓存有效期1 ± 0.25
|
||||
*/
|
||||
private final static CacheStore<JsonElement> illustPreLoadDataCache =
|
||||
CacheStoreUtils.hashLocalHotDataStore(
|
||||
new JsonRedisCacheStore(BotEventHandler.redisServer, "illustPreLoadData", gson),
|
||||
3600000, 900000);
|
||||
/**
|
||||
* 搜索内容缓存, 有效期 2 小时
|
||||
*/
|
||||
private final static CacheStore<JsonElement> searchBodyCache =
|
||||
new JsonRedisCacheStore(BotEventHandler.redisServer, "searchBody", gson);
|
||||
|
||||
/**
|
||||
* 排行榜缓存, 不过期
|
||||
*/
|
||||
private final static CacheStore<List<JsonObject>> rankingCache =
|
||||
new JsonObjectRedisListCacheStore(BotEventHandler.redisServer, "ranking", gson);
|
||||
|
||||
/**
|
||||
* 作品页面下载链接缓存 - 不过期
|
||||
*/
|
||||
private final static CacheStore<List<String>> pagesCache =
|
||||
new StringListRedisCacheStore(BotEventHandler.redisServer, "imagePages");
|
||||
|
||||
/**
|
||||
* 作品报告存储 - 不过期
|
||||
*/
|
||||
public final static CacheStore<JsonElement> reportStore =
|
||||
new JsonRedisCacheStore(BotEventHandler.redisServer, "report", gson);
|
||||
|
||||
private final static RankingUpdateTimer updateTimer = new RankingUpdateTimer();
|
||||
|
||||
public static void initialize() {
|
||||
log.info("正在初始化...");
|
||||
|
||||
File globalPropFile = new File("./global.properties");
|
||||
if(globalPropFile.exists() && globalPropFile.isFile()) {
|
||||
log.info("正在加载全局配置文件...");
|
||||
try {
|
||||
globalProp.load(new FileInputStream(globalPropFile));
|
||||
log.info("全局配置文件加载完成.");
|
||||
} catch (IOException e) {
|
||||
log.error("加载全局配置文件时发生异常", e);
|
||||
}
|
||||
} else {
|
||||
log.info("未找到全局配置文件,跳过加载.");
|
||||
}
|
||||
|
||||
try {
|
||||
imageCacheExecutor.addHandler(new ImageCacheHandler());
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
SettingProperties.loadProperties();
|
||||
|
||||
updateTimer.schedule(null);
|
||||
log.info("初始化完成.");
|
||||
@ -123,19 +112,91 @@ public class BotCommandProcess {
|
||||
public static String help() {
|
||||
StringBuilder helpStrBuilder = new StringBuilder();
|
||||
helpStrBuilder.append("CGJ Bot使用指南").append("\n");
|
||||
helpStrBuilder.append("使用方法:.cgj <命令> [参数...]").append("\n");
|
||||
helpStrBuilder.append("例如查询作品信息功能:").append("\n");
|
||||
helpStrBuilder.append(".cgj info -id 80846159").append("\n");
|
||||
helpStrBuilder.append("目前可用的命令:").append("\n");
|
||||
helpStrBuilder.append("\t").append("ranking - 获取今天或指定日期排行榜的前10名作品").append("\n");
|
||||
helpStrBuilder.append("\t\t").append("-date - 指定查询日期(年-月-日)").append("\n");
|
||||
helpStrBuilder.append("\t\t").append("-type - 排行榜类型(illust/插画, ugoira/动图, manga/漫画)").append("\n");
|
||||
|
||||
helpStrBuilder.append("\t").append("search - 搜索指定关键词并显示前10个作品").append("\n");
|
||||
helpStrBuilder.append("\t\t").append("-content - 搜索内容").append("\n");
|
||||
helpStrBuilder.append("\t").append("artworks - 获取作品的Pixiv页面").append("\n");
|
||||
|
||||
helpStrBuilder.append("\t").append("link - 获取作品的Pixiv页面").append("\n");
|
||||
helpStrBuilder.append("\t\t").append("-id - 作品id").append("\n");
|
||||
|
||||
helpStrBuilder.append("\t").append("info - 获取Pixiv作品信息").append("\n");
|
||||
helpStrBuilder.append("\t\t").append("-id - 作品id").append("\n");
|
||||
|
||||
helpStrBuilder.append("\t").append("image - 获取指定作品的图片").append("\n");
|
||||
helpStrBuilder.append("\t\t").append("-id - 作品id").append("\n");
|
||||
helpStrBuilder.append("\t\t").append("-quality - 图片质量(original/原图 regular/预览图)").append("\n");
|
||||
helpStrBuilder.append("\t\t").append("-page - 页数").append("\n");
|
||||
|
||||
helpStrBuilder.append("\t").append("report - 报告不当作品").append("\n");
|
||||
helpStrBuilder.append("\t\t").append("-id - 作品Id").append("\n");
|
||||
helpStrBuilder.append("\t\t").append("-msg - 报告原因").append("\n");
|
||||
return helpStrBuilder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 作品信息查询
|
||||
* @param fromGroup 来源群(系统提供)
|
||||
* @param illustId 作品Id
|
||||
* @return 返回作品信息
|
||||
*/
|
||||
@Command(commandName = "info")
|
||||
public static String artworkInfo(@Argument(name = "$fromGroup") long fromGroup, @Argument(name = "id") int illustId) {
|
||||
if(illustId <= 0) {
|
||||
return "这个作品Id是错误的!";
|
||||
}
|
||||
|
||||
try {
|
||||
if(isNoSafe(illustId, SettingProperties.getProperties(fromGroup), false) || isReported(illustId)) {
|
||||
return "阅览禁止:该作品已被封印!!";
|
||||
}
|
||||
|
||||
JsonObject illustPreLoadData = getIllustPreLoadData(illustId, false);
|
||||
StringBuilder builder = new StringBuilder("色图姬帮你了解了这个作品的信息!\n");
|
||||
builder.append("---------------- 作品信息 ----------------");
|
||||
builder.append("\n作品Id: ").append(illustId);
|
||||
builder.append("\n作品标题:").append(illustPreLoadData.get("illustTitle").getAsString());
|
||||
builder.append("\n作者(作者Id):").append(illustPreLoadData.get("userName").getAsString())
|
||||
.append("(").append(illustPreLoadData.get("userId").getAsInt()).append(")");
|
||||
builder.append("\n点赞数:").append(illustPreLoadData.get(PreLoadDataComparator.Attribute.LIKE.attrName).getAsInt());
|
||||
builder.append("\n收藏数:").append(illustPreLoadData.get(PreLoadDataComparator.Attribute.BOOKMARK.attrName).getAsInt());
|
||||
builder.append("\n围观数:").append(illustPreLoadData.get(PreLoadDataComparator.Attribute.VIEW.attrName).getAsInt());
|
||||
builder.append("\n评论数:").append(illustPreLoadData.get(PreLoadDataComparator.Attribute.COMMENT.attrName).getAsInt());
|
||||
builder.append("\n页数:").append(illustPreLoadData.get(PreLoadDataComparator.Attribute.PAGE.attrName).getAsInt()).append("页");
|
||||
builder.append("\n作品链接:").append(artworksLink(fromGroup, illustId)).append("\n");
|
||||
builder.append("---------------- 作品图片 ----------------\n");
|
||||
builder.append(getImageById(fromGroup, illustId, PixivDownload.PageQuality.REGULAR, 1)).append("\n");
|
||||
builder.append("使用 \".cgj image -id ")
|
||||
.append(illustId)
|
||||
.append("\" 获取原图。\n如有不当作品,可使用\".cgj report -id ")
|
||||
.append(illustId).append("\"向色图姬反馈。");
|
||||
return builder.toString();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "尚未支持";
|
||||
}
|
||||
|
||||
/**
|
||||
* 排行榜命令
|
||||
* @param fromGroup 来源群(系统提供)
|
||||
* @param queryTime 查询时间, 格式: 年-月-日
|
||||
* @param force 是否强制查询, 当主动提供的时间不在查询范围时, 是否强制查询, 仅系统可用
|
||||
* @param contentMode 内容模式
|
||||
* @param contentType 排行榜类型
|
||||
* @return 返回排行榜信息
|
||||
*/
|
||||
@Command
|
||||
public static String ranking(
|
||||
@Argument(name = "$fromGroup") long fromGroup,
|
||||
@Argument(force = false, name = "date") Date queryTime,
|
||||
@Argument(force = false, name = "force") boolean force,
|
||||
@Argument(force = false, name = "mode", defaultValue = "DAILY") String contentMode,
|
||||
@Argument(force = false, name = "type", defaultValue = "ILLUST") String contentType
|
||||
) {
|
||||
@ -152,24 +213,28 @@ public class BotCommandProcess {
|
||||
}
|
||||
queryDate = gregorianCalendar.getTime();
|
||||
} else {
|
||||
if(new Date().before(queryDate)) {
|
||||
if(new Date().before(queryDate) && !force) {
|
||||
log.warn("查询的日期过早, 无法查询排行榜.");
|
||||
return "查询日期过早, 暂未更新指定日期的排行榜!";
|
||||
}
|
||||
}
|
||||
|
||||
PixivURL.RankingMode mode = PixivURL.RankingMode.MODE_DAILY;
|
||||
PixivURL.RankingMode mode;
|
||||
try {
|
||||
mode = PixivURL.RankingMode.valueOf("MODE_" + contentMode.toUpperCase());
|
||||
String rankingModeValue = contentMode.toUpperCase();
|
||||
mode = PixivURL.RankingMode.valueOf(rankingModeValue.startsWith("MODE_") ? rankingModeValue : "MODE_" + rankingModeValue);
|
||||
} catch (IllegalArgumentException e) {
|
||||
log.warn("无效的RankingMode值: {}", contentMode);
|
||||
return "参数无效, 请查看帮助信息";
|
||||
}
|
||||
|
||||
PixivURL.RankingContentType type = PixivURL.RankingContentType.TYPE_ILLUST;
|
||||
PixivURL.RankingContentType type;
|
||||
try {
|
||||
type = PixivURL.RankingContentType.valueOf("TYPE_" + contentType.toUpperCase());
|
||||
String contentTypeValue = contentType.toUpperCase();
|
||||
type = PixivURL.RankingContentType.valueOf(contentTypeValue.startsWith("TYPE_") ? contentTypeValue : "TYPE_" + contentTypeValue);
|
||||
} catch (IllegalArgumentException e) {
|
||||
log.warn("无效的RankingContentType值: {}", contentType);
|
||||
return "参数无效, 请查看帮助信息";
|
||||
}
|
||||
|
||||
if(!type.isSupportedMode(mode)) {
|
||||
@ -182,9 +247,10 @@ public class BotCommandProcess {
|
||||
try {
|
||||
int index = 0;
|
||||
int itemLimit = 10;
|
||||
String itemLimitPropertyKey = "ranking.ItemCountLimit";
|
||||
String itemLimitPropertyKey = "ranking.itemCountLimit";
|
||||
try {
|
||||
itemLimit = Integer.parseInt(globalProp.getProperty(itemLimitPropertyKey, "10"));
|
||||
itemLimit = Integer.parseInt(SettingProperties
|
||||
.getProperty(fromGroup, itemLimitPropertyKey, "10"));
|
||||
} catch(NumberFormatException e) {
|
||||
log.warn("配置项 {} 的参数值格式有误!", itemLimitPropertyKey);
|
||||
}
|
||||
@ -192,12 +258,12 @@ public class BotCommandProcess {
|
||||
int imageLimit = 3;
|
||||
String imageLimitPropertyKey = "ranking.imageCountLimit";
|
||||
try {
|
||||
imageLimit = Integer.parseInt(globalProp.getProperty(imageLimitPropertyKey, "3"));
|
||||
imageLimit = Integer.parseInt(
|
||||
SettingProperties.getProperty(fromGroup, imageLimitPropertyKey, "3"));
|
||||
} catch(NumberFormatException e) {
|
||||
log.warn("配置项 {} 的参数值格式有误!", imageLimitPropertyKey);
|
||||
}
|
||||
|
||||
//TODO(LamGC, 2020.4.11): 将JsonRedisCacheStore更改为使用Redis的List集合, 以提高性能
|
||||
List<JsonObject> rankingInfoList = getRankingInfoByCache(type, mode, queryDate, 1, Math.max(0, itemLimit), false);
|
||||
if(rankingInfoList.isEmpty()) {
|
||||
return "无法查询排行榜,可能排行榜尚未更新。";
|
||||
@ -214,31 +280,61 @@ public class BotCommandProcess {
|
||||
resultBuilder.append(rank).append(". (id: ").append(illustId).append(") ").append(title)
|
||||
.append("(Author: ").append(authorName).append(",").append(authorId).append(") ").append(pagesCount).append("p.\n");
|
||||
if (index <= imageLimit) {
|
||||
resultBuilder.append(getImageById(illustId, PixivDownload.PageQuality.REGULAR, 1)).append("\n");
|
||||
resultBuilder.append(getImageById(fromGroup, illustId, PixivDownload.PageQuality.REGULAR, 1)).append("\n");
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("消息处理异常", e);
|
||||
return "排名榜获取失败!详情请查看机器人控制台。";
|
||||
}
|
||||
return resultBuilder.append("如查询当前时间获取到昨天时间,则今日排名榜未更新。").toString();
|
||||
return resultBuilder.append("如查询当前时间获取到昨天时间,则今日排名榜未更新。\n如有不当作品,可使用\".cgj report -id 作品id\"向色图姬反馈。").toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询指定作者的作品(尚未完成)
|
||||
* @return 返回作者信息和部分作品
|
||||
*/
|
||||
@Command(commandName = "userArt")
|
||||
public static String userArtworks() {
|
||||
|
||||
return "功能未完成";
|
||||
}
|
||||
|
||||
private final static Object searchCacheLock = new Object();
|
||||
@Command(commandName = "st")
|
||||
public static String r18Image() {
|
||||
BufferMessageEvent event = new BufferMessageEvent();
|
||||
RandomRankingArtworksSender artworksSender =
|
||||
new RandomRankingArtworksSender(event, 1, 200,
|
||||
RankingMode.MODE_MALE,
|
||||
RankingContentType.TYPE_ALL,
|
||||
PageQuality.ORIGINAL);
|
||||
artworksSender.send();
|
||||
return event.getBufferMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* 搜索命令
|
||||
* @param fromGroup 来源群(系统提供)
|
||||
* @param content 搜索内容
|
||||
* @param type 搜索类型
|
||||
* @param area 搜索区域
|
||||
* @param includeKeywords 包括关键字
|
||||
* @param excludeKeywords 排除关键字
|
||||
* @param contentOption 搜索选项
|
||||
* @param pagesIndex 搜索页索引
|
||||
* @return 返回搜索内容消息
|
||||
* @throws IOException 当搜索发生异常时抛出
|
||||
*/
|
||||
@Command
|
||||
public static String search(@Argument(name = "content") String content,
|
||||
@Argument(name = "type", force = false) String type,
|
||||
@Argument(name = "area", force = false) String area,
|
||||
@Argument(name = "in", force = false) String includeKeywords,
|
||||
@Argument(name = "ex", force = false) String excludeKeywords,
|
||||
@Argument(name = "contentOption", force = false) String contentOption,
|
||||
@Argument(name = "page", force = false, defaultValue = "1") int pagesIndex
|
||||
public static String search(
|
||||
@Argument(name = "$fromGroup") long fromGroup,
|
||||
@Argument(name = "content") String content,
|
||||
@Argument(name = "type", force = false) String type,
|
||||
@Argument(name = "area", force = false) String area,
|
||||
@Argument(name = "in", force = false) String includeKeywords,
|
||||
@Argument(name = "ex", force = false) String excludeKeywords,
|
||||
@Argument(name = "contentOption", force = false) String contentOption,
|
||||
@Argument(name = "page", force = false, defaultValue = "1") int pagesIndex
|
||||
) throws IOException {
|
||||
log.info("正在执行搜索...");
|
||||
PixivSearchBuilder searchBuilder = new PixivSearchBuilder(Strings.isNullOrEmpty(content) ? "" : content);
|
||||
@ -266,26 +362,26 @@ public class BotCommandProcess {
|
||||
|
||||
if (!Strings.isNullOrEmpty(includeKeywords)) {
|
||||
for (String keyword : includeKeywords.split(";")) {
|
||||
searchBuilder.removeExcludeKeyword(keyword);
|
||||
searchBuilder.addIncludeKeyword(keyword);
|
||||
searchBuilder.removeExcludeKeyword(keyword.trim());
|
||||
searchBuilder.addIncludeKeyword(keyword.trim());
|
||||
log.debug("已添加关键字: {}", keyword);
|
||||
}
|
||||
}
|
||||
if (!Strings.isNullOrEmpty(excludeKeywords)) {
|
||||
for (String keyword : excludeKeywords.split(";")) {
|
||||
searchBuilder.removeIncludeKeyword(keyword);
|
||||
searchBuilder.addExcludeKeyword(keyword);
|
||||
searchBuilder.removeIncludeKeyword(keyword.trim());
|
||||
searchBuilder.addExcludeKeyword(keyword.trim());
|
||||
log.debug("已添加排除关键字: {}", keyword);
|
||||
}
|
||||
}
|
||||
|
||||
log.info("正在搜索作品, 条件: {}", searchBuilder.getSearchCondition());
|
||||
|
||||
String requestUrl = searchBuilder.buildURL();
|
||||
String requestUrl = searchBuilder.buildURL().intern();
|
||||
log.debug("RequestUrl: {}", requestUrl);
|
||||
JsonObject resultBody = null;
|
||||
if(!searchBodyCache.exists(requestUrl)) {
|
||||
synchronized (searchCacheLock) {
|
||||
synchronized (requestUrl) {
|
||||
if (!searchBodyCache.exists(requestUrl)) {
|
||||
log.debug("searchBody缓存失效, 正在更新...");
|
||||
JsonObject jsonObject;
|
||||
@ -302,7 +398,8 @@ public class BotCommandProcess {
|
||||
}
|
||||
|
||||
long expire = 7200 * 1000;
|
||||
String propValue = globalProp.getProperty("cache.searchBody.expire", "7200000");
|
||||
String propValue = SettingProperties
|
||||
.getProperty(SettingProperties.GLOBAL, "cache.searchBody.expire", "7200000");
|
||||
try {
|
||||
expire = Long.parseLong(propValue);
|
||||
} catch (Exception e) {
|
||||
@ -327,7 +424,8 @@ public class BotCommandProcess {
|
||||
log.debug("正在处理信息...");
|
||||
int limit = 8;
|
||||
try {
|
||||
limit = Integer.parseInt(globalProp.getProperty("search.ItemCountLimit", "8"));
|
||||
limit = Integer.parseInt(SettingProperties.
|
||||
getProperty(fromGroup, "search.itemCountLimit", "8"));
|
||||
} catch (Exception e) {
|
||||
log.warn("参数转换异常!将使用默认值(" + limit + ")", e);
|
||||
}
|
||||
@ -356,7 +454,7 @@ public class BotCommandProcess {
|
||||
StringBuilder builder = new StringBuilder("[");
|
||||
illustObj.get("tags").getAsJsonArray().forEach(el -> builder.append(el.getAsString()).append(", "));
|
||||
builder.replace(builder.length() - 2, builder.length(), "]");
|
||||
log.debug("{} ({} / {})\n\t作品id: {}, \n\t作者名(作者id): {} ({}), \n\t作品标题: {}, \n\t作品Tags: {}, \n\t页数: {}, \n\t作品链接: {}",
|
||||
log.debug("{} ({} / {})\n\t作品id: {}, \n\t作者名(作者id): {} ({}), \n\t作品标题: {}, \n\t作品Tags: {}, \n\t页数: {}页, \n\t作品链接: {}",
|
||||
searchArea.name(),
|
||||
count,
|
||||
illustsList.size(),
|
||||
@ -371,16 +469,29 @@ public class BotCommandProcess {
|
||||
|
||||
//pageCount
|
||||
|
||||
String imageMsg = getImageById(illustId, PixivDownload.PageQuality.REGULAR, 1);
|
||||
if (isNoSafe(illustId, globalProp, true)) {
|
||||
String imageMsg = getImageById(fromGroup, illustId, PixivDownload.PageQuality.REGULAR, 1);
|
||||
if (isNoSafe(illustId, SettingProperties.getProperties(fromGroup), true)) {
|
||||
log.warn("作品Id {} 为R-18作品, 跳过.", illustId);
|
||||
continue;
|
||||
} else if(isReported(illustId)) {
|
||||
log.warn("作品Id {} 被报告, 正在等待审核, 跳过该作品.", illustId);
|
||||
continue;
|
||||
}
|
||||
|
||||
result.append(searchArea.name()).append(" (").append(count).append(" / ").append(limit).append(")\n\t作品id: ").append(illustId)
|
||||
JsonObject illustPreLoadData = getIllustPreLoadData(illustId, false);
|
||||
result.append(searchArea.name()).append(" (").append(count).append(" / ")
|
||||
.append(limit).append(")\n\t作品id: ").append(illustId)
|
||||
.append(", \n\t作者名: ").append(illustObj.get("userName").getAsString())
|
||||
.append("\n\t作品标题: ").append(illustObj.get("illustTitle").getAsString())
|
||||
.append("\n\t作品页数: ").append(illustObj.get("pageCount").getAsInt())
|
||||
.append("\n\t作品页数: ").append(illustObj.get("pageCount").getAsInt()).append("页")
|
||||
.append("\n\t点赞数:")
|
||||
.append(illustPreLoadData.get(PreLoadDataComparator.Attribute.LIKE.attrName).getAsInt())
|
||||
.append("\n\t收藏数:")
|
||||
.append(illustPreLoadData.get(PreLoadDataComparator.Attribute.BOOKMARK.attrName).getAsInt())
|
||||
.append("\n\t围观数:")
|
||||
.append(illustPreLoadData.get(PreLoadDataComparator.Attribute.VIEW.attrName).getAsInt())
|
||||
.append("\n\t评论数:")
|
||||
.append(illustPreLoadData.get(PreLoadDataComparator.Attribute.COMMENT.attrName).getAsInt())
|
||||
.append("\n").append(imageMsg).append("\n");
|
||||
count++;
|
||||
}
|
||||
@ -388,12 +499,25 @@ public class BotCommandProcess {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return Strings.nullToEmpty(result.toString()) + "预览图片并非原图,使用“.cgj image -id 作品id”获取原图";
|
||||
return Strings.nullToEmpty(result.toString()) + "预览图片并非原图,使用“.cgj image -id 作品id”获取原图\n如有不当作品,可使用\".cgj report -id 作品id\"向色图姬反馈。";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取作品页面的下载链接
|
||||
* @param illustId 作品Id
|
||||
* @param quality 画质类型
|
||||
* @return 返回作品所有页面在Pixiv的下载链接(有防盗链, 考虑要不要设置镜像站)
|
||||
*/
|
||||
@Command(commandName = "pages")
|
||||
public static String getPagesList(@Argument(name = "id") int illustId, @Argument(name = "quality", force = false) PixivDownload.PageQuality quality) {
|
||||
public static String getPagesList(
|
||||
@Argument(name = "$fromGroup") long fromGroup,
|
||||
@Argument(name = "id") int illustId,
|
||||
@Argument(name = "quality", force = false) PixivDownload.PageQuality quality) {
|
||||
try {
|
||||
if(isNoSafe(illustId, SettingProperties.getProperties(fromGroup), false)) {
|
||||
log.warn("来源群 {} 查询的作品Id {} 为R18作品, 根据配置设定, 屏蔽该作品.", fromGroup, illustId);
|
||||
return "该作品已被封印!";
|
||||
}
|
||||
List<String> pagesList = PixivDownload.getIllustAllPageDownload(pixivDownload.getHttpClient(), pixivDownload.getCookieStore(), illustId, quality);
|
||||
StringBuilder builder = new StringBuilder("作品ID ").append(illustId).append(" 共有").append(pagesList.size()).append("页:").append("\n");
|
||||
int index = 0;
|
||||
@ -407,12 +531,21 @@ public class BotCommandProcess {
|
||||
}
|
||||
}
|
||||
|
||||
@Command(commandName = "artworks")
|
||||
public static String artworksLink(@Argument(name = "id") int illustId) {
|
||||
/**
|
||||
* 获取作品链接
|
||||
* @param fromGroup 来源群(系统提供)
|
||||
* @param illustId 作品Id
|
||||
* @return 返回作品在Pixiv的链接
|
||||
*/
|
||||
@Command(commandName = "link")
|
||||
public static String artworksLink(@Argument(name = "$fromGroup") long fromGroup, @Argument(name = "id") int illustId) {
|
||||
try {
|
||||
if (isNoSafe(illustId, globalProp, false)) {
|
||||
if (isNoSafe(illustId, SettingProperties.getProperties(fromGroup), false)) {
|
||||
log.warn("作品Id {} 已被屏蔽.", illustId);
|
||||
return "由于相关设置,该作品已被屏蔽!";
|
||||
} else if(isReported(illustId)) {
|
||||
log.warn("作品Id {} 被报告, 正在等待审核, 跳过该作品.", illustId);
|
||||
return "该作品暂时被封印,请等待色图姬进一步审核!";
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("获取作品信息失败!", e);
|
||||
@ -423,16 +556,33 @@ public class BotCommandProcess {
|
||||
|
||||
/**
|
||||
* 通过illustId获取作品图片
|
||||
* @param fromGroup 来源群(系统提供)
|
||||
* @param illustId 作品Id
|
||||
* @param quality 图片质量
|
||||
* @param pageIndex 指定页面索引, 从1开始
|
||||
* @return 如果成功, 返回BotCode, 否则返回错误信息.
|
||||
*/
|
||||
@Command(commandName = "image")
|
||||
public static String getImageById(@Argument(name = "id") int illustId,
|
||||
@Argument(name = "quality", force = false) PixivDownload.PageQuality quality,
|
||||
@Argument(name = "page", force = false, defaultValue = "1") int pageIndex) {
|
||||
public static String getImageById(
|
||||
@Argument(name = "$fromGroup") long fromGroup,
|
||||
@Argument(name = "id") int illustId,
|
||||
@Argument(name = "quality", force = false) PixivDownload.PageQuality quality,
|
||||
@Argument(name = "page", force = false, defaultValue = "1") int pageIndex) {
|
||||
log.debug("IllustId: {}, Quality: {}, PageIndex: {}", illustId, quality.name(), pageIndex);
|
||||
|
||||
try {
|
||||
if (isNoSafe(illustId, SettingProperties.getProperties(fromGroup), false)) {
|
||||
log.warn("作品 {} 存在R-18内容且设置\"image.allowR18\"为false,将屏蔽该作品不发送.", illustId);
|
||||
return "(根据设置,该作品已被屏蔽!)";
|
||||
} else if(isReported(illustId)) {
|
||||
log.warn("作品Id {} 被报告, 正在等待审核, 跳过该作品.", illustId);
|
||||
return "(该作品已被封印)";
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.warn("作品信息无法获取!", e);
|
||||
return "发生网络异常,无法获取图片!";
|
||||
}
|
||||
|
||||
List<String> pagesList;
|
||||
try {
|
||||
pagesList = getIllustPages(illustId, quality, false);
|
||||
@ -442,7 +592,7 @@ public class BotCommandProcess {
|
||||
}
|
||||
|
||||
if(log.isDebugEnabled()) {
|
||||
StringBuilder logBuilder = new StringBuilder("作品Id {} 所有页面下载链接: \n");
|
||||
StringBuilder logBuilder = new StringBuilder("作品Id " + illustId + " 所有页面下载链接: \n");
|
||||
AtomicInteger index = new AtomicInteger();
|
||||
pagesList.forEach(item -> logBuilder.append(index.incrementAndGet()).append(". ").append(item).append("\n"));
|
||||
log.debug(logBuilder.toString());
|
||||
@ -453,16 +603,6 @@ public class BotCommandProcess {
|
||||
return "指定的页数超出了范围(总共 " + pagesList.size() + " 页)";
|
||||
}
|
||||
|
||||
try {
|
||||
if (isNoSafe(illustId, globalProp, false)) {
|
||||
log.warn("作品 {} 存在R-18内容且设置\"image.allowR18\"为false,将屏蔽该作品不发送.", illustId);
|
||||
return "(根据设置,该作品已被屏蔽!)";
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.warn("作品信息无法获取!", e);
|
||||
return "发生网络异常,无法获取图片!";
|
||||
}
|
||||
|
||||
String downloadLink = pagesList.get(pageIndex - 1);
|
||||
String fileName = URLs.getResourceName(Strings.nullToEmpty(downloadLink));
|
||||
File imageFile = new File(getImageStoreDir(), downloadLink.substring(downloadLink.lastIndexOf("/") + 1));
|
||||
@ -487,12 +627,11 @@ public class BotCommandProcess {
|
||||
}
|
||||
}
|
||||
|
||||
ImageCacheObject taskObject = new ImageCacheObject(imageCache, illustId, downloadLink, imageFile);
|
||||
try {
|
||||
imageCacheExecutor.executorSync(taskObject);
|
||||
ImageCacheStore.executeCacheRequest(new ImageCacheObject(imageCache, illustId, downloadLink, imageFile));
|
||||
} catch (InterruptedException e) {
|
||||
log.error("等待图片下载时发生中断", e);
|
||||
return "图片获取失败!";
|
||||
log.warn("图片缓存被中断", e);
|
||||
return "(错误:图片获取超时)";
|
||||
}
|
||||
} else {
|
||||
log.debug("图片 {} 缓存命中.", fileName);
|
||||
@ -501,6 +640,12 @@ public class BotCommandProcess {
|
||||
return getImageToBotCode(imageCache.get(fileName), false).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过文件获取图片的BotCode代码
|
||||
* @param targetFile 图片文件
|
||||
* @param updateCache 是否刷新缓存(只是让机器人重新上传, 如果上传接口有重复检测的话是无法处理的)
|
||||
* @return 返回设定好参数的BotCode
|
||||
*/
|
||||
private static BotCode getImageToBotCode(File targetFile, boolean updateCache) {
|
||||
String fileName = targetFile.getName();
|
||||
BotCode code = BotCode.parse(CQCode.image(getImageStoreDir().getName() + "/" + fileName));
|
||||
@ -517,7 +662,7 @@ public class BotCommandProcess {
|
||||
illustPreLoadDataCache.clear();
|
||||
pagesCache.clear();
|
||||
searchBodyCache.clear();
|
||||
File imageStoreDir = new File(System.getProperty("cgj.cqRootDir") + "data/image/cgj/");
|
||||
File imageStoreDir = new File(System.getProperty("cgj.botDataDir") + "data/image/cgj/");
|
||||
File[] listFiles = imageStoreDir.listFiles();
|
||||
if (listFiles == null) {
|
||||
log.debug("图片缓存目录为空或内部文件获取失败!");
|
||||
@ -530,23 +675,67 @@ public class BotCommandProcess {
|
||||
log.warn("缓存删除完成.");
|
||||
}
|
||||
|
||||
/*
|
||||
下一目标:
|
||||
添加定时发图
|
||||
定时发图支持设置关注标签
|
||||
标签....标签支持搜索吧
|
||||
/**
|
||||
* 举报某一作品
|
||||
* @param fromGroup 来源群(系统提供)
|
||||
* @param illustId 需要举报的作品id
|
||||
* @param reason 举报原因
|
||||
* @return 返回提示信息
|
||||
*/
|
||||
@Command
|
||||
public static String report(
|
||||
@Argument(name = "$fromGroup") long fromGroup,
|
||||
@Argument(name = "$fromQQ") long fromQQ,
|
||||
@Argument(name = "id") int illustId,
|
||||
@Argument(name = "msg", force = false) String reason
|
||||
) {
|
||||
log.warn("收到作品反馈(IllustId: {}, 原因: {})", illustId, reason);
|
||||
JsonObject reportJson = new JsonObject();
|
||||
reportJson.addProperty("illustId", illustId);
|
||||
reportJson.addProperty("reportTime", new Date().getTime());
|
||||
reportJson.addProperty("fromGroup", fromGroup);
|
||||
reportJson.addProperty("fromQQ", fromQQ);
|
||||
reportJson.addProperty("reason", reason);
|
||||
reportStore.update(String.valueOf(illustId), reportJson, 0);
|
||||
return "色图姬收到了你的报告,将屏蔽该作品并对作品违规情况进行核实,感谢你的反馈!";
|
||||
}
|
||||
|
||||
private static boolean isNoSafe(int illustId, Properties settingProp, boolean returnRaw) throws IOException {
|
||||
/**
|
||||
* 检查某一作品是否被报告
|
||||
* @param illustId 作品Id
|
||||
* @return 如果被报告了, 返回true
|
||||
*/
|
||||
public static boolean isReported(int illustId) {
|
||||
return reportStore.exists(String.valueOf(illustId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查指定作品是否为r18
|
||||
* @param illustId 作品Id
|
||||
* @param settingProp 配置项
|
||||
* @param returnRaw 是否返回原始值
|
||||
* @return 如果为true, 则不为全年龄
|
||||
* @throws IOException 获取数据时发生异常时抛出
|
||||
* @throws NoSuchElementException 当作品不存在时抛出
|
||||
*/
|
||||
public static boolean isNoSafe(int illustId, Properties settingProp, boolean returnRaw) throws IOException, NoSuchElementException {
|
||||
boolean rawValue = getIllustInfo(illustId, false).getAsJsonArray("tags").contains(new JsonPrimitive("R-18"));
|
||||
return returnRaw || settingProp == null ? rawValue : rawValue && !settingProp.getProperty("image.allowR18", "false").equalsIgnoreCase("true");
|
||||
}
|
||||
|
||||
private static JsonObject getIllustInfo(int illustId, boolean flushCache) throws IOException {
|
||||
/**
|
||||
* 获取作品信息
|
||||
* @param illustId 作品Id
|
||||
* @param flushCache 强制刷新缓存
|
||||
* @return 返回作品信息
|
||||
* @throws IOException 当Http请求发生异常时抛出
|
||||
* @throws NoSuchElementException 当作品未找到时抛出
|
||||
*/
|
||||
private static JsonObject getIllustInfo(int illustId, boolean flushCache) throws IOException, NoSuchElementException {
|
||||
String illustIdStr = buildSyncKey(Integer.toString(illustId));
|
||||
JsonObject illustInfoObj = null;
|
||||
if (!illustInfoCache.exists(illustIdStr) || flushCache) {
|
||||
synchronized (illustIdStr) { // TODO: 这里要不做成HashMap存储key而避免使用常量池?
|
||||
synchronized (illustIdStr) {
|
||||
if (!illustInfoCache.exists(illustIdStr) || flushCache) {
|
||||
illustInfoObj = pixivDownload.getIllustInfoByIllustId(illustId);
|
||||
illustInfoCache.update(illustIdStr, illustInfoObj, null);
|
||||
@ -575,13 +764,14 @@ public class BotCommandProcess {
|
||||
if (!illustPreLoadDataCache.exists(illustIdStr) || flushCache) {
|
||||
synchronized (illustIdStr) {
|
||||
if (!illustPreLoadDataCache.exists(illustIdStr) || flushCache) {
|
||||
log.info("IllustId {} 缓存失效, 正在更新...", illustId);
|
||||
log.debug("IllustId {} 缓存失效, 正在更新...", illustId);
|
||||
JsonObject preLoadDataObj = pixivDownload.getIllustPreLoadDataById(illustId)
|
||||
.getAsJsonObject("illust")
|
||||
.getAsJsonObject(Integer.toString(illustId));
|
||||
|
||||
long expire = 7200 * 1000;
|
||||
String propValue = globalProp.getProperty("cache.illustPreLoadData.expire", "7200000");
|
||||
String propValue = SettingProperties.
|
||||
getProperty(SettingProperties.GLOBAL, "cache.illustPreLoadData.expire", "7200000");
|
||||
log.debug("PreLoadData有效时间设定: {}", propValue);
|
||||
try {
|
||||
expire = Long.parseLong(propValue);
|
||||
@ -591,7 +781,7 @@ public class BotCommandProcess {
|
||||
|
||||
result = preLoadDataObj;
|
||||
illustPreLoadDataCache.update(illustIdStr, preLoadDataObj, expire);
|
||||
log.info("作品Id {} preLoadData缓存已更新(有效时间: {})", illustId, expire);
|
||||
log.debug("作品Id {} preLoadData缓存已更新(有效时间: {})", illustId, expire);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -622,11 +812,19 @@ public class BotCommandProcess {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取图片存储目录.
|
||||
* <p>每次调用都会检查目录是否存在, 如不存在则会抛出异常</p>
|
||||
* @return 返回File对象
|
||||
* @throws RuntimeException 当目录创建失败时将包装{@link IOException}异常并抛出.
|
||||
*/
|
||||
private static File getImageStoreDir() {
|
||||
if(!imageStoreDir.exists() && !imageStoreDir.mkdirs()) {
|
||||
log.warn("酷Q图片缓存目录失效!(Path: {} )", imageStoreDir.getAbsolutePath());
|
||||
throw new RuntimeException(new IOException("文件夹创建失败!"));
|
||||
if(!imageStoreDir.exists() && !Files.isSymbolicLink(imageStoreDir.toPath())) {
|
||||
if(!imageStoreDir.mkdirs()) {
|
||||
log.warn("酷Q图片缓存目录失效!(Path: {} )", imageStoreDir.getAbsolutePath());
|
||||
throw new RuntimeException(new IOException("文件夹创建失败!"));
|
||||
}
|
||||
}
|
||||
return imageStoreDir;
|
||||
}
|
||||
@ -663,15 +861,16 @@ public class BotCommandProcess {
|
||||
if(!rankingCache.exists(requestSign) || flushCache) {
|
||||
synchronized(requestSign) {
|
||||
if(!rankingCache.exists(requestSign) || flushCache) {
|
||||
log.info("Ranking缓存失效, 正在更新...(RequestSign: {})", requestSign);
|
||||
log.debug("Ranking缓存失效, 正在更新...(RequestSign: {})", requestSign);
|
||||
List<JsonObject> rankingResult = pixivDownload.getRanking(contentType, mode, queryDate, 1, 500);
|
||||
long expireTime = 0;
|
||||
if(rankingResult.size() == 0) {
|
||||
log.info("数据获取失败, 将设置浮动有效时间以准备下次更新.");
|
||||
expireTime = 5400000 + expireTimeFloatRandom.nextInt(1800000);
|
||||
log.warn("数据获取失败, 将设置浮动有效时间以准备下次更新. (ExpireTime: {}ms)", expireTime);
|
||||
}
|
||||
result = new ArrayList<>(rankingResult).subList(start - 1, range);
|
||||
rankingCache.update(requestSign, rankingResult,
|
||||
rankingResult.size() == 0 ? 5400000 + expireTimeFloatRandom.nextInt(1800000) : 0);
|
||||
log.info("Ranking缓存更新完成.(RequestSign: {})", requestSign);
|
||||
result = new ArrayList<>(rankingResult).subList(start - 1, start + range - 1);
|
||||
rankingCache.update(requestSign, rankingResult, expireTime);
|
||||
log.debug("Ranking缓存更新完成.(RequestSign: {})", requestSign);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,83 @@
|
||||
package net.lamgc.cgj.bot;
|
||||
|
||||
import net.lamgc.cgj.bot.event.MessageEvent;
|
||||
import net.lamgc.cgj.bot.event.VirtualLoadMessageEvent;
|
||||
import net.lamgc.utils.event.EventExecutor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* 消息事件处理调试器.
|
||||
* <p>当启用了消息事件处理调试后, 将会根据调试器代号调用指定调试器</p>
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public enum MessageEventExecutionDebugger {
|
||||
|
||||
/**
|
||||
* PM - 压力测试
|
||||
*/
|
||||
PM ((executor, event, properties, log) -> {
|
||||
MessageEvent virtualLoadEvent = VirtualLoadMessageEvent.toVirtualLoadMessageEvent(event, false);
|
||||
int rotation = 5;
|
||||
int number = 50;
|
||||
int interval = 2500;
|
||||
|
||||
try {
|
||||
rotation = Integer.parseInt(properties.getProperty("debug.pm.rotation", "5"));
|
||||
} catch(NumberFormatException e) {
|
||||
log.warn("配置项 {} 值无效, 将使用默认值.({})", "debug.pm.rotation", rotation);
|
||||
}
|
||||
try {
|
||||
number = Integer.parseInt(properties.getProperty("debug.pm.number", "50"));
|
||||
} catch(NumberFormatException e) {
|
||||
log.warn("配置项 {} 值无效, 将使用默认值.({})", "debug.pm.number", number);
|
||||
}
|
||||
try {
|
||||
interval = Integer.parseInt(properties.getProperty("debug.pm.interval", "2500"));
|
||||
} catch(NumberFormatException e) {
|
||||
log.warn("配置项 {} 值无效, 将使用默认值.({})", "debug.pm.interval", interval);
|
||||
}
|
||||
|
||||
boolean interrupted = false;
|
||||
Thread currentThread = Thread.currentThread();
|
||||
for(int rotationCount = 0; rotationCount < rotation && !interrupted; rotationCount++) {
|
||||
for(int sendCount = 0; sendCount < number; sendCount++) {
|
||||
if(currentThread.isInterrupted()) {
|
||||
interrupted = true;
|
||||
break;
|
||||
}
|
||||
executor.executor(virtualLoadEvent);
|
||||
}
|
||||
try {
|
||||
Thread.sleep(interval);
|
||||
} catch (InterruptedException e) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
public final MessageExecuteDebugger debugger;
|
||||
|
||||
MessageEventExecutionDebugger(MessageExecuteDebugger debugger) {
|
||||
this.debugger = debugger;
|
||||
}
|
||||
|
||||
public static Logger getDebuggerLogger(MessageEventExecutionDebugger debugger) {
|
||||
return LoggerFactory.getLogger(MessageEventExecutionDebugger.class.getName() + "." + debugger.name());
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface MessageExecuteDebugger {
|
||||
/**
|
||||
* 接收事件并根据指定需求处理
|
||||
* @param executor 事件执行器
|
||||
* @param event 消息事件对象
|
||||
* @param properties 配置项, 调试器应按'debug.[debuggerName].'为前缀存储相应调试信息
|
||||
* @throws Exception 当抛出异常则打断调试, 并输出至日志
|
||||
*/
|
||||
void accept(EventExecutor executor, MessageEvent event, Properties properties, Logger logger) throws Exception;
|
||||
}
|
||||
|
||||
}
|
@ -1,53 +0,0 @@
|
||||
package net.lamgc.cgj.bot;
|
||||
|
||||
import net.lamgc.cgj.bot.event.BotEventHandler;
|
||||
import net.lamgc.cgj.bot.event.MiraiMessageEvent;
|
||||
import net.mamoe.mirai.Bot;
|
||||
import net.mamoe.mirai.japt.Events;
|
||||
import net.mamoe.mirai.message.FriendMessage;
|
||||
import net.mamoe.mirai.message.GroupMessage;
|
||||
import net.mamoe.mirai.qqandroid.QQAndroid;
|
||||
import net.mamoe.mirai.utils.BotConfiguration;
|
||||
import org.apache.commons.net.util.Base64;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Properties;
|
||||
|
||||
public class MiraiMain implements Closeable {
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger(this.toString());
|
||||
|
||||
private Bot bot;
|
||||
|
||||
private final static Properties botProperties = new Properties();
|
||||
|
||||
public void init() {
|
||||
try {
|
||||
Class.forName(BotEventHandler.class.getName());
|
||||
} catch (ClassNotFoundException e) {
|
||||
log.error("加载BotEventHandler时发生异常", e);
|
||||
return;
|
||||
}
|
||||
|
||||
File botPropFile = new File("./bot.properties");
|
||||
try (Reader reader = new BufferedReader(new FileReader(botPropFile))) {
|
||||
botProperties.load(reader);
|
||||
} catch (IOException e) {
|
||||
log.error("机器人配置文件读取失败!", e);
|
||||
return;
|
||||
}
|
||||
|
||||
bot = QQAndroid.INSTANCE.newBot(Long.parseLong(botProperties.getProperty("bot.qq", "0")), Base64.decodeBase64(botProperties.getProperty("bot.password", "")), new BotConfiguration());
|
||||
Events.subscribeAlways(GroupMessage.class, (msg) -> BotEventHandler.executor.executor(new MiraiMessageEvent(msg)));
|
||||
Events.subscribeAlways(FriendMessage.class, (msg) -> BotEventHandler.executor.executor(new MiraiMessageEvent(msg)));
|
||||
bot.login();
|
||||
bot.join();
|
||||
}
|
||||
|
||||
public void close() {
|
||||
bot.close(null);
|
||||
}
|
||||
|
||||
}
|
151
src/main/java/net/lamgc/cgj/bot/RandomIntervalSendTimer.java
Normal file
151
src/main/java/net/lamgc/cgj/bot/RandomIntervalSendTimer.java
Normal file
@ -0,0 +1,151 @@
|
||||
package net.lamgc.cgj.bot;
|
||||
|
||||
import com.google.common.base.Throwables;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* 随机间隔发送器
|
||||
*/
|
||||
public class RandomIntervalSendTimer extends TimerTask {
|
||||
|
||||
private final static Timer timer = new Timer("Thread-RIST");
|
||||
private final static Logger log = LoggerFactory.getLogger(RandomIntervalSendTimer.class.getName());
|
||||
private final static Map<Long, RandomIntervalSendTimer> timerMap = new HashMap<>();
|
||||
|
||||
private final long timerId;
|
||||
private final Random timeRandom = new Random();
|
||||
private final AutoSender sender;
|
||||
private final long time;
|
||||
private final int floatTime;
|
||||
private AtomicBoolean loop = new AtomicBoolean();
|
||||
private final AtomicBoolean start = new AtomicBoolean();
|
||||
private final String hashId = Integer.toHexString(this.hashCode());
|
||||
|
||||
|
||||
/**
|
||||
* 创建一个随机延迟发送器
|
||||
* @param timerId 该Timer的标识,
|
||||
* 标识必须是唯一的, 当使用相同id创建时, 将会返回该id所属的Timer对象
|
||||
* @param sender 自动发送器
|
||||
* @param time 最低时间(ms)
|
||||
* @param floatTime 浮动时间(ms)
|
||||
* @param startNow 现在开始
|
||||
* @param loop 是否循环
|
||||
*/
|
||||
public static RandomIntervalSendTimer createTimer(long timerId, AutoSender sender, long time, int floatTime, boolean startNow, boolean loop) {
|
||||
if(timerMap.containsKey(timerId)) {
|
||||
return timerMap.get(timerId);
|
||||
}
|
||||
|
||||
RandomIntervalSendTimer timer = new RandomIntervalSendTimer(timerId, sender, time, floatTime, startNow, loop);
|
||||
timerMap.put(timerId, timer);
|
||||
return timer;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过Id获取Timer
|
||||
* @param id 待获取Timer对应的Id
|
||||
* @return 返回RandomIntervalSendTimer对象
|
||||
* @throws NoSuchElementException 当不存在Timer时抛出
|
||||
*/
|
||||
public static RandomIntervalSendTimer getTimerById(long id) {
|
||||
if(!timerMap.containsKey(id)) {
|
||||
throw new NoSuchElementException("id=" + id);
|
||||
}
|
||||
return timerMap.get(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有id
|
||||
* @return 所有TimerId的集合
|
||||
*/
|
||||
public static Set<Long> timerIdSet() {
|
||||
return new HashSet<>(timerMap.keySet());
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个随机延迟发送器
|
||||
* @param timerId 该Timer的标识
|
||||
* @param sender 自动发送器
|
||||
* @param time 最低时间(ms)
|
||||
* @param floatTime 浮动时间(ms)
|
||||
* @param startNow 现在开始
|
||||
* @param loop 是否循环
|
||||
*/
|
||||
private RandomIntervalSendTimer(long timerId, AutoSender sender, long time, int floatTime, boolean startNow, boolean loop) {
|
||||
this.timerId = timerId;
|
||||
this.sender = sender;
|
||||
this.time = time;
|
||||
this.floatTime = floatTime;
|
||||
timerMap.put(timerId, this);
|
||||
if(startNow) {
|
||||
start(loop);
|
||||
}
|
||||
}
|
||||
|
||||
public void start() {
|
||||
start(this.loop.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动定时器
|
||||
* @param loop 是否循环, 如果为true, 则任务完成后会自动调用start方法继续循环, 直到被调用{@code #}或总定时器被销毁;
|
||||
*/
|
||||
public void start(boolean loop) {
|
||||
this.loop.set(loop);
|
||||
long nextDelay = time + timeRandom.nextInt(floatTime);
|
||||
Date nextDate = new Date();
|
||||
nextDate.setTime(nextDate.getTime() + nextDelay);
|
||||
log.info("定时器 {} 下一延迟: {}ms ({})", hashId, nextDelay, nextDate);
|
||||
if(start.get()) {
|
||||
try {
|
||||
Field state = this.getClass().getSuperclass().getDeclaredField("state");
|
||||
state.setAccessible(true);
|
||||
state.setInt(this, 0);
|
||||
state.setAccessible(false);
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
start.set(true);
|
||||
timer.schedule(this, nextDelay);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
sender.send();
|
||||
} catch (Exception e) {
|
||||
log.error("定时器 {} 执行时发生异常:\n{}", Integer.toHexString(this.hashCode()), Throwables.getStackTraceAsString(e));
|
||||
}
|
||||
if (this.loop.get()) {
|
||||
start();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消该定时器
|
||||
* @return 取消成功返回true
|
||||
*/
|
||||
@Override
|
||||
public boolean cancel() {
|
||||
start.set(false);
|
||||
loop.set(false);
|
||||
return super.cancel();
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁这个定时器
|
||||
*/
|
||||
public void destroy() {
|
||||
cancel();
|
||||
timerMap.remove(this.timerId);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,99 @@
|
||||
package net.lamgc.cgj.bot;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import net.lamgc.cgj.bot.message.MessageSender;
|
||||
import net.lamgc.cgj.pixiv.PixivDownload;
|
||||
import net.lamgc.cgj.pixiv.PixivURL;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* 推荐作品发送器
|
||||
*/
|
||||
public class RandomRankingArtworksSender extends AutoSender {
|
||||
|
||||
private final Logger log;
|
||||
private final int rankingStart;
|
||||
private final int rankingStop;
|
||||
private final PixivURL.RankingMode mode;
|
||||
private final PixivURL.RankingContentType contentType;
|
||||
private final PixivDownload.PageQuality quality;
|
||||
|
||||
/**
|
||||
* 构造一个推荐作品发送器
|
||||
* @param messageSender 消息发送器
|
||||
* @param rankingStart 排行榜开始范围(从1开始, 名次),如传入0或负数则为默认值,默认为1
|
||||
* @param rankingStop 排名榜结束范围(包括该名次),如传入0或负数则为默认值,默认为150
|
||||
* @param mode 排行榜模式
|
||||
* @param contentType 排行榜内容类型
|
||||
* @param quality 图片质量, 详见{@link PixivDownload.PageQuality}
|
||||
* @throws IndexOutOfBoundsException 当 rankingStart > rankingStop时抛出
|
||||
*/
|
||||
public RandomRankingArtworksSender(MessageSender messageSender, int rankingStart, int rankingStop, PixivURL.RankingMode mode, PixivURL.RankingContentType contentType, PixivDownload.PageQuality quality) {
|
||||
super(messageSender);
|
||||
this.mode = mode;
|
||||
this.contentType = contentType;
|
||||
log = LoggerFactory.getLogger(this.toString());
|
||||
this.rankingStart = rankingStart > 0 ? rankingStart : 1;
|
||||
this.rankingStop = rankingStop > 0 ? rankingStop : 150;
|
||||
if(this.rankingStart > this.rankingStop) {
|
||||
throw new IndexOutOfBoundsException("rankingStart=" + this.rankingStart + ", rankingStop=" + this.rankingStop);
|
||||
}
|
||||
this.quality = quality == null ? PixivDownload.PageQuality.REGULAR : quality;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send() {
|
||||
Date queryDate = new Date();
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(queryDate);
|
||||
if(calendar.get(Calendar.HOUR_OF_DAY) < 12) {
|
||||
calendar.add(Calendar.DAY_OF_YEAR, -2);
|
||||
} else {
|
||||
calendar.add(Calendar.DAY_OF_YEAR, -1);
|
||||
}
|
||||
queryDate = calendar.getTime();
|
||||
|
||||
int selectRanking = rankingStart + new Random().nextInt(rankingStop - rankingStart + 1);
|
||||
try {
|
||||
List<JsonObject> rankingList = BotCommandProcess.getRankingInfoByCache(
|
||||
contentType,
|
||||
mode,
|
||||
queryDate,
|
||||
selectRanking,
|
||||
1, false);
|
||||
|
||||
log.debug("RankingResult.size: {}", rankingList.size());
|
||||
if(rankingList.size() != 1) {
|
||||
log.error("排行榜选取失败!(获取到了多个结果)");
|
||||
return;
|
||||
}
|
||||
|
||||
JsonObject rankingInfo = rankingList.get(0);
|
||||
int illustId = rankingInfo.get("illust_id").getAsInt();
|
||||
if(BotCommandProcess.isNoSafe(illustId, SettingProperties.getProperties(SettingProperties.GLOBAL), false)) {
|
||||
log.warn("作品为r18作品, 取消本次发送.");
|
||||
return;
|
||||
} else if(BotCommandProcess.isReported(illustId)) {
|
||||
log.warn("作品Id {} 被报告, 正在等待审核, 跳过该作品.", illustId);
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder message = new StringBuilder();
|
||||
message.append("#美图推送 - 今日排行榜 第 ").append(rankingInfo.get("rank").getAsInt()).append(" 名\n");
|
||||
message.append("标题:").append(rankingInfo.get("title").getAsString()).append("(").append(illustId).append(")\n");
|
||||
message.append("作者:").append(rankingInfo.get("user_name").getAsString()).append("\n");
|
||||
message.append(BotCommandProcess.getImageById(0, illustId, quality, 1));
|
||||
message.append("\n如有不当作品,可使用\".cgj report -id ").append(illustId).append("\"向色图姬反馈。");
|
||||
getMessageSender().sendMessage(message.toString());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,10 +1,12 @@
|
||||
package net.lamgc.cgj.bot;
|
||||
|
||||
import net.lamgc.cgj.bot.event.BotEventHandler;
|
||||
import net.lamgc.cgj.bot.event.VirtualLoadMessageEvent;
|
||||
import net.lamgc.cgj.pixiv.PixivURL;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
@ -25,38 +27,42 @@ public class RankingUpdateTimer {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(firstRunDate == null ? new Date() : firstRunDate);
|
||||
LocalDate currentLocalDate = LocalDate.now();
|
||||
if(cal.get(Calendar.DAY_OF_YEAR) <= currentLocalDate.getDayOfYear() && cal.get(Calendar.HOUR_OF_DAY) >= 12) {
|
||||
cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) + 1);
|
||||
if(cal.get(Calendar.DAY_OF_YEAR) < currentLocalDate.getDayOfYear() || (
|
||||
cal.get(Calendar.DAY_OF_YEAR) == currentLocalDate.getDayOfYear() &&
|
||||
(cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE) >= 690))) {
|
||||
cal.set(Calendar.DAY_OF_YEAR, currentLocalDate.getDayOfYear() + 1);
|
||||
}
|
||||
cal.set(Calendar.HOUR_OF_DAY, 12);
|
||||
cal.set(Calendar.HOUR_OF_DAY, 11);
|
||||
cal.set(Calendar.MINUTE, 30);
|
||||
cal.set(Calendar.SECOND, 0);
|
||||
cal.set(Calendar.MILLISECOND, 0);
|
||||
|
||||
log.warn("已设置排行榜定时更新, 首次运行时间: {}", cal.getTime());
|
||||
long delay = cal.getTime().getTime() - System.currentTimeMillis();
|
||||
log.warn("已设置排行榜定时更新, 首次运行时间: {} ({}min)", cal.getTime(), delay / 1000 / 60);
|
||||
timer.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
now(null);
|
||||
}
|
||||
}, cal.getTime(), 86400000); // 1 Day
|
||||
}, delay, 86400000); // 1 Day
|
||||
}
|
||||
|
||||
public void now(Date queryDate) {
|
||||
log.info("当前时间 {}, 定时任务开始执行...", new Date());
|
||||
log.warn("当前时间 {}, 定时任务开始执行...", new Date());
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(queryDate == null ? new Date() : queryDate);
|
||||
|
||||
LocalDate currentLocalDate = LocalDate.now();
|
||||
if(calendar.get(Calendar.DAY_OF_YEAR) == currentLocalDate.getDayOfYear() ||
|
||||
calendar.get(Calendar.DAY_OF_YEAR) == currentLocalDate.getDayOfYear() - 1) {
|
||||
if(calendar.get(Calendar.HOUR_OF_DAY) < 12) {
|
||||
if(calendar.get(Calendar.HOUR_OF_DAY) < 11) {
|
||||
calendar.add(Calendar.DAY_OF_YEAR, -2);
|
||||
} else {
|
||||
calendar.add(Calendar.DAY_OF_YEAR, -1);
|
||||
}
|
||||
}
|
||||
|
||||
String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
|
||||
log.info("正在获取 {} 期排行榜数据...", calendar.getTime());
|
||||
for (PixivURL.RankingMode rankingMode : PixivURL.RankingMode.values()) {
|
||||
for (PixivURL.RankingContentType contentType : PixivURL.RankingContentType.values()) {
|
||||
@ -64,15 +70,12 @@ public class RankingUpdateTimer {
|
||||
log.debug("不支持的类型, 填空值跳过...(类型: {}.{})", rankingMode.name(), contentType.name());
|
||||
}
|
||||
log.info("当前排行榜类型: {}.{}, 正在更新...", rankingMode.name(), contentType.name());
|
||||
try {
|
||||
BotCommandProcess.getRankingInfoByCache(contentType, rankingMode, calendar.getTime(), 1, 0, true);
|
||||
log.info("排行榜 {}.{} 更新完成.", rankingMode.name(), contentType.name());
|
||||
} catch (IOException e) {
|
||||
log.error("排行榜 {}.{} 更新时发生异常", rankingMode.name(), contentType.name());
|
||||
log.error("异常信息如下", e);
|
||||
}
|
||||
BotEventHandler.executeMessageEvent(new VirtualLoadMessageEvent(0,0,
|
||||
".cgj ranking -type=" + contentType.name() + " -mode=" + rankingMode.name() + " -force -date " + dateStr));
|
||||
log.info("排行榜 {}.{} 负载指令已投递.", rankingMode.name(), contentType.name());
|
||||
}
|
||||
}
|
||||
log.warn("定时任务更新完成.");
|
||||
}
|
||||
|
||||
/**
|
||||
|
241
src/main/java/net/lamgc/cgj/bot/SettingProperties.java
Normal file
241
src/main/java/net/lamgc/cgj/bot/SettingProperties.java
Normal file
@ -0,0 +1,241 @@
|
||||
package net.lamgc.cgj.bot;
|
||||
|
||||
import com.google.common.base.Throwables;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
|
||||
public final class SettingProperties {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(SettingProperties.class.getName());
|
||||
|
||||
private final static File globalPropFile = new File(getPropertiesDir(), "global.properties");
|
||||
private final static Properties globalProp = new Properties();
|
||||
|
||||
private final static Map<Long, Properties> groupPropMap = new HashMap<>();
|
||||
|
||||
private final static Set<Long> changeList = Collections.synchronizedSet(new HashSet<>());
|
||||
|
||||
/**
|
||||
* 全局配置项
|
||||
*/
|
||||
public final static long GLOBAL = 0;
|
||||
|
||||
/**
|
||||
* 清空所有Properties.
|
||||
*/
|
||||
public static void clearProperties() {
|
||||
groupPropMap.clear();
|
||||
globalProp.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载配置文件
|
||||
*/
|
||||
public static void loadProperties() {
|
||||
loadGlobalProperties();
|
||||
|
||||
File[] files = getPropertiesDir()
|
||||
.listFiles((dir, fileName) -> fileName.startsWith("group.") && fileName.endsWith(".properties"));
|
||||
if(files == null) {
|
||||
log.error("检索群组配置文件失败, 可能是被拒绝访问.");
|
||||
return;
|
||||
}
|
||||
|
||||
for (File file : files) {
|
||||
String name = file.getName();
|
||||
long groupId;
|
||||
try {
|
||||
groupId = Long.parseLong(name.substring(name.indexOf("group.") + 6, name.lastIndexOf(".properties")));
|
||||
} catch (NumberFormatException e) {
|
||||
log.error("非法的配置文件名: {}", name);
|
||||
continue;
|
||||
}
|
||||
if(!groupPropMap.containsKey(groupId)) {
|
||||
groupPropMap.put(groupId, new Properties(globalProp));
|
||||
}
|
||||
|
||||
loadGroupProperties(groupId, groupPropMap.get(groupId));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存配置项
|
||||
*/
|
||||
public static void saveProperties() {
|
||||
log.info("正在保存所有配置...");
|
||||
saveGlobalProperties();
|
||||
|
||||
for (Long groupId : groupPropMap.keySet()) {
|
||||
if(!changeList.contains(groupId)) {
|
||||
log.debug("群组 {} 配置无改动, 忽略保存.", groupId);
|
||||
return;
|
||||
}
|
||||
log.debug("正在保存群组 {} 配置文件...", groupId);
|
||||
saveGroupProperties(groupId);
|
||||
}
|
||||
log.info("配置保存完成.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存指定群组的配置文件
|
||||
* @param groupId 要保存配置的群组Id
|
||||
*/
|
||||
private static void saveGroupProperties(long groupId) {
|
||||
try {
|
||||
saveGroupProperties(groupId, getGroupProperties(groupId));
|
||||
} catch (IOException e) {
|
||||
log.error("群组 {} 配置保存失败\n{}", groupId, Throwables.getStackTraceAsString(e));
|
||||
}
|
||||
}
|
||||
|
||||
private static void saveGroupProperties(Long groupId, Properties properties) throws IOException {
|
||||
File groupPropFile = new File(getPropertiesDir(), "group." + groupId + ".properties");
|
||||
if(!groupPropFile.exists() && !groupPropFile.createNewFile()) {
|
||||
log.error("群组 {} 配置文件创建失败!", groupId);
|
||||
return;
|
||||
}
|
||||
|
||||
saveProperties(properties, new FileOutputStream(groupPropFile));
|
||||
}
|
||||
|
||||
private static void loadGlobalProperties() {
|
||||
if(globalPropFile.exists() && globalPropFile.isFile()) {
|
||||
log.info("正在加载全局配置文件...");
|
||||
try (Reader reader = new InputStreamReader(new FileInputStream(globalPropFile), StandardCharsets.UTF_8)) {
|
||||
globalProp.load(reader);
|
||||
log.info("全局配置文件加载完成.");
|
||||
} catch (IOException e) {
|
||||
log.error("加载全局配置文件时发生异常", e);
|
||||
}
|
||||
} else {
|
||||
log.info("未找到全局配置文件,跳过加载.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存全局配置项
|
||||
*/
|
||||
private static void saveGlobalProperties() {
|
||||
try {
|
||||
if(!globalPropFile.exists() && !globalPropFile.createNewFile()) {
|
||||
log.error("创建全局配置文件失败.");
|
||||
return;
|
||||
}
|
||||
|
||||
saveProperties(globalProp, new FileOutputStream(globalPropFile));
|
||||
} catch (IOException e) {
|
||||
log.error("全局配置文件保存时发生异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void loadGroupProperties(long groupId, Properties properties) {
|
||||
File propFile = new File(getPropertiesDir(), "group." + groupId + ".properties");
|
||||
Properties groupProp = Objects.requireNonNull(properties);
|
||||
if(!propFile.exists() || !propFile.isFile()) {
|
||||
log.warn("群组 {} 配置文件不存在, 或不是一个文件.({})", groupId, propFile.getAbsolutePath());
|
||||
return;
|
||||
}
|
||||
|
||||
try (Reader reader = new InputStreamReader(new FileInputStream(propFile), StandardCharsets.UTF_8)) {
|
||||
groupProp.load(reader);
|
||||
} catch (IOException e) {
|
||||
log.error("读取群组 {} 群配置文件时发生异常:\n{}", groupId, Throwables.getStackTraceAsString(e));
|
||||
}
|
||||
}
|
||||
|
||||
private static void saveProperties(Properties properties, OutputStream stream) throws IOException {
|
||||
properties.store(new OutputStreamWriter(stream, StandardCharsets.UTF_8), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置文件目录
|
||||
* @return 返回目录File对象.
|
||||
*/
|
||||
private static File getPropertiesDir() {
|
||||
File propDir = new File(System.getProperty("cgj.botDataDir"), "/setting/");
|
||||
if(!propDir.exists() && !propDir.mkdirs()) {
|
||||
log.warn("Setting文件夹创建失败!");
|
||||
}
|
||||
return propDir;
|
||||
}
|
||||
|
||||
public static String getProperty(long groupId, String key) {
|
||||
return getProperty(groupId, key, null);
|
||||
}
|
||||
|
||||
public static String getProperty(long groupId, String key, String defaultValue) {
|
||||
if(groupId <= 0) {
|
||||
return globalProp.getProperty(key, defaultValue);
|
||||
} else {
|
||||
Properties properties = groupPropMap.get(groupId);
|
||||
return properties == null ? defaultValue : properties.getProperty(key, defaultValue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置配置项
|
||||
* @param groupId 群组Id, 如为0或负数则为全局配置
|
||||
* @param key 配置项key名
|
||||
* @param value 欲设置的新值, 如为null则删除该配置项
|
||||
* @return 返回上一次设定值
|
||||
*/
|
||||
public static String setProperty(long groupId, String key, String value) {
|
||||
Objects.requireNonNull(key);
|
||||
Properties targetProperties;
|
||||
if(groupId <= 0) {
|
||||
targetProperties = globalProp;
|
||||
} else {
|
||||
changeList.add(groupId);
|
||||
targetProperties = getGroupProperties(groupId);
|
||||
}
|
||||
String lastValue = targetProperties.getProperty(key);
|
||||
if(value != null) {
|
||||
targetProperties.setProperty(key, value);
|
||||
} else {
|
||||
targetProperties.remove(key);
|
||||
}
|
||||
return lastValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取GlobalProperties
|
||||
* @return 全局Properties
|
||||
*/
|
||||
private static Properties getGlobalProperties() {
|
||||
return globalProp;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取群组Properties
|
||||
* @param groupId 群组Id
|
||||
* @return 如果存在, 返回Properties, 不存在返回null.
|
||||
* @throws IllegalArgumentException 当群组Id 小于或等于0 时抛出.
|
||||
*/
|
||||
private static Properties getGroupProperties(long groupId) {
|
||||
if (groupId <= 0) {
|
||||
throw new IllegalArgumentException("Group number cannot be 0 or negative: " + groupId);
|
||||
}
|
||||
if(!groupPropMap.containsKey(groupId)) {
|
||||
groupPropMap.put(groupId, new Properties(globalProp));
|
||||
}
|
||||
return groupPropMap.get(groupId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取群组 Properties, 如果指定群组没有 Properties, 则使用GlobalProperties.
|
||||
* @param groupId 指定群组Id
|
||||
* @return 如果群组存在所属Properties, 则返回群组Properties, 否则返回GlobalProperties.
|
||||
*/
|
||||
public static Properties getProperties(long groupId) {
|
||||
if(groupPropMap.containsKey(groupId)) {
|
||||
return groupPropMap.get(groupId);
|
||||
}
|
||||
return getGlobalProperties();
|
||||
}
|
||||
|
||||
}
|
52
src/main/java/net/lamgc/cgj/bot/cache/AutoCleanTimer.java
vendored
Normal file
52
src/main/java/net/lamgc/cgj/bot/cache/AutoCleanTimer.java
vendored
Normal file
@ -0,0 +1,52 @@
|
||||
package net.lamgc.cgj.bot.cache;
|
||||
|
||||
import com.google.common.base.Throwables;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
public class AutoCleanTimer extends TimerTask {
|
||||
|
||||
private final static Set<Cleanable> cleanSet = new CopyOnWriteArraySet<>();
|
||||
|
||||
private final static Timer cleanTimer = new Timer("Thread-AutoClean", true);
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(AutoCleanTimer.class.getName());
|
||||
|
||||
static {
|
||||
cleanTimer.schedule(new AutoCleanTimer(), 100L);
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加需要定时执行清理的缓存库
|
||||
* @param store 已实现Cleanable的对象
|
||||
*/
|
||||
public static void add(Cleanable store) {
|
||||
cleanSet.add(store);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除已添加的缓存库
|
||||
* @param store 需要从AutoCleanTimer移除的对象
|
||||
*/
|
||||
public static void remove(Cleanable store) {
|
||||
cleanSet.remove(store);
|
||||
}
|
||||
|
||||
private AutoCleanTimer() {}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
cleanSet.forEach(cleanable -> {
|
||||
try {
|
||||
cleanable.clean();
|
||||
} catch (Exception e) {
|
||||
log.error("{} 执行清理动作时发生异常:\n{}", cleanable.toString(), Throwables.getStackTraceAsString(e));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -1,7 +1,12 @@
|
||||
package net.lamgc.cgj.bot.cache;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 缓存库接口
|
||||
* @param <T> 缓存数据类型
|
||||
*/
|
||||
public interface CacheStore<T> {
|
||||
|
||||
/**
|
||||
@ -64,6 +69,19 @@ public interface CacheStore<T> {
|
||||
*/
|
||||
boolean clear();
|
||||
|
||||
/**
|
||||
* 获取key集合
|
||||
* @return 返回存储缓存库中所有缓存项key的集合
|
||||
*/
|
||||
Set<String> keys();
|
||||
|
||||
/**
|
||||
* 删除指定缓存项
|
||||
* @param key 缓存项key
|
||||
* @return 删除成功返回true
|
||||
*/
|
||||
boolean remove(String key);
|
||||
|
||||
/**
|
||||
* 是否支持持久化
|
||||
* @return 如果支持返回true
|
||||
|
35
src/main/java/net/lamgc/cgj/bot/cache/CacheStoreUtils.java
vendored
Normal file
35
src/main/java/net/lamgc/cgj/bot/cache/CacheStoreUtils.java
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
package net.lamgc.cgj.bot.cache;
|
||||
|
||||
public final class CacheStoreUtils {
|
||||
|
||||
private CacheStoreUtils() {}
|
||||
|
||||
/**
|
||||
* 将 CacheStore 转换成 基于 {@link LocalHashCacheStore} 的 {@link HotDataCacheStore}
|
||||
* <p>通过该方法转换, 会自动启用 自动清理</p>
|
||||
* @param cacheStore 上游缓存库
|
||||
* @param expireTime 热点缓存最小有效期
|
||||
* @param floatRange 缓存浮动最大范围
|
||||
* @param <T> 缓存库数据类型
|
||||
* @return 返回 {@link HotDataCacheStore}
|
||||
*/
|
||||
public static <T> CacheStore<T> hashLocalHotDataStore(CacheStore<T> cacheStore, long expireTime, int floatRange) {
|
||||
return hashLocalHotDataStore(cacheStore, expireTime, floatRange, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 CacheStore 转换成 基于 {@link LocalHashCacheStore} 的 {@link HotDataCacheStore}
|
||||
* @param cacheStore 上游缓存库
|
||||
* @param expireTime 热点缓存最小有效期
|
||||
* @param floatRange 缓存浮动最大范围
|
||||
* @param autoClean 是否启用自动清理
|
||||
* @param <T> 缓存库数据类型
|
||||
* @return 返回 {@link HotDataCacheStore}
|
||||
*/
|
||||
public static <T> CacheStore<T> hashLocalHotDataStore(CacheStore<T> cacheStore,
|
||||
long expireTime, int floatRange, boolean autoClean) {
|
||||
return new HotDataCacheStore<>(cacheStore, new LocalHashCacheStore<>(), expireTime, floatRange, autoClean);
|
||||
}
|
||||
|
||||
|
||||
}
|
10
src/main/java/net/lamgc/cgj/bot/cache/Cleanable.java
vendored
Normal file
10
src/main/java/net/lamgc/cgj/bot/cache/Cleanable.java
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
package net.lamgc.cgj.bot.cache;
|
||||
|
||||
/**
|
||||
* 可清理接口, 实现该接口代表该类拥有清理动作.
|
||||
*/
|
||||
public interface Cleanable {
|
||||
|
||||
void clean() throws Exception;
|
||||
|
||||
}
|
@ -3,40 +3,43 @@ package net.lamgc.cgj.bot.cache;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
import java.util.Random;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 具有继承性的热点数据缓存库
|
||||
* @param <T> 存储类型
|
||||
* @author LamGC
|
||||
*/
|
||||
public class HotDataCacheStore<T> implements CacheStore<T> {
|
||||
public class HotDataCacheStore<T> implements CacheStore<T>, Cleanable {
|
||||
|
||||
private final CacheStore<T> parent;
|
||||
private final CacheStore<T> current;
|
||||
private final long expireTime;
|
||||
private final int expireFloatRange;
|
||||
private final Random random = new Random();
|
||||
private final Logger log = LoggerFactory.getLogger(HotDataCacheStore.class.getSimpleName() + "@" + Integer.toHexString(this.hashCode()));
|
||||
private final Logger log = LoggerFactory.getLogger(this.toString());
|
||||
|
||||
/**
|
||||
* 构造热点缓存存储对象
|
||||
* @param parent 上级缓存存储库
|
||||
* @param current 热点缓存存储库, 最好使用本地缓存(例如 {@linkplain LocalHashCacheStore LocalHashCacheStore})
|
||||
* @param expireTime 本地缓存库的缓存项过期时间, 单位毫秒;
|
||||
* 该时间并不是所有缓存项的最终过期时间, 还需要根据expireFloatRange的设定随机设置, 公式:
|
||||
* {@code expireTime + new Random().nextInt(expireFloatRange)}
|
||||
* 该时间并不是所有缓存项的最终过期时间, 还需要根据expireFloatRange的设定随机设置, 公式:
|
||||
* {@code expireTime + new Random().nextInt(expireFloatRange)}
|
||||
* @param expireFloatRange 过期时间的浮动范围(单位毫秒), 用于防止短时间内大量缓存项失效导致的缓存雪崩
|
||||
* @param autoClean 是否交由{@link AutoCleanTimer}自动执行清理
|
||||
*/
|
||||
public HotDataCacheStore(CacheStore<T> parent, CacheStore<T> current, long expireTime, int expireFloatRange) {
|
||||
public HotDataCacheStore(CacheStore<T> parent, CacheStore<T> current, long expireTime, int expireFloatRange, boolean autoClean) {
|
||||
this.parent = parent;
|
||||
this.current = current;
|
||||
this.expireTime = expireTime;
|
||||
this.expireFloatRange = expireFloatRange;
|
||||
log.debug("HotDataCacheStore初始化完成. (Parent: {}, Current: {}, expireTime: {}, expireFloatRange: {})",
|
||||
parent, current, expireTime, expireFloatRange);
|
||||
if(autoClean) {
|
||||
AutoCleanTimer.add(this);
|
||||
}
|
||||
|
||||
log.debug("HotDataCacheStore初始化完成. (Parent: {}, Current: {}, expireTime: {}, expireFloatRange: {}, autoClean: {})",
|
||||
parent, current, expireTime, expireFloatRange, autoClean);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -99,6 +102,21 @@ public class HotDataCacheStore<T> implements CacheStore<T> {
|
||||
return current.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> keys() {
|
||||
Set<String> keys = new HashSet<>();
|
||||
keys.addAll(current.keys());
|
||||
keys.addAll(parent.keys());
|
||||
return keys;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(String key) {
|
||||
parent.remove(key);
|
||||
current.remove(key);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportedPersistence() {
|
||||
return current.supportedPersistence() || parent.supportedPersistence();
|
||||
@ -108,4 +126,13 @@ public class HotDataCacheStore<T> implements CacheStore<T> {
|
||||
public boolean supportedList() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clean() {
|
||||
for(String key : this.current.keys()) {
|
||||
if(current.exists(key)) {
|
||||
current.remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -21,15 +21,16 @@ import java.util.Set;
|
||||
|
||||
public class ImageCacheHandler implements EventHandler {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger("ImageCacheHandler");
|
||||
private final static Logger log = LoggerFactory.getLogger(ImageCacheHandler.class.getName());
|
||||
|
||||
private final static HttpClient httpClient = HttpClientBuilder.create().setProxy(Main.proxy).build();
|
||||
|
||||
private final static Set<ImageCacheObject> cacheQueue = Collections.synchronizedSet(new HashSet<>());
|
||||
|
||||
public void getImageToCache(ImageCacheObject event) {
|
||||
@SuppressWarnings("unused")
|
||||
public void getImageToCache(ImageCacheObject event) throws Exception {
|
||||
if(cacheQueue.contains(event)) {
|
||||
log.info("图片 {} 已存在相同缓存任务, 跳过.", event.getStoreFile().getName());
|
||||
log.warn("图片 {} 已存在相同缓存任务, 跳过.", event.getStoreFile().getName());
|
||||
return;
|
||||
} else {
|
||||
cacheQueue.add(event);
|
||||
@ -38,15 +39,15 @@ public class ImageCacheHandler implements EventHandler {
|
||||
try {
|
||||
log.info("图片 {} Event正在进行...({})", event.getStoreFile().getName(), Integer.toHexString(event.hashCode()));
|
||||
File storeFile = event.getStoreFile();
|
||||
log.info("正在缓存图片 {} (Path: {})", storeFile.getName(), storeFile.getAbsolutePath());
|
||||
log.debug("正在缓存图片 {} (Path: {})", storeFile.getName(), storeFile.getAbsolutePath());
|
||||
try {
|
||||
if(!storeFile.exists() && !storeFile.createNewFile()) {
|
||||
log.error("无法创建文件(Path: {})", storeFile.getAbsolutePath());
|
||||
return;
|
||||
throw new IOException("Failed to create file");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("无法创建文件(Path: {})", storeFile.getAbsolutePath());
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
|
||||
HttpGet request = new HttpGet(event.getDownloadLink());
|
||||
@ -56,19 +57,19 @@ public class ImageCacheHandler implements EventHandler {
|
||||
response = httpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
log.error("Http请求时发生异常", e);
|
||||
return;
|
||||
throw e;
|
||||
}
|
||||
if(response.getStatusLine().getStatusCode() != 200) {
|
||||
log.warn("Http请求异常:{}", response.getStatusLine());
|
||||
return;
|
||||
throw new IOException("Http Response Error: " + response.getStatusLine());
|
||||
}
|
||||
|
||||
log.info("正在下载...(Content-Length: {}KB)", response.getEntity().getContentLength() / 1024);
|
||||
log.debug("正在下载...(Content-Length: {}KB)", response.getEntity().getContentLength() / 1024);
|
||||
try(FileOutputStream fos = new FileOutputStream(storeFile)) {
|
||||
IOUtils.copy(response.getEntity().getContent(), fos);
|
||||
} catch (IOException e) {
|
||||
log.error("下载图片时发生异常", e);
|
||||
return;
|
||||
throw e;
|
||||
}
|
||||
event.getImageCache().put(URLs.getResourceName(event.getDownloadLink()), storeFile);
|
||||
} finally {
|
||||
|
106
src/main/java/net/lamgc/cgj/bot/cache/ImageCacheStore.java
vendored
Normal file
106
src/main/java/net/lamgc/cgj/bot/cache/ImageCacheStore.java
vendored
Normal file
@ -0,0 +1,106 @@
|
||||
package net.lamgc.cgj.bot.cache;
|
||||
|
||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public final class ImageCacheStore {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(ImageCacheStore.class.getName());
|
||||
|
||||
private final static Map<ImageCacheObject, Task> cacheMap = new Hashtable<>();
|
||||
|
||||
private final static ThreadPoolExecutor imageCacheExecutor = new ThreadPoolExecutor(
|
||||
4, 6,
|
||||
30L,
|
||||
TimeUnit.SECONDS,
|
||||
new LinkedBlockingQueue<>(100),
|
||||
new ThreadFactoryBuilder()
|
||||
.setNameFormat("ImageCacheThread-%d")
|
||||
.build()
|
||||
);
|
||||
|
||||
private final static ImageCacheHandler handler = new ImageCacheHandler();
|
||||
|
||||
private ImageCacheStore() {}
|
||||
|
||||
/**
|
||||
* 传递图片缓存任务, 并等待缓存完成.
|
||||
* @param cacheObject 缓存任务组
|
||||
*/
|
||||
public static void executeCacheRequest(ImageCacheObject cacheObject) throws InterruptedException {
|
||||
Task task = getTaskState(cacheObject);
|
||||
if(task.taskState.get() == TaskState.COMPLETE) {
|
||||
return;
|
||||
}
|
||||
|
||||
boolean locked = false;
|
||||
try {
|
||||
if(task.taskState.get() == TaskState.COMPLETE) {
|
||||
return;
|
||||
}
|
||||
task.lock.lock();
|
||||
locked = true;
|
||||
// 双重检查
|
||||
if(task.taskState.get() == TaskState.COMPLETE) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 置任务状态
|
||||
task.taskState.set(TaskState.RUNNING);
|
||||
|
||||
try {
|
||||
Throwable throwable = imageCacheExecutor.submit(() -> {
|
||||
try {
|
||||
handler.getImageToCache(cacheObject);
|
||||
} catch (Throwable e) {
|
||||
return e;
|
||||
}
|
||||
return null;
|
||||
}).get();
|
||||
|
||||
if(throwable == null) {
|
||||
task.taskState.set(TaskState.COMPLETE);
|
||||
} else {
|
||||
task.taskState.set(TaskState.ERROR);
|
||||
}
|
||||
} catch (ExecutionException e) {
|
||||
log.error("执行图片缓存任务时发生异常", e);
|
||||
}
|
||||
} finally {
|
||||
if(locked) {
|
||||
task.lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static Task getTaskState(ImageCacheObject cacheObject) {
|
||||
if(!cacheMap.containsKey(cacheObject)) {
|
||||
cacheMap.put(cacheObject, new Task());
|
||||
}
|
||||
return cacheMap.get(cacheObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* 任务状态
|
||||
*/
|
||||
private enum TaskState {
|
||||
READY, RUNNING, COMPLETE, ERROR
|
||||
}
|
||||
|
||||
private static class Task {
|
||||
|
||||
public final ReentrantLock lock = new ReentrantLock(true);
|
||||
|
||||
public final AtomicReference<TaskState> taskState = new AtomicReference<>(TaskState.READY);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -5,21 +5,52 @@ import org.jetbrains.annotations.NotNull;
|
||||
import java.util.Date;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public class LocalHashCacheStore<T> implements CacheStore<T> {
|
||||
/**
|
||||
* 基于Hashtable的本地缓存库
|
||||
* @param <T> 缓存类型
|
||||
*/
|
||||
public class LocalHashCacheStore<T> implements CacheStore<T>, Cleanable {
|
||||
|
||||
private final Hashtable<String, CacheObject<T>> cache;
|
||||
|
||||
/**
|
||||
* 构造一个基于Hashtable的本地缓存库
|
||||
* @see Hashtable
|
||||
*/
|
||||
public LocalHashCacheStore() {
|
||||
this(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造一个基于Hashtable的本地缓存库
|
||||
* @param initialCapacity 初始容量
|
||||
* @see Hashtable
|
||||
*/
|
||||
public LocalHashCacheStore(int initialCapacity) {
|
||||
this(initialCapacity, 0F);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造一个基于Hashtable的本地缓存库
|
||||
* @param initialCapacity 初始容量
|
||||
* @param loadFactor 重载因子
|
||||
* @see Hashtable
|
||||
*/
|
||||
public LocalHashCacheStore(int initialCapacity, float loadFactor) {
|
||||
this(initialCapacity, loadFactor, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造一个基于Hashtable的本地缓存库
|
||||
* @param initialCapacity 初始容量
|
||||
* @param loadFactor 重载因子
|
||||
* @param autoClean 是否自动清理
|
||||
* @see Hashtable
|
||||
*/
|
||||
public LocalHashCacheStore(int initialCapacity, float loadFactor, boolean autoClean) {
|
||||
if(initialCapacity != 0) {
|
||||
if(loadFactor <= 0F) {
|
||||
cache = new Hashtable<>(initialCapacity);
|
||||
@ -29,6 +60,10 @@ public class LocalHashCacheStore<T> implements CacheStore<T> {
|
||||
} else {
|
||||
cache = new Hashtable<>();
|
||||
}
|
||||
|
||||
if(autoClean) {
|
||||
AutoCleanTimer.add(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -93,6 +128,16 @@ public class LocalHashCacheStore<T> implements CacheStore<T> {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> keys() {
|
||||
return cache.keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(String key) {
|
||||
return cache.remove(key) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportedPersistence() {
|
||||
return false;
|
||||
@ -103,6 +148,15 @@ public class LocalHashCacheStore<T> implements CacheStore<T> {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clean() throws Exception {
|
||||
Date currentDate = new Date();
|
||||
cache.forEach((key, value) -> {
|
||||
if(value.isExpire(currentDate)) {
|
||||
cache.remove(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static class CacheObject<T> implements Comparable<CacheObject<T>> {
|
||||
|
||||
|
@ -9,10 +9,11 @@ import redis.clients.jedis.*;
|
||||
import java.net.URI;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
public abstract class RedisPoolCacheStore<T> implements CacheStore<T> {
|
||||
abstract class RedisPoolCacheStore<T> implements CacheStore<T> {
|
||||
|
||||
private final JedisPool jedisPool;
|
||||
private final String keyPrefix;
|
||||
@ -35,7 +36,7 @@ public abstract class RedisPoolCacheStore<T> implements CacheStore<T> {
|
||||
if(jedisPool.isClosed()) {
|
||||
throw new IllegalStateException("JedisPool is closed");
|
||||
}
|
||||
log = LoggerFactory.getLogger(this.getClass().getSimpleName() + "@" + Integer.toHexString(jedisPool.hashCode()));
|
||||
log = LoggerFactory.getLogger(this.getClass().getName() + "@" + Integer.toHexString(jedisPool.hashCode()));
|
||||
if(!Strings.isNullOrEmpty(keyPrefix)) {
|
||||
this.keyPrefix = keyPrefix.endsWith(".") ? keyPrefix : keyPrefix + ".";
|
||||
} else {
|
||||
@ -50,29 +51,27 @@ public abstract class RedisPoolCacheStore<T> implements CacheStore<T> {
|
||||
|
||||
@Override
|
||||
public void update(String key, T value, Date expire) {
|
||||
Jedis jedis = jedisPool.getResource();
|
||||
jedis.set(keyPrefix + key, parse(value));
|
||||
if(expire != null) {
|
||||
jedis.pexpireAt(keyPrefix + key, expire.getTime());
|
||||
log.debug("已设置Key {} 的过期时间(Expire: {})", key, expire.getTime());
|
||||
try (Jedis jedis = jedisPool.getResource()) {
|
||||
jedis.set(keyPrefix + key, parse(value));
|
||||
if(expire != null) {
|
||||
jedis.pexpireAt(keyPrefix + key, expire.getTime());
|
||||
log.debug("已设置Key {} 的过期时间(Expire: {})", key, expire.getTime());
|
||||
}
|
||||
}
|
||||
jedis.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getCache(String key) {
|
||||
Jedis jedis = jedisPool.getResource();
|
||||
T result = analysis(jedis.get(keyPrefix + key));
|
||||
jedis.close();
|
||||
return result;
|
||||
try (Jedis jedis = jedisPool.getResource()) {
|
||||
return analysis(jedis.get(keyPrefix + key));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists(String key) {
|
||||
Jedis jedis = jedisPool.getResource();
|
||||
boolean result = jedis.exists(keyPrefix + key);
|
||||
jedis.close();
|
||||
return result;
|
||||
try (Jedis jedis = jedisPool.getResource()) {
|
||||
return jedis.exists(keyPrefix + key);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -82,11 +81,23 @@ public abstract class RedisPoolCacheStore<T> implements CacheStore<T> {
|
||||
|
||||
@Override
|
||||
public boolean clear() {
|
||||
Jedis jedis = jedisPool.getResource();
|
||||
String result = jedis.flushDB();
|
||||
jedis.close();
|
||||
log.info("flushDB返回结果: {}", result);
|
||||
return true;
|
||||
try (Jedis jedis = jedisPool.getResource()) {
|
||||
return jedis.flushDB().equalsIgnoreCase("ok");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> keys() {
|
||||
try (Jedis jedis = jedisPool.getResource()) {
|
||||
return jedis.keys(keyPrefix + "*");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(String key) {
|
||||
try (Jedis jedis = jedisPool.getResource()) {
|
||||
return jedis.del(keyPrefix + key) == 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -145,13 +156,4 @@ public abstract class RedisPoolCacheStore<T> implements CacheStore<T> {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换原本的分隔符(.)为(:).<br/>
|
||||
* 即将启用
|
||||
* @param key 要处理的键名
|
||||
* @return 处理后的键名
|
||||
*/
|
||||
public static String replaceKey(String key) {
|
||||
return key.replaceAll("\\.", ":");
|
||||
}
|
||||
}
|
||||
|
30
src/main/java/net/lamgc/cgj/bot/cache/StringListRedisCacheStore.java
vendored
Normal file
30
src/main/java/net/lamgc/cgj/bot/cache/StringListRedisCacheStore.java
vendored
Normal file
@ -0,0 +1,30 @@
|
||||
package net.lamgc.cgj.bot.cache;
|
||||
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
public class StringListRedisCacheStore extends RedisListCacheStore<String> {
|
||||
public StringListRedisCacheStore(URI redisServerUri, String prefix) {
|
||||
super(redisServerUri, prefix);
|
||||
}
|
||||
|
||||
public StringListRedisCacheStore(URI redisServerUri, JedisPoolConfig config, int timeout, String password, String prefix) {
|
||||
super(redisServerUri, config, timeout, password, prefix);
|
||||
}
|
||||
|
||||
public StringListRedisCacheStore(JedisPool pool, String prefix) {
|
||||
super(pool, prefix);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String parseData(String dataObj) {
|
||||
return dataObj;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String analysisData(String str) {
|
||||
return str;
|
||||
}
|
||||
}
|
@ -5,17 +5,18 @@ import com.google.common.base.Throwables;
|
||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
import net.lamgc.cgj.bot.BotAdminCommandProcess;
|
||||
import net.lamgc.cgj.bot.BotCommandProcess;
|
||||
import net.lamgc.cgj.bot.MessageEventExecutionDebugger;
|
||||
import net.lamgc.cgj.bot.SettingProperties;
|
||||
import net.lamgc.cgj.util.DateParser;
|
||||
import net.lamgc.cgj.util.PagesQualityParser;
|
||||
import net.lamgc.cgj.util.TimeLimitThreadPoolExecutor;
|
||||
import net.lamgc.utils.base.runner.ArgumentsRunner;
|
||||
import net.lamgc.utils.base.runner.ArgumentsRunnerConfig;
|
||||
import net.lamgc.utils.base.runner.exception.DeveloperRunnerException;
|
||||
import net.lamgc.utils.base.runner.exception.NoSuchCommandException;
|
||||
import net.lamgc.utils.base.runner.exception.ParameterNoFoundException;
|
||||
import net.lamgc.utils.event.EventExecutor;
|
||||
import net.lamgc.utils.event.EventHandler;
|
||||
import net.lamgc.utils.event.*;
|
||||
import net.lamgc.utils.event.EventObject;
|
||||
import net.lamgc.utils.event.EventUncaughtExceptionHandler;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
@ -23,24 +24,24 @@ import redis.clients.jedis.JedisPool;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URI;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class BotEventHandler implements EventHandler {
|
||||
|
||||
public final static String COMMAND_PREFIX = ".cgj";
|
||||
public final static String ADMIN_COMMAND_PREFIX = ".cgjadmin ";
|
||||
|
||||
private final ArgumentsRunner processRunner;
|
||||
private final ArgumentsRunner adminRunner;
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger("BotEventHandler@" + Integer.toHexString(this.hashCode()));
|
||||
private final static Logger log = LoggerFactory.getLogger(BotEventHandler.class.getName());
|
||||
|
||||
private final static Map<Long, AtomicBoolean> muteStateMap = new Hashtable<>();
|
||||
|
||||
/**
|
||||
* 所有缓存共用的JedisPool
|
||||
@ -51,9 +52,10 @@ public class BotEventHandler implements EventHandler {
|
||||
/**
|
||||
* 消息事件执行器
|
||||
*/
|
||||
public final static EventExecutor executor = new EventExecutor(new ThreadPoolExecutor(
|
||||
(int) Math.ceil(Runtime.getRuntime().availableProcessors() / 2F),
|
||||
Runtime.getRuntime().availableProcessors(),
|
||||
private final static EventExecutor executor = new EventExecutor(new TimeLimitThreadPoolExecutor(
|
||||
0,
|
||||
Math.max(Runtime.getRuntime().availableProcessors(), 4),
|
||||
Math.max(Math.max(Runtime.getRuntime().availableProcessors() * 2, 4), 32),
|
||||
30L,
|
||||
TimeUnit.SECONDS,
|
||||
new LinkedBlockingQueue<>(1536),
|
||||
@ -95,6 +97,21 @@ public class BotEventHandler implements EventHandler {
|
||||
initialled = true;
|
||||
}
|
||||
|
||||
private final static AtomicBoolean preLoaded = new AtomicBoolean();
|
||||
/**
|
||||
* 预加载
|
||||
*/
|
||||
public synchronized static void preLoad() {
|
||||
if(preLoaded.get()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
BotAdminCommandProcess.loadPushList();
|
||||
} finally {
|
||||
preLoaded.set(true);
|
||||
}
|
||||
}
|
||||
|
||||
private BotEventHandler() {
|
||||
ArgumentsRunnerConfig runnerConfig = new ArgumentsRunnerConfig();
|
||||
runnerConfig.setUseDefaultValueInsteadOfException(true);
|
||||
@ -110,16 +127,47 @@ public class BotEventHandler implements EventHandler {
|
||||
BotCommandProcess.initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* 投递消息事件
|
||||
* @param event 事件对象
|
||||
*/
|
||||
@NotAccepted
|
||||
public static void executeMessageEvent(MessageEvent event) {
|
||||
String debuggerName = SettingProperties.getProperty(0, "debug.debugger");
|
||||
if(!event.getMessage().startsWith(ADMIN_COMMAND_PREFIX) &&
|
||||
!Strings.isNullOrEmpty(debuggerName)) {
|
||||
try {
|
||||
MessageEventExecutionDebugger debugger = MessageEventExecutionDebugger.valueOf(debuggerName.toUpperCase());
|
||||
debugger.debugger.accept(executor, event, SettingProperties.getProperties(SettingProperties.GLOBAL),
|
||||
MessageEventExecutionDebugger.getDebuggerLogger(debugger));
|
||||
} catch(IllegalArgumentException e) {
|
||||
log.warn("未找到指定调试器: '{}'", debuggerName);
|
||||
} catch (Exception e) {
|
||||
log.error("事件调试处理时发生异常", e);
|
||||
}
|
||||
} else {
|
||||
BotEventHandler.executor.executor(event);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 以事件形式处理消息事件
|
||||
* @param event 消息事件对象
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public void processMessage(MessageEvent event) {
|
||||
String msg = event.getMessage();
|
||||
log.debug(event.toString());
|
||||
if(!match(msg)) {
|
||||
return;
|
||||
} else if(isMute(event.getFromGroup())) {
|
||||
log.debug("机器人已被禁言, 忽略请求.");
|
||||
return;
|
||||
}
|
||||
|
||||
Pattern pattern = Pattern.compile("/\\s*(\".+?\"|[^:\\s])+((\\s*:\\s*(\".+?\"|[^\\s])+)|)|(\".+?\"|[^\"\\s])+");
|
||||
Matcher matcher = pattern.matcher(Strings.nullToEmpty(msg));
|
||||
ArrayList<String> argsList = new ArrayList<>();
|
||||
List<String> argsList = new ArrayList<>();
|
||||
while (matcher.find()) {
|
||||
String arg = matcher.group();
|
||||
int startIndex = 0;
|
||||
@ -141,17 +189,23 @@ public class BotEventHandler implements EventHandler {
|
||||
String[] args = new String[argsList.size()];
|
||||
argsList.toArray(args);
|
||||
log.debug("传入参数: {}", Arrays.toString(args));
|
||||
argsList.add("-$fromGroup");
|
||||
argsList.add(String.valueOf(event.getFromGroup()));
|
||||
argsList.add("-$fromQQ");
|
||||
argsList.add(String.valueOf(event.getFromQQ()));
|
||||
args = Arrays.copyOf(args, args.length + 4);
|
||||
argsList.toArray(args);
|
||||
|
||||
log.info("正在处理命令...");
|
||||
long time = System.currentTimeMillis();
|
||||
Object result;
|
||||
try {
|
||||
if(msg.toLowerCase().startsWith(COMMAND_PREFIX + "admin")) {
|
||||
if(!String.valueOf(event.getFromQQ()).equals(BotCommandProcess.globalProp.getProperty("admin.adminId"))) {
|
||||
event.sendMessage("你没有执行该命令的权限!");
|
||||
return;
|
||||
if(msg.toLowerCase().startsWith(ADMIN_COMMAND_PREFIX)) {
|
||||
if(!String.valueOf(event.getFromQQ())
|
||||
.equals(SettingProperties.getProperty(0, "admin.adminId"))) {
|
||||
result = "你没有执行该命令的权限!";
|
||||
} else {
|
||||
result = adminRunner.run(new BotAdminCommandProcess(), args.length <= 1 ? new String[0] : Arrays.copyOfRange(args, 1, args.length));
|
||||
result = adminRunner.run(args.length <= 1 ? new String[0] : Arrays.copyOfRange(args, 1, args.length));
|
||||
}
|
||||
} else {
|
||||
result = processRunner.run(args.length <= 1 ? new String[0] : Arrays.copyOfRange(args, 1, args.length));
|
||||
@ -161,18 +215,33 @@ public class BotEventHandler implements EventHandler {
|
||||
} catch(ParameterNoFoundException e) {
|
||||
result = "命令缺少参数: " + e.getParameterName();
|
||||
} catch(DeveloperRunnerException e) {
|
||||
log.error("执行命令时发生异常", e);
|
||||
result = "命令执行时发生错误,无法完成!";
|
||||
Throwable cause = e.getCause();
|
||||
if (cause instanceof InterruptedException) {
|
||||
log.error("命令执行超时, 终止执行.");
|
||||
result = "色图姬发现这个命令的处理时间太久了!所以打断了这个命令。";
|
||||
} else if(cause instanceof NoSuchElementException && cause.getMessage().startsWith("No work found: ")) {
|
||||
String message = cause.getMessage();
|
||||
log.error("指定作品不存在.(Id: {})", message.substring(message.lastIndexOf(": ") + 2));
|
||||
result = "色图姬找不到这个作品!";
|
||||
} else {
|
||||
log.error("执行命令时发生异常", e);
|
||||
result = "色图姬在执行命令时遇到了一个错误!";
|
||||
}
|
||||
}
|
||||
log.info("命令处理完成.(耗时: {}ms)", System.currentTimeMillis() - time);
|
||||
if(Objects.requireNonNull(result) instanceof String) {
|
||||
long processTime = System.currentTimeMillis() - time;
|
||||
if(!Objects.isNull(result) && result instanceof String && !isMute(event.getFromGroup())) {
|
||||
try {
|
||||
event.sendMessage((String) result);
|
||||
} catch (Exception e) {
|
||||
log.error("发送消息时发生异常", e);
|
||||
}
|
||||
} else if(isMute(event.getFromGroup())) {
|
||||
log.warn("命令反馈时机器人已被禁言, 跳过反馈.");
|
||||
}
|
||||
log.info("命令反馈完成.(耗时: {}ms)", System.currentTimeMillis() - time);
|
||||
long totalTime = System.currentTimeMillis() - time;
|
||||
log.info("命令反馈完成.(事件耗时: {}ms, P: {}%({}ms), R: {}%({}ms))", totalTime,
|
||||
String.format("%.3f", ((double) processTime / (double)totalTime) * 100F), processTime,
|
||||
String.format("%.3f", ((double) (totalTime - processTime) / (double)totalTime) * 100F), totalTime - processTime);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -181,7 +250,43 @@ public class BotEventHandler implements EventHandler {
|
||||
* @return 如果为true则提交
|
||||
*/
|
||||
public static boolean match(String message) {
|
||||
return message.startsWith(COMMAND_PREFIX);
|
||||
return message.startsWith(COMMAND_PREFIX) || message.startsWith(ADMIN_COMMAND_PREFIX);
|
||||
}
|
||||
|
||||
private static boolean isMute(long groupId) {
|
||||
Boolean mute = isMute(groupId, false);
|
||||
return mute != null && mute;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询某群是否被禁言.
|
||||
* @param groupId 群组Id
|
||||
* @param rawValue 是否返回原始值(当没有该群状态, 且本参数为true时, 将返回null)
|
||||
* @return 返回状态值, 如无该群禁言记录且rawValue = true, 则返回null
|
||||
*/
|
||||
public static Boolean isMute(long groupId, boolean rawValue) {
|
||||
if(groupId <= 0) {
|
||||
return false;
|
||||
}
|
||||
AtomicBoolean state = muteStateMap.get(groupId);
|
||||
if(state == null && rawValue) {
|
||||
return null;
|
||||
}
|
||||
return state != null && state.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置机器人禁言状态.
|
||||
* <p>设置该项可防止因机器人在禁言期间反馈请求导致被封号.</p>
|
||||
* @param mute 如果被禁言, 传入true
|
||||
*/
|
||||
public static void setMuteState(long groupId, boolean mute) {
|
||||
if(!muteStateMap.containsKey(groupId)) {
|
||||
muteStateMap.put(groupId, new AtomicBoolean(mute));
|
||||
} else {
|
||||
muteStateMap.get(groupId).set(mute);
|
||||
}
|
||||
log.warn("群组 {} 机器人禁言状态已变更: {}", groupId, mute ? "已禁言" : "已解除");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,59 @@
|
||||
package net.lamgc.cgj.bot.event;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class BufferMessageEvent extends MessageEvent {
|
||||
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
|
||||
public final MessageEvent parent;
|
||||
|
||||
/**
|
||||
* 以空消息空Id生成BufferMessageEvent
|
||||
*/
|
||||
public BufferMessageEvent() {
|
||||
super(0, 0, "");
|
||||
parent = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 提供消息内容构造BufferMessageEvent
|
||||
* @param message 传入的消息内容
|
||||
*/
|
||||
public BufferMessageEvent(String message) {
|
||||
super(0, 0, message);
|
||||
parent = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用事件构造BufferMessageEvent
|
||||
* @param parentEvent 父级消息事件对象
|
||||
*/
|
||||
public BufferMessageEvent(MessageEvent parentEvent) {
|
||||
super(parentEvent.getFromGroup(), parentEvent.getFromQQ(), parentEvent.getMessage());
|
||||
parent = parentEvent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int sendMessage(String message) {
|
||||
buffer.append(message);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 当提供了父级消息事件时, 本方法调用父级消息事件对象的{@code getImageUrl(String)}, 如果没有, 返回{@code null}
|
||||
*/
|
||||
@Override
|
||||
public String getImageUrl(String image) {
|
||||
return Objects.isNull(this.parent) ? null : this.parent.getImageUrl(image);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓冲区消息内容
|
||||
* @return 消息内容
|
||||
*/
|
||||
public String getBufferMessage() {
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
}
|
@ -1,8 +1,9 @@
|
||||
package net.lamgc.cgj.bot.event;
|
||||
|
||||
import net.lamgc.cgj.bot.message.MessageSender;
|
||||
import net.lamgc.utils.event.EventObject;
|
||||
|
||||
public abstract class MessageEvent implements EventObject {
|
||||
public abstract class MessageEvent implements EventObject, MessageSender {
|
||||
|
||||
private final long fromGroup;
|
||||
private final long fromQQ;
|
||||
@ -11,7 +12,7 @@ public abstract class MessageEvent implements EventObject {
|
||||
public MessageEvent(long fromGroup, long fromQQ, String message) {
|
||||
this.fromGroup = fromGroup;
|
||||
this.fromQQ = fromQQ;
|
||||
this.message = message;
|
||||
this.message = message.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -19,6 +20,7 @@ public abstract class MessageEvent implements EventObject {
|
||||
* @param message 消息内容
|
||||
* @return 成功返回MessageId, 如没有MessageId则返回0, 失败返回负数错误码
|
||||
*/
|
||||
@Override
|
||||
public abstract int sendMessage(final String message);
|
||||
|
||||
/**
|
||||
@ -28,12 +30,6 @@ public abstract class MessageEvent implements EventObject {
|
||||
*/
|
||||
public abstract String getImageUrl(String image);
|
||||
|
||||
/**
|
||||
* 获取原始消息对象.(不推荐使用)
|
||||
* @return 消息对象
|
||||
*/
|
||||
public abstract Object getRawMessage();
|
||||
|
||||
/**
|
||||
* 获取来源群组号
|
||||
* @return 如非群组消息, 返回0
|
||||
@ -60,7 +56,7 @@ public abstract class MessageEvent implements EventObject {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MessageEvent{" +
|
||||
return this.getClass().getSimpleName() + "{" +
|
||||
"fromGroup=" + getFromGroup() +
|
||||
", fromQQ=" + getFromQQ() +
|
||||
", message='" + getMessage() + '\'' +
|
||||
|
@ -0,0 +1,44 @@
|
||||
package net.lamgc.cgj.bot.event;
|
||||
|
||||
/**
|
||||
* 假负载消息事件, 一般用于预处理某个命令使用,可以增强在高峰期来临时的处理速度.
|
||||
*/
|
||||
public class VirtualLoadMessageEvent extends MessageEvent {
|
||||
|
||||
/**
|
||||
* 将任意消息事件转换为假负载消息事件.
|
||||
* <p>转换之后, 除了fromGroup, fromQQ, message外其他信息不会保留</p>
|
||||
* @param event 待转换的消息事件
|
||||
* @param inheritImpl 是否继承除 sendMessage 外的其他 MessageEvent 实现
|
||||
* @return 转换后的消息事件
|
||||
*/
|
||||
public static VirtualLoadMessageEvent toVirtualLoadMessageEvent(MessageEvent event, boolean inheritImpl) {
|
||||
if(event instanceof VirtualLoadMessageEvent) {
|
||||
return (VirtualLoadMessageEvent) event;
|
||||
} else if(!inheritImpl) {
|
||||
return new VirtualLoadMessageEvent(event.getFromGroup(), event.getFromQQ(), event.getMessage());
|
||||
} else {
|
||||
return new VirtualLoadMessageEvent(event.getFromGroup(), event.getFromQQ(), event.getMessage()) {
|
||||
@Override
|
||||
public String getImageUrl(String image) {
|
||||
return event.getImageUrl(image);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public VirtualLoadMessageEvent(long fromGroup, long fromQQ, String message) {
|
||||
super(fromGroup, fromQQ, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int sendMessage(String message) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getImageUrl(String image) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package net.lamgc.cgj.bot;
|
||||
package net.lamgc.cgj.bot.framework.coolq;
|
||||
|
||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
import net.lz1998.cq.CQGlobal;
|
@ -1,7 +1,7 @@
|
||||
package net.lamgc.cgj.bot;
|
||||
package net.lamgc.cgj.bot.framework.coolq;
|
||||
|
||||
import net.lamgc.cgj.bot.event.BotEventHandler;
|
||||
import net.lamgc.cgj.bot.event.SpringCQMessageEvent;
|
||||
import net.lamgc.cgj.bot.framework.coolq.message.SpringCQMessageEvent;
|
||||
import net.lamgc.utils.event.EventHandler;
|
||||
import net.lz1998.cq.event.message.CQDiscussMessageEvent;
|
||||
import net.lz1998.cq.event.message.CQGroupMessageEvent;
|
||||
@ -16,25 +16,24 @@ import org.springframework.stereotype.Component;
|
||||
public class CQPluginMain extends CQPlugin implements EventHandler {
|
||||
|
||||
public CQPluginMain() {
|
||||
LoggerFactory.getLogger(this.toString())
|
||||
// TODO(LamGC, 2020.04.21): SpringCQ无法适配MessageSenderBuilder
|
||||
BotEventHandler.preLoad();
|
||||
LoggerFactory.getLogger(CQPluginMain.class.getName())
|
||||
.info("BotEventHandler.COMMAND_PREFIX = {}", BotEventHandler.COMMAND_PREFIX);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onPrivateMessage(CoolQ cq, CQPrivateMessageEvent event) {
|
||||
//log.info("私聊消息到达: 发送者[{}], 消息内容: {}", event.getSender().getUserId(), event.getMessage());
|
||||
return processMessage(cq, event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onGroupMessage(CoolQ cq, CQGroupMessageEvent event) {
|
||||
//log.info("群消息到达: 群[{}], 发送者[{}], 消息内容: {}", event.getGroupId(), event.getSender().getUserId(), event.getMessage());
|
||||
return processMessage(cq, event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onDiscussMessage(CoolQ cq, CQDiscussMessageEvent event) {
|
||||
//log.info("讨论组消息到达: 群[{}], 发送者[{}], 消息内容: {}", event.getDiscussId(), event.getSender().getUserId(), event.getMessage());
|
||||
return processMessage(cq, event);
|
||||
}
|
||||
|
||||
@ -42,7 +41,7 @@ public class CQPluginMain extends CQPlugin implements EventHandler {
|
||||
if(!BotEventHandler.match(event.getMessage())) {
|
||||
return MESSAGE_IGNORE;
|
||||
}
|
||||
BotEventHandler.executor.executor(new SpringCQMessageEvent(cq, event));
|
||||
BotEventHandler.executeMessageEvent(new SpringCQMessageEvent(cq, event));
|
||||
return MESSAGE_BLOCK;
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
package net.lamgc.cgj.bot.event;
|
||||
package net.lamgc.cgj.bot.framework.coolq.message;
|
||||
|
||||
import net.lamgc.cgj.bot.BotCode;
|
||||
import net.lamgc.cgj.bot.event.MessageEvent;
|
||||
import net.lamgc.cgj.bot.message.MessageSender;
|
||||
import net.lamgc.cgj.bot.message.MessageSource;
|
||||
import net.lz1998.cq.event.message.CQDiscussMessageEvent;
|
||||
import net.lz1998.cq.event.message.CQGroupMessageEvent;
|
||||
import net.lz1998.cq.event.message.CQMessageEvent;
|
||||
@ -11,13 +14,8 @@ import java.util.Objects;
|
||||
|
||||
public class SpringCQMessageEvent extends MessageEvent {
|
||||
|
||||
private final static int TYPE_PRIVATE = 0;
|
||||
private final static int TYPE_GROUP = 1;
|
||||
private final static int TYPE_DISCUSS = 2;
|
||||
|
||||
private final CoolQ cq;
|
||||
private final int type;
|
||||
private final CQMessageEvent messageEvent;
|
||||
private final MessageSender messageSender;
|
||||
|
||||
public SpringCQMessageEvent(CoolQ cq, CQMessageEvent messageEvent) {
|
||||
super(messageEvent instanceof CQGroupMessageEvent ? (
|
||||
@ -26,27 +24,20 @@ public class SpringCQMessageEvent extends MessageEvent {
|
||||
((CQDiscussMessageEvent) messageEvent).getDiscussId() : 0,
|
||||
messageEvent.getUserId(), messageEvent.getMessage());
|
||||
this.cq = Objects.requireNonNull(cq);
|
||||
MessageSource source;
|
||||
if(messageEvent instanceof CQGroupMessageEvent) {
|
||||
type = TYPE_GROUP;
|
||||
source = MessageSource.Group;
|
||||
} else if (messageEvent instanceof CQDiscussMessageEvent) {
|
||||
type = TYPE_DISCUSS;
|
||||
source = MessageSource.Discuss;
|
||||
} else {
|
||||
type = TYPE_PRIVATE;
|
||||
source = MessageSource.Private;
|
||||
}
|
||||
this.messageEvent = messageEvent;
|
||||
messageSender = new SpringCQMessageSender(cq, source, source == MessageSource.Private ? getFromQQ() : getFromGroup());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int sendMessage(final String message) {
|
||||
switch(type) {
|
||||
case TYPE_PRIVATE:
|
||||
return cq.sendPrivateMsg(getFromQQ(), message, false).getData().getMessageId();
|
||||
case TYPE_GROUP:
|
||||
case TYPE_DISCUSS:
|
||||
return cq.sendGroupMsg(getFromGroup(), message, false).getData().getMessageId();
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
return messageSender.sendMessage(message);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -70,8 +61,4 @@ public class SpringCQMessageEvent extends MessageEvent {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRawMessage() {
|
||||
return messageEvent;
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package net.lamgc.cgj.bot.framework.coolq.message;
|
||||
|
||||
import net.lamgc.cgj.bot.message.MessageSender;
|
||||
import net.lamgc.cgj.bot.message.MessageSource;
|
||||
import net.lz1998.cq.robot.CoolQ;
|
||||
|
||||
public class SpringCQMessageSender implements MessageSender {
|
||||
|
||||
private CoolQ coolQ;
|
||||
private MessageSource source;
|
||||
private long target;
|
||||
|
||||
public SpringCQMessageSender(CoolQ coolQ, MessageSource source, long target) {
|
||||
this.coolQ = coolQ;
|
||||
this.source = source;
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int sendMessage(String message) {
|
||||
switch (source) {
|
||||
case Private:
|
||||
return coolQ.sendPrivateMsg(target, message, false).getData().getMessageId();
|
||||
case Group:
|
||||
return coolQ.sendGroupMsg(target, message, false).getData().getMessageId();
|
||||
case Discuss:
|
||||
return coolQ.sendDiscussMsg(target, message, false).getData().getMessageId();
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package net.lamgc.cgj.bot.framework.coolq.message;
|
||||
|
||||
import net.lamgc.cgj.bot.message.MessageSender;
|
||||
import net.lamgc.cgj.bot.message.MessageSenderFactory;
|
||||
import net.lamgc.cgj.bot.message.MessageSource;
|
||||
import net.lz1998.cq.robot.CoolQ;
|
||||
|
||||
public class SpringCQMessageSenderFactory implements MessageSenderFactory {
|
||||
|
||||
private final CoolQ coolQ;
|
||||
|
||||
public SpringCQMessageSenderFactory(CoolQ coolQ) {
|
||||
this.coolQ = coolQ;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageSender createMessageSender(MessageSource source, long id) {
|
||||
return new SpringCQMessageSender(coolQ, source, id);
|
||||
}
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
package net.lamgc.cgj.bot.framework.mirai;
|
||||
|
||||
import net.lamgc.cgj.bot.event.BotEventHandler;
|
||||
import net.lamgc.cgj.bot.framework.mirai.message.MiraiMessageEvent;
|
||||
import net.lamgc.cgj.bot.message.MessageSenderBuilder;
|
||||
import net.lamgc.cgj.bot.framework.mirai.message.MiraiMessageSenderFactory;
|
||||
import net.mamoe.mirai.Bot;
|
||||
import net.mamoe.mirai.BotFactoryJvm;
|
||||
import net.mamoe.mirai.event.events.BotMuteEvent;
|
||||
import net.mamoe.mirai.event.events.BotUnmuteEvent;
|
||||
import net.mamoe.mirai.japt.Events;
|
||||
import net.mamoe.mirai.message.*;
|
||||
import net.mamoe.mirai.utils.BotConfiguration;
|
||||
import org.apache.commons.net.util.Base64;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Properties;
|
||||
|
||||
public class MiraiMain implements Closeable {
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger(MiraiMain.class.getName());
|
||||
|
||||
private Bot bot;
|
||||
|
||||
private final static Properties botProperties = new Properties();
|
||||
|
||||
public void init() {
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(this::close));
|
||||
try {
|
||||
Class.forName(BotEventHandler.class.getName());
|
||||
} catch (ClassNotFoundException e) {
|
||||
log.error("加载BotEventHandler时发生异常", e);
|
||||
return;
|
||||
}
|
||||
|
||||
File botPropFile = new File(System.getProperty("cgj.botDataDir"), "./bot.properties");
|
||||
try (Reader reader = new BufferedReader(new FileReader(botPropFile))) {
|
||||
botProperties.load(reader);
|
||||
} catch (IOException e) {
|
||||
log.error("机器人配置文件读取失败!", e);
|
||||
return;
|
||||
}
|
||||
|
||||
BotConfiguration configuration = new BotConfiguration();
|
||||
configuration.setProtocol(BotConfiguration.MiraiProtocol.ANDROID_PAD);
|
||||
bot = BotFactoryJvm.newBot(Long.parseLong(botProperties.getProperty("bot.qq", "0")), Base64.decodeBase64(botProperties.getProperty("bot.password", "")), configuration);
|
||||
Events.subscribeAlways(GroupMessageEvent.class, this::executeMessageEvent);
|
||||
Events.subscribeAlways(FriendMessageEvent.class, this::executeMessageEvent);
|
||||
Events.subscribeAlways(BotMuteEvent.class,
|
||||
event -> BotEventHandler.setMuteState(event.getGroup().getId(), true));
|
||||
Events.subscribeAlways(BotUnmuteEvent.class,
|
||||
event -> BotEventHandler.setMuteState(event.getGroup().getId(), false));
|
||||
bot.login();
|
||||
MessageSenderBuilder.setCurrentMessageSenderFactory(new MiraiMessageSenderFactory(bot));
|
||||
BotEventHandler.preLoad();
|
||||
bot.join();
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理消息事件
|
||||
* @param message 消息事件对象
|
||||
*/
|
||||
private void executeMessageEvent(MessageEvent message) {
|
||||
if(message instanceof GroupMessageEvent) {
|
||||
GroupMessageEvent GroupMessageEvent = (GroupMessageEvent) message;
|
||||
if(BotEventHandler.isMute(GroupMessageEvent.getGroup().getId(), true) == null) {
|
||||
BotEventHandler.setMuteState(GroupMessageEvent.getGroup().getId(),
|
||||
((GroupMessageEvent) message).getGroup().getBotMuteRemaining() != 0);
|
||||
}
|
||||
}
|
||||
BotEventHandler.executeMessageEvent(new MiraiMessageEvent(message));
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭机器人
|
||||
*/
|
||||
public void close() {
|
||||
if(bot == null) {
|
||||
return;
|
||||
}
|
||||
log.warn("正在关闭机器人...");
|
||||
bot.close(null);
|
||||
bot = null;
|
||||
log.warn("机器人已关闭.");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package net.lamgc.cgj.bot.framework.mirai.message;
|
||||
|
||||
import net.lamgc.cgj.bot.message.MessageSender;
|
||||
import net.lamgc.cgj.bot.message.MessageSource;
|
||||
import net.mamoe.mirai.message.GroupMessageEvent;
|
||||
import net.mamoe.mirai.message.MessageEvent;
|
||||
import net.mamoe.mirai.message.data.MessageUtils;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class MiraiMessageEvent extends net.lamgc.cgj.bot.event.MessageEvent {
|
||||
|
||||
private final MessageEvent messageObject;
|
||||
private final MessageSender messageSender;
|
||||
|
||||
public MiraiMessageEvent(MessageEvent message) {
|
||||
super(message instanceof GroupMessageEvent ? ((GroupMessageEvent) message).getGroup().getId() : 0,
|
||||
message.getSender().getId(), getMessageBodyWithoutSource(message.getMessage().toString()));
|
||||
this.messageObject = Objects.requireNonNull(message);
|
||||
if(message instanceof GroupMessageEvent) {
|
||||
messageSender = new MiraiMessageSender(((GroupMessageEvent) message).getGroup(), MessageSource.Group);
|
||||
} else {
|
||||
messageSender = new MiraiMessageSender(message.getSender(), MessageSource.Private);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将ContactMessage获得的消息内容删除 Mirai:source 并返回.
|
||||
* <p>该做法比较保守, 防止Mirai:source位置出现变动.</p>
|
||||
* @param message ContactMessage的消息内容;
|
||||
* @return 返回删除了Mirai:source的消息
|
||||
*/
|
||||
private static String getMessageBodyWithoutSource(String message) {
|
||||
StringBuilder builder = new StringBuilder(message);
|
||||
int startIndex = builder.indexOf("[mirai:source:");
|
||||
int endIndex = builder.indexOf("]", startIndex) + 1;
|
||||
return builder.delete(startIndex, endIndex).toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int sendMessage(final String message) {
|
||||
return messageSender.sendMessage(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getImageUrl(String imageId) {
|
||||
return messageObject.getBot().queryImageUrl(MessageUtils.newImage(imageId));
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package net.lamgc.cgj.bot.event;
|
||||
package net.lamgc.cgj.bot.framework.mirai.message;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import net.lamgc.cgj.bot.BotCode;
|
||||
@ -6,65 +6,63 @@ import net.lamgc.cgj.bot.cache.CacheStore;
|
||||
import net.lamgc.cgj.bot.cache.HotDataCacheStore;
|
||||
import net.lamgc.cgj.bot.cache.LocalHashCacheStore;
|
||||
import net.lamgc.cgj.bot.cache.StringRedisCacheStore;
|
||||
import net.mamoe.mirai.message.ContactMessage;
|
||||
import net.mamoe.mirai.message.FriendMessage;
|
||||
import net.mamoe.mirai.message.GroupMessage;
|
||||
import net.mamoe.mirai.message.data.CombinedMessage;
|
||||
import net.mamoe.mirai.message.data.Image;
|
||||
import net.mamoe.mirai.message.data.Message;
|
||||
import net.mamoe.mirai.message.data.MessageUtils;
|
||||
import net.lamgc.cgj.bot.event.BotEventHandler;
|
||||
import net.lamgc.cgj.bot.message.MessageSender;
|
||||
import net.lamgc.cgj.bot.message.MessageSource;
|
||||
import net.mamoe.mirai.Bot;
|
||||
import net.mamoe.mirai.contact.Contact;
|
||||
import net.mamoe.mirai.message.data.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class MiraiMessageEvent extends MessageEvent {
|
||||
public class MiraiMessageSender implements MessageSender {
|
||||
|
||||
private final ContactMessage messageObject;
|
||||
private final Logger log = LoggerFactory.getLogger(this.getClass().getSimpleName() + "@" + Integer.toHexString(this.hashCode()));
|
||||
private final Contact member;
|
||||
private final MessageSource source;
|
||||
private final static Logger log = LoggerFactory.getLogger(MiraiMessageSender.class.getName());
|
||||
private final static CacheStore<String> imageIdCache = new HotDataCacheStore<>(
|
||||
new StringRedisCacheStore(BotEventHandler.redisServer, "mirai.imageId"),
|
||||
new LocalHashCacheStore<>(),
|
||||
5400000, 1800000);
|
||||
5400000, 1800000, true);
|
||||
|
||||
/**
|
||||
* 使用id构造发送器
|
||||
* @param bot 机器人对象
|
||||
* @param source 消息源类型
|
||||
* @param id id, 将会根据消息源类型判断为什么号(QQ号或群号)
|
||||
* @throws NoSuchElementException 当在机器人好友列表或群列表里没有这个好友或群的时候抛出
|
||||
*/
|
||||
public MiraiMessageSender(Bot bot, MessageSource source, long id) {
|
||||
this(source == MessageSource.Private ? bot.getFriend(id) : bot.getGroup(id), source);
|
||||
}
|
||||
|
||||
public MiraiMessageEvent(ContactMessage message) {
|
||||
super(message instanceof GroupMessage ? ((GroupMessage) message).getGroup().getId() : 0,
|
||||
message.getSender().getId(), message.getMessage().toString());
|
||||
this.messageObject = Objects.requireNonNull(message);
|
||||
/**
|
||||
* 通过联系人对象构造发送器
|
||||
* @param contact 联系人
|
||||
* @param source 消息源类型
|
||||
*/
|
||||
public MiraiMessageSender(Contact contact, MessageSource source) {
|
||||
this.member = contact;
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int sendMessage(final String message) {
|
||||
log.debug("处理前的消息内容:\n{}", message);
|
||||
Message msgBody = processMessage(Objects.requireNonNull(message));
|
||||
log.debug("处理后的消息内容(可能出现乱序的情况, 但实际上顺序是没问题的):\n{}", msgBody);
|
||||
if(getFromGroup() == 0) {
|
||||
FriendMessage msgObject = (FriendMessage) messageObject;
|
||||
//FIXME(LamGC, 2020.04.10): 当前 Mirai 不支持私聊长文本, 所以发生异常是正常情况...
|
||||
msgObject.getSender().sendMessage(msgBody);
|
||||
} else {
|
||||
GroupMessage msgObject = (GroupMessage) messageObject;
|
||||
msgObject.getGroup().sendMessage(msgBody);
|
||||
}
|
||||
log.debug("处理后的消息内容(可能出现乱序的情况, 但实际上顺序是没问题的):\n{}", msgBody.contentToString());
|
||||
member.sendMessage(msgBody);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getImageUrl(String imageId) {
|
||||
return messageObject.getBot().queryImageUrl(MessageUtils.newImage(imageId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRawMessage() {
|
||||
return messageObject;
|
||||
}
|
||||
|
||||
private final static Pattern cqCodePattern = Pattern.compile("\\[.*?:.*?]");
|
||||
private final static Pattern cqCodePattern = BotCode.getCodePattern();
|
||||
private Message processMessage(final String message) {
|
||||
Matcher matcher = cqCodePattern.matcher(message);
|
||||
ArrayList<String> cqCode = new ArrayList<>();
|
||||
@ -79,7 +77,7 @@ public class MiraiMessageEvent extends MessageEvent {
|
||||
.replaceAll("&38", "&")
|
||||
.split("\\|");
|
||||
|
||||
CombinedMessage chain = MessageUtils.newChain().plus("");
|
||||
MessageChain messages = MessageUtils.newChain().plus("");
|
||||
int codeIndex = 0;
|
||||
for(String text : texts) {
|
||||
if(text.equals("{BotCode}")) {
|
||||
@ -90,32 +88,53 @@ public class MiraiMessageEvent extends MessageEvent {
|
||||
log.warn("解析待发送消息内的BotCode时发生异常, 请检查错误格式BotCode的来源并尽快排错!", e);
|
||||
continue;
|
||||
}
|
||||
chain = chain.plus(processBotCode(code));
|
||||
messages = messages.plus(processBotCode(code));
|
||||
} else {
|
||||
chain = chain.plus(text);
|
||||
messages = messages.plus(text);
|
||||
}
|
||||
}
|
||||
|
||||
return chain;
|
||||
return messages;
|
||||
}
|
||||
|
||||
private Message processBotCode(BotCode code) {
|
||||
switch(code.getFunctionName().toLowerCase()) {
|
||||
case "image":
|
||||
Image img;
|
||||
if(code.containsParameter("id")) {
|
||||
return MessageUtils.newImage(code.getParameter("id"));
|
||||
img = MessageUtils.newImage(code.getParameter("id"));
|
||||
} else if(code.containsParameter("absolutePath")) {
|
||||
return uploadImage(code);
|
||||
img = uploadImage(code);
|
||||
} else {
|
||||
return MessageUtils.newChain("(参数不存在)");
|
||||
}
|
||||
if(Strings.nullToEmpty(code.getParameter("flashImage"))
|
||||
.equalsIgnoreCase("true")) {
|
||||
return MessageUtils.flash(img);
|
||||
} else {
|
||||
return img;
|
||||
}
|
||||
case "face":
|
||||
if(!code.containsParameter("id")) {
|
||||
return MessageUtils.newChain("(无效的表情Id)");
|
||||
}
|
||||
int faceId = Integer.parseInt(code.getParameter("id"));
|
||||
if(faceId <= 0) {
|
||||
return MessageUtils.newChain("(无效的表情Id)");
|
||||
}
|
||||
return new Face(faceId);
|
||||
default:
|
||||
log.warn("解析到不支持的BotCode: {}", code);
|
||||
return MessageUtils.newChain("(不支持的BotCode)");
|
||||
}
|
||||
}
|
||||
|
||||
private Image uploadImage(BotCode code) {
|
||||
/**
|
||||
* 存在缓存的上传图片.
|
||||
* @param code 图片BotCode
|
||||
* @return Image对象
|
||||
*/
|
||||
public Image uploadImage(BotCode code) {
|
||||
log.debug("传入BotCode信息:\n{}", code);
|
||||
String absolutePath = code.getParameter("absolutePath");
|
||||
if(Strings.isNullOrEmpty(absolutePath)) {
|
||||
@ -125,20 +144,16 @@ public class MiraiMessageEvent extends MessageEvent {
|
||||
String imageName = code.getParameter("imageName");
|
||||
if(!Strings.isNullOrEmpty(imageName)) {
|
||||
Image image = null;
|
||||
imageName = (getMessageSource() + "." + imageName).intern();
|
||||
imageName = (source + "." + imageName).intern();
|
||||
if(!imageIdCache.exists(imageName) ||
|
||||
Strings.nullToEmpty(code.getParameter("updateCache")).equalsIgnoreCase("true")) {
|
||||
Strings.nullToEmpty(code.getParameter("updateCache")).equalsIgnoreCase("true")) {
|
||||
synchronized (imageName) {
|
||||
if(!imageIdCache.exists(imageName) ||
|
||||
Strings.nullToEmpty(code.getParameter("updateCache")) .equalsIgnoreCase("true")) {
|
||||
Strings.nullToEmpty(code.getParameter("updateCache")) .equalsIgnoreCase("true")) {
|
||||
log.debug("imageName [{}] 缓存失效或强制更新, 正在更新缓存...", imageName);
|
||||
image = uploadImage0(new File(absolutePath));
|
||||
if(Objects.isNull(image)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String cacheExpireAt;
|
||||
long expireTime = 0;
|
||||
long expireTime = 864000000; // 10d
|
||||
if(!Strings.isNullOrEmpty(cacheExpireAt = code.getParameter("cacheExpireAt"))) {
|
||||
try {
|
||||
expireTime = Integer.parseInt(cacheExpireAt);
|
||||
@ -147,7 +162,7 @@ public class MiraiMessageEvent extends MessageEvent {
|
||||
}
|
||||
}
|
||||
imageIdCache.update(imageName, image.getImageId(), expireTime);
|
||||
log.info("imageName [{}] 缓存更新完成.(有效时间: {})", imageName, expireTime);
|
||||
log.debug("imageName [{}] 缓存更新完成.(有效时间: {})", imageName, expireTime);
|
||||
} else {
|
||||
log.debug("ImageName: [{}] 缓存命中.", imageName);
|
||||
}
|
||||
@ -169,25 +184,7 @@ public class MiraiMessageEvent extends MessageEvent {
|
||||
}
|
||||
|
||||
private Image uploadImage0(File imageFile) {
|
||||
if(messageObject instanceof FriendMessage) {
|
||||
return messageObject.getSender().uploadImage(imageFile);
|
||||
} else if(messageObject instanceof GroupMessage) {
|
||||
return ((GroupMessage) messageObject).getGroup().uploadImage(imageFile);
|
||||
} else {
|
||||
log.warn("未知的ContactMessage类型: " + messageObject.toString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private String getMessageSource() {
|
||||
if(messageObject instanceof FriendMessage) {
|
||||
return "Private";
|
||||
} else if(messageObject instanceof GroupMessage) {
|
||||
return "Group";
|
||||
} else {
|
||||
log.warn("未知的ContactMessage类型: " + messageObject.toString());
|
||||
return "Unknown";
|
||||
}
|
||||
return member.uploadImage(imageFile);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package net.lamgc.cgj.bot.framework.mirai.message;
|
||||
|
||||
import net.lamgc.cgj.bot.message.MessageSender;
|
||||
import net.lamgc.cgj.bot.message.MessageSenderFactory;
|
||||
import net.lamgc.cgj.bot.message.MessageSource;
|
||||
import net.mamoe.mirai.Bot;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class MiraiMessageSenderFactory implements MessageSenderFactory {
|
||||
|
||||
private final Bot bot;
|
||||
|
||||
public MiraiMessageSenderFactory(Bot bot) {
|
||||
this.bot = bot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageSender createMessageSender(MessageSource source, long id) {
|
||||
Objects.requireNonNull(source);
|
||||
if(id <= 0) {
|
||||
throw new IllegalArgumentException("id cannot be 0 or negative: " + id);
|
||||
}
|
||||
return new MiraiMessageSender(bot, source, id);
|
||||
}
|
||||
}
|
15
src/main/java/net/lamgc/cgj/bot/message/MessageSender.java
Normal file
15
src/main/java/net/lamgc/cgj/bot/message/MessageSender.java
Normal file
@ -0,0 +1,15 @@
|
||||
package net.lamgc.cgj.bot.message;
|
||||
|
||||
public interface MessageSender {
|
||||
|
||||
/**
|
||||
* 发送消息并返回消息id
|
||||
* @param message 消息内容
|
||||
* @return 返回非负数则发送成功,
|
||||
* 返回0则发送器不支持消息Id,
|
||||
* 返回非0正整数则为消息Id,
|
||||
* 返回负数则为错误.
|
||||
*/
|
||||
int sendMessage(final String message);
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package net.lamgc.cgj.bot.message;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* 消息发送器构造
|
||||
*/
|
||||
public final class MessageSenderBuilder {
|
||||
|
||||
private final static AtomicReference<MessageSenderFactory> currentFactory = new AtomicReference<>();
|
||||
|
||||
private MessageSenderBuilder() {}
|
||||
|
||||
/**
|
||||
* 获取消息发送器
|
||||
* @param source 消息源类型
|
||||
* @param id 消息源Id
|
||||
* @return 返回新建的发送器
|
||||
*/
|
||||
public static MessageSender getMessageSender(MessageSource source, long id) {
|
||||
MessageSenderFactory messageSenderFactory = currentFactory.get();
|
||||
if(messageSenderFactory == null) {
|
||||
throw new IllegalStateException("The factory is not ready");
|
||||
}
|
||||
try {
|
||||
return messageSenderFactory.createMessageSender(source, id);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置消息发送器工厂
|
||||
* @param factory 工厂对象
|
||||
*/
|
||||
public static void setCurrentMessageSenderFactory(MessageSenderFactory factory) {
|
||||
if(currentFactory.get() != null) {
|
||||
throw new IllegalStateException("Factory already exists");
|
||||
}
|
||||
currentFactory.set(factory);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package net.lamgc.cgj.bot.message;
|
||||
|
||||
public interface MessageSenderFactory {
|
||||
|
||||
/**
|
||||
* 通过Id创建发送器
|
||||
* @param source 消息源
|
||||
* @param id 消息源id
|
||||
* @return 如果成功返回MessageSender
|
||||
*/
|
||||
MessageSender createMessageSender(MessageSource source, long id) throws Exception;
|
||||
|
||||
}
|
23
src/main/java/net/lamgc/cgj/bot/message/MessageSource.java
Normal file
23
src/main/java/net/lamgc/cgj/bot/message/MessageSource.java
Normal file
@ -0,0 +1,23 @@
|
||||
package net.lamgc.cgj.bot.message;
|
||||
|
||||
/**
|
||||
* 消息来源
|
||||
*/
|
||||
public enum MessageSource {
|
||||
/**
|
||||
* 私聊消息
|
||||
*/
|
||||
Private,
|
||||
/**
|
||||
* 群组消息
|
||||
*/
|
||||
Group,
|
||||
/**
|
||||
* 讨论组消息
|
||||
*/
|
||||
Discuss,
|
||||
/**
|
||||
* 未知来源
|
||||
*/
|
||||
Unknown
|
||||
}
|
@ -71,7 +71,7 @@ public class PreLoadDataComparator implements Comparator<JsonElement> {
|
||||
VIEW("viewCount"),
|
||||
;
|
||||
|
||||
final String attrName;
|
||||
public final String attrName;
|
||||
|
||||
Attribute(String attrName) {
|
||||
this.attrName = attrName;
|
||||
|
33
src/main/java/net/lamgc/cgj/pixiv/AdultContentDetector.java
Normal file
33
src/main/java/net/lamgc/cgj/pixiv/AdultContentDetector.java
Normal file
@ -0,0 +1,33 @@
|
||||
package net.lamgc.cgj.pixiv;
|
||||
|
||||
public interface AdultContentDetector {
|
||||
|
||||
/**
|
||||
* 检查某一作品的成人内容判断指数
|
||||
* @param illustId 作品Id
|
||||
* @param isUgoira 是否为动图
|
||||
* @param pageIndex 指定页数, 设为0或负数则视为单页面作品
|
||||
* @return 返回成人作品判断指数(0 ~ 1), 需按照情况设置阀值.
|
||||
*/
|
||||
double detect(int illustId, boolean isUgoira, int pageIndex) throws Exception;
|
||||
|
||||
/**
|
||||
* 检查某一作品是否为成人内容
|
||||
* @param illustId 作品Id
|
||||
* @param isUgoira 是否为动图
|
||||
* @param pageIndex 指定页数, 设为0或负数则视为单页面作品
|
||||
* @return 如果为true则为成人作品, 该方法将由检测器决定如何为成人作品.
|
||||
*/
|
||||
boolean isAdultContent(int illustId, boolean isUgoira, int pageIndex) throws Exception;
|
||||
|
||||
/**
|
||||
* 检查某一作品是否为成人内容
|
||||
* @param illustId 作品Id
|
||||
* @param isUgoira 是否为动图
|
||||
* @param pageIndex 指定页数, 设为0或负数则视为单页面作品
|
||||
* @param threshold 指数阀值, 当等于或大于该阀值时返回true.
|
||||
* @return 如果为true则为成人作品, 该方法将由 threshold 参数决定是否为成人作品.
|
||||
*/
|
||||
boolean isAdultContent(int illustId, boolean isUgoira, int pageIndex, double threshold) throws Exception;
|
||||
|
||||
}
|
@ -1,69 +0,0 @@
|
||||
package net.lamgc.cgj.pixiv;
|
||||
|
||||
/**
|
||||
* 插图(集)的信息
|
||||
*/
|
||||
public class IllustInfo{
|
||||
|
||||
/**
|
||||
* 插图(集)ID
|
||||
*/
|
||||
public final int illustID;
|
||||
|
||||
/**
|
||||
* 第几页
|
||||
*/
|
||||
public final int page;
|
||||
|
||||
/**
|
||||
* 插图标题
|
||||
*/
|
||||
public final String title;
|
||||
|
||||
/**
|
||||
* 插图说明
|
||||
*/
|
||||
public final String description;
|
||||
|
||||
/**
|
||||
* 插图标签
|
||||
*/
|
||||
public final String[] tags;
|
||||
|
||||
/**
|
||||
* 插图图片长度
|
||||
*/
|
||||
public final int width;
|
||||
|
||||
/**
|
||||
* 插图图片高度
|
||||
*/
|
||||
public final int height;
|
||||
|
||||
/**
|
||||
* 作者名
|
||||
*/
|
||||
public final String authorName;
|
||||
|
||||
/**
|
||||
* 作者用户ID
|
||||
*/
|
||||
public final int authorUserID;
|
||||
|
||||
public IllustInfo(int illustID, String title, String description, String[] tags, int width, int height, String authorName, int authorUserID){
|
||||
this(illustID, 0, title, description, tags, width, height, authorName, authorUserID);
|
||||
}
|
||||
|
||||
public IllustInfo(int illustID, int p, String title, String description, String[] tags, int width, int height, String authorName, int authorUserID){
|
||||
this.illustID = illustID;
|
||||
this.page = p;
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
this.tags = tags;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.authorName = authorName;
|
||||
this.authorUserID = authorUserID;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package net.lamgc.cgj.pixiv;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 使用ModerateContent服务开发的检测器.<br/>
|
||||
* ModerateContent: www.moderatecontent.com
|
||||
*/
|
||||
public class ModerateContentDetector implements AdultContentDetector {
|
||||
|
||||
private final static HttpClient httpClient = HttpClientBuilder.create().build();
|
||||
private final static Gson gson = new Gson();
|
||||
private final String requestUrl;
|
||||
|
||||
private final static String API_URL = "https://www.moderatecontent.com/api/v2?key={key}&url=https://pixiv.cat/";
|
||||
|
||||
/**
|
||||
* 创建一个使用ModerateContent鉴黄服务的检测器
|
||||
* @param apiKey API密钥
|
||||
*/
|
||||
public ModerateContentDetector(String apiKey) {
|
||||
requestUrl = API_URL.replace("{key}", apiKey);
|
||||
}
|
||||
|
||||
private JsonObject accessInterface(int illustId, boolean isUgoira, int pageIndex) throws IOException {
|
||||
HttpResponse response;
|
||||
if(pageIndex <= 0) {
|
||||
response = httpClient.execute(new HttpGet(requestUrl + illustId + (isUgoira ? ".gif" : ".jpg")));
|
||||
} else {
|
||||
response = httpClient.execute(new HttpGet(requestUrl + illustId + "-" + pageIndex + (isUgoira ? ".gif" : ".jpg")));
|
||||
}
|
||||
if(response.getStatusLine().getStatusCode() != 200) {
|
||||
throw new IOException("Http response error: " + response.getStatusLine());
|
||||
}
|
||||
|
||||
JsonObject result = gson.fromJson(EntityUtils.toString(response.getEntity()), JsonObject.class);
|
||||
if (result.get("error_code").getAsInt() != 0) {
|
||||
throw new IOException("Interface result error: " + (result.has("error") ? result.get("error").getAsString() : "(error message is empty)"));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double detect(int illustId, boolean isUgoira, int pageIndex) throws IOException {
|
||||
return accessInterface(illustId, isUgoira, pageIndex).getAsJsonObject("predictions").get("adult").getAsDouble();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAdultContent(int illustId, boolean isUgoira, int pageIndex) throws IOException {
|
||||
return accessInterface(illustId, isUgoira, pageIndex).get("rating_index").getAsInt() == 3;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAdultContent(int illustId, boolean isUgoira, int pageIndex, double threshold) throws IOException {
|
||||
return detect(illustId, isUgoira, pageIndex) >= threshold;
|
||||
}
|
||||
}
|
@ -1,267 +0,0 @@
|
||||
package net.lamgc.cgj.pixiv;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.conn.ConnectionPoolTimeoutException;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
|
||||
public class Pixiv {
|
||||
|
||||
/**
|
||||
* illust Link
|
||||
*/
|
||||
public final static String ATTR_LINK = "link";
|
||||
|
||||
/**
|
||||
* illust Id
|
||||
*/
|
||||
public final static String ATTR_ILLUST_ID = "illustId";
|
||||
|
||||
/**
|
||||
* illust Title
|
||||
*/
|
||||
public final static String ATTR_TITLE = "title";
|
||||
|
||||
/**
|
||||
* illust Author Name
|
||||
*/
|
||||
public final static String ATTR_AUTHOR_NAME = "authorName";
|
||||
|
||||
/**
|
||||
* illust Author UserID
|
||||
*/
|
||||
public final static String ATTR_AUTHOR_ID = "authorId";
|
||||
|
||||
private final HttpClient httpClient;
|
||||
|
||||
public Pixiv(HttpClient client){
|
||||
this.httpClient = client;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用帐号密码登录Pixiv
|
||||
* @param PixivID Pixiv账户登录名
|
||||
* @param Password Pixiv帐号密码
|
||||
* @throws IOException 当登录连接出现异常时抛出
|
||||
* @deprecated {@link PixivSession#Login(String, String)} 已经废除, 故本方法不可用
|
||||
*/
|
||||
public Pixiv(String PixivID, String Password) throws IOException {
|
||||
this(PixivID, Password, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用帐号密码登录Pixiv
|
||||
* @param PixivID Pixiv账户登录名
|
||||
* @param Password Pixiv帐号密码
|
||||
* @param proxy 代理设置
|
||||
* @throws IOException 当登录连接出现异常时抛出
|
||||
* @deprecated {@link PixivSession#Login(String, String)} 已经废除, 故本方法不可用
|
||||
*/
|
||||
public Pixiv(String PixivID, String Password, HttpHost proxy) throws IOException {
|
||||
PixivSession pixivSession = new PixivSession(proxy, null);
|
||||
if(pixivSession.Login(PixivID, Password)){
|
||||
System.out.println("P站登录成功!");
|
||||
}else{
|
||||
System.out.println("P站登录失败!错误信息: " + pixivSession.getErrMsg());
|
||||
throw new RuntimeException(pixivSession.getErrMsg());
|
||||
}
|
||||
//httpClient = pixivSession.getHttpClient();
|
||||
httpClient = HttpClientBuilder.create()
|
||||
.setDefaultCookieStore(pixivSession.getCookieStore())
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取首页推荐列表
|
||||
* @return 首页推荐列表, 一个Map对应一个推荐项, 使用<code>ATTR_</code>开头常量访问即可
|
||||
* @throws IOException
|
||||
*/
|
||||
public List<Map<String, String>> getRecommend() throws IOException {
|
||||
HttpGet getRequest = new HttpGet(PixivURL.PIXIV_INDEX_URL);
|
||||
HttpResponse response = httpClient.execute(getRequest);
|
||||
String pageAsXML = EntityUtils.toString(response.getEntity(),"utf-8");
|
||||
|
||||
//获取推荐图列表(li)
|
||||
//System.out.println(pageAsXML);
|
||||
|
||||
Document document = Jsoup.parse(pageAsXML);
|
||||
|
||||
List<String> links = document.select("._image-items.gtm-illust-recommend-zone>li>.gtm-illust-recommend-thumbnail-link").eachAttr("href");
|
||||
List<String> illustId = document.select("._image-items.gtm-illust-recommend-zone>li>.gtm-illust-recommend-thumbnail-link").eachAttr("data-gtm-recommend-illust-id");
|
||||
List<String> title = document.select("._image-items.gtm-illust-recommend-zone>li>.gtm-illust-recommend-title>h1").eachAttr("title");
|
||||
List<String> authorName = document.select("._image-items.gtm-illust-recommend-zone>li>.gtm-illust-recommend-user-name").eachText();
|
||||
List<String> authorId = document.select("._image-items.gtm-illust-recommend-zone>li>.gtm-illust-recommend-user-name").eachAttr("data-user_id");
|
||||
|
||||
List<Map<String, String>> recommendList = new ArrayList<>();
|
||||
for(int i = 0; i < links.size(); i++){
|
||||
//System.out.println(links.get(i));
|
||||
Map<String, String> info = new HashMap<>();
|
||||
info.put(ATTR_LINK, PixivURL.PIXIV_INDEX_URL + links.get(i));
|
||||
info.put(ATTR_ILLUST_ID, illustId.get(i));
|
||||
info.put(ATTR_TITLE, title.get(i));
|
||||
info.put(ATTR_AUTHOR_NAME, authorName.get(i));
|
||||
info.put(ATTR_AUTHOR_ID, authorId.get(i));
|
||||
recommendList.add(info);
|
||||
}
|
||||
return recommendList;
|
||||
}
|
||||
|
||||
public String[] getAllDownloadLink(int illustID) throws IOException {
|
||||
HttpGet illustPage = new HttpGet(PixivURL.PIXIV_ILLUST_API_URL.replaceAll("\\{illustId}", String.valueOf(illustID)));
|
||||
HttpResponse response = httpClient.execute(illustPage);
|
||||
String pageAsXML = EntityUtils.toString(response.getEntity(),"utf-8");
|
||||
//System.out.println(pageAsXML);
|
||||
JsonObject resultObj = (JsonObject) new JsonParser().parse(pageAsXML);
|
||||
if(!resultObj.get("error").getAsBoolean()){
|
||||
JsonArray bodyArray = resultObj.get("body").getAsJsonArray();
|
||||
int length = bodyArray.size();
|
||||
String[] result = new String[length];
|
||||
for(int i = 0; i < length; i++){
|
||||
JsonObject childObj = bodyArray.get(i).getAsJsonObject();
|
||||
result[i] = childObj.get("urls").getAsJsonObject().get("original").getAsString();
|
||||
}
|
||||
return result;
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 下载P站图片
|
||||
* @param illustID 插图ID
|
||||
* @return 成功返回图片输入流,失败或为多图则返回null
|
||||
*/
|
||||
public InputStream[] downloadIllustImage(int illustID) throws IOException {
|
||||
String[] links = getAllDownloadLink(illustID);
|
||||
List<InputStream> inputStreamList = new ArrayList<>();
|
||||
int count = 1;
|
||||
boolean retry = false;
|
||||
for(int i = 0; i < links.length; i++){
|
||||
try {
|
||||
long sleepTime = (new Random().nextInt(4) + 2) * 1000;
|
||||
System.out.println("nextTime: " + (float)(sleepTime / 1000));
|
||||
Thread.sleep(sleepTime);
|
||||
} catch (InterruptedException ignored) {}
|
||||
String link = links[i];
|
||||
System.out.print("page:" + count++ + "/" + links.length + " ...");
|
||||
HttpGet imgGet = new HttpGet(link);
|
||||
//关键!如果不加上Referer的话,会返回403
|
||||
imgGet.setHeader("Referer", PixivURL.PIXIV_ILLUST_MEDIUM_URL.replaceAll("\\{illustId}", String.valueOf(illustID)));
|
||||
RequestConfig config = RequestConfig.custom()
|
||||
.setConnectTimeout(20 * 1000)
|
||||
.setConnectionRequestTimeout(20 * 1000)
|
||||
.setSocketTimeout(30 * 1000)
|
||||
.build();
|
||||
imgGet.setConfig(config);
|
||||
HttpResponse response;
|
||||
try {
|
||||
response = httpClient.execute(imgGet);
|
||||
}catch(ConnectionPoolTimeoutException e){
|
||||
if(retry){
|
||||
retry = false;
|
||||
System.out.println("获取失败,跳过...");
|
||||
continue;
|
||||
}
|
||||
System.out.println("连接超时,重新获取...");
|
||||
retry = true;
|
||||
i--;
|
||||
continue;
|
||||
}
|
||||
retry = false;
|
||||
|
||||
ByteArrayOutputStream cacheOutputStream = new ByteArrayOutputStream((int)response.getEntity().getContentLength());
|
||||
InputStream content = response.getEntity().getContent();
|
||||
int readLength;
|
||||
byte[] cache = new byte[4096];
|
||||
while((readLength = content.read(cache)) != -1){
|
||||
cacheOutputStream.write(cache, 0, readLength);
|
||||
}
|
||||
byte[] data = cacheOutputStream.toByteArray();
|
||||
//System.out.println("读到数据: " + data.length);
|
||||
inputStreamList.add(new ByteArrayInputStream(data));
|
||||
System.out.println("done!(length: " + response.getEntity().getContentLength() + ")");
|
||||
|
||||
}
|
||||
return inputStreamList.toArray(new InputStream[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载P站图片
|
||||
* @param illustID 插图ID
|
||||
* @return 成功返回图片输入流,失败或为多图则返回null
|
||||
*/
|
||||
public InputStream downloadIllustImages(int illustID){
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过解析插图详情页获取
|
||||
* - 插图标题
|
||||
* - 插图作者(及其UserId)
|
||||
* - 插图上传时间
|
||||
* - 插图标签(原始标签)
|
||||
* ...
|
||||
* @return 成功返回IllustInfo对象,失败返回null
|
||||
*/
|
||||
public IllustInfo[] getIllustInfo(int[] illustIDs) throws IOException {
|
||||
//获取Api
|
||||
HttpGet apiRequest = new HttpGet(PixivURL.getPixivIllustInfoAPI(illustIDs));
|
||||
HttpResponse response = httpClient.execute(apiRequest);
|
||||
String resultText = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
||||
System.out.println(resultText);
|
||||
JsonObject resultObj = ((JsonObject) new JsonParser().parse(resultText));
|
||||
if(resultObj.get("error").getAsBoolean()){
|
||||
System.err.println("获取失败!");
|
||||
return null;
|
||||
}
|
||||
List<IllustInfo> illustInfoList = new ArrayList<>();
|
||||
JsonArray illustArray = resultObj.get("body").getAsJsonObject().get("illusts").getAsJsonArray();
|
||||
illustArray.forEach(jsonElement -> {
|
||||
JsonObject illustInfoObj = jsonElement.getAsJsonObject();
|
||||
JsonArray tagsArray = illustInfoObj.get("tags").getAsJsonArray();
|
||||
String[] tags = new String[tagsArray.size()];
|
||||
for(int i = 0; i < tags.length; i++){
|
||||
tags[i] = tagsArray.get(i).getAsString();
|
||||
}
|
||||
//TODO: 通过不需要作者id就能获取图片信息的api无法获取图片尺寸
|
||||
IllustInfo illustInfo = new IllustInfo(
|
||||
illustInfoObj.get("workId").getAsInt(),
|
||||
illustInfoObj.get("title").getAsString(),
|
||||
null,
|
||||
tags,
|
||||
-1,
|
||||
-1,
|
||||
illustInfoObj.get("userName").getAsString(),
|
||||
illustInfoObj.get("userId").getAsInt()
|
||||
);
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定用户的所有插画
|
||||
*/
|
||||
public void getUserAllIllustTest() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -4,6 +4,7 @@ import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import io.netty.handler.codec.http.HttpHeaderNames;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpHost;
|
||||
@ -32,7 +33,7 @@ import java.util.function.BiConsumer;
|
||||
|
||||
public class PixivDownload {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger("PixivDownload");
|
||||
private final static Logger log = LoggerFactory.getLogger(PixivDownload.class.getName());
|
||||
|
||||
private final HttpClient httpClient;
|
||||
|
||||
@ -256,8 +257,8 @@ public class PixivDownload {
|
||||
public static List<JsonObject> getRanking(List<JsonObject> rankingList, int rankStart, int range) {
|
||||
log.debug("正在读取JsonArray...(rankStart: {}, range: {})", rankStart, range);
|
||||
ArrayList<JsonObject> results = new ArrayList<>(rankingList.size());
|
||||
for (int rankIndex = rankStart; rankIndex < rankingList.size() && rankIndex < range; rankIndex++) {
|
||||
JsonElement jsonElement = rankingList.get(rankIndex);
|
||||
for (int rankIndex = rankStart; rankIndex < rankStart + range; rankIndex++) {
|
||||
JsonElement jsonElement = rankingList.get(rankIndex - rankStart);
|
||||
JsonObject rankInfo = jsonElement.getAsJsonObject();
|
||||
int rank = rankInfo.get("rank").getAsInt();
|
||||
int illustId = rankInfo.get("illust_id").getAsInt();
|
||||
@ -279,90 +280,64 @@ public class PixivDownload {
|
||||
* @return 返回List对象
|
||||
*/
|
||||
public static List<JsonObject> getRanking(JsonArray rankingArray, int rankStart, int range) {
|
||||
log.debug("正在读取JsonArray...(rankStart: {}, range: {})", rankStart, range);
|
||||
ArrayList<JsonObject> results = new ArrayList<>(rankingArray.size());
|
||||
for (int rankIndex = rankStart; rankIndex < rankingArray.size() && rankIndex < range; rankIndex++) {
|
||||
JsonElement jsonElement = rankingArray.get(rankIndex);
|
||||
JsonObject rankInfo = jsonElement.getAsJsonObject();
|
||||
int rank = rankInfo.get("rank").getAsInt();
|
||||
int illustId = rankInfo.get("illust_id").getAsInt();
|
||||
int authorId = rankInfo.get("user_id").getAsInt();
|
||||
String authorName = rankInfo.get("user_name").getAsString();
|
||||
String title = rankInfo.get("title").getAsString();
|
||||
log.debug("Array-当前到第 {}/{} 名(总共 {} 名), IllustID: {}, Author: ({}) {}, Title: {}", rank, rankStart + range, range, illustId, authorId, authorName, title);
|
||||
results.add(rankInfo);
|
||||
}
|
||||
log.debug("JsonArray读取完成.");
|
||||
return results;
|
||||
List<JsonObject> list = new Gson().fromJson(rankingArray, new TypeToken<List<JsonObject>>(){}.getType());
|
||||
return getRanking(list, rankStart, range);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取排行榜
|
||||
* 获取排行榜.
|
||||
* <p>注意: 如果范围实际上没超出, 但返回排行榜不足, 会导致与实际请求的数量不符, 需要检查</p>
|
||||
* @param contentType 排行榜类型
|
||||
* @param mode 排行榜模式
|
||||
* @param time 查询时间
|
||||
* @param rankStart 开始排名, 从1开始
|
||||
* @param range 取范围
|
||||
* @return 成功返回有值List, 失败且无异常返回空
|
||||
* @throws IOException 获取异常时抛出
|
||||
* @throws IllegalArgumentException 当{@linkplain net.lamgc.cgj.pixiv.PixivURL.RankingContentType RankingContentType}
|
||||
* 与{@linkplain net.lamgc.cgj.pixiv.PixivURL.RankingMode RankingMode}互不兼容时抛出
|
||||
* @throws IndexOutOfBoundsException 当排行榜选取范围超出排行榜范围时抛出(排行榜范围为 1 ~ 500 名)
|
||||
* @throws IOException 当Http请求发生异常时抛出, 或Http请求响应码非200时抛出
|
||||
*/
|
||||
public List<JsonObject> getRanking(PixivURL.RankingContentType contentType, PixivURL.RankingMode mode,
|
||||
Date time, int rankStart, int range) throws IOException {
|
||||
if(rankStart <= 0) {
|
||||
throw new IllegalArgumentException("rankStart cannot be less than or equal to zero");
|
||||
}
|
||||
if(range <= 0) {
|
||||
throw new IllegalArgumentException("range cannot be less than or equal to zero");
|
||||
}
|
||||
|
||||
if(!contentType.isSupportedMode(mode)) {
|
||||
Date time, int rankStart, int range) throws IOException {
|
||||
Objects.requireNonNull(time);
|
||||
if(!Objects.requireNonNull(contentType).isSupportedMode(Objects.requireNonNull(mode))) {
|
||||
throw new IllegalArgumentException("ContentType不支持指定的RankingMode: ContentType: " + contentType.name() + ", Mode: " + mode.name());
|
||||
} else if(rankStart <= 0) {
|
||||
throw new IndexOutOfBoundsException("rankStart cannot be less than or equal to zero: " + rankStart);
|
||||
} else if(range <= 0) {
|
||||
throw new IndexOutOfBoundsException("range cannot be less than or equal to zero:" + range);
|
||||
} else if(rankStart + range - 1 > 500) {
|
||||
throw new IndexOutOfBoundsException("排名选取范围超出了排行榜范围: rankStart=" + rankStart + ", range=" + range + ", length:" + (rankStart + range - 1));
|
||||
}
|
||||
|
||||
int startPage = (int) Math.ceil(rankStart / 50F);
|
||||
int requestFrequency = (int) Math.ceil((rankStart + (range - 1)) / 50F);
|
||||
int surplusQuantity = range;
|
||||
boolean firstRequest = true;
|
||||
int startPages = (int) Math.max(1, Math.floor(rankStart / 50F));
|
||||
int endPages = (int) Math.min(10, Math.ceil((rankStart + range) / 50F));
|
||||
int startIndex = rankStart - 1;
|
||||
int count = 0;
|
||||
Gson gson = new Gson();
|
||||
ArrayList<JsonObject> results = new ArrayList<>();
|
||||
for (int requestCount = startPage; requestCount <= requestFrequency && requestCount <= 10; requestCount++) {
|
||||
int rangeStart = (requestCount - 1) * 50 + 1;
|
||||
log.debug("正在请求第 {} 到 {} 位排名榜 (第{}次请求, 共 {} 次)", rangeStart, rangeStart + 49, requestCount - startPage + 1, requestFrequency - startPage + 1);
|
||||
HttpGet request = createHttpGetRequest(PixivURL.getRankingLink(contentType, mode, time, requestCount, true));
|
||||
log.debug("Request URL: {}", request.getURI());
|
||||
ArrayList<JsonObject> results = new ArrayList<>(range);
|
||||
boolean canNext = true;
|
||||
for (int pageIndex = startPages; canNext && pageIndex <= endPages && count < range; pageIndex++) {
|
||||
HttpGet request = createHttpGetRequest(PixivURL.getRankingLink(contentType, mode, time, pageIndex, true));
|
||||
log.debug("RequestUri: {}", request.getURI());
|
||||
HttpResponse response = httpClient.execute(request);
|
||||
String content = EntityUtils.toString(response.getEntity());
|
||||
log.debug("Content: " + content);
|
||||
JsonObject contentObject = gson.fromJson(content, JsonObject.class);
|
||||
if(contentObject.has("error")) {
|
||||
log.warn("接口报错, 返回信息: {}", contentObject.get("error").getAsString());
|
||||
break;
|
||||
String responseBody = EntityUtils.toString(response.getEntity());
|
||||
log.debug("ResponseBody: {}", responseBody);
|
||||
if(response.getStatusLine().getStatusCode() != 200) {
|
||||
throw new IOException("Http Response Error: '" + response.getStatusLine() + "', ResponseBody: '" + responseBody + '\'');
|
||||
}
|
||||
JsonArray rankingArray = contentObject.getAsJsonArray("contents");
|
||||
log.debug("正在解析数据...");
|
||||
|
||||
//需要添加一个总量, 否则会完整跑完一次.
|
||||
//检查是否为最后一次请求,和剩余量有多少
|
||||
int firstRequestStartIndex = (rankStart % 50) - 1;
|
||||
for (int rankIndex = firstRequest ? firstRequestStartIndex : 0; rankIndex < rankingArray.size() && surplusQuantity > 0; rankIndex++, surplusQuantity--) {
|
||||
JsonElement jsonElement = rankingArray.get(rankIndex);
|
||||
JsonObject rankInfo = jsonElement.getAsJsonObject();
|
||||
int rank = rankInfo.get("rank").getAsInt();
|
||||
int illustId = rankInfo.get("illust_id").getAsInt();
|
||||
int authorId = rankInfo.get("user_id").getAsInt();
|
||||
String authorName = rankInfo.get("user_name").getAsString();
|
||||
String title = rankInfo.get("title").getAsString();
|
||||
log.debug("Download-当前到第 {}/{} 名(总共 {} 名), IllustID: {}, Author: ({}) {}, Title: {}", rank, rankStart + range - 1, range, illustId, authorId, authorName, title);
|
||||
results.add(rankInfo);
|
||||
JsonObject resultObject = gson.fromJson(responseBody, JsonObject.class);
|
||||
canNext = resultObject.get("next").getAsJsonPrimitive().isNumber();
|
||||
JsonArray resultArray = resultObject.getAsJsonArray("contents");
|
||||
for (int resultIndex = startIndex; resultIndex < resultArray.size() && count < range; resultIndex++, count++) {
|
||||
results.add(resultArray.get(resultIndex).getAsJsonObject());
|
||||
}
|
||||
firstRequest = false;
|
||||
log.debug("第 {} 到 {} 位排名榜完成. (第{}次请求)", rangeStart, rangeStart + 49, requestCount);
|
||||
}
|
||||
|
||||
if(requestFrequency > 10) {
|
||||
log.warn("请求的排名榜范围超出所支持的范围, 已终止请求.");
|
||||
// 重置索引
|
||||
startIndex = 0;
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
@ -463,13 +438,14 @@ public class PixivDownload {
|
||||
THUMB_MINI
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取帐号所有的收藏插画,并以输入流形式提供
|
||||
* @return 获取所有链接的InputStream, 请注意关闭InputStream
|
||||
* @throws IOException 当获取时发生异常则直接抛出
|
||||
* @deprecated 该方法可能会导致已经打开的InputStream超时, 使图片获取失败,
|
||||
* 请直接使用{@linkplain #getCollectionAsInputStream(PageQuality, BiConsumer)}
|
||||
*/
|
||||
@Deprecated
|
||||
public Set<Map.Entry<String, InputStream>> getCollectionAsInputStream(PageQuality quality) throws IOException {
|
||||
HashSet<Map.Entry<String, InputStream>> illustInputStreamSet = new HashSet<>();
|
||||
getCollectionAsInputStream(quality, (link, inputStream) -> illustInputStreamSet.add(new AbstractMap.SimpleEntry<>(link, inputStream)));
|
||||
@ -566,8 +542,9 @@ public class PixivDownload {
|
||||
* }
|
||||
* </pre>
|
||||
* @throws IOException 当请求发生异常, 或接口返回错误信息时抛出.
|
||||
* @throws NoSuchElementException 当该作品不存在时抛出异常
|
||||
*/
|
||||
public JsonObject getIllustInfoByIllustId(int illustId) throws IOException {
|
||||
public JsonObject getIllustInfoByIllustId(int illustId) throws IOException, NoSuchElementException {
|
||||
HttpGet request = createHttpGetRequest(PixivURL.getPixivIllustInfoAPI(illustId));
|
||||
HttpResponse response = httpClient.execute(request);
|
||||
String responseStr = EntityUtils.toString(response.getEntity());
|
||||
@ -582,7 +559,7 @@ public class PixivDownload {
|
||||
if(illustsArray.size() == 1) {
|
||||
return illustsArray.get(0).getAsJsonObject();
|
||||
} else {
|
||||
return null;
|
||||
throw new NoSuchElementException("No work found: " + illustId);
|
||||
}
|
||||
}
|
||||
|
||||
@ -630,12 +607,29 @@ public class PixivDownload {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过预加载数据获取作品类型
|
||||
* @param illustId 作品Id
|
||||
* @param preLoadDataObject 预加载数据(IllustInfo也可以)
|
||||
* @return 如果存在illustType属性, 则返回对应项, 如没有, 或数据内不存在指定作品id的数据, 返回null
|
||||
*/
|
||||
public static PixivIllustType getIllustTypeByPreLoadData(int illustId, JsonObject preLoadDataObject) {
|
||||
JsonObject illustData;
|
||||
JsonObject illustData = null;
|
||||
if(preLoadDataObject.has("illust")) {
|
||||
illustData = preLoadDataObject.getAsJsonObject("illust").getAsJsonObject(String.valueOf(illustId));
|
||||
} else if(preLoadDataObject.has(String.valueOf(illustId))) {
|
||||
illustData = preLoadDataObject.getAsJsonObject(String.valueOf(illustId));
|
||||
} else if(preLoadDataObject.has("body")) { // 解析IllustInfo
|
||||
for (JsonElement jsonElement : preLoadDataObject.getAsJsonObject("body").getAsJsonArray("illusts")) {
|
||||
JsonObject illustInfo = jsonElement.getAsJsonObject();
|
||||
if (illustInfo.get("illustId").getAsInt() == illustId) {
|
||||
illustData = illustInfo;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(illustData == null) {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
illustData = preLoadDataObject;
|
||||
}
|
||||
|
@ -9,6 +9,13 @@ import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Pixiv搜索URL构造器
|
||||
* <p>该构造器通过分析Pixiv搜索链接可用的参数而开发, 对搜索链接的构造有高度自定义能力.</p>
|
||||
* @author LamGC
|
||||
* @see PixivURL#PIXIV_SEARCH_CONTENT_URL
|
||||
*/
|
||||
@SuppressWarnings("ALL")
|
||||
public class PixivSearchBuilder {
|
||||
|
||||
private final String content;
|
||||
|
@ -1,171 +0,0 @@
|
||||
package net.lamgc.cgj.pixiv;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.CookieStore;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.impl.client.BasicCookieStore;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PixivSession {
|
||||
|
||||
public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36";
|
||||
|
||||
/**
|
||||
* 全登陆过程的关键,
|
||||
* 保存会话用的cookieStore!
|
||||
*/
|
||||
private CookieStore cookieStore = new BasicCookieStore();
|
||||
|
||||
/**
|
||||
* 可以直接使用的HttpClient对象
|
||||
*/
|
||||
private HttpClient httpClient;
|
||||
|
||||
/**
|
||||
* 最后一次登录的错误信息
|
||||
*/
|
||||
private String errMsg;
|
||||
|
||||
public PixivSession(){
|
||||
this(null);
|
||||
}
|
||||
|
||||
public PixivSession(CookieStore cookieStore){
|
||||
this(null, cookieStore);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个Pixiv登录会话
|
||||
*/
|
||||
public PixivSession(HttpHost proxy, CookieStore cookieStore) {
|
||||
if(cookieStore != null){
|
||||
this.cookieStore = cookieStore;
|
||||
}
|
||||
List<Header> defaultHeader = new ArrayList<>();
|
||||
defaultHeader.add(new BasicHeader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"));
|
||||
defaultHeader.add(new BasicHeader("user-agent", PixivSession.USER_AGENT));
|
||||
defaultHeader.add(new BasicHeader("accept-encoding", "gzip, deflate, br"));
|
||||
defaultHeader.add(new BasicHeader("accept-language", "zh-CN,zh;q=0.9"));
|
||||
|
||||
/*defaultHeader.add(new BasicHeader("sec-fetch-mode", "navigate"));
|
||||
defaultHeader.add(new BasicHeader("sec-fetch-site", "same-origin"));
|
||||
defaultHeader.add(new BasicHeader("upgrade-insecure-requests", "1"));*/
|
||||
//创建一个Http访问器
|
||||
httpClient = HttpClients.custom()
|
||||
.setDefaultCookieStore(cookieStore)
|
||||
.setDefaultHeaders(defaultHeader)
|
||||
.setProxy(proxy)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 程序自行通过帐号密码登录Pixiv.
|
||||
* @param PixivID Pixiv帐号
|
||||
* @param Password Pixiv密码
|
||||
* @return 登录成功返回true
|
||||
* @throws IOException 当登录抛出异常时返回
|
||||
* @deprecated Pixiv已经新增Google人机验证, 程序已无法自行登录Pixiv
|
||||
*/
|
||||
public boolean Login(String PixivID, String Password) throws IOException {
|
||||
// 获取登录接口所需的PostKey
|
||||
String post_key = getPostKey();
|
||||
HttpPost postRequest = new HttpPost(PixivURL.PIXIV_LOGIN_URL); //https://accounts.pixiv.net/api/login?lang=zh
|
||||
List<NameValuePair> params = new ArrayList<>();
|
||||
params.add(new BasicNameValuePair("pixiv_id", PixivID));
|
||||
params.add(new BasicNameValuePair("password", Password));
|
||||
params.add(new BasicNameValuePair("post_key", post_key));
|
||||
//Form编码表单,作为Post的数据
|
||||
postRequest.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));
|
||||
//启动访问
|
||||
HttpResponse response = httpClient.execute(postRequest);
|
||||
//获取接口返回数据
|
||||
String httpXML = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
||||
System.out.println(httpXML);
|
||||
JsonObject responseJson = (JsonObject) new JsonParser().parse(httpXML);
|
||||
if(!responseJson.get("error").getAsBoolean() && !responseJson.get("body").getAsJsonObject().has("validation_errors")){
|
||||
errMsg = null;
|
||||
return true;
|
||||
}else{
|
||||
errMsg = responseJson.get("body").getAsJsonObject().get("validation_errors").toString();
|
||||
//System.err.println("登录失败!MSG: " + errMsg);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录前准备, 获取PostKey
|
||||
* @return Post_Key
|
||||
*/
|
||||
private String getPostKey() throws IOException {
|
||||
//创建请求,获取PostKey
|
||||
HttpGet getRequest = new HttpGet(PixivURL.PIXIV_LOGIN_PAGE_URL);
|
||||
//设置请求
|
||||
//getRequest.setConfig(config);
|
||||
getRequest.setHeader("User-Agent", USER_AGENT);
|
||||
//启动访问
|
||||
HttpResponse response = httpClient.execute(getRequest);
|
||||
//获取网页内容
|
||||
String pageAsXML = EntityUtils.toString(response.getEntity(),"utf-8");
|
||||
//创建Http解析器
|
||||
Document document = Jsoup.parse(pageAsXML);
|
||||
//获取init-config内容
|
||||
String init_config = document.getElementById("init-config").val();
|
||||
//System.out.println(init_config);
|
||||
//创建Json解析器解析init-config
|
||||
JsonObject initConfigObj = (JsonObject) new JsonParser().parse(init_config);
|
||||
//检查是否有postKey
|
||||
if(!initConfigObj.has("pixivAccount.postKey")){
|
||||
throw new RuntimeException("postKey获取失败!可能是Pixiv修改了登录过程!");
|
||||
}
|
||||
//获取postKey
|
||||
return initConfigObj.get("pixivAccount.postKey").getAsString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取CookieStore
|
||||
* @return CookieStore
|
||||
*/
|
||||
public CookieStore getCookieStore(){
|
||||
return cookieStore;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取可直接使用的HttpClient对象
|
||||
* @return 已配置好的HttpClient对象
|
||||
*/
|
||||
public HttpClient getHttpClient(){
|
||||
return this.httpClient;
|
||||
}
|
||||
|
||||
public boolean hasError(){
|
||||
return errMsg == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取错误信息
|
||||
* @return 返回登录错误信息
|
||||
* @deprecated {@link #Login(String, String)}已经废除, 故本接口废除
|
||||
*/
|
||||
public String getErrMsg(){
|
||||
return errMsg;
|
||||
}
|
||||
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
package net.lamgc.cgj.pixiv;
|
||||
|
||||
public class PixivTag {
|
||||
|
||||
public static PixivTag create(String tagName) {
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private PixivTag() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -9,6 +9,7 @@ import java.util.GregorianCalendar;
|
||||
/**
|
||||
* 目前已整理的一些Pixiv接口列表
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class PixivURL {
|
||||
|
||||
|
||||
@ -52,10 +53,10 @@ public class PixivURL {
|
||||
|
||||
/**
|
||||
* P站用户插图列表获取API
|
||||
* <p>所需数据在 body属性内的 illusts(属性名,属性值不重要), manga(多图) pickup(精选)</p>
|
||||
* 需要替换的文本:
|
||||
* {userId} - 用户ID
|
||||
*/
|
||||
//TODO: 所需数据在 body属性内的 illusts(属性名,属性值不重要), manga(多图) pickup(精选)
|
||||
//{"error":false,"message":"","body":{"illusts":{"74369837":null,"70990542":null,"70608653":null,"69755191":null,"69729450":null,"69729416":null,"69503608":null,"69288766":null,"69083882":null,"69051458":null,"68484200":null,"68216927":null,"68216866":null,"68192333":null,"67915106":null,"67914932":null,"67854803":null,"67854745":null,"67854670":null,"67787211":null,"67772199":null,"67770637":null,"67754861":null,"67754804":null,"67754726":null,"67740486":null,"67740480":null,"67740450":null,"67740434":null,"67726337":null,"67499196":null,"67499163":null,"67499145":null,"67499111":null,"67499085":null,"67499038":null,"67498987":null,"67473178":null,"66271465":null,"63682753":null,"63682697":null,"59385148":null,"59383265":null,"59383240":null,"59383227":null,"59383173":null},"manga":[],"novels":[],"mangaSeries":[],"novelSeries":[],"pickup":[],"bookmarkCount":{"public":{"illust":1,"novel":0},"private":{"illust":0,"novel":0}}}}
|
||||
public static final String PIXIV_USER_ILLUST_LIST_URL = "https://www.pixiv.net/ajax/user/{userId}/profile/all";
|
||||
|
||||
@ -186,7 +187,7 @@ public class PixivURL {
|
||||
public static String getRankingLink(RankingContentType contentType, RankingMode mode, Date time, int pageIndex, boolean json){
|
||||
StringBuilder linkBuilder = new StringBuilder(PIXIV_RANKING_LINK);
|
||||
linkBuilder.append("mode=").append(mode == null ? RankingMode.MODE_DAILY.modeParam : mode.modeParam);
|
||||
if(contentType != null && !contentType.equals(RankingContentType.ALL)){
|
||||
if(contentType != null && !contentType.equals(RankingContentType.TYPE_ALL)){
|
||||
linkBuilder.append("&content=").append(contentType.typeName);
|
||||
}
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
|
||||
@ -278,7 +279,7 @@ public class PixivURL {
|
||||
* 排名榜类型
|
||||
*/
|
||||
public enum RankingContentType{
|
||||
ALL("", RankingMode.values()),
|
||||
TYPE_ALL("", RankingMode.values()),
|
||||
/**
|
||||
* 插画
|
||||
* 支持的时间类型: 每天, 每周, 每月, 新人
|
||||
@ -336,7 +337,7 @@ public class PixivURL {
|
||||
* @return 如果支持返回true
|
||||
*/
|
||||
public boolean isSupportedMode(RankingMode mode) {
|
||||
return Arrays.binarySearch(supportedMode, mode) != -1;
|
||||
return Arrays.binarySearch(supportedMode, mode) >= 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,12 +26,11 @@ import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
/**
|
||||
* Pixiv动图构建器.
|
||||
* 可便捷的接收并处理动图.
|
||||
* Pixiv动图构建器
|
||||
*/
|
||||
public final class PixivUgoiraBuilder {
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger(PixivUgoiraBuilder.class.getSimpleName() + "@" + Integer.toHexString(this.hashCode()));
|
||||
private final Logger log = LoggerFactory.getLogger(this.toString());
|
||||
|
||||
private final HttpClient httpClient;
|
||||
private final JsonObject ugoiraMeta;
|
||||
@ -135,7 +134,7 @@ public final class PixivUgoiraBuilder {
|
||||
image.getRGB(0, 0, image.getWidth(), image.getHeight(), rgb, 0, image.getWidth());
|
||||
log.trace("帧解析完成, 正在插入...");
|
||||
encoder.addImage(Image.fromRgb(rgb, image.getWidth()), new ImageOptions().setDelay(frameInfo.get("delay").getAsLong(), TimeUnit.MILLISECONDS));
|
||||
log.debug("帧 {} 插入完成.", frameFileName);
|
||||
log.trace("帧 {} 插入完成.", frameFileName);
|
||||
} catch (IOException e) {
|
||||
log.error("解析帧图片数据时发生异常", e);
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ import java.util.List;
|
||||
*/
|
||||
public class PixivAccessProxyServer {
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger(this.getClass().getSimpleName());
|
||||
private final Logger log = LoggerFactory.getLogger(PixivAccessProxyServer.class.getName());
|
||||
|
||||
private final HttpProxyServer proxyServer;
|
||||
|
||||
@ -99,7 +99,7 @@ public class PixivAccessProxyServer {
|
||||
}*/
|
||||
log.info("Response Cookie: " + value);
|
||||
BasicClientCookie cookie = parseRawCookie(value);
|
||||
cookieStore.addCookie(null);
|
||||
cookieStore.addCookie(cookie);
|
||||
});
|
||||
httpResponse.headers().remove(HttpHeaderNames.SET_COOKIE);
|
||||
super.afterResponse(clientChannel, proxyChannel, httpResponse, pipeline);
|
||||
|
@ -1,31 +0,0 @@
|
||||
package net.lamgc.cgj.util;
|
||||
|
||||
import org.apache.http.client.CookieStore;
|
||||
import org.apache.http.impl.client.BasicCookieStore;
|
||||
import org.apache.http.impl.cookie.BasicClientCookie;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class CookieUtil {
|
||||
|
||||
/**
|
||||
* 将{@link java.net.CookieStore}转换到{@link CookieStore}
|
||||
* @param netCookieStore java.net.CookieStore
|
||||
* @return org.apache.http.client.CookieStore
|
||||
*/
|
||||
public static CookieStore NAParse(java.net.CookieStore netCookieStore){
|
||||
CookieStore apacheCookieStore = new BasicCookieStore();
|
||||
netCookieStore.getCookies().forEach(netCookie -> {
|
||||
BasicClientCookie aCookie = new BasicClientCookie(netCookie.getName(), netCookie.getValue());
|
||||
aCookie.setComment(netCookie.getComment());
|
||||
aCookie.setDomain(netCookie.getDomain());
|
||||
aCookie.setExpiryDate(new Date(netCookie.getMaxAge()));
|
||||
aCookie.setPath(netCookie.getPath());
|
||||
aCookie.setSecure(netCookie.getSecure());
|
||||
aCookie.setVersion(netCookie.getVersion());
|
||||
apacheCookieStore.addCookie(aCookie);
|
||||
});
|
||||
return apacheCookieStore;
|
||||
}
|
||||
|
||||
}
|
@ -1,13 +1,12 @@
|
||||
package net.lamgc.cgj.util;
|
||||
|
||||
import net.lamgc.cgj.pixiv.PixivDownload;
|
||||
import net.lamgc.cgj.pixiv.PixivURL;
|
||||
import net.lamgc.utils.base.runner.StringParameterParser;
|
||||
|
||||
public class PagesQualityParser implements StringParameterParser<PixivDownload.PageQuality> {
|
||||
|
||||
@Override
|
||||
public PixivDownload.PageQuality parse(String strValue) throws Exception {
|
||||
public PixivDownload.PageQuality parse(String strValue) {
|
||||
return PixivDownload.PageQuality.valueOf(strValue.toUpperCase());
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,151 @@
|
||||
package net.lamgc.cgj.util;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
* 带有时间限制的线程池.
|
||||
* 当线程超出了限制时间时, 将会对该线程发出中断.
|
||||
*/
|
||||
public class TimeLimitThreadPoolExecutor extends ThreadPoolExecutor {
|
||||
|
||||
/**
|
||||
* 执行时间限制, 单位毫秒.
|
||||
* 默认30s.
|
||||
*/
|
||||
private final AtomicLong executeTimeLimit = new AtomicLong();
|
||||
|
||||
/**
|
||||
* 检查间隔时间.
|
||||
* 默认100ms.
|
||||
*/
|
||||
private final AtomicLong timeoutCheckInterval = new AtomicLong(100);
|
||||
|
||||
private final Map<Thread, AtomicLong> workerThreadMap = new Hashtable<>();
|
||||
|
||||
private final Thread timeoutCheckThread = createTimeoutCheckThread();
|
||||
|
||||
public TimeLimitThreadPoolExecutor(long executeLimitTime, int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
|
||||
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
|
||||
setInitialTime(0, executeLimitTime);
|
||||
timeoutCheckThread.start();
|
||||
}
|
||||
|
||||
public TimeLimitThreadPoolExecutor(long executeLimitTime, int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
|
||||
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
|
||||
setInitialTime(0, executeLimitTime);
|
||||
timeoutCheckThread.start();
|
||||
}
|
||||
|
||||
public TimeLimitThreadPoolExecutor(long executeLimitTime, int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
|
||||
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
|
||||
setInitialTime(0, executeLimitTime);
|
||||
timeoutCheckThread.start();
|
||||
}
|
||||
|
||||
public TimeLimitThreadPoolExecutor(long executeLimitTime, int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
|
||||
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
|
||||
setInitialTime(0, executeLimitTime);
|
||||
timeoutCheckThread.start();
|
||||
}
|
||||
|
||||
private void setInitialTime(long checkInterval, long executeLimitTime) {
|
||||
if(checkInterval > 0) {
|
||||
timeoutCheckInterval.set(checkInterval);
|
||||
}
|
||||
if(executeLimitTime > 0) {
|
||||
executeTimeLimit.set(executeLimitTime);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置执行时间.
|
||||
* <p>注意: 该修改仅在线程池完全停止后才有效</p>
|
||||
* @see #isTerminated()
|
||||
* @param time 新的限制时间(ms)
|
||||
*/
|
||||
public void setExecuteTimeLimit(long time) {
|
||||
if(time <= 0) {
|
||||
throw new IllegalArgumentException("Time is not allowed to be set to 0 or less");
|
||||
}
|
||||
if(this.isTerminated()) {
|
||||
executeTimeLimit.set(time);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置超时检查间隔.
|
||||
* <p>该方法仅会在当前检查后生效.</p>
|
||||
* @param time 新的检查间隔(ms)
|
||||
*/
|
||||
public void setTimeoutCheckInterval(long time) {
|
||||
if(time <= 0) {
|
||||
throw new IllegalArgumentException("Time is not allowed to be set to 0 or less");
|
||||
}
|
||||
timeoutCheckInterval.set(time);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前设置的执行时间限制.
|
||||
* @return 执行时间限制(ms).
|
||||
*/
|
||||
public long getExecuteTimeLimit() {
|
||||
return executeTimeLimit.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前设定的超时检查间隔
|
||||
* @return 间隔时间(ms).
|
||||
*/
|
||||
public long getTimeoutCheckInterval() {
|
||||
return timeoutCheckInterval.get();
|
||||
}
|
||||
|
||||
private Thread createTimeoutCheckThread() {
|
||||
Thread checkThread = new Thread(() -> {
|
||||
if(executeTimeLimit.get() <= 0) {
|
||||
return;
|
||||
}
|
||||
while (true) {
|
||||
try {
|
||||
long interval = this.timeoutCheckInterval.get();
|
||||
Thread.sleep(interval);
|
||||
|
||||
// 检查是否存在超时的任务
|
||||
workerThreadMap.forEach((thread, time) -> {
|
||||
long currentTime = time.getAndAdd(interval);
|
||||
if(currentTime > executeTimeLimit.get()) {
|
||||
if(!thread.isInterrupted()) {
|
||||
thread.interrupt();
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch(InterruptedException ignored) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
checkThread.setName("ThreadPool-" + Integer.toHexString(this.hashCode()) +"-TimeoutCheck");
|
||||
return checkThread;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void beforeExecute(Thread t, Runnable r) {
|
||||
workerThreadMap.put(t, new AtomicLong());
|
||||
super.beforeExecute(t, r);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void afterExecute(Runnable r, Throwable t) {
|
||||
workerThreadMap.remove(Thread.currentThread());
|
||||
super.afterExecute(r, t);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void terminated() {
|
||||
this.timeoutCheckThread.interrupt();
|
||||
super.terminated();
|
||||
}
|
||||
}
|
40
src/main/resources/log4j2-test.xml
Normal file
40
src/main/resources/log4j2-test.xml
Normal file
@ -0,0 +1,40 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration status="WARN">
|
||||
<properties>
|
||||
<property name="logStorePath">./logs</property>
|
||||
<property name="charset">UTF-8</property>
|
||||
<property name="pattern">[%-d{HH:mm:ss.SSS} %5level][%logger.%method():%-3L][%thread]: %msg%n</property>
|
||||
<property name="logsDir">${sys:cgj.logsPath:-logs}</property>
|
||||
</properties>
|
||||
|
||||
<Appenders>
|
||||
<Console name="CONSOLE_STDOUT" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="${pattern}" charset="${charset}"/>
|
||||
<Filters>
|
||||
<ThresholdFilter level="WARN" onMatch="DENY" onMismatch="ACCEPT"/>
|
||||
</Filters>
|
||||
</Console>
|
||||
<Console name="CONSOLE_STDERR" target="SYSTEM_ERR">
|
||||
<PatternLayout pattern="${pattern}" charset="${charset}"/>
|
||||
<Filters>
|
||||
<ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
|
||||
</Filters>
|
||||
</Console>
|
||||
|
||||
<RollingFile name="rollingFile" fileName="${logsDir}/latest.log" filePattern="${logsDir}/running.%-d{yyyy-MM-dd_HH-mm-ss}.log.gz">
|
||||
<PatternLayout pattern="${pattern}" charset="${charset}"/>
|
||||
<Policies>
|
||||
<OnStartupTriggeringPolicy />
|
||||
</Policies>
|
||||
</RollingFile>
|
||||
</Appenders>
|
||||
|
||||
<Loggers>
|
||||
<Logger level="INFO" name="org.apache.http"/>
|
||||
<Root level="TRACE">
|
||||
<AppenderRef ref="CONSOLE_STDOUT"/>
|
||||
<AppenderRef ref="CONSOLE_STDERR"/>
|
||||
<AppenderRef ref="rollingFile"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</configuration>
|
@ -4,13 +4,14 @@
|
||||
<property name="logStorePath">./logs</property>
|
||||
<property name="charset">UTF-8</property>
|
||||
<property name="pattern">[%-d{HH:mm:ss.SSS} %5level][%logger.%method():%-3L][%thread]: %msg%n</property>
|
||||
<property name="logsDir">${sys:cgj.logsPath:-logs}</property>
|
||||
</properties>
|
||||
|
||||
<Appenders>
|
||||
<Console name="CONSOLE_STDOUT" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="${pattern}" charset="${charset}"/>
|
||||
<Filters>
|
||||
<ThresholdFilter level="WARN" onMatch="DENY" onMismatch="ACCEPT"/>
|
||||
<LevelRangeFilter minLevel="INFO" maxLevel="INFO" />
|
||||
</Filters>
|
||||
</Console>
|
||||
<Console name="CONSOLE_STDERR" target="SYSTEM_ERR">
|
||||
@ -20,7 +21,7 @@
|
||||
</Filters>
|
||||
</Console>
|
||||
|
||||
<RollingFile name="rollingFile" fileName="logs/latest.log" filePattern="logs/running.%-d{yyyy-MM-dd_HH-mm-ss}.log.gz">
|
||||
<RollingFile name="rollingFile" fileName="${logsDir}/latest.log" filePattern="${logsDir}/running.%-d{yyyy-MM-dd_HH-mm-ss}.log.gz">
|
||||
<PatternLayout pattern="${pattern}" charset="${charset}"/>
|
||||
<Policies>
|
||||
<OnStartupTriggeringPolicy />
|
||||
@ -30,7 +31,7 @@
|
||||
|
||||
<Loggers>
|
||||
<Logger level="INFO" name="org.apache.http"/>
|
||||
<Root level="DEBUG">
|
||||
<Root level="TRACE">
|
||||
<AppenderRef ref="CONSOLE_STDOUT"/>
|
||||
<AppenderRef ref="CONSOLE_STDERR"/>
|
||||
<AppenderRef ref="rollingFile"/>
|
||||
|
@ -0,0 +1,22 @@
|
||||
package net.lamgc.cgj.pixiv;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@Ignore
|
||||
public class ModerateContentDetectorTest {
|
||||
|
||||
private final static AdultContentDetector acd = new ModerateContentDetector("d91b6c3fa2bba9ee8f9e68827ba0d937");
|
||||
private final static Logger log = LoggerFactory.getLogger(ModerateContentDetector.class);
|
||||
|
||||
@Test
|
||||
public void checkTest() throws Exception {
|
||||
log.info("Detect: {}, isAdult: {}",
|
||||
acd.detect(80840411, false, 0),
|
||||
acd.isAdultContent(80840411, false, 0));
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,8 +1,15 @@
|
||||
package net.lamgc.cgj.pixiv;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.CookieStore;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Ignore;
|
||||
@ -24,7 +31,7 @@ public class PixivDownloadTest {
|
||||
|
||||
private static CookieStore cookieStore;
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger("PixivDownloadTest");
|
||||
private final static Logger log = LoggerFactory.getLogger(PixivDownloadTest.class.getName());
|
||||
|
||||
private static HttpHost proxy = new HttpHost("127.0.0.1", 1001);
|
||||
|
||||
@ -173,22 +180,8 @@ public class PixivDownloadTest {
|
||||
|
||||
log.info("正在调用方法...");
|
||||
try {
|
||||
pixivDownload.getRankingAsInputStream(null, null, queryDate, 5, 50, PixivDownload.PageQuality.ORIGINAL, (rank, link, rankInfo, inputStream) -> {
|
||||
/*try {
|
||||
|
||||
ZipEntry entry = new ZipEntry("Rank" + rank + "-" + link.substring(link.lastIndexOf("/") + 1));
|
||||
entry.setComment(rankInfo.toString());
|
||||
log.info("正在写入: " + entry.getName());
|
||||
zos.putNextEntry(entry);
|
||||
IOUtils.copy(inputStream, zos);
|
||||
zos.flush();
|
||||
log.info("已成功写入 {}", entry.getName());
|
||||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
log.error("写入文件项时发生异常", e);
|
||||
}*/
|
||||
log.info("空操作");
|
||||
});
|
||||
pixivDownload.getRankingAsInputStream(null, null, queryDate, 5, 50,
|
||||
PixivDownload.PageQuality.ORIGINAL, (rank, link, rankInfo, inputStream) -> log.info("空操作"));
|
||||
} finally {
|
||||
zos.finish();
|
||||
zos.flush();
|
||||
@ -202,4 +195,17 @@ public class PixivDownloadTest {
|
||||
log.info(new PixivDownload(cookieStore, proxy).getIllustPreLoadDataById(64076261).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void illustInfoTest() throws IOException {
|
||||
CloseableHttpClient httpClient = HttpClientBuilder.create().setProxy(new HttpHost("127.0.0.1", 1001)).build();
|
||||
HttpGet request = new HttpGet(PixivURL.getPixivIllustInfoAPI(80880547));
|
||||
CloseableHttpResponse response = httpClient.execute(request);
|
||||
String body = EntityUtils.toString(response.getEntity());
|
||||
JsonObject illustInfoResult = new Gson().fromJson(body, JsonObject.class);
|
||||
log.info("IllustInfoJsonResult: {}", illustInfoResult);
|
||||
PixivDownload.PixivIllustType illustType = PixivDownload.PixivIllustType.getIllustTypeByPreLoadData(80880547, illustInfoResult);
|
||||
log.info("IllustType: {}", illustType);
|
||||
Assert.assertEquals(PixivDownload.PixivIllustType.UGOIRA, illustType);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,30 @@
|
||||
package net.lamgc.cgj.util;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class TimeLimitThreadPoolExecutorTest {
|
||||
|
||||
@Test
|
||||
public void timeoutTest() throws InterruptedException {
|
||||
TimeLimitThreadPoolExecutor executor = new TimeLimitThreadPoolExecutor(1000, 1, 1, 30, TimeUnit.SECONDS, new ArrayBlockingQueue<>(50));
|
||||
System.out.println(executor.isTerminated());
|
||||
System.out.println(executor.isShutdown());
|
||||
|
||||
executor.setTimeoutCheckInterval(150);
|
||||
System.out.println("当前设定: ETL: " + executor.getExecuteTimeLimit() + "ms, TCI: " + executor.getTimeoutCheckInterval() + "ms");
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
Thread.sleep(5 * 1000);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("线程 " + Thread.currentThread().getName() + " 被中断");
|
||||
}
|
||||
});
|
||||
executor.shutdown();
|
||||
Assert.assertTrue(executor.awaitTermination(5 * 1000, TimeUnit.MILLISECONDS));
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user