NewsfeedPlugin.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import time
  2. import re
  3. from Plugin import PluginManager
  4. from Db import DbQuery
  5. @PluginManager.registerTo("UiWebsocket")
  6. class UiWebsocketPlugin(object):
  7. def actionFeedFollow(self, to, feeds):
  8. self.user.setFeedFollow(self.site.address, feeds)
  9. self.user.save()
  10. self.response(to, "ok")
  11. def actionFeedListFollow(self, to):
  12. feeds = self.user.sites[self.site.address].get("follow", {})
  13. self.response(to, feeds)
  14. def actionFeedQuery(self, to):
  15. if "ADMIN" not in self.site.settings["permissions"]:
  16. return self.response(to, "FeedQuery not allowed")
  17. from Site import SiteManager
  18. rows = []
  19. for address, site_data in self.user.sites.iteritems():
  20. feeds = site_data.get("follow")
  21. if not feeds:
  22. continue
  23. for name, query_set in feeds.iteritems():
  24. site = SiteManager.site_manager.get(address)
  25. try:
  26. query, params = query_set
  27. query_parts = query.split("UNION")
  28. for i, query_part in enumerate(query_parts):
  29. db_query = DbQuery(query_part)
  30. where = " WHERE %s > strftime('%%s', 'now', '-3 day')" % db_query.fields.get("date_added", "date_added")
  31. if "WHERE" in query_part:
  32. query_part = re.sub("WHERE (.*?)(?=$| GROUP BY)", where+" AND (\\1)", query_part)
  33. else:
  34. query_part += where
  35. query_parts[i] = query_part
  36. query = " UNION ".join(query_parts)
  37. if ":params" in query:
  38. query = query.replace(":params", ",".join(["?"] * len(params)))
  39. res = site.storage.query(query + " ORDER BY date_added DESC LIMIT 10", params)
  40. else:
  41. res = site.storage.query(query + " ORDER BY date_added DESC LIMIT 10")
  42. except Exception, err: # Log error
  43. self.log.error("%s feed query %s error: %s" % (address, name, err))
  44. continue
  45. for row in res:
  46. row = dict(row)
  47. if "date_added" not in row or row["date_added"] > time.time() + 120:
  48. continue # Feed item is in the future, skip it
  49. row["site"] = address
  50. row["feed_name"] = name
  51. rows.append(row)
  52. return self.response(to, rows)
  53. def actionFeedSearch(self, to, search):
  54. if "ADMIN" not in self.site.settings["permissions"]:
  55. return self.response(to, "FeedSearch not allowed")
  56. from Site import SiteManager
  57. rows = []
  58. num_sites = 0
  59. s = time.time()
  60. for address, site in SiteManager.site_manager.list().iteritems():
  61. if not site.storage.has_db:
  62. continue
  63. if site.storage.db: # Database loaded
  64. feeds = site.storage.db.schema.get("feeds")
  65. else:
  66. try:
  67. feeds = site.storage.loadJson("dbschema.json").get("feeds")
  68. except:
  69. continue
  70. if not feeds:
  71. continue
  72. num_sites += 1
  73. for name, query in feeds.iteritems():
  74. try:
  75. db_query = DbQuery(query)
  76. db_query.wheres.append("%s LIKE ? OR %s LIKE ?" % (db_query.fields["body"], db_query.fields["title"]))
  77. db_query.parts["ORDER BY"] = "date_added DESC"
  78. db_query.parts["LIMIT"] = "30"
  79. search_like = "%" + search.replace(" ", "%") + "%"
  80. res = site.storage.query(str(db_query), [search_like, search_like])
  81. except Exception, err:
  82. self.log.error("%s feed query %s error: %s" % (address, name, err))
  83. continue
  84. for row in res:
  85. row = dict(row)
  86. if row["date_added"] > time.time() + 120:
  87. continue # Feed item is in the future, skip it
  88. row["site"] = address
  89. row["feed_name"] = name
  90. rows.append(row)
  91. return self.response(to, {"rows": rows, "num": len(rows), "sites": num_sites, "taken": time.time() - s})
  92. @PluginManager.registerTo("User")
  93. class UserPlugin(object):
  94. # Set queries that user follows
  95. def setFeedFollow(self, address, feeds):
  96. site_data = self.getSiteData(address)
  97. site_data["follow"] = feeds
  98. self.save()
  99. return site_data