populate.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python3
  2. import datetime
  3. import json
  4. import os
  5. import lc.config as c
  6. import lc.model as m
  7. import lc.request as r
  8. def main():
  9. c.db.init("test.db")
  10. m.create_tables()
  11. u = m.User.get_or_none(name="gdritter")
  12. if not u:
  13. u = m.User.from_request(
  14. r.User(name="gdritter", password=os.getenv("PASSWORD", "behest").strip(),)
  15. )
  16. u.set_as_admin()
  17. c.log(f"created user {u.name}")
  18. with open("scripts/aisamanra.json") as f:
  19. links = json.load(f)
  20. tags = {}
  21. for l in links:
  22. for t in l["tags"].split():
  23. if t in tags:
  24. continue
  25. tags[t] = m.Tag.get_or_create_tag(u, t)
  26. with c.db.atomic():
  27. for l in links:
  28. time = datetime.datetime.strptime(l["time"], "%Y-%m-%dT%H:%M:%SZ")
  29. ln = m.Link.create(
  30. url=l["href"],
  31. name=l["description"],
  32. description=l["extended"],
  33. private=l["shared"] == "no",
  34. created=time,
  35. user=u,
  36. )
  37. for t in l["tags"].split():
  38. m.HasTag.create(link=ln, tag=tags[t])
  39. c.log(f"created link {ln.url}")
  40. if __name__ == "__main__":
  41. main()