Created
August 24, 2010 09:14
-
-
Save alcides/547245 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import datetime | |
import foursquare | |
from orm.models import * | |
from utils import geo | |
username = os.environ.get('FS_USER','') | |
passw = os.environ.get('FS_PASS','') | |
fs = foursquare.Foursquare(foursquare.BasicCredentials(username, passw)) | |
friends = {} | |
for f in fs.friends()['friends']: | |
id = f['id'] | |
friends[id] = f | |
def insert_check(ck, user=False): | |
if 'venue' not in ck: | |
return False | |
u = user or ck['user'] | |
place = ck['venue'] | |
name = "" | |
id = u['id'] | |
if id in friends: | |
if 'twitter' in friends[id]: | |
name = friends[id]['twitter'] | |
if not name: | |
name = "%s %s" % (u['firstname'], u['lastname']) | |
user, gb = User.objects.get_or_create(name = name) | |
user.location = place['city'] | |
if 'gender' in u: | |
user.gender = u['gender'] | |
user.photo = u['photo'] | |
user.save() | |
time_s = ck['created'].split("+")[0].strip() | |
time = datetime.datetime.strptime(time_s, '%a, %d %b %y %H:%M:%S') | |
url = "http://4sq.com/%s/checking/%s" % (name, ck['id']) | |
if Position.objects.filter(url=url).count() == 0: | |
tw = Position.objects.create( | |
user=user, time=time, | |
name=place['name'], | |
url=url, source="4SQ" | |
) | |
if 'shout' in ck: | |
tw.tweet = ck['shout'] | |
tw.place = "%s, %s, %s" % ( place['address'], place['state'], | |
place['city']) | |
tw.latitude, tw.longitude = place['geolat'], place['geolong'] | |
tw.save() | |
return True | |
else: | |
return False | |
venues = [] | |
for ck in fs.checkins()[u'checkins']: | |
insert_check(ck) | |
if 'venue' in ck: | |
venues.append(ck['venue']['id']) | |
sids = [] | |
for fid in friends: | |
f = fs.friends(uid=fid)['friends'] | |
for u in f: | |
id = u['id'] | |
if id not in friends and id not in sids: | |
sids.append(id) | |
for id in sids: | |
u = fs.user(uid=id)['user'] | |
if 'checkin' in u: | |
insert_check(u['checkin'], user=u) | |
if 'venue' in u['checkin']: | |
venues.append(u['checkin']['venue']['id']) | |
for v in fs.venues(l=50, geolat="38.740", geolong="-9.1354"): | |
if v['id'] not in venues: | |
venues.append(v['id']) | |
for vid in venues: | |
v = fs.venue(vid=vid)['venue'] | |
if 'checkins' not in v: | |
continue | |
for ck in v['checkins']: | |
insert_check(ck) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment