예제 #1
0
    def get_id_groups(self):
        """

        (None) -> list

        Function gets list of id groups of user VK: list(int, int, ..., int)

        """
        if self.groups is not None:
            value = (0, "", self.groups)
            return value

        params_here = vk_common.params.copy()
        params_here["user_id"] = self.uid
        params_here["count"] = 1000

        error, msg, response = vk_common.request_get(
            "https://api.vk.com/method/groups.get", params_here,
            vk_common.func_name())
        if error != 0:
            value = (error, msg, None)
            return value

        error, msg, count, items = vk_common.get_count_items_by_response(
            response)
        if error != 0:
            value = (error, msg, None)
            return value

        self.count_groups = count
        self.groups = items

        value = (0, "", self.groups)
        return value
예제 #2
0
    def get_id_friends(self):
        """

        (None) -> tuple(int, string, list or None)

        Function gets list of id friends of user VK: list(int, int, ..., int)

        """

        if self.friends is not None:
            value = (0, "", self.friends)
            return value

        params_here = vk_common.params.copy()
        params_here["user_id"] = self.uid

        error, msg, response = vk_common.request_get(
            "https://api.vk.com/method/friends.get", params_here,
            vk_common.func_name())
        if error != 0:
            value = (error, msg, None)
            return value

        error, msg, count, items = vk_common.get_count_items_by_response(
            response)
        if error != 0:
            value = (error, msg, None)
            return value

        self.count_friends = count
        self.friends = items

        value = (0, "", self.friends)
        return value
예제 #3
0
    def get_info(self, nickname):
        """

        (string) -> (int, string, None)

        Function gets information about user through api vk by nickname of user

        """

        params_here = vk_common.params.copy()
        params_here["user_ids"] = nickname
        params_here["fields"] = vk_common.FIELDS_USER_INFO
        error, msg, response = vk_common.request_get(
            "https://api.vk.com/method/users.get", params_here,
            vk_common.func_name())
        if error != 0:
            value = (error, msg, None)
            return value

        error, msg, info_json_list = vk_common.get_field_of_response(
            response.json(), "response")
        if error != 0:
            value = (error, msg, None)
            return value

        if len(info_json_list) <= 0:
            value = (-1, "Пустой ответ", None)
            return value

        info_json = info_json_list[0]
        error, msg, deactivated = vk_common.get_field_of_response(
            info_json, "deactivated")
        if error == 0 and (deactivated == "DELETED"
                           or deactivated == "BANNED"):
            value = (error, msg, None)
            return value

        # Get values of information fields of user
        error, msg, fields = vk_common.get_fields_of_response(
            info_json, vk_common.FIELDS_USER_INFO)
        if error != 0:
            value = (error, msg, None)
            return value

        # id,first_name,last_name,bdate,deactivated,is_closed,interests,books,music,sex,city
        self.uid = fields.get('id')
        self.first_name = get_attr_value(fields, 'first_name')
        self.last_name = get_attr_value(fields, 'last_name')
        self.bdate = get_attr_value(fields, 'bdate')
        self.age = vk_common.get_age(self.bdate)
        self.interests = get_attr_value(fields, 'interests')
        self.books = get_attr_value(fields, 'books')
        self.music = get_attr_value(fields, 'music')
        self.sex = get_attr_value(fields, 'sex')
        self.city = get_attr_value(fields, 'city')

        return 0, "", self.uid
예제 #4
0
    def get_id_friends(self):
        """

        (None) -> tuple(int, string, list or None)

        Function gets list of id friends of user VK: list(int, int, ..., int)

        """
        if self.users is None:
            return -1

        params_here = dict()
        params_here["count"] = 1000
        params_code = vk_common.params.copy()
        start = 0
        while True:
            if start > len(self.users):
                break

            script = ""
            user_idx = dict()
            for number, user in enumerate(
                    self.users[start:start +
                               vk_common.COUNT_REQUESTS_EXECUTE:1]):
                if user.friends is not None:
                    continue

                params_here["user_id"] = user.uid
                script += f"API.friends.get({params_here}),"
                user_idx[number] = user

            params_code["code"] = f"return [{script}];"
            error, msg, response = vk_common.request_get(
                'https://api.vk.com/method/execute', params_code,
                vk_common.func_name())
            if error != 0:
                value = (error, msg, None)
                return value
            response_json = response.json()
            response_json_ = response_json["response"]
            for number, response_item in enumerate(response_json_):
                user = user_idx[number]
                if response_item:
                    count = response_item["count"]
                    items = response_item["items"]

                    user.count_friends = count
                    user.friends = items
                else:
                    user.count_friends = 0
                    user.friends = None

            start = start + vk_common.COUNT_REQUESTS_EXECUTE

        return 0, "", None
예제 #5
0
    def get_name(self):
        """

        (None) -> str

        Function gets name of group VK

        """

        if 'self.group_name' in locals():
            value = (0, "", self.group_name)
            return value

        params_here = vk_common.params.copy()
        params_here["group_ids"] = self.group_id
        params_here["fields"] = "name,members_count"

        error, msg, response = vk_common.request_get(
            'https://api.vk.com/method/groups.getById', params_here,
            vk_common.func_name())
        if error != 0:
            value = (error, msg, None)
            return value

        response_json = response.json()
        error, msg, info_json_list = vk_common.get_field_of_response(
            response_json, 'response')
        if error != 0:
            value = (error, msg, None)
            return value

        info_json = info_json_list[0]

        error, msg, self.group_name = vk_common.get_field_of_response(
            info_json, 'name')
        if error != 0:
            value = (error, msg, None)
            return value

        error, msg, self.members_count = vk_common.get_field_of_response(
            info_json, 'members_count')
        if error != 0:
            value = (error, msg, None)
            return value

        value = (0, "", self.group_name)
        return value
예제 #6
0
def search_by(search_info):
    """

    (dict) -> (integer, string, list of objects or None)

    Function executes search users by search parameters

    """

    params_here = vk_common.params.copy()
    params_here.update(search_info)
    vk_common.dprint(2, vk_common.func_name(), "Параметры поиска: ",
                     params_here)
    value = vk_common.request_get("https://api.vk.com/method/users.search",
                                  params_here, vk_common.func_name())
    vk_common.dprint(2, vk_common.func_name(), "Результат поиска: ", value)
    return value
예제 #7
0
def get_groups_with_common_friends(id_groups_user, count_friends_limit):
    # Get groups which have <=N common friends
    params_here = dict()
    params_here["count"] = count_friends_limit
    params_here["filter"] = "friends"

    params_code = vk_common.params.copy()

    start = 0
    groups_common_friends_n = list()
    while True:
        if start > len(id_groups_user):
            break

        script = ""
        for group in id_groups_user[start:start +
                                    vk_common.COUNT_REQUESTS_EXECUTE:1]:
            params_here["group_id"] = group
            script += f"API.groups.getMembers({params_here}),"

        params_code["code"] = f"return [{script}];"
        error, msg, response = vk_common.request_get(
            'https://api.vk.com/method/execute', params_code,
            vk_common.func_name())
        if error != 0:
            value = (error, msg, None)
            return value

        response_json = response.json()
        response_json_ = response_json["response"]
        for group_id, response_item in zip(id_groups_user, response_json_):
            # print(group_id, response_item)
            if not response_item:
                continue

            # print(group_id, response_item)
            count = response_item["count"]
            # Accumulate id groups which have <=N=count_friends_limit common friends
            if count in range(1, count_friends_limit + 1):
                groups_common_friends_n.append(group_id)

        start = start + vk_common.COUNT_REQUESTS_EXECUTE

    value = (0, "", groups_common_friends_n)
    return value
예제 #8
0
    def get_top_photos(self, top_count):
        """

        (None) -> (int, string, int)

        Function gets photos of user

        """

        params_here = vk_common.params.copy()
        params_here["owner_id"] = self.uid
        params_here["album_id"] = 'profile'
        params_here["rev"] = 1
        params_here["extended"] = 1
        params_here["photo_sizes"] = 1

        error, msg, response = vk_common.request_get(
            "https://api.vk.com/method/photos.get", params_here,
            vk_common.func_name())
        if error != 0:
            if error == 30:
                value = (0, "", (None, None, None))
            else:
                value = (error, msg, None)
            return value

        error, msg, count, items = vk_common.get_count_items_by_response(
            response)
        if error != 0:
            value = (error, msg, None)
            return value

        if len(items) == 0:
            value = (0, "Нет фотографий в профиле", (None, None, None))
            return value

        url_dict = {}
        for item in items:
            vk_common.dprint(2, item)
            # Select max size
            sizes = item['sizes']
            max_size_elem = None
            max_size_w = 0
            max_size_h = 0
            for picture in sizes:
                if picture['width'] > vk_common.MAX_IMAGE_WIDTH or picture[
                        'height'] > vk_common.MAX_IMAGE_HEIGHT:
                    continue
                if picture['width'] >= max_size_w and picture[
                        'height'] >= max_size_h:
                    max_size_w = picture['width']
                    max_size_h = picture['height']
                    max_size_elem = picture
            item['sizes'] = max_size_elem
            url_dict[(item['id'],
                      max_size_elem['url'])] = item['likes']['count']

            vk_common.dprint(
                2,
                f"Выбрали фото с максимальным размером: width = {max_size_w} height = {max_size_h} "
                f"url = {max_size_elem['url']} owner_id = {item['owner_id']} "
                f"id = {item['id']} likes_count = {item['likes']['count']}")

        # Sort dict by values
        list_d = list(url_dict.items())
        vk_common.dprint(2, "До сортировки картинок по лайкам", list_d)
        list_d.sort(reverse=True, key=lambda i: i[1])
        vk_common.dprint(2, "После сортировки картинок по лайкам", list_d)

        top_url = []
        for item in list_d[:top_count:1]:
            top_url.append(item[0])

        vk_common.dprint(2, "В итоге получили список url: ", top_url)
        value = (0, "", top_url)
        return value