Advertisement
Guest User

Python RSS Notifier

a guest
Jun 12th, 2015
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. import feedparser
  2. import os
  3. import time
  4. import sys
  5.  
  6. #uses cyclingtorrents' RSS feed to check for new videos every 30 seconds
  7.  
  8. #class for RSS feeds
  9. class Feed:
  10.     def __init__(self, url):
  11.         self.feedUrl = url
  12.  
  13.     #get top entry of feed at feedUrl
  14.     def setTopEntry(self):
  15.         entryList = feedparser.parse(self.feedUrl)
  16.         self.curTop = entryList.entries[0]
  17.  
  18.     #parse size information from entry's description
  19.     def setSize(self):
  20.         description = self.curTop.description
  21.         sizeIndex = description.find('Size:')
  22.         sizeSub = description[sizeIndex:sizeIndex+15]
  23.         endIndex = sizeSub.find('B')
  24.         self.size = sizeSub[0:endIndex+1]
  25.  
  26.     def getTop(self):
  27.         return self.curTop
  28.  
  29.     #get download/view link from top entry
  30.     def getLink(self):
  31.         return self.curTop.link
  32.  
  33.     def getTitle(self):
  34.         return self.curTop.title
  35.  
  36.     def getDescription(self):
  37.         return self.curTop.description
  38.  
  39.     def getSize(self):
  40.         return self.size
  41.  
  42. # notifier function (using terminal-notifier)
  43. def notify(title, subtitle, message, url):
  44.     t = '-title {!r}'.format(title)
  45.     s = '-subtitle {!r}'.format(subtitle)
  46.     m = '-message {!r}'.format(message)
  47.     snd = '-sound default'
  48.     l = '-open {!r}'.format(url)
  49.     os.system('terminal-notifier {}'.format(' '.join([m, t, s, snd, l])))
  50.  
  51.  
  52.  
  53. ##___________________________Main Program Body______________________##
  54.  
  55. # initial notification
  56.  
  57. notify(title    = 'Welcome!',
  58.        subtitle = '',
  59.        message  = 'I\'ll be sure to let you know if there\'s a new video!',
  60.        url = 'http://www.cyclingtorrents.nl')
  61.  
  62. #construct initial feed object
  63.  
  64. initFeed = Feed(sys.argv[1])
  65. initFeed.setTopEntry()
  66. initFeed.setSize()
  67.  
  68.  
  69. #construct new feed object every 30 seconds and compare to initial
  70.  
  71. while (1):
  72.     curFeed = Feed(sys.argv[1])
  73.     curFeed.setTopEntry()
  74.     curFeed.setSize()
  75.  
  76.     if (curFeed.getLink() != initFeed.getLink()):
  77.         initFeed = curFeed
  78.         notify(title    = 'New video available!',
  79.         subtitle = str(curFeed.getTitle()),
  80.         message  = str(curFeed.getSize()),
  81.         url = str(curFeed.getLink()))
  82.  
  83.     del curFeed
  84.     time.sleep(30)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement