Skip to content

Commit

Permalink
fix #55 and refractor
Browse files Browse the repository at this point in the history
  • Loading branch information
khjxiaogu committed Nov 21, 2022
1 parent 61eef74 commit 6a1a41e
Show file tree
Hide file tree
Showing 20 changed files with 708 additions and 433 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
- XML卡片在群聊中会变成链接,经查是被阻挡了。
- Share卡片在群聊中无法发送,同上。
- LightApp已经完全改成Mirai,旧版本LightApp卡片无法继续发送。
- 百度音乐(千千)API暂时失效,请等待修复。
# 声明
- 本插件仅作为学习交流等使用,请勿用于盈利,否则法律后果自负。
- 本插件提供的所有API均来源于公开资料,如有侵权请发邮件或者issue联系进行删除。
Expand Down Expand Up @@ -129,7 +130,8 @@ Linux:
|`hintsourcenotfound`|找不到源文本提示|
|`enable_local`|全部搜索时是否包含本地搜索,默认不包含。|
|`verbose`|是否启用命令执行输出,缺省默认为true。<br>设置值:true/执行语音操作时在控制台输出详细信息 false/不输出信息,直接执行|
|`adddefault`|是否添加默认指令,缺省默认为true。<br>设置值:true/添加readme所述的指令列表 false/不添加任何指令|
|`adddefault`|是否添加默认指令,缺省默认为true。<br>设置值:true/添加readme所述的指令列表并添加默认解析 false/不添加任何指令和解析|
|`enableParser`|是否启用链接解析,缺省默认为true。启用机器人会对每一条信息进行正则匹配并在找到音乐分享链接时发送对应卡片,因此对机器人可能会有一定的性能负担。true/启用,false/关闭。详见[后文](#自定义解析)|
|`extracommands`|通过配置添加新指令的列表,可以完全自定义指令。详见[后文](#自定义指令)|
# 自定义指令
范例:
Expand Down Expand Up @@ -175,6 +177,25 @@ extracommands:
source: 网易电台节目
card: Mirai
```
# 自定义解析
范例:
```
extraparsers:
"0": #解析名称,只要各个解析的名称不重复即可
pattern: "music\\.163\\.com/.*song/([0-9]+)"
source: 网易 #搜索来源
card: AMR #分享外观
"1":
...
```
|参数|值范围|用途|
|------|------|------|
|pattern|正则表达式|在搜索消息中的歌曲时,会使用该正则表达式进行匹配,如果匹配成功,则会把group(1)的结果作为歌曲ID进行搜索,并发送卡片|
|source|QQ音乐<br>网易<br>网易HQ<br>酷狗<br>千千<br>喜马拉雅<br>本地<br>Bilibili|设定搜索歌曲的来源<br>注意:部分平台不支持。|
|card|LightApp:小程序分享<br>Mirai:采用Mirai的MusicShare卡片,如果不存在则fallback为XML卡片<br>XML:卡片分享<br>Share:普通分享(不能播放)<br>Message:以纯信息形式分享,可以很方便取得音乐的各种链接。<br>AMR:AMR语音,需要配置好`ffmpeg_path`<br>Silk:SILK语音,需要同时配置好`silkenc_path``ffmpeg_path`,由于tx限流,质量可能很差(不推荐使用)|设定分享出来的音乐的外观|

如果不需要原版的解析,可以设置配置项`adddefault`为false。

# 权限系统
权限系统可以用于配置各个机器人、群和好友的权限。
权限文件位于`data/com.khjxiaogu.mirai.MiraiSongPlugin`目录下。
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.khjxiaogu</groupId>
<artifactId>MiraiSongPlugin</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<name>MiraiSongPlugin</name>
<description>使用“#音乐 关键词”、“#QQ 关键词”、“#网易 关键词”等方式进行点歌的插件。</description>
<build>
Expand Down
154 changes: 154 additions & 0 deletions src/main/java/com/khjxiaogu/MiraiSongPlugin/HttpRequestBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package com.khjxiaogu.MiraiSongPlugin;

import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class HttpRequestBuilder {
private StringBuilder url;

List<String[]> headers=new ArrayList<>();

public HttpRequestBuilder(String ourl) {
url=new StringBuilder(ourl);
}
public static HttpRequestBuilder create(String host) {
return new HttpRequestBuilder("https://"+host).host(host);
}
public static HttpRequestBuilder create(String protocol,String host) {
return new HttpRequestBuilder(protocol+"://"+host).host(host);
}
public HttpRequestBuilder url(String v) {
url.append(v);
return this;
}
public HttpRequestBuilder url(int v) {
url.append(v);
return this;
}
public HttpRequestBuilder url(char v) {
url.append(v);
return this;
}
public HttpRequestBuilder header(String k,String v) {
headers.add(new String[] {k,v});
return this;
}
public HttpRequestBuilder referer(String v) {
headers.add(new String[] {"referer",v});
return this;
}
public HttpRequestBuilder contenttype(String v) {
headers.add(new String[] {"content-type",v});
return this;
}
public HttpRequestBuilder cookie(String v) {
headers.add(new String[] {"cookie",v});
return this;
}
public HttpRequestBuilder host(String v) {
headers.add(new String[] {"Host",v});
return this;
}
public HttpRequestBuilder defUA() {
headers.add(new String[] {"User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"});
return this;
}
public HttpRequestBuilder ua(String v) {
headers.add(new String[] {"User-Agent",v});
return this;
}
public static class OpenedConnectionBuilder {
HttpURLConnection huc;
public OutputStream os;
public OpenedConnectionBuilder(HttpURLConnection huc,boolean doOut) throws IOException {
super();
this.huc = huc;
if(doOut)
this.os = huc.getOutputStream();
}
public OpenedConnectionBuilder(HttpURLConnection huc) {
super();
this.huc = huc;
}
public OpenedConnectionBuilder send(String s) throws IOException {
if(os==null)
os=huc.getOutputStream();
os.write(s.getBytes(StandardCharsets.UTF_8));
return this;
}
public OpenedConnectionBuilder send(byte[] s) throws IOException {
if(os==null)
os=huc.getOutputStream();
os.write(s);
return this;
}
public String readString() throws IOException {
return new String(Utils.readAll(huc.getInputStream()), StandardCharsets.UTF_8);
}
public JsonObject readJson() throws IOException {
return JsonParser.parseString(new String(Utils.readAll(huc.getInputStream()), StandardCharsets.UTF_8)).getAsJsonObject();
}
}
private HttpURLConnection openConn() throws IOException {
URL url = new URL(this.url.toString());
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
huc.setInstanceFollowRedirects(true);
huc.setConnectTimeout(5000);
huc.setReadTimeout(5000);
for(String[] header:headers) {
huc.setRequestProperty(header[0], header[1]);
}

return huc;
}
public OpenedConnectionBuilder post(boolean doOutput) throws IOException {
HttpURLConnection huc=openConn();
huc.setRequestMethod("POST");
huc.setDoOutput(doOutput);
huc.setDoInput(true);
huc.connect();
return new OpenedConnectionBuilder(huc,doOutput);

}
public OpenedConnectionBuilder post() throws IOException {
HttpURLConnection huc=openConn();
huc.setRequestMethod("POST");
huc.setDoOutput(true);
huc.setDoInput(true);
huc.connect();
return new OpenedConnectionBuilder(huc,true);
}
public OpenedConnectionBuilder get(boolean doOutput) throws IOException {
HttpURLConnection huc=openConn();
huc.setRequestMethod("GET");
huc.setDoOutput(doOutput);
huc.setDoInput(true);
huc.connect();
return new OpenedConnectionBuilder(huc,doOutput);
}
public OpenedConnectionBuilder get() throws IOException {
HttpURLConnection huc=openConn();
huc.setRequestMethod("GET");
huc.connect();
return new OpenedConnectionBuilder(huc,false);
}
public OpenedConnectionBuilder open(String met,boolean doInput,boolean doOutput) throws IOException {
HttpURLConnection huc=openConn();
huc.setRequestMethod(met);
huc.setDoOutput(doOutput);
huc.setDoInput(doInput);
huc.connect();
return new OpenedConnectionBuilder(huc,doOutput);
}


}
114 changes: 114 additions & 0 deletions src/main/java/com/khjxiaogu/MiraiSongPlugin/JsonBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.khjxiaogu.MiraiSongPlugin;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class JsonBuilder {

public JsonBuilder() {
}
public static JsonArrayBuilder<JsonArray> array(){
return new JsonArrayBuilder<>(null);
}
public static JsonObjectBuilder<JsonObject> object(){
return new JsonObjectBuilder<>(null);
}
public static class JsonArrayBuilder<T> {
JsonArray jo=new JsonArray();
T parent;
private JsonArrayBuilder(T par) {
parent=par;
}
public T end() {
if(parent==null)
return (T) jo;
return parent;
}
public JsonArrayBuilder<T> add(String v){
jo.add(v);
return this;
}
public JsonArrayBuilder<T> add(Number v){
jo.add(v);
return this;
}
public JsonArrayBuilder<T> add(boolean v){
jo.add(v);
return this;
}
public JsonArrayBuilder<T> add(Character v){
jo.add(v);
return this;
}
public JsonArrayBuilder<T> add(JsonElement v){
jo.add(v);
return this;
}
public JsonObjectBuilder<JsonArrayBuilder<T>> object(){
JsonObjectBuilder<JsonArrayBuilder<T>> job= new JsonObjectBuilder<>(this);
this.add(job.get());
return job;
}
public JsonArrayBuilder<JsonArrayBuilder<T>> array(){
JsonArrayBuilder<JsonArrayBuilder<T>> job= new JsonArrayBuilder<>(this);
this.add(job.get());
return job;
}
public JsonArray get() {
return jo;
};
public String toString() {
return jo.toString();
}
}
public static class JsonObjectBuilder<T> {
JsonObject jo=new JsonObject();
T parent;
private JsonObjectBuilder(T par) {
parent=par;
}

public T end() {
if(parent==null)
return (T) jo;
return parent;
}
public JsonObjectBuilder<T> add(String k,String v){
jo.addProperty(k,v);
return this;
}
public JsonObjectBuilder<T> add(String k,Number v){
jo.addProperty(k,v);
return this;
}
public JsonObjectBuilder<T> add(String k,boolean v){
jo.addProperty(k,v);
return this;
}
public JsonObjectBuilder<T> add(String k,Character v){
jo.addProperty(k, v);
return this;
}
public JsonObjectBuilder<T> add(String k,JsonElement v){
jo.add(k, v);
return this;
}
public JsonObjectBuilder<JsonObjectBuilder<T>> object(String k){
JsonObjectBuilder<JsonObjectBuilder<T>> job= new JsonObjectBuilder<JsonObjectBuilder<T>>(this);
this.add(k, job.get());
return job;
}
public JsonArrayBuilder<JsonObjectBuilder<T>> array(String k){
JsonArrayBuilder<JsonObjectBuilder<T>> job= new JsonArrayBuilder<>(this);
this.add(k,job.get());
return job;
}
public JsonObject get() {
return jo;
};
public String toString() {
return jo.toString();
}
}
}
Loading

0 comments on commit 6a1a41e

Please sign in to comment.