#20 Improve runtime of populate script

Chiuso
getty vorrebbe unire 1 commit da getty/gr/different-base-image a getty/master
1 ha cambiato i file con 22 aggiunte e 11 eliminazioni
  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__":