2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on commands
|
|
|
|
#--depends-on config
|
2016-03-30 18:31:23 +00:00
|
|
|
#--require-config trakt-api-key
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, utils
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
URL_TRAKT = "https://api-v2launch.trakt.tv/users/%s/watching"
|
|
|
|
URL_TRAKTSLUG = "https://trakt.tv/%s/%s"
|
|
|
|
|
2019-06-28 22:16:05 +00:00
|
|
|
@utils.export("set", utils.Setting("trakt", "Set username on trakt.tv",
|
|
|
|
example="jesopo"))
|
2018-09-27 11:08:07 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2018-10-10 09:42:41 +00:00
|
|
|
@utils.hook("received.command.nw", alias_of="nowwatching")
|
|
|
|
@utils.hook("received.command.nowwatching")
|
2016-03-29 11:56:58 +00:00
|
|
|
def now_watching(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Get what you or another user is now watching on trakt.tv
|
|
|
|
:usage: [username]
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2016-03-29 11:56:58 +00:00
|
|
|
if event["args"]:
|
|
|
|
username = event["args_split"][0]
|
|
|
|
else:
|
|
|
|
username = event["user"].get_setting("trakt",
|
|
|
|
event["user"].nickname)
|
2018-12-11 22:26:38 +00:00
|
|
|
page = utils.http.request(URL_TRAKT % username, headers={
|
2016-03-29 11:56:58 +00:00
|
|
|
"Content-Type": "application/json",
|
|
|
|
"trakt-api-version": "2", "trakt-api-key":
|
2019-11-26 13:41:40 +00:00
|
|
|
self.bot.config["trakt-api-key"]})
|
2018-12-11 22:27:58 +00:00
|
|
|
if page:
|
2018-12-11 22:26:38 +00:00
|
|
|
if page.code == 200:
|
2019-11-26 13:41:40 +00:00
|
|
|
page = page.json()
|
|
|
|
type = page["type"]
|
2016-03-29 11:56:58 +00:00
|
|
|
if type == "movie":
|
2019-11-26 13:41:40 +00:00
|
|
|
title = page["movie"]["title"]
|
|
|
|
year = page["movie"]["year"]
|
|
|
|
slug = page["movie"]["ids"]["slug"]
|
2016-03-29 11:56:58 +00:00
|
|
|
event["stdout"].write(
|
|
|
|
"%s is now watching %s (%s) %s" % (
|
|
|
|
username, title, year,
|
|
|
|
URL_TRAKTSLUG % ("movie", slug)))
|
|
|
|
elif type == "episode":
|
2019-11-26 13:41:40 +00:00
|
|
|
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"]
|
2016-03-29 11:56:58 +00:00
|
|
|
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:
|
2019-11-18 12:06:59 +00:00
|
|
|
raise utils.EventResultsError()
|