Fix breaking URL limit in Inoreader sync
authorJack Miller <jack@codezen.org>
Mon, 8 Jun 2015 07:06:20 +0000 (02:06 -0500)
committerJack Miller <jack@codezen.org>
Mon, 8 Jun 2015 07:11:32 +0000 (02:11 -0500)
Grouping tag add / remove calls is good, but when you start pushing the
URL size too high, then the Inoreader server doesn't like it.

NOTE: I couldn't find actual documentation on Inoreader's server/API
limit, so I took a guess at 2048 bytes as a reasonable limit. Further
tweaking may be required.

plugins/sync-inoreader.py

index 44f3528..ada4d18 100644 (file)
@@ -158,18 +158,30 @@ class CantoInoreaderAPI():
             else:
                 self.del_tags_queued[tag] = [ino_id]
 
+    def _urllimit(self, prefix, ino_ids):
+        t = prefix
+        l = len(t)
+
+        for i, ino_id in enumerate(ino_ids):
+            if l + len(ino_id) > 2048:
+                self.inoreader_req(t)
+                return ino_ids[i:]
+            t += ino_id
+            l += len(ino_id)
+
+        self.inoreader_req(t)
+        return []
+
     def flush_changes(self):
-        add_url = "api/0/edit-tag?a="
         for key in self.add_tags_queued:
-            tag_add_url = add_url + quote(self.full_ino_tag(key))
-            tag_add_url += "".join([ "&i=" + quote(x) for x in self.add_tags_queued[key]])
-            self.inoreader_req(tag_add_url)
+            to_add = [ "&i=" + quote(x) for x in self.add_tags_queued[key]]
+            while to_add:
+                to_add = self._urllimit("api/0/edit-tag?a=" + quote(self.full_ino_tag(key)), to_add)
 
-        del_url = "api/0/edit-tag?r="
         for key in self.del_tags_queued:
-            tag_del_url = del_url + quote(self.full_ino_tag(key))
-            tag_del_url += "".join([ "&i=" + quote(x) for x in self.del_tags_queued[key]])
-            self.inoread_req(tag_del_url)
+            to_del = [ "&i=" + quote(x) for x in self.del_tags_queued[key]]
+            while to_del:
+                to_del = self._urllimit("api/0/edit-tag?r=" + quote(self.full_ino_tag(key)), to_del)
 
         self.add_tags_queued = {}
         self.del_tags_queued = {}