2016-03-30 18:31:23 +00:00
|
|
|
#--require-config trakt-api-key
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
import Utils
|
|
|
|
|
|
|
|
URL_TRAKT = "https://api-v2launch.trakt.tv/users/%s/watching"
|
|
|
|
URL_TRAKTSLUG = "https://trakt.tv/%s/%s"
|
|
|
|
|
|
|
|
class Module(object):
|
2018-08-31 11:55:52 +00:00
|
|
|
def __init__(self, bot, events):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.bot = bot
|
2018-08-31 11:55:52 +00:00
|
|
|
events.on("received").on("command").on("nowwatching",
|
2016-03-29 13:37:35 +00:00
|
|
|
"nw").hook(self.now_watching,
|
|
|
|
help="Get what you or another user is now watching "
|
2016-04-06 11:02:44 +00:00
|
|
|
"on trakt.tv", usage="[username]")
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-08-31 11:55:52 +00:00
|
|
|
events.on("postboot").on("configure").on("set"
|
2018-08-30 17:06:46 +00:00
|
|
|
).assure_call(setting="trakt", help="Set username on trakt.tv")
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
def now_watching(self, event):
|
|
|
|
if event["args"]:
|
|
|
|
username = event["args_split"][0]
|
|
|
|
else:
|
|
|
|
username = event["user"].get_setting("trakt",
|
|
|
|
event["user"].nickname)
|
|
|
|
page = Utils.get_url(URL_TRAKT % username, headers={
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"trakt-api-version": "2", "trakt-api-key":
|
|
|
|
self.bot.config["trakt-api-key"]}, json=True,
|
|
|
|
code=True)
|
2016-04-03 12:20:05 +00:00
|
|
|
if page[0]:
|
2016-03-29 11:56:58 +00:00
|
|
|
code, page = page
|
2016-04-03 12:20:05 +00:00
|
|
|
if code == 200:
|
2016-03-29 11:56:58 +00:00
|
|
|
type = page["type"]
|
|
|
|
if type == "movie":
|
|
|
|
title = page["movie"]["title"]
|
|
|
|
year = page["movie"]["year"]
|
|
|
|
slug = page["movie"]["ids"]["slug"]
|
|
|
|
event["stdout"].write(
|
|
|
|
"%s is now watching %s (%s) %s" % (
|
|
|
|
username, title, year,
|
|
|
|
URL_TRAKTSLUG % ("movie", slug)))
|
|
|
|
elif type == "episode":
|
|
|
|
season = page["episode"]["season"]
|
|
|
|
episode_number = page["episode"]["number"]
|
|
|
|
episode_title = page["episode"]["title"]
|
|
|
|
show_title = page["show"]["title"]
|
|
|
|
show_year = page["show"]["year"]
|
|
|
|
slug = page["show"]["ids"]["slug"]
|
|
|
|
event["stdout"].write(
|
|
|
|
"%s is now watching %s s%se%s - %s %s" % (
|
|
|
|
username, show_title, str(season).zfill(2),
|
|
|
|
str(episode_number).zfill(2), episode_title,
|
|
|
|
URL_TRAKTSLUG % ("shows", slug)))
|
|
|
|
else:
|
|
|
|
print("ack! unknown trakt media type!")
|
2016-04-03 12:20:05 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write(
|
|
|
|
"%s is not watching anything" % username)
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("Failed to load results")
|