-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
221 lines (192 loc) · 6.23 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from flask import Flask, request, abort
import requests
from os import environ
import yaml
app = Flask(__name__)
def get_token():
with open("/config/config.yaml") as data:
info_data = yaml.safe_load(data)
token = info_data["token"]
token = token[0]
return token
def get_emby_url():
with open("/config/config.yaml") as data:
info_data = yaml.safe_load(data)
server = info_data["emby-server"]
server = server[0]
return server
t_token = get_token()
url_send_photo = f"https://api.telegram.org/bot{t_token}/sendPhoto"
url_send_message = f"https://api.telegram.org/bot{t_token}/sendMessage"
e_server = get_emby_url()
def get_admins():
with open("/config/config.yaml") as data:
info_data = yaml.safe_load(data)
try:
admins = info_data["admins"]
return admins
except KeyError:
return None
def get_users():
with open("/config/config.yaml") as data:
info_data = yaml.safe_load(data)
try:
users = info_data["users"]
return users
except KeyError:
return None
def convert_list(string):
li = list(string.split(" "))
return li
def get_icon(argument):
switcher = {
"playback.start": "▶ ",
"playback.stop": "⏹ ",
"playback.pause": "⏸ ",
"playback.unpause": "⏯ ",
"library.deleted": "🗑 ",
"item.markunplayed": "❎",
"item.markplayed": "✅",
"system.updateavailable": "💾",
"user.authenticationfailed": "🔒",
"user.authenticated": "🔐",
"system.serverrestartrequired": "🔄",
"plugins.pluginuninstalled": "📤",
"plugins.plugininstalled": "📥",
}
return switcher.get(argument, "")
def update():
global response
item = response["Server"]
server_version = item["Version"]
item = response["PackageVersionInfo"]
new_version = item["versionStr"]
info = item["infoUrl"]
desc = item["description"]
get_event_marked = response["Event"]
icon = get_icon(get_event_marked)
text = f"{icon} Update from version {server_version} to {new_version} available \nDescription: {desc} \nMore info: {info}"
url = f"https://api.telegram.org/bot{t_token}/sendMessage?chat_id={send_id}&text={text}"
requests.post(url)
def marked():
global response, text
get_event_marked = response["Event"]
response = response["Item"]
icon = get_icon(get_event_marked)
if "item.markplayed" in get_event_marked:
if response["Type"] == "Movie":
movie_name = response["Name"]
text = f"{icon} Marked-played: {movie_name}"
elif response["Type"] == "Episode":
series_name = response["SeriesName"]
season = response["SeasonName"]
episode_name = response["Name"]
episode_nun = response["IndexNumber"]
text = f"{icon} Marked-played: {series_name} {season} episode {episode_nun} - {episode_name}"
url = f"https://api.telegram.org/bot{t_token}/sendMessage?chat_id={send_id}&text={text}"
requests.post(url)
elif "item.markunplayed" in get_event_marked:
if response["Type"] == "Movie":
movie_name = response["Name"]
text = f"{icon} Marked-unplayed: {movie_name}"
elif response["Type"] == "Episode":
series_name = response["SeriesName"]
season = response["SeasonName"]
episode_name = response["Name"]
episode_nun = response["IndexNumber"]
text = f"{icon} Marked-unplayed: {series_name} {season} episode {episode_nun} - {episode_name}"
url = f"https://api.telegram.org/bot{t_token}/sendMessage?chat_id={send_id}&text={text}"
requests.post(url)
def send_message():
get_event = response["Event"]
text_new = response["Title"]
icon = get_icon(get_event)
data = {
"chat_id": send_id,
"caption": text_new,
"parse_mode": "Markdown",
"text": icon + text_new,
}
requests.post(url_send_message, data=data)
def lib_new():
text_new = response["Title"]
try:
desc = response["Description"]
except KeyError:
desc = (
"**Can't get a description from the server, edit its identity manually**"
)
item = response["Item"]
photo_id = item["Id"]
base_photo_url = (
f"{e_server}/emby/Items/{photo_id}/Images/Primary" if photo_id else None
)
image_response = requests.get(base_photo_url)
image = ("photo.jpg", image_response.content, "image/jpeg")
data_new = {
"chat_id": send_id,
"caption": text_new + "\n\nDescription: " + desc,
"parse_mode": "Markdown",
}
requests.post(url_send_photo, data=data_new, files={"photo": image})
def switch_case(argument):
switcher = {
"playback.start": send_message,
"playback.stop": send_message,
"playback.pause": send_message,
"playback.unpause": send_message,
"library.new": lib_new,
"library.deleted": send_message,
"item.markunplayed": marked,
"item.markplayed": marked,
"system.updateavailable": update,
"user.authenticationfailed": send_message,
"user.authenticated": send_message,
"system.serverrestartrequired": send_message,
"plugins.pluginuninstalled": send_message,
"plugins.plugininstalled": send_message,
}
return switcher.get(argument, "")
@app.route("/webhook", methods=["POST"])
def webhook():
global list_id, response, send_id, token
if request.method == "POST":
response = request.json
get_event_now = response["Event"]
admin_id = get_admins()
if admin_id is not None:
for i in range(len(admin_id)):
send_id = admin_id[i]
print(get_event_now)
arg_check = (
"playback.start",
"playback.stop",
"playback.pause",
"playback.unpause",
"library.new",
"library.deleted",
"item.markunplayed",
"item.markplayed",
"system.updateavailable",
"user.authenticationfailed",
"user.authenticated",
"system.serverrestartrequired",
"plugins.pluginuninstalled",
"plugins.plugininstalled",
)
if get_event_now.startswith(arg_check):
switch_case(get_event_now)()
else:
send_message()
user_id = get_users()
if user_id is not None:
for i in range(len(user_id)):
send_id = user_id[i]
arg_check = ("library.new", "library.deleted")
if get_event_now.startswith(arg_check):
switch_case(get_event_now)()
return "success", 200
else:
abort(400)
if __name__ == "__main__":
app.run()