]> www.vanbest.org Git - tweet-django-debian/commitdiff
Store reverse geolocated addresses in database
authorJan-Pascal van Best <janpascal@vanbest.org>
Sat, 20 Feb 2016 20:35:55 +0000 (21:35 +0100)
committerJan-Pascal van Best <janpascal@vanbest.org>
Sat, 20 Feb 2016 20:35:55 +0000 (21:35 +0100)
tweet/migrations/0009_tweet_address.py [new file with mode: 0644]
tweet/models.py
tweet/streamrunner.py
tweet/utils.py

diff --git a/tweet/migrations/0009_tweet_address.py b/tweet/migrations/0009_tweet_address.py
new file mode 100644 (file)
index 0000000..47c65aa
--- /dev/null
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.9.2 on 2016-02-18 22:53
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('tweet', '0008_auto_20160218_2208'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='tweet',
+            name='address',
+            field=models.TextField(blank=True, default=None, max_length=1020, null=True, verbose_name='address'),
+        ),
+    ]
index ec72eb8b7ff9185e7ca9b0fcc8a9f0d61c7b999b..3133409edf964c4ed92d924bd6bc6225cad316a2 100644 (file)
@@ -53,6 +53,7 @@ class Tweet(models.Model):
     text = models.TextField('tweet text', max_length=140)
     latitude = models.FloatField('latitude', null=True)
     longitude = models.FloatField('longitude', null=True)
+    address = models.TextField('address', max_length=1020, null=True, blank=True, default=None)
     conforms_to_terms = models.BooleanField('tweet confirms to filters')
 
     def __str__(self):
@@ -84,12 +85,15 @@ class Tweet(models.Model):
             (tweet.latitude,tweet.longitude) = status['geo']['coordinates']
         return tweet
 
-    def location(self):
+    def fill_address(self):
+        if self.address is not None:
+            return self.address
         if self.latitude is not None and self.longitude is not None:
             try:
                 geolocator = OpenMapQuest(api_key = settings.TWEET_MAPQUEST_API_KEY, timeout=10)
                 #print("lat,long:[{},{}]".format(self.latitude,self.longitude))
                 location = geolocator.reverse((self.latitude, self.longitude), exactly_one=True)
+                self.address = location.address
                 return location.address
             except GeocoderServiceError as e:
                 logger.exception("Error during reverse geocoding")
index f87de6cb362c906d46018ad0b566fe7915e3de50..da0310777b8b41587e31f0f9912b8cebdaf8608c 100644 (file)
@@ -27,6 +27,7 @@ class Streamer(TwythonStreamer):
             #logger.info("Stream tweet: {}".format(data))
             tweet = Tweet.from_status(data)
             tweet.conforms_to_terms = check_match(data)
+            tweet.fill_address()
             tweet.save()
             self.count += 1
             if self.count%100 == 0:
@@ -93,6 +94,8 @@ def export_tweets(filename):
     exporter = ExcelExporter(filename)
     tweets = Tweet.objects.filter(conforms_to_terms=True).order_by('-pk')
     for tweet in tweets:
+        tweet.fill_address()
+        tweet.save(update_fields=['address'])
         exporter.add_tweet(tweet)
     exporter.close()
 
index 33eb1d79f006762766a938eda0cb1c17edda5178..0451eff29ee07ba5dc3ce3ccefae618d60228261 100644 (file)
@@ -62,7 +62,8 @@ class ExcelExporter:
         #self.worksheet.write(row, 6, tweet["user"]["name"])
         if tweet.latitude is not None and tweet.longitude is not None:
             self.worksheet.write(row, 7, "{},{}".format(tweet.latitude, tweet.longitude))
-            self.worksheet.write(row, 8, tweet.location())
+            address = tweet.fill_address()
+            self.worksheet.write(row, 8, address)
 
         #self.worksheet.write(row, 19, tweet["user"]["location"])