bitbot-3.11-fork/modules/trakt.py

58 lines
2.4 KiB
Python
Raw Normal View History

#--require-config trakt-api-key
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-05-23 09:28:14 +00:00
@utils.export("set", {"setting": "trakt", "help": "Set username on trakt.tv",
"example": "jesopo"})
class Module(ModuleManager.BaseModule):
@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):
"""
:help: Get what you or another user is now watching on trakt.tv
:usage: [username]
"""
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)
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":
self.bot.config["trakt-api-key"]}, json=True,
code=True)
if page:
if page.code == 200:
type = page.data["type"]
2016-03-29 11:56:58 +00:00
if type == "movie":
title = page.data["movie"]["title"]
year = page.data["movie"]["year"]
slug = page.data["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":
season = page.data["episode"]["season"]
episode_number = page.data["episode"]["number"]
episode_title = page.data["episode"]["title"]
show_title = page.data["show"]["title"]
show_year = page.data["show"]["year"]
slug = page.data["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!")
else:
event["stderr"].write(
"%s is not watching anything" % username)
2016-03-29 11:56:58 +00:00
else:
raise utils.EventsResultsError()