Repository: data-for-change/anyway

Issue:
Move load_discussions into main
I can't figure out what's the purpose of this file


Discussion:
It might relate to this feature?
![image](https://user-images.githubusercontent.com/16196934/30341455-bb25c11a-97fe-11e7-9e00-754e47cdd223.png)

Yes! I implemented the discussion feature a while ago. Discussions are kept
as markers with Disqus id. The script gets a list of ids and adds the
discussion markers.

On Tue, Sep 12, 2017, 21:10 Simona Holstein <notifications@github.com>
wrote:

> It might relate to this feature?
> [image: image]
> <https://user-images.githubusercontent.com/16196934/30341455-bb25c11a-97fe-11e7-9e00-754e47cdd223.png>
>
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/hasadna/anyway/issues/730#issuecomment-328936503>, or mute
> the thread
> <https://github.com/notifications/unsubscribe-auth/AAQEw4sZRCanFv4F-SgbkK4vRLpf9dhNks5shskMgaJpZM4PVCpA>
> .
>


Resolving patch:
diff --git a/anyway/load_discussions.py b/anyway/load_discussions.py
deleted file mode 100755
--- a/anyway/load_discussions.py
+++ /dev/null
@@ -1,39 +0,0 @@
-# -*- coding: utf-8 -*-
-import argparse
-from .models import DiscussionMarker
-import re
-from .database import db_session
-import sys
-import logging
-
-def main():
-    parser = argparse.ArgumentParser()
-    parser.add_argument('identifiers', type=str, nargs='*',
-                        help='Disqus identifiers to create markers for')
-    args = parser.parse_args()
-
-    identifiers = args.identifiers if args.identifiers else sys.stdin
-
-    for identifier in identifiers:
-        m = re.match(r'\((\d+\.\d+),\s*(\d+\.\d+)\)', identifier)
-        if not m:
-            logging.error("Failed processing: " + identifier)
-            continue
-        (latitude, longitude) = m.group(1, 2)
-        marker = DiscussionMarker.parse({
-            'latitude': latitude,
-            'longitude': longitude,
-            'title': identifier,
-            'identifier': identifier
-        })
-        try:
-            db_session.add(marker)
-            db_session.commit()
-            logging.info("Added:  " + identifier, end="")
-        except:
-            db_session.rollback()
-            logging.warn("Failed: " + identifier, end="")
-
-
-if __name__ == "__main__":
-    main()
diff --git a/main.py b/main.py
--- a/main.py
+++ b/main.py
@@ -1,8 +1,10 @@
 #!/usr/bin/env python
-
-import os
 import click
 import logging
+import os
+import re
+import sys
+
 
 @click.group()
 def cli():
@@ -59,5 +61,36 @@ def import_united_data(light, username, password, lastmail):
 
     return main(light=light, username=username, password=password, lastmail=lastmail)
 
+
+@cli.command()
+@click.argument('identifiers', nargs=-1)
+def load_discussions(identifiers):
+    from anyway.database import db_session
+    from anyway.models import DiscussionMarker
+
+    identifiers = identifiers or sys.stdin
+
+    for identifier in identifiers:
+        identifier = identifier.strip()
+        m = re.match(r'\((\d+\.\d+),\s*(\d+\.\d+)\)', identifier)
+        if not m:
+            logging.error("Failed processing: " + identifier)
+            continue
+        (latitude, longitude) = m.group(1, 2)
+        marker = DiscussionMarker.parse({
+            'latitude': latitude,
+            'longitude': longitude,
+            'title': identifier,
+            'identifier': identifier
+        })
+        try:
+            db_session.add(marker)
+            db_session.commit()
+            logging.info("Added:  " + identifier)
+        except Exception as e:
+            db_session.rollback()
+            logging.warn("Failed: " + identifier + ": " + e.message)
+
+
 if __name__ == '__main__':
     cli()


Test patch: