feedgin_conflict-20140929-161909.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python2
  2. ################
  3. # Configuration
  4. feeds = {
  5. "TITLE" : {
  6. "url" : "URLGOESHERE",
  7. },
  8. "TITLE" : {
  9. "url" : "URLGOESHERE",
  10. }
  11. }
  12. dataFilePath = '~/.appdata/feedgin.data'
  13. ################
  14. import dbus
  15. import feedparser
  16. import calendar
  17. import csv
  18. import os.path
  19. feedProcessedTimes = {name:0 for name in feeds}
  20. dataFilePathExpanded = os.path.expanduser(dataFilePath)
  21. if os.path.exists(dataFilePathExpanded):
  22. dataFile = open(dataFilePathExpanded, 'rb+')
  23. dataReader = csv.reader(dataFile)
  24. for row in dataReader:
  25. feedProcessedTimes[row[0]] = long(row[1])
  26. bus = dbus.SessionBus()
  27. obj = bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
  28. purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")
  29. activeAccounts = purple.PurpleAccountsGetAllActive()
  30. messageFlags = 0x2
  31. from datetime import datetime
  32. if (len(activeAccounts) > 0) :
  33. account = activeAccounts[0]
  34. conversation = purple.PurpleConversationNew(1, account, "feedgin")
  35. for feedName in feeds:
  36. print "Fetching", feedName, "[", datetime.now(), "]"
  37. feed = feeds[feedName]
  38. url = feed["url"]
  39. ast = feedparser.parse(url)
  40. #purple.PurpleConversationWrite(conversation, feedName, ast.feed.title, 0, 0)
  41. #print "feed:", ast.feed
  42. #print "entries:"
  43. for x in sorted(ast.entries, key = lambda entry: entry.updated_parsed):
  44. # for e in x: print e,':', x[e]
  45. time_since_epoch = calendar.timegm(x.updated_parsed)
  46. if time_since_epoch > feedProcessedTimes[feedName]:
  47. feedProcessedTimes[feedName] = time_since_epoch
  48. if x.has_key("author_detail"):
  49. author = x.author_detail
  50. else:
  51. class DummyAuthor:
  52. href='#'
  53. name='-'
  54. author = DummyAuthor()
  55. # Pidgin apparently doesn't show <img/> correctly
  56. #text = "<img id='42' src='"+x.media_thumbnail[0]["url"]+"' />"
  57. text = "<br/><b><a href='"+ x.link + "'>"+x.title+"</a></b> <em>by</em> <b><a href='"+author.href+"'>"+author.name+"</a></b>"
  58. if (feed.get("showSummary", True)):
  59. text += "<br/><small>" + x.summary + '</small>'
  60. im = purple.PurpleConvIm(conversation)
  61. purple.PurpleConvImWrite(im, feedName, text, messageFlags, time_since_epoch)
  62. else:
  63. print "No account is online right now."
  64. dataFile = open(dataFilePathExpanded, 'wb+')
  65. dataWriter = csv.writer(dataFile)
  66. for feedName in feedProcessedTimes:
  67. dataWriter.writerow([feedName, feedProcessedTimes[feedName]])