Feed index: convert items to IDs later
authorJack Miller <jack@codezen.org>
Mon, 15 Jun 2015 15:59:48 +0000 (10:59 -0500)
committerJack Miller <jack@codezen.org>
Mon, 15 Jun 2015 16:05:44 +0000 (11:05 -0500)
- Less confusing for sync plugins
- Easier to work with for the next stuff

canto_next/feed.py
plugins/sync-inoreader.py
tests/test-feed-index.py

index 1362dbd..5c25d45 100644 (file)
@@ -250,11 +250,8 @@ class CantoFeed(PluginHandler):
 
         self.lock.release_write()
 
-    def _cacheitem(self, item):
-        cacheitem = {}
-        cacheitem["id"] = json.dumps(\
-                { "URL" : self.URL, "ID" : item["id"] })
-        return cacheitem
+    def _item_id(self, item):
+        return json.dumps({ "URL" : self.URL, "ID" : item["id"] })
 
     # Re-index contents
     # If we have update_contents, use that
@@ -316,9 +313,7 @@ class CantoFeed(PluginHandler):
                 continue
 
             to_add.append(item)
-
-            cacheitem = self._cacheitem(item)
-            tags_to_add.append((cacheitem["id"], "maintag:" + self.name))
+            tags_to_add.append((item, "maintag:" + self.name))
 
             # Move over custom content from item.  Custom content is denoted
             # with a key that starts with "canto", but not "canto_update",
@@ -332,7 +327,7 @@ class CantoFeed(PluginHandler):
                         if key == "canto-tags":
                             for user_tag in olditem[key]:
                                 log.debug("index adding user tag: %s - %s", user_tag,item["id"])
-                                tags_to_add.append((cacheitem["id"], user_tag))
+                                tags_to_add.append((item, user_tag))
                             item[key] = olditem[key]
                         elif key.startswith("canto"):
                             item[key] = olditem[key]
@@ -377,12 +372,11 @@ class CantoFeed(PluginHandler):
                     log.debug("Discarding: %s", olditem["id"])
                     continue
 
-                cacheitem = self._cacheitem(olditem)
-                tags_to_add.append((cacheitem["id"], "maintag:" + self.name))
+                tags_to_add.append((olditem, "maintag:" + self.name))
 
                 if "canto-tags" in olditem:
                     for user_tag in olditem["canto-tags"]:
-                        tags_to_add.append((cacheitem["id"], user_tag))
+                        tags_to_add.append((olditem, user_tag))
 
                 update_contents["entries"].append(olditem)
 
@@ -423,20 +417,18 @@ class CantoFeed(PluginHandler):
 
             self.shelf[self.URL] = update_contents
 
-            to_remove = [ self._cacheitem(x)["id"] for x in old_contents["entries"] ]
-
             self.lock.release_write()
 
             tag_lock.acquire_write()
 
-            for item in to_remove:
-                alltags.remove_id(item)
+            for item in old_contents["entries"]:
+                alltags.remove_id(self._item_id(item))
 
             for item, tag in tags_to_add:
-                alltags.add_tag(item, tag)
+                alltags.add_tag(self._item_id(item), tag)
 
             for item, tag in tags_to_remove:
-                alltags.remove_tag(item, tag)
+                alltags.remove_tag(self._item_id(item), tag)
 
             # Go through and take items in old_contents that didn't make it
             # into update_contents / self.items and remove them from all tags.
index 9df6ecd..9cc2d0a 100644 (file)
@@ -289,7 +289,7 @@ class CantoFeedInoReader(DaemonFeedPlugin):
 
     def add_utag(self, item, tags_to_add, tag):
         self._list_add(item, "canto-tags", "user:" + tag)
-        tags_to_add.append((self.feed._cacheitem(item)["id"], "user:" + tag))
+        tags_to_add.append((item, "user:" + tag))
 
     def add_state(self, item, state):
         self._list_add(item, "canto-state", state)
@@ -329,7 +329,7 @@ class CantoFeedInoReader(DaemonFeedPlugin):
                 ino_entry["link"] = ino_entry["canonical"][0]["href"]
 
                 newcontent["entries"].append(ino_entry)
-                tags_to_add.append((self.feed._cacheitem(ino_entry)["id"], "maintag:" + feed.name ))
+                tags_to_add.append((ino_entry, "maintag:" + feed.name ))
 
     def edit_inoreader_sync(self, **kwargs):
         feed = kwargs["feed"]
@@ -402,7 +402,7 @@ class CantoFeedInoReader(DaemonFeedPlugin):
             for tag in entry["canto-tags"][:]:
                 if not api.has_tag(entry, tag.split(":", 1)[1]):
                     entry["canto-tags"].remove(tag)
-                    tags_to_remove.append((self.feed._cacheitem(entry)["id"], tag))
+                    tags_to_remove.append((entry, tag))
 
         api.flush_changes()
 
index 5c5cbc8..35feb5d 100755 (executable)
@@ -18,7 +18,7 @@ class TestFeedIndex(Test):
         for feed in allfeeds.get_feeds():
             maintag = "maintag:" + feed.name
             for entry in shelf[feed.URL]["entries"]:
-                full_id = feed._cacheitem(entry)["id"]
+                full_id = feed._item_id(entry)
                 if maintag not in alltags.items_to_tags([full_id]):
                     raise Exception("Item %s didn't make it into tag %s" % (entry, maintag))