/modules/rss.py

https://github.com/astronouth7303/tgg-BotSteve · Python · 194 lines · 98 code · 45 blank · 51 comment · 25 complexity · 08f0ba13e39a6157f04a0a4a06851858 MD5 · raw file

  1. #!/usr/bin/env python
  2. """
  3. rss.py - The Geek Group RSS Feedreader Phenny Module
  4. Phenny Copyright 2008, Sean B. Palmer, inamidst.com
  5. http://inamidst.com/phenny/
  6. This module is copyright 2011, Steven Vaught
  7. Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
  8. """
  9. #later we can make this use SQlite if we want
  10. #import sqlite3
  11. import time, threading, feedparser, gdata.youtube, gdata.youtube.service
  12. def setup(phenny):
  13. def monitor(phenny):
  14. #set up the channel that messages will be transmitted to
  15. #FIXME
  16. #this should be read from a config file
  17. mainChannel = '#thegeekgroup'
  18. testChannel = '#tgg-bots'
  19. youtubeUserName = 'physicsduck'
  20. tggUserName = 'thegeekgroup'
  21. #pull original forum feed
  22. oldFeed = feedparser.parse("http://thegeekgroup.org/bb/?xfeed=all&feedkey=60635da5-d00a-4f9e-a007-a9102251b1c1")
  23. #pull physicsduck original youtube feed
  24. youtubeServe = gdata.youtube.service.YouTubeService()
  25. youtubeUri = 'http://gdata.youtube.com/feeds/api/users/%s/uploads' % youtubeUserName
  26. oldYoutubeFeed = youtubeServe.GetYouTubeVideoFeed(youtubeUri)
  27. #pull thegeekgroup original youtube feed
  28. youtubeTggServe = gdata.youtube.service.YouTubeService()
  29. youtubeTggUri = 'http://gdata.youtube.com/feeds/api/users/%s/uploads' % tggUserName
  30. oldTggYoutubeFeed = youtubeServe.GetYouTubeVideoFeed(youtubeTggUri)
  31. import time
  32. time.sleep(20)
  33. while True:
  34. #pull forum feed again
  35. #phenny.msg(testChannel, "Pulling new video feeds")
  36. currentFeed = ''
  37. currentFeed = feedparser.parse("http://thegeekgroup.org/bb/?xfeed=all&feedkey=60635da5-d00a-4f9e-a007-a9102251b1c1")
  38. #compare forum feeds
  39. titlesOld = []
  40. titlesCurrent = []
  41. titlesChanged = []
  42. for items in oldFeed.entries:
  43. titlesOld.append(items.updated)
  44. for items in currentFeed.entries:
  45. titlesCurrent.append( (items.title,items.updated) )
  46. for title,time in titlesCurrent:
  47. if time not in titlesOld:
  48. titlesChanged.append(title)
  49. #build the output string
  50. outputString = 'In the last hour, there have been '
  51. outputString += str( len(titlesChanged) )
  52. outputString += " new posts on the Geek Group forums ( http://goo.gl/t0vze ). New posts made by: "
  53. for eachPost in titlesChanged:
  54. outputString += eachPost
  55. if eachPost != titlesChanged[-1]:
  56. outputString += "....."
  57. #print the string only if there's something to output
  58. if titlesChanged:
  59. phenny.msg(mainChannel, outputString)
  60. oldFeed = currentFeed #don't forget to update
  61. #=======================
  62. #set up the output string as blank
  63. outputString = ""
  64. #pull physicsduck feed again
  65. currentYoutubeFeed = youtubeServe.GetYouTubeVideoFeed(youtubeUri)
  66. #compare forum feeds
  67. youtubeURLsOld = []
  68. youtubeTitlesCurrent = []
  69. youtubeTitlesChanged = []
  70. for items in oldYoutubeFeed.entry:
  71. youtubeURLsOld.append( str( items.GetSwfUrl() ).split("?")[0] )
  72. for items in currentYoutubeFeed.entry:
  73. youtubeTitlesCurrent.append( (items.media.title.text, str( items.GetSwfUrl() ).split("?")[0] ) )
  74. for title,url in youtubeTitlesCurrent:
  75. if url not in youtubeURLsOld:
  76. youtubeTitlesChanged.append( [title, url] )
  77. #rebuild the output string
  78. if youtubeTitlesChanged:
  79. outputString += 'In the last hour, there have been '
  80. outputString += str( len(youtubeTitlesChanged) )
  81. outputString += " new YouTube videos posted by PhysicsDuck. New videos: "
  82. #print the header
  83. #phenny.msg(mainChannel, outputString)
  84. #print the videos
  85. for eachTitle, eachURL in youtubeTitlesChanged:
  86. formattedURL = eachURL.replace("http://www.youtube.com/v/","http://youtu.be/")
  87. outputString += eachTitle
  88. outputString += " "
  89. outputString += formattedURL
  90. #don't display the string - preserved for historical purposes
  91. #phenny.msg(mainChannel, outputString)
  92. #update to the new feed
  93. oldYoutubeFeed = currentYoutubeFeed
  94. #debugging
  95. else:
  96. pass
  97. #phenny.msg(testChannel, "No new feeds from PhysicsDuck")
  98. #=======================
  99. #pull thegeekgroup feed again
  100. currentYoutubeTggFeed = youtubeTggServe.GetYouTubeVideoFeed(youtubeTggUri)
  101. #compare forum feeds
  102. youtubeTggURLsOld = []
  103. youtubeTggTitlesCurrent = []
  104. youtubeTggTitlesChanged = []
  105. for items in oldTggYoutubeFeed.entry:
  106. youtubeTggURLsOld.append( str( items.GetSwfUrl() ).split("?")[0] )
  107. for items in currentYoutubeTggFeed.entry:
  108. youtubeTggTitlesCurrent.append( (items.media.title.text, str( items.GetSwfUrl() ).split("?")[0] ) )
  109. for title,url in youtubeTggTitlesCurrent:
  110. if url not in youtubeTggURLsOld:
  111. youtubeTggTitlesChanged.append( [title, url] )
  112. #rebuild the output string
  113. if youtubeTggTitlesChanged:
  114. #if there's something already in the output string from above
  115. if outputString:
  116. outputString += " ||| "
  117. outputString += 'In the last hour, there have been '
  118. outputString += str( len(youtubeTggTitlesChanged) )
  119. outputString += " new YouTube videos posted by TheGeekGroup. New videos: "
  120. #print the header
  121. #phenny.msg(mainChannel, outputString)
  122. #print the videos
  123. for eachTitle, eachURL in youtubeTggTitlesChanged:
  124. formattedURL = eachURL.replace("http://www.youtube.com/v/","http://youtu.be/")
  125. outputString += eachTitle
  126. outputString += " "
  127. outputString += formattedURL
  128. #don't display the string - preserved for historical purposes
  129. #phenny.msg(mainChannel, outputString)
  130. #update to the new feed
  131. oldTggYoutubeFeed = currentYoutubeTggFeed
  132. #debugging
  133. else:
  134. pass
  135. # phenny.msg(testChannel, "No new feeds from TheGeekGroup")
  136. #display the string, if there's anything to display
  137. if outputString:
  138. phenny.msg(mainChannel, outputString)
  139. #phenny.msg(testChannel, "sleeping...")
  140. import time
  141. time.sleep(3600)
  142. targs = (phenny,)
  143. t = threading.Thread(target=monitor, args=targs)
  144. t.daemon = True
  145. t.start()
  146. if __name__ == '__main__':
  147. print __doc__.strip()