-
Notifications
You must be signed in to change notification settings - Fork 305
/
twitter_notify.py
118 lines (99 loc) · 5.06 KB
/
twitter_notify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
1. Install the requests module for python.
pip install requests
pip install twitter
Tautulli > Settings > Notification Agents > Scripts > Bell icon:
[X] Notify on Recently Added
Tautulli > Settings > Notification Agents > Scripts > Gear icon:
Playback Recently Added: twitter_notify.py
Tautulli > 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}
https://gist.github.com/blacktwin/261c416dbed08291e6d12f6987d9bafa
"""
from __future__ import unicode_literals
from twitter import Twitter, OAuth
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)