forked from MisakaFxxk/MisakaF_Emby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91c6a4e
commit 133ad24
Showing
4 changed files
with
425 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
## 1、说在前面 | ||
|
||
本代码的部分功能依赖Emby的Webhook实现,而Webhook是需要激活服务端的,如何激活请自行寻找。 | ||
|
||
本代码的部分功能需要使用post传送数据,请尽量保证两台服务器的连接稳定。若无法保证,可以将3.3中的api搭建放在Emby服务端本地。 | ||
|
||
这个功能实现起来比较复杂,涉及到了好几个脚本,绝大部分人应该会搭建失败,请不要因此而发issue或私聊我。只要我公益服的通知还在正常运行,代码本身就是没问题的。 | ||
|
||
|
||
|
||
## 2、功能实现 | ||
|
||
#### 2.1、收藏通知 | ||
|
||
`Webhook`插件post发送用户收藏剧集的信息,服务器B搭建api接收信息并提醒。 | ||
|
||
#### 2.2、更新通知 | ||
|
||
`Scripter-X → Actions`插件在有新媒体入库时运行bash脚本,调用本地python脚本,发送更新通知。 | ||
|
||
|
||
|
||
## 3、环境搭建 | ||
|
||
### 3.1、数据库 | ||
|
||
本次代码最好配合我写的注册机器人使用。若你使用的是其他公益服机器人,数据库结构不同,不用担心,只需修改一个子函数即可完美适配你的数据库。 | ||
|
||
在同一个数据库下创建名为`favorite`的表,表内结构如下,按图创建即可: | ||
|
||
![](http://tva1.sinaimg.cn/large/007dA9Degy1h771lcytk8j30uv03zgnd.jpg) | ||
|
||
#### 3.1.1、适配他人机器人数据库 | ||
|
||
如果你使用的是我写的机器人,那就不需要看这条,只要保证user表和favorite表在同一个数据库下即可。 | ||
|
||
如果你使用的是其他机器人,请确保你的用户表中记录了TG用户ID和Emby用户ID这两个字段,他们大概长这样 | ||
|
||
![](http://tva1.sinaimg.cn/large/007dA9Degy1h771phzvp9j30da02ddgo.jpg) | ||
|
||
打开`/收藏通知/api_notify.py`,找到20行`idtochatid`这个子函数,修改22行`create_sqli = "SELECT 空1 FROM 空2 WHERE 空3 = "+'"'+ str(id)+'"'`,将空1替换为TG用户ID的字段名(chatid),空2替换为用户表名(user),空3替换为Emby用户ID的字段名(emby_userid)。 | ||
|
||
|
||
|
||
### 3.2、Emby | ||
|
||
#### 3.2.1、Docker安装的Emby | ||
|
||
注意,由于用官方镜像内缺失相关环境,请使用 | ||
|
||
[linuxserver/emby]: https://hub.docker.com/r/linuxserver/emby | ||
|
||
提供的镜像,也算半个官方性质,与官方同步更新,启动命令无需修改。 | ||
|
||
将**服务端**文件夹内的两个文件上传到服务器内,修改update.py内的数据库连接信息与BOT API。本文假设在/mnt/notify文件夹下,需要将此文件夹映射到容器内。在docker run命令中加入`--volume /mnt/notify:/mnt/notify`,启动容器。 | ||
|
||
容器启动后,输入`docker exec -it 容器ID bash`进入容器。 | ||
|
||
![](http://tva1.sinaimg.cn/large/007dA9Degy1h771ewl3q5j30ee01o0tl.jpg) | ||
|
||
显示这样即进入容器成功,接下来开始搭建环境: | ||
|
||
``` | ||
apt update | ||
apt install python3 | ||
apt install python3-pip | ||
pip3 install requests | ||
pip3 install python-telegram-bot==13.11 | ||
pip3 install pymysql | ||
``` | ||
|
||
没有任何报错即搭建完毕,进入Emby后台,在插件市场中安装`Scripter-X → Actions`插件,重启服务器,在后台左下角找到此插件的设置界面。 | ||
|
||
![](http://tva1.sinaimg.cn/large/007dA9Degy1h7724vgxhij31z40z3wny.jpg) | ||
|
||
在右侧找到**onMedialtemAdded**和**onMedialtemAddedComplete**选项,展开,点击加号添加一条命令,再点右侧笔图标,编辑此条命令。 | ||
|
||
![](http://tva1.sinaimg.cn/large/007dA9Degy1h77279cxvhj31nj0hek82.jpg) | ||
|
||
按上图进行设置,最后一行的触发条件是把上面这些属性拖下来,最后的**Episode**是拖个Text下来,双击编辑。 | ||
|
||
``` | ||
#供复制 | ||
/mnt/notify/notify.sh | ||
%series.id% %series.name% %season.number% %episode.number% | ||
``` | ||
|
||
|
||
|
||
#### 3.2.2 命令行安装的Emby | ||
|
||
将**服务端**文件夹内的两个文件上传到服务器内,本文假设在/mnt/notify文件夹下。修改update.py内的数据库连接信息与BOT API | ||
|
||
安装python环境 | ||
|
||
``` | ||
apt update | ||
apt install python3 | ||
apt install python3-pip | ||
pip3 install requests | ||
pip3 install python-telegram-bot==13.11 | ||
pip3 install pymysql | ||
``` | ||
|
||
没有任何报错即搭建完毕,进入Emby后台,在插件市场中安装`Scripter-X → Actions`插件,重启服务器,在后台左下角找到此插件的设置界面。 | ||
|
||
![](http://tva1.sinaimg.cn/large/007dA9Degy1h7724vgxhij31z40z3wny.jpg) | ||
|
||
在右侧找到**onMedialtemAdded**和**onMedialtemAddedComplete**选项,展开,点击加号添加一条命令,再点右侧笔图标,编辑此条命令。 | ||
|
||
![](http://tva1.sinaimg.cn/large/007dA9Degy1h77279cxvhj31nj0hek82.jpg) | ||
|
||
按上图进行设置,最后一行的触发条件是把上面这些属性拖下来,最后的**Episode**是拖个Text下来,双击编辑。 | ||
|
||
``` | ||
#供复制 | ||
/mnt/notify/notify.sh | ||
%series.id% %series.name% %season.number% %episode.number% | ||
``` | ||
|
||
|
||
|
||
### 3.3、 API搭建 | ||
|
||
找一个服务器B,开放12345端口,安装python等环境: | ||
|
||
``` | ||
apt update | ||
apt install python3 | ||
apt install python3-pip | ||
pip3 install requests | ||
pip3 install python-telegram-bot==13.11 | ||
pip3 install pymysql | ||
pip3 install flask | ||
``` | ||
|
||
将**收藏通知**文件夹内的文件上传到服务器内,本文假设路径为/root/api_notify.py,修改api_notify.py内的数据库连接信息与BOT API。 | ||
|
||
输入命令`python3 /root/api_notify.py`尝试启动,一切顺利应该没有什么报错。 | ||
|
||
进入Emby后台,安装Webhook插件(一般自带,服务端需要激活)。进入Webhook插件设置界面,点击**添加 Webhook**: | ||
|
||
URL一栏输入:`http://IP:12345/update`,勾选**User**,储存即可,测试会报错,正常。 | ||
|
||
![](http://tva1.sinaimg.cn/large/007dA9Degy1h772qezp4ej316u0s7n0c.jpg) | ||
|
||
现在你可以去收藏一个电视剧,看看机器人是否会提示你收藏成功。若一切顺利,Ctrl+C结束前台运行,输入命令`nohup python3 /root/api_notify.py > /root/api_notify.log 2>&1 &`后台运行。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
import flask,json | ||
from flask import request | ||
from urllib.parse import unquote | ||
import json | ||
import telegram | ||
import pymysql | ||
import requests | ||
|
||
api = flask.Flask(__name__) | ||
bot = telegram.Bot(token='****') | ||
embyurl = '****' | ||
embyapikey = '****' | ||
#连接数据库 | ||
connect = pymysql.connect(host='****', | ||
user='****', | ||
password='****', | ||
db='****', | ||
charset='utf8') #服务器名,账户,密码,数据库名称 | ||
db = connect.cursor() | ||
|
||
def idtochatid(id): | ||
try: | ||
create_sqli = "SELECT chatid FROM user WHERE emby_userid = "+'"'+ str(id)+'"' | ||
db.execute(create_sqli) | ||
result = db.fetchall() | ||
except Exception as e: | ||
return(0) | ||
else: | ||
print(id) | ||
if(len(result) == 0): | ||
return(0) | ||
else: | ||
userid=result[0][0] | ||
return(userid) | ||
|
||
def check_chatid_in_like(chatid): | ||
try: | ||
create_sqli = "SELECT * FROM favorite WHERE chatid = "+'"'+ str(chatid)+'"' | ||
db.execute(create_sqli) | ||
result = db.fetchall() | ||
except Exception as e: | ||
print(e) | ||
else: | ||
if(result): | ||
return 1 #存在 | ||
else: | ||
return 0 #不存在 | ||
|
||
def favorite_nums(chatid): | ||
try: | ||
create_sqli = "SELECT * FROM favorite WHERE chatid = "+'"'+ str(chatid)+'"' | ||
db.execute(create_sqli) | ||
result = db.fetchall() | ||
except Exception as e: | ||
print(e) | ||
else: | ||
text = result[0][2] | ||
texts = str(text).split(',') | ||
return len(texts) | ||
|
||
def check_allready_like(chatid,itemid): | ||
try: | ||
create_sqli = "SELECT * FROM favorite WHERE chatid = "+'"'+ str(chatid)+'"' | ||
db.execute(create_sqli) | ||
result = db.fetchall() | ||
except Exception as e: | ||
print(e) | ||
else: | ||
check = 0 | ||
text = result[0][2] | ||
texts = str(text).split(',') | ||
length = len(texts) | ||
for i in range(length): | ||
if(texts[i] == itemid): | ||
check = 1 | ||
return check | ||
|
||
def cancel_favourite(chatid,itemid): | ||
try: | ||
create_sqli = "SELECT * FROM favorite WHERE chatid = "+'"'+ str(chatid)+'"' | ||
db.execute(create_sqli) | ||
result = db.fetchall() | ||
except Exception as e: | ||
print(e) | ||
else: | ||
text = result[0][2] | ||
texts = str(text).split(',') | ||
texts.remove(itemid) | ||
mes = ','.join(str(i) for i in texts) | ||
|
||
try: | ||
sql = "UPDATE favorite set fav="+'"'+str(mes)+'"'+" where chatid="+str(chatid) | ||
print(sql) | ||
db.execute(sql) | ||
connect.commit() | ||
except Exception as e: | ||
print(e) | ||
else: | ||
return 1 | ||
|
||
def add_favourite(chatid,itemid): | ||
try: | ||
create_sqli = "SELECT * FROM favorite WHERE chatid = "+'"'+ str(chatid)+'"' | ||
db.execute(create_sqli) | ||
result = db.fetchall() | ||
except Exception as e: | ||
print(e) | ||
else: | ||
text = result[0][2] | ||
texts = str(text).split(',') | ||
texts.append(itemid) | ||
mes = ','.join(str(i) for i in texts) | ||
|
||
try: | ||
sql = "UPDATE favorite set fav="+'"'+str(mes)+'"'+" where chatid="+str(chatid) | ||
print(sql) | ||
db.execute(sql) | ||
connect.commit() | ||
except Exception as e: | ||
print(e) | ||
else: | ||
return 1 | ||
|
||
def server_add(embyid,itemid,chatid): | ||
headers = { | ||
'accept': 'application/json', | ||
} | ||
|
||
params = { | ||
'api_key': embyapikey, | ||
} | ||
|
||
response = requests.post(embyurl + 'emby/Users/'+embyid+'/FavoriteItems/'+itemid, params=params, headers=headers) | ||
|
||
try: | ||
sql = "UPDATE favorite set locked=1 where chatid="+str(chatid) | ||
print(sql) | ||
db.execute(sql) | ||
connect.commit() | ||
except Exception as e: | ||
print(e) | ||
else: | ||
return 1 | ||
|
||
def server_cancel(embyid,itemid,chatid): | ||
headers = { | ||
'accept': 'application/json', | ||
} | ||
|
||
params = { | ||
'api_key': embyapikey, | ||
} | ||
|
||
response = requests.delete(embyurl + 'emby/Users/'+embyid+'/FavoriteItems/'+itemid, params=params, headers=headers) | ||
|
||
try: | ||
sql = "UPDATE favorite set locked=1 where chatid="+str(chatid) | ||
print(sql) | ||
db.execute(sql) | ||
connect.commit() | ||
except Exception as e: | ||
print(e) | ||
else: | ||
return 1 | ||
|
||
|
||
@api.route('/update',methods=['post']) | ||
def update(): | ||
data = request.get_data() | ||
data_decode = unquote(str(data,encoding='utf-8')) | ||
split = data_decode.split('\n') | ||
datajson = json.loads(split[4]) | ||
emby_id = datajson["User"]["Id"] | ||
item_id = datajson["Item"]["Id"] | ||
item_name = datajson["Item"]["Name"] | ||
item_type = datajson["Item"]["Type"] | ||
print(emby_id,item_id,item_name,item_type) | ||
chatid = idtochatid(emby_id) | ||
check_exist = check_chatid_in_like(chatid) | ||
if(item_type == 'Series'): | ||
if(check_exist == 0): | ||
try: | ||
sql = "insert into favorite (chatid,emby_userid,fav,locked) values ("+str(chatid)+","+'"'+str(emby_id)+'"'+","+'"'+str(item_id)+'"'+",0)" | ||
print(sql) | ||
db.execute(sql) | ||
connect.commit() | ||
except Exception as e: | ||
print(e) | ||
else: | ||
server_add(emby_id,item_id,chatid) | ||
message = "[Emby公益服]"+item_name+":收藏成功" | ||
bot.send_message(chat_id=chatid, text=message) | ||
elif(check_exist == 1): | ||
check_like = check_allready_like(chatid,item_id) | ||
try: | ||
create_sqli = "SELECT * FROM favorite WHERE chatid = "+'"'+ str(chatid)+'"' | ||
db.execute(create_sqli) | ||
result = db.fetchall() | ||
except Exception as e: | ||
print(e) | ||
else: | ||
lock = result[0][3] | ||
if(lock == '0'): | ||
if(check_like == 0): | ||
back = add_favourite(chatid,item_id) | ||
server_add(emby_id,item_id,chatid) | ||
if(back == 1): | ||
message = "[Emby公益服]"+item_name+":收藏成功" | ||
bot.send_message(chat_id=chatid, text=message) | ||
elif(check_like == 1): | ||
back = cancel_favourite(chatid,item_id) | ||
server_cancel(emby_id,item_id,chatid) | ||
if(back == 1): | ||
message = "[Emby公益服]"+item_name+":取消收藏成功" | ||
bot.send_message(chat_id=chatid, text=message) | ||
else: | ||
try: | ||
sql = "UPDATE favorite set locked=0 where chatid="+str(chatid) | ||
print(sql) | ||
db.execute(sql) | ||
connect.commit() | ||
except Exception as e: | ||
print(e) | ||
else: | ||
print("skip!") | ||
|
||
return('200') | ||
|
||
if __name__ == '__main__': | ||
api.run(port=12345,debug=True,host='0.0.0.0') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/sh | ||
BASE_ROOT=$(cd "$(dirname "$0")";pwd) | ||
python3 /$BASE_ROOT/update.py $1 $2 $3 $4 |
Oops, something went wrong.