forked from BenjV/autosub-bootstrapbill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExamplePostProcess.py
240 lines (203 loc) · 7.55 KB
/
ExamplePostProcess.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#This Script is an example of what is possible with the
#PostProcess feature of Auto-Sub
import sys
import os
import library.pythontwitter as twitter
import library.oauth2 as oauth
import library.pynma as pynma
import socket
import email.utils
import smtplib
from library.growl import gntp
from email.mime.text import MIMEText
try:
from urlparse import parse_qsl
except:
from cgi import parse_qsl
# TODO: create notification library and integrate it
#OPTIONS EDIT HERE
#What options:
# twitter - send notifications to twitter
# growl - send notification to a growl server
# echo - echo's the arguments given to the script
# mail - send an email when a subtitle is downloaded
# nma - Notify Your Android
what = "echo"
#Growl Options:
growl_host = ""
growl_port = 23053
growl_pass = ""
#/Growl Options
#Twitter options:
token_key = ''
token_secret = ''
#/Twitter options
#Mail Options:
#If you don't need a password and username, set them to (leave them blank): ''
#Currently, only TLS is supported for encryption. If you don't need encryption, leave it blank.
#WARNING! YOUR PASSWORD IS STORED IN PLAIN TEXT MAKE SURE THE FILE IS NOT
#READABLE BY EVERYONE.
#!USE AT OWN RISK!
srv = 'smtp.gmail.com:587'
fromaddr = '[email protected]'
toaddrs = '[email protected]'
username = '[email protected]'
password = 'mysecretpassword'
subject = 'Subs info'
encryption = 'TLS'
#/Mail Options
#Notify My Android options:
nma_apikey = ''
#/Notify My Android options:
#/OPTIONS STOP EDITING BELOW THIS LINE
#MAIL APPLICATION SOURCE
def send_mail(message):
"""
Construct and send the email.
Keyword arguments:
message -- Full name of the subtitle
"""
msg = MIMEText('Hi,\n \n AutoSub downloaded the following subtitle:\n %s'\
% message)
msg['To'] = email.utils.formataddr(('Recipient', toaddrs))
msg['Subject'] = subject
msg = msg.as_string()
try:
server = smtplib.SMTP(srv)
if encryption == 'TLS':
server.starttls()
if username != '' and password != '':
server.login(username, password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
print "Email notification send"
except:
print "ERROR: Unable to send an email notification. Check your email settings."
#GROWL APPLICATION SOURCE
def send_growl(host, port, message):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.send(message)
response = gntp.parse_gntp(s.recv(1024))
s.close()
print "Growl notification send"
except socket.error:
print "ERROR: Unable to send growl notification to growl server. Check your settings."
def create_growl(message):
notice = gntp.GNTPNotice()
notice.add_header('Application-Name', "AutoSub")
notice.add_header('Notification-Name', "Subtitle Download")
notice.add_header('Notification-Title', "AutoSub: Subtitle Download")
notice.add_header('Notification-Text', message)
if growl_pass != "":
notice.set_password(growl_pass)
send_growl(growl_host, growl_port, notice.encode())
def register_growl():
register = gntp.GNTPRegister()
register.add_header('Application-Name', "AutoSub")
register.add_notification('Test', True)
register.add_notification('Subtitle Download', True)
if growl_pass != "":
register.set_password(growl_pass)
send_growl(growl_host, growl_port, register.encode())
notice = gntp.GNTPNotice()
notice.add_header('Application-Name', "AutoSub")
notice.add_header('Notification-Name', "Test")
notice.add_header('Notification-Title', "Testing notification")
notice.add_header('Notification-Text', "This is a test notification send by AutoSub, check!")
if growl_pass != "":
notice.set_password(growl_pass)
send_growl(growl_host, growl_port, notice.encode())
#TWITTER APPLICATION SOURCE
consumer_key = 'CRMvUogoJ5kMErtU9fiw'
consumer_secret = 'JqS5jJIWdokl5iijZmoBHNwRsknw7xmCxPggEsmo8'
REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token'
ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token'
AUTHORIZATION_URL = 'https://api.twitter.com/oauth/authorize'
SIGNIN_URL = 'https://api.twitter.com/oauth/authenticate'
def authorize_application():
consumer = oauth.Consumer(key=consumer_key, secret=consumer_secret)
oauth_client = oauth.Client(consumer)
response, content = oauth_client.request(REQUEST_TOKEN_URL, 'GET')
if response['status'] != '200':
print "ERROR: Invalid response"
else:
request_token = dict(parse_qsl(content))
print "Visit: " + AUTHORIZATION_URL + "?oauth_token=" + request_token['oauth_token']
token_key = request_token['oauth_token']
token_secret = request_token['oauth_token_secret']
token = oauth.Token(token_key, token_secret)
pin = raw_input('PIN: ').strip()
token.set_verifier(pin)
oauth_client2 = oauth.Client(consumer, token)
response, content = oauth_client2.request(ACCESS_TOKEN_URL, method='POST', body='oauth_verifier=%s' % pin)
access_token = dict(parse_qsl(content))
if response['status'] != '200':
print "ERROR: Invalid response"
else:
token_key = access_token['oauth_token']
token_secret = access_token['oauth_token_secret']
print "token_key='%s'" % token_key
print "token_secret='%s'" % token_secret
def send_tweet(message):
try:
api = twitter.Api(consumer_key, consumer_secret, token_key, token_secret)
api.PostUpdate(message)
print "Tweet sended!"
except:
print "ERROR: Could not send tweet, make sure token_key & token_secret are correct."
#Notify My Android source:
def notify_my_android(subtitle):
nma_instance = pynma.PyNMA(nma_apikey)
message = "Auto-Sub just downloaded the following subtitle \n %s" %subtitle
resp = nma_instance.push('Auto-Sub','Downloaded a Subtitle',message)
if not resp[nma_apikey][u'code'] == u'200':
print "ERROR: Something went wrong!"
else:
print "Android notified!"
#MAIN FUNCTION
if what == "echo":
print
try:
print sys.argv[1]
print sys.argv[2]
except IndexError:
print "ERROR: Trying to setup this script? Make sure you set the 'what' variable. "
if what == "twitter":
if token_key == "":
authorize_application()
else:
if (sys.argv[1] and sys.argv[2] and token_key != ""):
var = sys.argv[1]
var = os.path.basename(var)
tweet = "AutoSub Downloaded: " + var
print "Sending the following tweet %s" % tweet[:140]
send_tweet(tweet[:140])
elif (sys.argv[1] and sys.argv[2]):
print "ERROR: token_key not set yet, run script without any arguments"
if what == "growl":
if len(sys.argv) == 1:
register_growl()
elif (sys.argv[1] and sys.argv[2] and growl_host != ""):
var = sys.argv[1]
var = os.path.basename(var)
create_growl(var)
if what == "mail":
if (sys.argv[1] and sys.argv[2] and srv != "" and fromaddr != "" and toaddrs != ""):
var = sys.argv[1]
var = os.path.basename(var)
send_mail(var)
elif (sys.argv[1] and sys.argv[2]):
print "ERROR: Minimum mail settings aren't set."
else:
print "ERROR: Mail settings not set"
if what == "nma":
if (sys.argv[1] and sys.argv[2] and nma_apikey):
var = sys.argv[1]
var = os.path.basename(var)
notify_my_android(var)
elif (sys.argv[1] and sys.argv[2]):
print "ERROR: No APIKey set!"
else:
print "ERROR: Something went wrong!"