Advertisement
Guest User

notify daemon for ii(51t)

a guest
Aug 12th, 2014
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.60 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import urllib, os, subprocess, time
  4. from daemon import runner
  5.  
  6. URL = 'http://51t.ru/list.txt?h=1&el='
  7. ECHO = 'ii.14/pipe.2032/obsd.talk.14'
  8. MSGS_LST = '/var/tmp/check_new_msgs.lst'
  9. #ECHO = '/'.join(os.listdir('echo'))
  10.  
  11. class Check():
  12.  
  13.     def __init__(self):
  14.         self.stdin_path = '/dev/null'
  15.         self.stdout_path = '/dev/null' # or /dev/tty for debug
  16.         self.stderr_path = '/dev/null'
  17.         self.pidfile_path =  '/tmp/check_new_msgs.pid'
  18.         self.pidfile_timeout = 5
  19.  
  20.     def run(self):
  21.         while True:
  22.             hashes = getf(URL+ECHO)
  23.             oldhash = open(MSGS_LST).read().splitlines() if os.path.exists(MSGS_LST) else []
  24.             check_hashes(hashes, oldhash)
  25.             time.sleep(600)
  26.  
  27. def getf(l):
  28.     return urllib.urlopen(l).read()
  29.  
  30.  
  31. def sendmessage(message):
  32.     subprocess.Popen(['notify-send', message])
  33.     return
  34.  
  35.  
  36. # Check hashes
  37. def check_hashes(hashes, oldhash):
  38.     for n in hashes.splitlines():
  39.         ea,cnt,hsh = n.split(':',2)
  40.         check = [x for x in oldhash if x.startswith(ea + ':')]
  41.         if check:
  42.             echoes = check[0].split(':',2)
  43.             if not echoes[2].startswith('hsh/'):
  44.                 print 'error hsh: ' % ea
  45.             elif echoes[2] != hsh:
  46.                 print ea
  47.                 sendmessage("ii\nНовые сообщения в\n"+ea)
  48.         else:
  49.             print ea
  50.             sendmessage("ii\nНовые сообщения в\n"+ea)
  51.  
  52.     open(MSGS_LST,'w').write(hashes)
  53.  
  54. app = Check()
  55. daemon_runner = runner.DaemonRunner(app)
  56. daemon_runner.do_action()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement