Skip to content

Instantly share code, notes, and snippets.

@JonnyWong16
Last active July 17, 2018 06:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonnyWong16/6e3b07bbc99eeb15183ba86be5bdf9a7 to your computer and use it in GitHub Desktop.
Save JonnyWong16/6e3b07bbc99eeb15183ba86be5bdf9a7 to your computer and use it in GitHub Desktop.
Send a random Chuck Norris joke when a movie starring Chuck Norris is played.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Description: Send a random Chuck Norris joke when a movie
# starring Chuck Norris is played.
# Author: /u/SwiftPanda16
# Requires: requests, lxml
# PlexPy script trigger: Playback start
# PlexPy script arguments: "{actors}"
import random
import requests
import sys
from lxml.html import parse
### EDIT SETTINGS ###
PLEXPY_URL = 'http://localhost:8181'
PLEXPY_APIKEY = 'xxxxxxxxxx'
AGENT_ID = 10 # The PlexPy notifier agent id found here: https://github.com/JonnyWong16/plexpy/blob/master/API.md#notify
NOTIFY_SUBJECT = 'Random Chuck Norris Joke' # The notification subject
NOTIFY_BODY = '{joke}' # The notification body
CHUCK_NORRIS_JOKES_URL = 'http://chucknorrisjokes.linkpress.info/top-100'
### CODE BELOW ###
def main():
try:
actors = sys.argv[1]
except:
print("Invalid PlexPy script argument passed.")
return
if 'Chuck Norris' in actors:
print("Chuck Norris in actors list.")
print("Retrieving random Chuck Norris joke.")
tree = parse(CHUCK_NORRIS_JOKES_URL)
jokes = tree.xpath("//div[@style='float:left; width: 350px; margin-top: 8px;']/text()")
random_joke = random.choice(jokes)
print("Sending PlexPy notification to agent ID: {agent_id}.".format(agent_id=AGENT_ID))
params = {'apikey': PLEXPY_APIKEY,
'cmd': 'notify',
'agent_id': AGENT_ID,
'subject': NOTIFY_SUBJECT,
'body': NOTIFY_BODY.format(joke=random_joke)}
r = requests.post(PLEXPY_URL.rstrip('/') + '/api/v2', params=params)
else:
return
if __name__ == "__main__":
main()
@mew1033
Copy link

mew1033 commented Mar 23, 2017

Haha, I love it. :-)
FYI, there is a Chuck Norris jokes API: http://www.icndb.com/api/

@tjsnow
Copy link

tjsnow commented Jul 17, 2018

I resolved my issue below, I didn't have the modules installed. Once installed, I saw a new error about resolving the external entity. I went to the base url defined in the script and it looks like it redirected somewhere else, so I updated the URL and still have the same issue. I also verified the XML it is parsing is still accurate. Any thoughts?


I cannot get this to work. I did the following..

  1. Create folder for custom scripts
  2. Add notification agent (the one called script)
  3. Point agent to scripts folder
  4. select the script file
  5. Check Playback Start as my trigger
  6. Under arguments, added {actors} to the playback start
    When testing script, get nothing. Checked the logs and see this..

Tautulli Notifiers :: Script error:
Traceback (most recent call last):
File "E:\Tautulli\Scripts\notify_random_chuck_norris.py", line 13, in
import requests
ImportError: No module named requests

I am a total newb if you can't tell. Any help would be appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment