-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
PlexRecs.py
executable file
·308 lines (275 loc) · 14.8 KB
/
PlexRecs.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import asyncio
import inspect
import sys
from typing import Union
import discord
from discord.ext import commands, tasks
import modules.analytics as ga
import modules.imdb_connector as imdb
import modules.picker as picker
import modules.plex_connector as plex_connector
import modules.trakt_connector as trakt_connector
from modules import discord_utils, config_parser
from modules.library_database import PlexContentDatabase, Content
from modules.logs import *
config = config_parser.Config(app_name="PlexRecs", config_path="config.yaml")
analytics = ga.GoogleAnalytics(analytics_id='UA-174268200-1', anonymous_ip=True,
do_not_track=not config.extras.allow_analytics)
plex = plex_connector.PlexConnector(url=config.plex.url, token=config.plex.token,
server_name=config.plex.server_name,
library_list=config.plex.libraries, tautulli_url=config.tautulli.url,
tautulli_key=config.tautulli.api_key, analytics=analytics,
database=PlexContentDatabase("content.db"))
trakt = trakt_connector.TraktConnector(username=config.trakt.username,
client_id=config.trakt.client_id,
client_secret=config.trakt.client_secret, analytics=analytics)
trakt.store_public_lists(lists_dict=config.trakt.lists)
emoji_numbers = [u"1\u20e3", u"2\u20e3", u"3\u20e3", u"4\u20e3", u"5\u20e3"]
def error_and_analytics(error_message, function_name):
error(error_message)
analytics.event(event_category="Error", event_action=function_name, random_uuid_if_needed=True)
def find_rec(media_type: str, unwatched: bool = False, username: str = None, rating: float = None,
above: bool = True, trakt_list_name: str = None, attempts: int = 10):
"""
:param attempts:
:param trakt_list_name:
:param above:
:param rating:
:param username:
:param unwatched:
:param media_type: 'movie', 'show' or 'artist'
:return:
"""
try:
if unwatched:
return picker.pick_unwatched(plex_connector=plex, username=username, media_type=media_type,
attempts=attempts)
elif rating:
return picker.pick_with_rating(plex_connector=plex, media_type=media_type, rating=rating, above=above,
attempts=attempts)
elif trakt_list_name:
return picker.pick_from_trakt_list(trakt_connector=trakt, trakt_list_name=trakt_list_name,
plex_connector=plex, attempts=attempts)
else:
return picker.pick_random(plex_connector=plex, media_type=media_type)
except Exception as e:
error_and_analytics(error_message=f"Error in findRec: {e}", function_name=inspect.currentframe().f_code.co_name)
return False
def make_recommendation(media_type: str, unwatched: bool = False, plex_username: str = None, rating: float = None,
above: bool = True, trakt_list_name: str = None):
if unwatched:
if not plex_username:
return "Please include a Plex username"
recommendation = find_rec(media_type=media_type, unwatched=True, username=plex_username)
if not recommendation:
return "I couldn't find that Plex username", None, None
if recommendation == "Too many attempts":
return "Sorry, it took too long to find something for you", None, None
elif rating:
recommendation = find_rec(media_type=media_type, rating=rating, above=above)
if recommendation == "Too many attempts":
return "Sorry, it took too long to find something for you", None, None
elif trakt_list_name:
recommendation = find_rec(media_type=media_type, trakt_list_name=trakt_list_name)
if recommendation == "Too many attempts":
return "Sorry, it took too long to find something for you", None, None
else:
recommendation = find_rec(media_type=media_type, unwatched=False)
embed = discord_utils.make_embed(plex=plex, media_item=recommendation, analytics=analytics)
return f"How about {recommendation.Title}?", embed, recommendation
class PlexRecs(commands.Cog):
async def user_response(self, ctx, media_type: str, media_item: Content):
if str(ctx.message.author.id) == str(config.discord.owner_id):
response, number_of_players = plex.get_available_players(media_type=media_type)
if response:
ask_about_player = True
while ask_about_player:
def check(react, react_user, num_players):
return str(react.emoji) in emoji_numbers[:number_of_players] \
and react_user.id == config.discord.owner_id
player_question = await ctx.send(response)
try:
for i in range(0, number_of_players - 1):
await player_question.add_reaction(emoji_numbers[i])
reaction, user = await self.bot.wait_for('reaction_add', timeout=60.0, check=check)
if reaction:
player_number = emoji_numbers.index(str(reaction.emoji))
media_item = plex.get_full_media_item(content_media_item=media_item)
if media_item:
plex.play_media(player_number, media_item)
else:
await ctx.send(
f"Sorry, something went wrong while loading that {media_type}.")
except asyncio.TimeoutError:
await player_question.delete()
ask_about_player = False
@tasks.loop(minutes=60.0) # update library every hour
async def make_libraries(self):
plex.populate_libraries()
@commands.group(aliases=['recommend', 'suggest', 'rec', 'sugg'], pass_context=True)
async def plex_rec(self, ctx: commands.Context, media_type: str):
"""
Get a recommendation item from Plex.
Required:
- what kind of content you want (i.e. 'movie', 'show', 'anime', 'song')
Optional filters:
- new [PLEX_USERNAME]: Only get recommended something you haven't played before
- imdb +/-[SCORE]: Only get recommended something that scores better/worse than [SCORE] on IMDb
- trakt [LIST_NAME]: Only get recommended something from a specific Trakt.tv list
- genre [GENRE]: Only get recommended something of a specific genre
- year [YEAR]: Only get recommended something from a specific year
Other Plex filters are supported:
- actor [ACTOR_ID]
- collection [COLLECTION_ID]
- contentRating [CONTENT_RATING]
- country [COUNTRY]
- decade [DECADE]
- director [DIRECTOR_ID]
- network [NETWORK_NAME]
- resolution [RESOLUTION]
- studio [STUDIO_NAME]
"""
if ctx.invoked_subcommand is None:
if media_type.lower() not in plex.library_config.keys():
accepted_types = "', '".join(plex.library_config.keys())
await ctx.send(f"Please try again, indicating '{accepted_types}'")
else:
hold_message = await ctx.send(
"Looking for a{} {}...".format("n" if (media_type[0] in ['a', 'e', 'i', 'o', 'u']) else "",
media_type))
async with ctx.typing():
response, embed, media_item = make_recommendation(media_type, False, None)
await hold_message.delete()
if embed is not None:
await ctx.send(response, embed=embed)
else:
await ctx.send(response)
await self.user_response(ctx=ctx, media_type=media_type, media_item=media_item)
analytics.event(event_category="Rec", event_action="Successful rec")
@plex_rec.error
async def plex_rec_error(self, ctx, error_msg):
error_and_analytics(error_message=error_msg, function_name=inspect.currentframe().f_code.co_name)
await ctx.send("Sorry, something went wrong while looking for a recommendation.")
@plex_rec.command(name="new", aliases=['unwatched', 'unseen', 'unlistened'])
async def plex_rec_new(self, ctx: commands.Context, plex_username: str):
"""
Get a new movie, show or artist recommendation
Include your Plex username
"""
media_type = None
for group in plex.library_config.keys():
if group in ctx.message.content:
media_type = group
break
if not media_type:
accepted_types = "', '".join(plex.library_config.keys())
await ctx.send(f"Please try again, indicating '{accepted_types}'")
else:
hold_message = await ctx.send(f"Looking for a new {media_type}...")
async with ctx.typing():
response, embed, media_item = make_recommendation(media_type, True, plex_username)
await hold_message.delete()
if embed is not None:
await ctx.send(response, embed=embed)
else:
await ctx.send(response)
analytics.event(event_category="Rec", event_action="Successful new rec")
await self.user_response(ctx=ctx, media_type=media_type, media_item=media_item)
@plex_rec_new.error
async def plex_rec_new_error(self, ctx, error_msg):
error_and_analytics(error_message=error_msg, function_name=inspect.currentframe().f_code.co_name)
await ctx.send("Sorry, something went wrong while looking for a new recommendation.")
@plex_rec.command(name="above", aliases=['over', 'better'])
async def plex_rec_above(self, ctx: commands.Context, rating: Union[float, int]):
"""
Get a movie or show above a certain IMDb rating (not music)
Include your rating
"""
media_type = None
for group in plex.library_config.keys():
if group in ctx.message.content:
media_type = group
break
if not media_type or media_type not in ['movie', 'show']:
await ctx.send(f"Sorry, this feature only works for movies and TV shows.")
else:
hold_message = await ctx.send(f"Looking for a {media_type} that's rated at least {rating} on IMDb...")
async with ctx.typing():
response, embed, media_item = make_recommendation(media_type=media_type, rating=float(rating),
above=True)
await hold_message.delete()
if embed is not None:
await ctx.send(response, embed=embed)
else:
await ctx.send(response)
analytics.event(event_category="Rec", event_action="Successful IMDb above rec")
await self.user_response(ctx=ctx, media_type=media_type, media_item=media_item)
@plex_rec_above.error
async def plex_rec_above_error(self, ctx, error_msg):
error_and_analytics(error_message=error_msg, function_name=inspect.currentframe().f_code.co_name)
await ctx.send("Sorry, something went wrong while looking for a new recommendation.")
@plex_rec.command(name="below", aliases=['under', 'worse'])
async def plex_rec_below(self, ctx: commands.Context, rating: Union[float, int]):
"""
Get a movie or show below a certain IMDb rating (not music)
Include your rating
"""
media_type = None
for group in plex.library_config.keys():
if group in ctx.message.content:
media_type = group
break
if not media_type or media_type not in ['movie', 'show']:
await ctx.send(f"Sorry, this feature only works for movies and TV shows.")
else:
hold_message = await ctx.send(f"Looking for a {media_type} that's rated less than {rating} on IMDb...")
async with ctx.typing():
response, embed, media_item = make_recommendation(media_type=media_type, rating=float(rating),
above=False)
await hold_message.delete()
if embed is not None:
await ctx.send(response, embed=embed)
else:
await ctx.send(response)
analytics.event(event_category="Rec", event_action="Successful IMDb below rec")
await self.user_response(ctx=ctx, media_type=media_type, media_item=media_item)
@plex_rec_below.error
async def plex_rec_below_error(self, ctx, error_msg):
error_and_analytics(error_message=error_msg, function_name=inspect.currentframe().f_code.co_name)
await ctx.send("Sorry, something went wrong while looking for a new recommendation.")
@plex_rec.command(name="trakt")
async def plex_rec_trakt(self, ctx: commands.Context, *, list_name: str):
"""
Get a movie or show from a specific Trakt.tv list
Heads up: Large Trakt list may take a while to parse, and your request may time out.
Once a choice is made from Trakt, the item is searched for in your Plex library. False matches are possible.
"""
media_type = None
for group in plex.library_config.keys():
if group in ctx.message.content:
media_type = group
break
if not media_type or media_type not in ['movie', 'show']:
await ctx.send(f"Sorry, this feature only works for movies and TV shows.")
else:
hold_message = await ctx.send(f"Looking for a {media_type} from the '{list_name}' list on Trakt.tv")
async with ctx.typing():
response, embed, media_item = make_recommendation(media_type=media_type, trakt_list_name=list_name)
await hold_message.delete()
if embed is not None:
await ctx.send(response, embed=embed)
else:
await ctx.send(response)
analytics.event(event_category="Rec", event_action="Successful Trakt rec")
await self.user_response(ctx=ctx, media_type=media_type, media_item=media_item)
@plex_rec_trakt.error
async def plex_rec_trakt_error(self, ctx, error_msg):
error_and_analytics(error_message=error_msg, function_name=inspect.currentframe().f_code.co_name)
await ctx.send("Sorry, something went wrong while looking for a new recommendation.")
def __init__(self, bot):
self.bot = bot
analytics.event(event_category="Platform", event_action=sys.platform)
info("Updating Plex libraries...")
self.make_libraries.start()
def setup(bot):
bot.add_cog(PlexRecs(bot))