def approve_pending_friends(api=None): if not api: api = FSQ(HEADERS) p = api.get_pending_friends(_USER, _PASS) if not p or 'requests' not in p: return friends = p['requests'] for f in friends: uid = f['id'] resp = api.approve_friend(_USER, _PASS, uid) if friends: _log.warn("Approved %s 4sq friends:\n%s", len(friends), friends) return friends
def do_foursquare_venues(dt=None): """Collect foursquare checkin stats for each venue that has events happening today.""" from fsq import FoursquareApi from rdutils.mathutils import farey from event.models import Event, Venue, FoursquareTrend etz = tz.tzstr('EST5EDT') # Eastern timezone headers = { 'User-Agent': getattr(settings, 'FOURSQUARE_USER_AGENT', settings.USER_AGENT) } fsq = FoursquareApi(headers) dt = dt or date.today() # Active venues events = Event.objects.filter(is_approved=True, event_date=dt).order_by( 'event_date', 'event_start_time', 'pk') venue_ids = set() venues = [] for ev in events: if ev.venue.pk not in venue_ids: venues.append(ev.venue) venue_ids.add(ev.venue.pk) previous_venues = Venue.objects.exclude(pk__in=list(venue_ids)).filter( fsq_checkins__gt=0) previous_venues.update(fsq_checkins=0, fsq_ratio=u'', fsq_mf=0, fsq_fm=0, fsq_m=0, fsq_f=0) # Get and populate FSQ ids of active venues where fsq_id was not previously obtained for v in venues: if not v.fsq_id and v.geo_loc and ',' in v.geo_loc: keyx = shorten_key(u"venue_id_4sq:%s" % v.pk) vx = cache.cache.get(keyx, None) if vx is None: lat, lng = v.geo_loc.split(',') venue_name = v.name.encode("utf-8", "ignore") _log.debug("Getting 4sq venue ID for %s", venue_name) sleep(.25) vx = fsq.get_venues(geolat=lat, geolong=lng, q=venue_name, l=1) try: vid = vx['groups'][0]['venues'][0]['id'] v.fsq_id = unicode(vid) v.save() except Exception, e: vid = 0 _log.debug( "FSQ ID for venue %s could not be obtained: %s\n%s", v.pk, e, vx) cache.cache.set(keyx, vid, 7200) # cache for 2 hours
def do_foursquare_venues(dt=None): """Collect foursquare checkin stats for each venue that has events happening today.""" from fsq import FoursquareApi from rdutils.mathutils import farey from event.models import Event, Venue, FoursquareTrend etz = tz.tzstr('EST5EDT') # Eastern timezone headers = { 'User-Agent': getattr(settings, 'FOURSQUARE_USER_AGENT', settings.USER_AGENT) } fsq = FoursquareApi(headers) dt = dt or date.today() # Active venues events = Event.objects.filter(is_approved=True, event_date=dt).order_by('event_date', 'event_start_time', 'pk') venue_ids = set() venues = [] for ev in events: if ev.venue.pk not in venue_ids: venues.append(ev.venue) venue_ids.add(ev.venue.pk) previous_venues = Venue.objects.exclude(pk__in=list(venue_ids)).filter(fsq_checkins__gt=0) previous_venues.update(fsq_checkins=0, fsq_ratio=u'', fsq_mf=0, fsq_fm=0, fsq_m=0, fsq_f=0) # Get and populate FSQ ids of active venues where fsq_id was not previously obtained for v in venues: if not v.fsq_id and v.geo_loc and ',' in v.geo_loc: keyx = shorten_key(u"venue_id_4sq:%s" % v.pk) vx = cache.cache.get(keyx, None) if vx is None: lat, lng = v.geo_loc.split(',') venue_name = v.name.encode("utf-8", "ignore") _log.debug("Getting 4sq venue ID for %s", venue_name) sleep(.25) vx = fsq.get_venues(geolat=lat, geolong=lng, q=venue_name, l=1) try: vid = vx['groups'][0]['venues'][0]['id'] v.fsq_id = unicode(vid) v.save() except Exception, e: vid = 0 _log.debug("FSQ ID for venue %s could not be obtained: %s\n%s", v.pk, e, vx) cache.cache.set(keyx, vid, 7200) # cache for 2 hours
def connect_uids(*args, **kwargs): """Connect foursquare userids to our user profiles""" api = FSQ(HEADERS) approve_pending_friends(api) resp = api.get_friends(_USER, _PASS) _log.debug("4sq friends resp:\n%s", resp) if 'friends' not in resp: return friends = resp['friends'] n = 0 connected = 0 uid = [] email = [] twitter = [] fb = [] uid_map = {} for f in friends: uidx = unicode(f['id']) uid.append(uidx) emx = f.get('email', None) fbx = f.get('facebook', None) twx = f.get('twitter', None) if emx: emx = unicode(emx.lower()) email.append(emx) uid_map[u'email-%s' % emx] = uidx if fbx: fbx = unicode(fbx) fb.append(fbx) uid_map[u'fb-%s' % fbx] = uidx if twx: twx = twx.lower() twitter.append(twx) uid_map[u'tw-%s' % twx] = uidx uids_done = {} q = UserProfile.objects.active().filter(fsq_userid=u'') # only include profiles not yet connected to 4sq for email_chunk in chunks(email, 50): if not email_chunk: break qx = q.filter(user__email__in=email_chunk) for up in qx: key = u'email-%s' % up.user.email uid = uid_map.get(key, None) if uid and not uid in uids_done: up.fsq_userid = uid up.save() uids_done[uid] = up for fb_chunk in chunks(fb, 50): if not fb_chunk: break qx = q.filter(fb_userid__in=fb_chunk) for up in qx: key = u'fb-%s' % up.fb_userid uid = uid_map.get(key, None) if uid and not uid in uids_done: up.fsq_userid = uid up.save() uids_done[uid] = up q = TwitterProfile.objects.active().filter(user_profile__fsq_userid=u'') for tw_chunk in chunks(twitter, 50): if not tw_chunk: break qx = q.filter(screen_name_lower__in=tw_chunk) for t in qx: up = t.user_profile key = u'tw-%s' % t.screen_name_lower uid = uid_map.get(key, None) if uid and not uid in uids_done: up.fsq_userid = uid up.save() uids_done[uid] = up checkin_uids = uids_done.keys() # connect user_profiles to past checkins EventCheckin.objects.connect_profiles_to_checkins(checkin_uids, uids_done) if uids_done: _log.warn("Foursquare IDs connected (%s):\n%s", len(uids_done), uids_done)