Browse Source

Add transaction into populate script to speed it up

Getty Ritter 4 years ago
parent
commit
c64199b626
1 changed files with 22 additions and 11 deletions
  1. 22 11
      scripts/populate.py

+ 22 - 11
scripts/populate.py

@@ -24,18 +24,29 @@ def main():
 
     with open("scripts/aisamanra.json") as f:
         links = json.load(f)
+
+    tags = {}
     for l in links:
-        time = datetime.datetime.strptime(l["time"], "%Y-%m-%dT%H:%M:%SZ")
-        req = r.Link(
-            url=l["href"],
-            name=l["description"],
-            description=l["extended"],
-            private=l["shared"] == "no",
-            tags=l["tags"].split(),
-            created=time,
-        )
-        l = m.Link.from_request(u, req)
-        c.log(f"created link {l.url}")
+        for t in l["tags"].split():
+            if t in tags:
+                continue
+
+            tags[t] = m.Tag.get_or_create_tag(u, t)
+
+    with c.db.atomic():
+        for l in links:
+            time = datetime.datetime.strptime(l["time"], "%Y-%m-%dT%H:%M:%SZ")
+            ln = m.Link.create(
+                url=l["href"],
+                name=l["description"],
+                description=l["extended"],
+                private=l["shared"] == "no",
+                created=time,
+                user=u,
+            )
+            for t in l["tags"].split():
+                m.HasTag.create(link=ln, tag=tags[t])
+            c.log(f"created link {ln.url}")
 
 
 if __name__ == "__main__":