Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
wushuo894 committed Jun 15, 2024
2 parents 7c9d569 + f7b20a4 commit 08a491e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Dockerfile.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ COPY target/EmbyPinyin-jar-with-dependencies.jar /usr/app/EmbyPinyin-jar-with-de
WORKDIR /usr/app
ENV PORT="9198"
ENV HOST="http://192.168.5.4:8096"
ENV KEY="c30e784137134792b2907b78f5c23b60"
ENV KEY=""
ENV ITEM="电影,番剧"
ENV CRON="0 1 * * *"
ENV RUN="TRUE"
Expand Down
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
### EmbyPinyin
# EmbyPinyin

Emby 电影、电视剧支持排序
Emby 支持拼音首字母排序

### Docker部署

`docker run -d \
--name emby-pinyin \
-p 9198:9198 \
-e PORT="9198" \
-e HOST="http://192.168.5.4:8096" \
-e KEY="" \
-e ITEM="电影,番剧" \
-e CRON="0 1 * * *" \
-e RUN="TRUE" \
-e TZ=Asia/Shanghai \
--restart always \
wushuo894/emby-pinyin`

| 参数 | 作用 | 默认值 |
|------|-------------|-----------------------|
| PORT | 端口号 | 9877 |
| HOST | emby 地址 | http://127.0.0.1:3000 |
| KEY | API Key ||
| ITEM | 媒体库(可以用,分割) | 电影,番剧 |
| RUN | 启动时运行 | TRUE |
| CRON | 计划任务 | 0 1 * * * |
| TZ | 时区 | Asia/Shanghai |
60 changes: 41 additions & 19 deletions src/main/java/com/emby/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import cn.hutool.cron.CronUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.log.Log;
import com.google.gson.*;
Expand Down Expand Up @@ -96,24 +97,9 @@ public static void main(String[] args) {

@Override
public synchronized void run() {
JsonObject adminUser = HttpRequest.get(host + "/Users?api_key=" + key)
.thenFunction(res -> {
JsonArray jsonElements = gson.fromJson(res.body(), JsonArray.class);
for (JsonElement jsonElement : jsonElements) {
JsonObject user = jsonElement.getAsJsonObject();
JsonObject policy = user.get("Policy").getAsJsonObject();
boolean isAdministrator = policy.get("IsAdministrator").getAsBoolean();
if (!isAdministrator) {
continue;
}
return user;
}
return null;
});
Assert.notNull(adminUser, "未找到管理员账户,请检查你的API KEY参数");

adminUserId = adminUser.get("Id").getAsString();
getAdmin();

// 获取媒体库列表
JsonArray items = HttpRequest.get(host + "/Users/" + adminUserId + "/Views?api_key=" + key)
.thenFunction(res -> {
JsonObject body = gson.fromJson(res.body(), JsonObject.class);
Expand All @@ -134,10 +120,12 @@ public synchronized void run() {
items.add(item);
}

// 遍历媒体库
for (JsonElement item : items) {
JsonObject itemAsJsonObject = item.getAsJsonObject();
String itemId = itemAsJsonObject.get("Id").getAsString();
String name = itemAsJsonObject.get("Name").getAsString();
// 匹配媒体库名称 - 则全匹配
if (StrUtil.isNotBlank(itemStr) && !itemStr.equals("-")) {
if (!Arrays.asList(itemStr.split(",")).contains(name)) {
continue;
Expand All @@ -149,6 +137,34 @@ public synchronized void run() {
}
}

/**
* 获取管理员账户
*/
private static void getAdmin() {
JsonObject adminUser = HttpRequest.get(host + "/Users?api_key=" + key)
.thenFunction(res -> {
JsonArray jsonElements = gson.fromJson(res.body(), JsonArray.class);
for (JsonElement jsonElement : jsonElements) {
JsonObject user = jsonElement.getAsJsonObject();
JsonObject policy = user.get("Policy").getAsJsonObject();
boolean isAdministrator = policy.get("IsAdministrator").getAsBoolean();
if (!isAdministrator) {
continue;
}
return user;
}
return null;
});
Assert.notNull(adminUser, "未找到管理员账户,请检查你的API KEY参数");

adminUserId = adminUser.get("Id").getAsString();
}

/**
* 拼音排序
*
* @param itemAsJsonObject
*/
public static void pinyin(JsonObject itemAsJsonObject) {
String id = itemAsJsonObject.get("Id").getAsString();
JsonObject jsonObject = HttpRequest.get(host + "/Users/" + adminUserId + "/Items/" + id + "?api_key=" + key)
Expand All @@ -164,9 +180,15 @@ public static void pinyin(JsonObject itemAsJsonObject) {
});
HttpRequest.post(host + "/Items/" + id + "?api_key=" + key)
.body(gson.toJson(jsonObject))
.execute();
.thenFunction(HttpResponse::isOk);
}

/**
* 递归获取所有视频
*
* @param ItemId
* @return
*/
public static JsonArray getItems(String ItemId) {
return HttpRequest.get(host + "/Users/" + adminUserId + "/Items?api_key=" + key)
.form("ParentId", ItemId)
Expand Down Expand Up @@ -199,4 +221,4 @@ public static JsonArray getItems(String ItemId) {
});
}

}
}

0 comments on commit 08a491e

Please sign in to comment.