Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

API plutôt stable

  • Loading branch information...
commit 0493b5620146370d85e6482dd4f94b2d901f23eb 1 parent 117df28
@brunobord authored
View
4 admin.py
@@ -3,11 +3,11 @@
class ZipcodeAdmin(admin.ModelAdmin):
- pass
+ list_display = ('city', 'longitude', 'latitude')
class SnowflakeAdmin(admin.ModelAdmin):
- pass
+ list_display = ('pk', 'tweet_id', 'zipcode', 'rank')
admin.site.register(Zipcode, ZipcodeAdmin)
admin.site.register(Snowflake, SnowflakeAdmin)
View
37 flake.py
@@ -1,25 +1,19 @@
-import re
import json
+from neigefr.helpers import find_zipcode, parse_body
from neigefr.models import Snowflake, Zipcode
from neigefr.geo import get_geo
-ZIPCODE = re.compile('(F-)?([0-9]{5})', re.IGNORECASE)
-RANKING = re.compile('([0-9])/10')
-
-
-class Flake(object):
- zipcode = None
- ranking = None
-
def process(data):
"Process JSON data"
flake = parse_body(data['text'])
if not flake:
return None
- if Zipcode.objects.filter(zipcode=flake.zipcode).exists():
- zipcode = Zipcode.objects.get(zipcode=flake.zipcode)
- else:
+ if not flake.zipcode:
+ return None
+
+ zipcode = find_zipcode(flake.zipcode)
+ if not zipcode:
longitude, latitude, city = get_geo(flake.zipcode)
if city:
zipcode = Zipcode.objects.create(
@@ -28,6 +22,9 @@ def process(data):
longitude=longitude,
latitude=latitude
)
+ else:
+ print "Zipcode pas trouve", flake.zipcode
+ return
snowflake = Snowflake.objects.create(
tweet_id=data['id'],
tweet=json.dumps(data),
@@ -37,19 +34,3 @@ def process(data):
zipcode=zipcode,
)
return snowflake
-
-
-def parse_body(body):
- flake = Flake()
- if '#neigefr' not in body:
- return None
-
- matcher = ZIPCODE.search(body)
- if matcher:
- flake.zipcode = matcher.group(2)
-
- matcher = RANKING.search(body)
- if matcher:
- flake.ranking = int(matcher.group(1))
-
- return flake
View
10 geo.py
@@ -2,13 +2,19 @@
import json
import requests
+SPECIAL_CASES = {'75000': 'Paris'}
+
def get_geo(zipcode):
- url = "http://maps.google.com/maps/geo?q="+zipcode+",France&output=json&sensor=false&key="+settings.GOOGLE_MAPS_KEY
+ searched_code = zipcode
+ if zipcode in SPECIAL_CASES:
+ searched_code = SPECIAL_CASES[zipcode]
+ url = "http://maps.google.com/maps/geo?q=%s,+France&output=json&sensor=false&key=%s" % (searched_code, settings.GOOGLE_MAPS_KEY)
location = get_from_geo(url)
+ # print json.dumps(location, indent=3)
status = location["Status"]["code"]
if status != 200:
- return None, None
+ return None, None, None
place = location["Placemark"][0]
country_code = place["AddressDetails"]["Country"]["CountryNameCode"]
if country_code != "FR":
View
31 helpers.py
@@ -0,0 +1,31 @@
+import re
+from neigefr.models import Zipcode
+
+
+ZIPCODE = re.compile('(F-)?([0-9]{5})', re.IGNORECASE)
+RANKING = re.compile('([0-9])/10')
+
+
+class Flake(object):

A namedtuple is probably a good fit here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ zipcode = None
+ ranking = None
+
+
+def find_zipcode(zipcode):
+ "Find a Zipcode object. Or None"
+ if Zipcode.objects.filter(zipcode=zipcode).exists():
+ return Zipcode.objects.get(zipcode=zipcode)
+
+
+def parse_body(body):
+ "Parse the tweet body. Return a Flake object"
+ flake = Flake()
+ if '#neigefr' not in body:
+ return None
+ matcher = ZIPCODE.search(body)
+ if matcher:
+ flake.zipcode = matcher.group(2)
+ matcher = RANKING.search(body)
+ if matcher:
+ flake.ranking = int(matcher.group(1))
+ return flake
View
16 management/commands/search.py
@@ -2,21 +2,25 @@
import requests
from django.core.management.base import NoArgsCommand
from neigefr.flake import process
-from neigefr.models import Zipcode, Snowflake
+from neigefr.models import Snowflake
+import json
-logger = logging.getLogger(__name__)
+logger = logging.getLogger('neigefr')
# https://twitter.com/#!/search/%40twitterapi
+
class Command(NoArgsCommand):
help = "Is searching for tweets with #neigefr and store them in DB"
def handle(self, **options):
url = 'http://search.twitter.com/search.json?q=%23neigefr'
-
- last_snowflakes = Snowflake.objects.order_by('-twitter_id')
+ logger.critical(url)
+ last_snowflakes = Snowflake.objects.order_by('-tweet_id')
if last_snowflakes.count():
last = last_snowflakes[0]
- url = "%s&since_id=%d" % (url, last.twitter_id)
+ url = "%s&since_id=%d" % (url, last.tweet_id)
data = requests.get(url)
- print data.text
+ json_data = json.loads(data.text)
+ for tweet in json_data['results']:
+ process(tweet)
View
2  models.py
@@ -16,7 +16,7 @@ class Snowflake(models.Model):
tweet = models.TextField("raw tweet")
latitude = models.CharField('latitude', max_length=100)
longitude = models.CharField('longitude', max_length=100)
- rank = models.IntegerField('rank')
+ rank = models.IntegerField('rank', blank=True, null=True)
zipcode = models.ForeignKey(Zipcode, blank=True)
class Meta:
@davidbgk

A namedtuple is probably a good fit here.

Please sign in to comment.
Something went wrong with that request. Please try again.