Permalink
Browse files

partial db implementation

  • Loading branch information...
1 parent 55f7ccb commit 0500e604a1e109ba88448562bc68424101fe2351 @bradmann committed Apr 27, 2012
Showing with 53 additions and 19 deletions.
  1. +34 −12 muggedapp/api/mugapi.py
  2. +18 −6 muggedapp/models.py
  3. +1 −1 muggedapp/templates/mugshot.html
@@ -1,3 +1,5 @@
+from muggedapp.models import FacebookUser, MugshotSearch, MugshotSearchResult
+
import datetime
import requests
import re
@@ -10,29 +12,49 @@ def get_age(birthday):
delta = datetime.datetime.now() - birthdate
return int(delta.days / 365.25)
-def run_search(request, fbuser):
- first_name = fbuser['first_name']
- last_name = fbuser['last_name']
- birthday = fbuser.get('birthday', None)
- gender = fbuser.get('gender', None)
- request_params = {'fname': first_name, 'lname': last_name, 'fpartial': 'True'}
- gender_map = {'male': 'M', 'female': 'F'}
- request_params['sex'] = gender_map.get(gender, 'M')
+def check_db(id):
+ dbUser = FacebookUser.objects.get(fbid=id)
+ if dbUser:
+ dbSearch = MugshotSearch.objects.get(fbuser=dbUser.fbid)
+ if dbSearch.last_searched > (datetime.datetime.now() - datetime.timedelta(1)):
+ dbResults = MugshotSearchResult.object.filter(search=dbSearch.id)
+ return [(imgpath, arrestpath) for imgpath, arrestpath in dbResults]
+ else:
+ return None
+
+def scrape_mugshot(first_name, last_name, birthday, gender):
+ request_params = {'fname': first_name, 'lname': last_name, 'fpartial': 'True', 'sex': gender}
if birthday and len(birthday.split('/')) == 3:
age = get_age(birthday)
request_params.update({'minage': age, 'maxage': age})
req = requests.get(base_uri, params=request_params)
page = req.content
- paths = re.findall("<img src='(/thumbs/[^']*)'", page)
- if len(paths) == 0:
+ imgpaths = re.findall("<img src='(/thumbs/[^']*)'", page)
+ arrestpaths = re.findall("<a href='(/Arrests/[^']*)'", page)
+ if len(imgpaths) == 0:
return None
+ paths = zip(imgpaths, arrestpaths)
pagenum = 1
patharr = paths[:]
while len(paths) == 42:
request_params['page'] = pagenum
req = requests.get(base_uri, params=request_params)
page = req.content
- paths = re.findall("<img src='(/thumbs/[^']*)'", page)
+ imgpaths = re.findall("<img src='(/thumbs/[^']*)'", page)
+ arrestpaths = re.findall("<a href='(/Arrests/[^']*)'", page)
+ paths = zip(imgpaths, arrestpaths)
patharr.extend(paths)
pagenum += 1
- return patharr
+ return patharr
+
+def run_search(request, fbuser):
+ id = fbuser['id']
+ first_name = fbuser['first_name']
+ last_name = fbuser['last_name']
+ birthday = fbuser.get('birthday', None)
+ gender_map = {'male': 'M', 'female': 'F'}
+ gender = gender_map.get(fbuser.get('gender', None), None)
+ results = check_db(id)
+ if results:
+ return results
+ return scrape_mugshot(first_name, last_name, birthday, gender)
View
@@ -1,15 +1,27 @@
from django.db import models
class FacebookUser(models.Model):
- fbid = models.CharField(max_length=255)
- name = models.CharField(max_length=255)
- avatar_url = models.URLField(max_length=1023)
- last_searched = models.DateTimeField()
+ fbid = models.CharField(primary_key=True, max_length=255)
def __unicode__(self):
- return self.name
-
+ return self.fbid
+class MugshotSearch(models.Model):
+ fbuser = models.ForeignKey('FacebookUser')
+ fname = models.CharField(max_length=255)
+ lname = models.CharField(max_length=255)
+ birthdate = models.CharField(max_length=20)
+ gender = models.CharField(max_length=1)
+ last_searched = models.DateTimeField()
+
+ class Meta:
+ unique_together = ('fname', 'lname', 'birthdate', 'gender')
+
+class MugshotSearchResult(models.Model):
+ search = models.ForeignKey('MugshotSearch')
+ thumbpath = models.URLField(max_length=1023)
+ arrestpath = models.URLField(max_length=1023)
+
class Arrest(models.Model):
fbuser = models.ForeignKey(FacebookUser)
name = models.CharField(max_length=255)
@@ -2,7 +2,7 @@
<h2><img src="https://graph.facebook.com/{{id}}/picture"/> {{name}}</h2>
<div class="shots">
{% for path in patharr %}
- <img src="http://florida.arrests.org{{path}}" class="mugshot"/>
+ <img src="http://florida.arrests.org{{path.0}}" class="mugshot"/>
{% endfor %}
</div>
</div>

0 comments on commit 0500e60

Please sign in to comment.