def get_costs(args): try: center = Location.get(Location.name == args.center) except pw.DoesNotExist: print('Location by the name {} is not known'.format(args.center)) return locations = [ util.random_location_in_circle(center, args.radius) for i in range(2 * args.samples) ] trips = zip(*[iter(locations)] * 2) for start, end in trips: cost_json = lyft.get_cost(ride_type='lyft', **start.as_start(), **end.as_end()) cost = Cost.create(**cost_json.__dict__['__data__']) start = Location.create(**start.__dict__['__data__']) end = Location.create(**end.__dict__['__data__']) CostEstimate.create(cost=cost, start_location=start, end_location=end, time=datetime.now())
def create(self, facebook): me = facebook.api(u'/me', {u'fields': USER_FIELDS}) friends = [user[u'id'] for user in me[u'friends'][u'data']] location = me.get(u'location') hometown = me.get(u'hometown') if location: location_id = int(location[u'id']) elif hometown: location_id = int(hometown[u'id']) else: location_id = 0 location_id = str(location_id) if location_id != "0" and not Location.get_by_key_name(location_id): Location.create(location_id, facebook) user = User( key_name=facebook.user_id, user_id=facebook.user_id, dirty=False, name=me.get(u'name'), first_name=me.get(u'first_name'), last_name=me.get(u'last_name'), gender=me.get(u'gender'), username=me.get(u'username'), email=me.get(u'email'), picture=me.get(u'picture'), link=me.get(u'link'), friends=friends, location_id=location_id, access_token=facebook.access_token, ) user.put() return user
def add_location(args): try: Location.create(name=args.name, lat=args.lat, lng=args.lng) except pw.IntegrityError: print('Location by that name already exists')