Skip to content

Instantly share code, notes, and snippets.

@blacktwin
Last active May 4, 2017 19:25
Show Gist options
  • Save blacktwin/261c416dbed08291e6d12f6987d9bafa to your computer and use it in GitHub Desktop.
Save blacktwin/261c416dbed08291e6d12f6987d9bafa to your computer and use it in GitHub Desktop.
Post to Twitter when TV/Movie is added to Plex. Include custom message and embed poster image. Option to tweet to TWITTER_USER if title is inside TITLE_FIND.
"""
1. Install the requests module for python.
pip install requests
pip install twitter
PlexPy > Settings > Notification Agents > Scripts > Bell icon:
[X] Notify on Recently Added
PlexPy > Settings > Notification Agents > Scripts > Gear icon:
Playback Recently Added: twitter_notify.py
PlexPy > Settings > Notifications > Script > Script Arguments:
-sn {show_name} -ena {episode_name} -ssn {season_num00} -enu {episode_num00} -dur {duration}
-srv {server_name} -med {media_type} -tt {title} -purl {plex_url} -post {poster_url}
"""
from twitter import *
import argparse
import requests
import os
## EDIT THESE SETTINGS ##
TOKEN = ''
TOKEN_SECRET = ''
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
TITLE_FIND = ['Friends'] # Title to ignore ['Snow White']
TWITTER_USER = ' @username'
BODY_TEXT = ''
MOVIE_TEXT = "New {media_type}: {title} [ duration: {duration} mins ] was recently added to PLEX"
TV_TEXT = "New {media_type} of {show_name}: {title} [ S{season_num00}E{episode_num00} ] " \
"[ duration: {duration} mins ] was recently added to PLEX"
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-ip', '--ip_address', action='store', default='',
help='The IP address of the stream')
parser.add_argument('-us', '--user', action='store', default='',
help='Username of the person watching the stream')
parser.add_argument('-uid', '--user_id', action='store', default='',
help='User_ID of the person watching the stream')
parser.add_argument('-med', '--media_type', action='store', default='',
help='The media type of the stream')
parser.add_argument('-tt', '--title', action='store', default='',
help='The title of the media')
parser.add_argument('-pf', '--platform', action='store', default='',
help='The platform of the stream')
parser.add_argument('-pl', '--player', action='store', default='',
help='The player of the stream')
parser.add_argument('-da', '--datestamp', action='store', default='',
help='The date of the stream')
parser.add_argument('-ti', '--timestamp', action='store', default='',
help='The time of the stream')
parser.add_argument('-sn', '--show_name', action='store', default='',
help='The name of the TV show')
parser.add_argument('-ena', '--episode_name', action='store', default='',
help='The name of the episode')
parser.add_argument('-ssn', '--season_num', action='store', default='',
help='The season number of the TV show')
parser.add_argument('-enu', '--episode_num', action='store', default='',
help='The episode number of the TV show')
parser.add_argument('-srv', '--plex_server', action='store', default='',
help='The name of the Plex server')
parser.add_argument('-pos', '--poster', action='store', default='',
help='The poster url')
parser.add_argument('-sum', '--summary', action='store', default='',
help='The summary of the TV show')
parser.add_argument('-lbn', '--library_name', action='store', default='',
help='The name of the TV show')
parser.add_argument('-grk', '--grandparent_rating_key', action='store', default='',
help='The key of the TV show')
parser.add_argument('-purl', '--plex_url', action='store', default='',
help='Url to Plex video')
parser.add_argument('-dur', '--duration', action='store', default='',
help='The time of the stream')
p = parser.parse_args()
if p.media_type == 'movie':
BODY_TEXT = MOVIE_TEXT.format(media_type=p.media_type, title=p.title, duration=p.duration)
elif p.media_type == 'episode':
BODY_TEXT = TV_TEXT.format(media_type=p.media_type, show_name=p.show_name, title=p.title,
season_num00=p.season_num, episode_num00=p.episode_num, duration=p.duration)
else:
exit()
if p.title in TITLE_FIND:
BODY_TEXT += TWITTER_USER
t = Twitter(auth=OAuth(TOKEN, TOKEN_SECRET, CONSUMER_KEY, CONSUMER_SECRET))
filename = 'temp.jpg'
request = requests.get(p.poster, stream=True)
if request.status_code == 200:
with open(filename, 'wb') as image:
for chunk in request:
image.write(chunk)
t_upload = Twitter(domain='upload.twitter.com',
auth=OAuth(TOKEN, TOKEN_SECRET, CONSUMER_KEY, CONSUMER_SECRET))
file = open(filename, 'rb')
data = file.read()
id_img1 = t_upload.media.upload(media=data)["media_id_string"]
t.statuses.update(status=BODY_TEXT, media_ids=",".join([id_img1]))
os.remove(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment