Skip to content

Instantly share code, notes, and snippets.

@starenka
Created February 28, 2011 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save starenka/848247 to your computer and use it in GitHub Desktop.
Save starenka/848247 to your computer and use it in GitHub Desktop.
is it up, yet?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# need to check a site if contains a regexp? cron stuff, eh?
#
# @author: starenka
# @email: 'moc]tod[liamg].T.E[0aknerats'[::-1]
# @version: 1.0
# @since 2/28/11
# @depends urlgrabber
import re,smtplib,datetime,sys
from email.mime.text import MIMEText
from optparse import OptionParser
from subprocess import Popen
import urlgrabber
TIMEOUT = 5
EMAIL_MESS = "The regexp you've been looking for it's on!\n\n%s"
EMAIL_FROM = 'ItsOnYet@domain.net'
usage = '%s -swww.icanhascheezburger.com -rchee[sz]burger -mmy@email.com'%(sys.argv[0])
oparser = OptionParser(usage)
oparser.add_option('-s','--site',action='store',dest='site',default = False,\
help='site URL')
oparser.add_option('-r','--regexp',action='store',dest='regexp',default = False,\
help='regexp to grep with')
oparser.add_option('-m','--mail',action='store',dest='email',default = False,\
help='send mail to this address')
oparser.add_option('-n','--notify',action='store_true',dest='knotify',default = False,\
help='notify (KDE4)')
(options,args) = oparser.parse_args()
if len(sys.argv) == 1: sys.exit(usage)
def is_mail(str):
if not str:
return False
return re.match(r'^[0-9a-zA-Z]+@[0-9a-zA-Z]+\.[0-9a-zA-Z]{1,4}$',str)
def mail(to,site,regexp,mess=EMAIL_MESS,from_=EMAIL_FROM):
now = datetime.datetime.now()
msg = MIMEText(mess%now)
msg['Subject'] = "It's up! '%s'@%s %s"%(regexp,site,now)
msg['From'] = from_
msg['To'] = to
s = smtplib.SMTP()
s.connect()
try:
s.sendmail(from_, [to], msg.as_string())
s.quit()
except Exception,e:
print e
def knotify():
cmd = ['kdialog',"It's on!",'--passivepopup','Hey! Regexp "%s" found at %s!'%
(options.regexp,options.site),'5']
try:
proc = Popen(cmd,shell=False)
proc.communicate()
except Exception,e: print e
if options.site and options.regexp and (is_mail(options.email) or options.knotify):
try:
if options.site[0:4] != 'http':
options.site = 'http://%s'%options.site
page = urlgrabber.urlread(options.site,None,timeout=TIMEOUT)
except Exception, e:
sys.exit('Failed to get uri %s %s'%(options.site,e))
if re.search(re.compile(options.regexp,re.IGNORECASE),page):
if options.knotify:
knotify()
if options.email:
mail(options.email,options.site,options.regexp)
else: sys.exit('Need site URL, regexp and some notification method to start...')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment