Last active
March 6, 2016 01:05
-
-
Save cyphunk/548ecc40fe71bb75a5fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# check url for a text and send alert via nexmo.com sms api | |
# | |
# example, checking for machina ex tickets: | |
# | |
# while [ 1 ]; do | |
# ./checkurlthensms.py \ | |
# http://www.hebbel-am-ufer.de/programm/spielplan/machina-ex-lessons-of-leaking/2354/ \ | |
# ticketLink "Tickets available" \ | |
# <nexmo_apikey> <nexmo_apisecret> \ | |
# <49###########-yourphone> | |
# sleep $((60*5)) | |
# done | |
# | |
lockfile = ".checkurl" | |
import urllib | |
import json | |
import os | |
import sys | |
if len(sys.argv) < 7: | |
print """ | |
%s <url> <findstr> <shortmessage> <sms_apikey> <apisecret> <to_phone> [<testrun>] | |
If <findstr> found anywhere in <url> contents, send <shortmessage> | |
to <to_phone>, using nexmo.com sms gateway. Set <testrun> field to 1 for test run. | |
Will check .checkurl lockfile and if exists program exits. That means sms | |
will only be sent on first match. To clear just delete .checkurl file. | |
example: | |
rm .checkurl; %s \\ | |
http://www.hebbel-am-ufer.de/programm/spielplan/machina-ex-lessons-of-leaking/2354/ \\ | |
ticketLink "Tickets available" <nexmo_apikey> <apisecret> 4917122223333 | |
"""%(sys.argv[0],sys.argv[0],) | |
sys.exit() | |
url = sys.argv[1] | |
findstr = sys.argv[2] | |
shortmessage = sys.argv[3] | |
apikey = sys.argv[4] | |
apisecret = sys.argv[5] | |
to = sys.argv[6] #must be whitelisted https://dashboard.nexmo.com/private/profile/phonenumber | |
if len(sys.argv) == 8: | |
testrun = True | |
else: | |
testrun = False | |
if os.path.isfile(lockfile) and not testrun: | |
print "Exit." | |
sys.exit() | |
response = urllib.urlopen(url) | |
html = response.readlines() | |
for line in html: | |
if findstr in line: | |
if not testrun: | |
with open(lockfile, 'a'): | |
os.utime(lockfile, None) | |
balanceurl = "https://rest.nexmo.com/account/get-balance?api_key=%s&api_secret=%s"%(apikey,apisecret) | |
balanceresponse = urllib.urlopen(balanceurl) | |
balance = json.loads(balanceresponse.read()) | |
if 'value' in balance: | |
shortmessage += " (sms account balance: "+str(balance['value']-0.07)+")" | |
else: | |
shortmessage += " (could not check sms account balance)" | |
smsurl = "https://rest.nexmo.com/sms/json?api_key=%s&api_secret=%s"%(apikey,apisecret) | |
smsurl += "&from=CheckBot&to=%s"%to | |
smsurl += "&text="+urllib.quote_plus(shortmessage) | |
if not testrun: | |
smsresponse = urllib.urlopen(smsurl) | |
if sys.stdout.isatty(): | |
print "MATCH LINE:", | |
print line.strip() | |
print "SMS URL:", | |
print smsurl | |
if not testrun: | |
print "SMS RESPONSE:" | |
print smsresponse.read() | |
print "status:0 means success" | |
# print "VERIFY TO PHONE IF UNVERIFIED:" | |
# print "https://api.nexmo.com/verify/json?api_key=%s&api_secret=%s&brand=CheckBot&number=%s"%(apikey,apisecret,to) | |
# print "THEN:" | |
# print "https://api.nexmo.com/verify/check/json?api_key=%s&api_secret=%s&request_id=<ID>&code=<CODE>"%(apikey,apisecret) | |
sys.exit() | |
if sys.stdout.isatty(): | |
print "No match found" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment