如何在Python中使用VKontakte中的偏移量?

时间:2015-02-11 14:17:30

标签: python offset checkin vk

我正在尝试构建一个脚本,我可以在其中获取特定位置的签到。出于某种原因,当我指定lat时,长坐标VK永远不会返回任何签到,所以我必须首先获取位置ID,然后从该列表请求签入。但是我不确定如何使用偏移功能,我认为它应该像分页功能一样工作。

到目前为止,我有这个:

import vk
import json


app_id =   #enter app id 
login_nr =    #enter your login phone or email
password = ''   #enter password  
vkapi = vk.API(app_id, login_nr, password)

vkapi.getServerTime()

def get_places(lat, lon, rad):

    name_list = []
    try:
        locations = vkapi.places.search(latitude=lat, longitude=lon, radius=rad)
        name_list.append(locations['items'])
    except Exception, e:
        print '*********------------ ERROR ------------*********'
        print str(e)

    return name_list


 # Returns last checkins up to a maximum of 100
 # Define the number of checkins you want, 100 being maximum

def get_checkins_id(place_id,check_count):

    checkin_list= []

    try:
        checkins = vkapi.places.getCheckins(place = place_id, count = check_count)
        checkin_list.append(checkins['items'])
    except Exception, e:
        print '*********------------ ERROR ------------*********'
        print str(e)

    return checkin_list

我最终要做的是将两者合并为一个函数,但在此之前我必须弄清楚偏移是如何工作的,当前的VK API文档并没有解释得那么好。我希望代码能够读取类似的内容:

def get_users_list_geo(lat, lon, rad, count):
    users_list = []
    locations_lists = []
    users = []

    locations = vkapi.places.search(latitude=lat, longitude=lon, radius=rad)

    for i in locations[0]:
        locations_list.append(i['id'])

    for i in locations:
        # Get each location ID 
        # Get Checkins for location
        # Append checkin and ID to the list

根据我的理解,我必须在办理登机手续时计算抵消额,然后以某种方式计算超过100次办理登机手续的地点。无论如何,我会非常感谢任何类型的帮助,建议或任何事情。如果您对脚本有任何建议,我也很乐意听到它们。我正在自学Python,所以到目前为止我并不是很好。

谢谢!

2 个答案:

答案 0 :(得分:1)

我使用javascript使用VK API,但我认为,逻辑是一样的。

TL; DR:Offset是一些结果(从第一个开始),应该跳过哪个API

例如,您进行查询,该结果应返回1000个结果(假设您知道确切的结果数)。 但是VK每次请求只返回100。那么,如何获得其他900?

你对API说:给我next 100个结果。下一步是偏移 - 您想要跳过的结果数,因为您已经处理过它们。因此,VK API需要1000个结果,先跳过100,然后返回给你(第二个)100。

答案 1 :(得分:0)

此外,如果您在第一段中讨论此方法(http://vk.com/dev/places.getCheckins),请检查您的lat / long是浮点数,而不是整数。尝试交换lat / long可能很有用 - 也许你把它们混淆了?