from appfd.models import Place, Wikipedia
from broth import Broth
import csv
from datetime import datetime
from django.contrib.gis.geos import Point
from django.contrib.gis.measure import Distance
from multiprocessing import Process
from os.path import devnull, isfile
from os import remove
from re import match, search
from requests import get
from subprocess import call
from time import sleep

def run(debug=True):

    try:

        if debug: start = datetime.now()

        if debug: print "starting to initialize wikipedia"

        with open("/tmp/notability.tsv") as f:
            count = 0
            for row in csv.reader(f, delimiter="\t", quotechar='"'):
                try:
                    count += 1
                    if count != 1:
                        name, latitude, longitude, charcount = row
                        places = Place.objects.filter(name=name)
                        #print "places:", places
                        point = Point([float(longitude), float(latitude)])
                        found = places.filter(point__distance_lt=(point, Distance(mi=500)))
                        if not found:
                            found = places.filter(mpoly__contains=point)
                        if found.count() > 1:
                            filtered = found.exclude(population=0)
                            if filtered:
                                found = filtered
                            if found.count() > 1:
                                found = found.exclude(admin_level=None)
                        #    print "found within:", found
                        #print name, ":", found.values("admin_level","population").count(), ":", charcount
                        place = found.first()
                        if not place:
                            place = Place.objects.create(name=name, point=point)
                            #print "created:", place
                        Wikipedia.objects.get_or_create(place_id=place.id, charcount=charcount)
                        if count > 500:
                            break
                except Exception as e:
                    print e
            

        if debug: print "initializing wikipedia took " + str((datetime.now() - start).total_seconds()) + " seconds"

    except Exception as e:

        print e

@@ -388,16 +388,47 @@
 rt sleep
+%0Afrom urllib import urlretrieve
 %0A%0Adef ru
@@ -567,39 +567,244 @@
     
-with open(%22/tmp/notability.tsv%22
+path_to_tsv = %22/tmp/notability.tsv%22%0A        if not isfile(path_to_tsv):%0A            urlretrieve(%22https://raw.githubusercontent.com/DanielJDufour/geo_notability_assessor/master/notability.tsv%22, path_to_tsv)%0A%0A        with open(path_to_tsv
 ) as
@@ -1052,16 +1052,71 @@
 t = row%0A
+                        if latitude and longitude:%0A    
         
@@ -1188,32 +1188,36 @@
                 
+    
 #print %22places:%22
@@ -1225,16 +1225,57 @@
  places%0A
+                            try:%0A        
         
@@ -1341,16 +1341,243 @@
 tude)%5D)%0A
+                            except Exception as e:%0A                                print %22e:%22, e%0A                                print %22%5Ctlongitude:%22, longitude%0A                                print %22%5Ctlatitude:%22, latitude%0A    
         
@@ -1680,24 +1680,28 @@
             
+   
+ 
 if not found
@@ -1702,16 +1702,20 @@
  found:%0A
+    
         
@@ -1795,32 +1795,36 @@
                 
+    
 if found.count()
@@ -1857,16 +1857,20 @@
         
+ 
+   
 filtered
@@ -1924,24 +1924,28 @@
             
+    
 if filtered:
@@ -1969,32 +1969,36 @@
                 
+   
+ 
 found = filtered
@@ -1998,16 +1998,20 @@
 iltered%0A
+    
         
@@ -2076,32 +2076,36 @@
                 
+    
 found = found.ex
@@ -2152,16 +2152,20 @@
         
+ 
+   
 #    pri
@@ -2190,16 +2190,20 @@
 , found%0A
+    
         
@@ -2317,32 +2317,36 @@
                 
+  
+  
 place = found.fi
@@ -2371,24 +2371,28 @@
             
+    
 if not place
@@ -2393,16 +2393,20 @@
  place:%0A
+    
         
@@ -2498,32 +2498,36 @@
                 
+    
 #print %22created:
@@ -2535,16 +2535,20 @@
 , place%0A
+    
         
@@ -2651,32 +2651,37 @@
                 
+ 
+   #
 if count %3E 500:%0A
@@ -2700,24 +2700,29 @@
             
+    #
     break%0A  
@@ -2857,16 +2857,20 @@
 ook %22 + 
+str(
 str((dat
@@ -2906,16 +2906,22 @@
 conds())
+ / 60)
  + %22 sec

Initialize wikipedia