Пример #1
0
    def get_media_likes(self, url, token_id):
        likes_data = {}

        url = "%s%s" (self.API_PARTS['oembed'], url)
        post_meta = request_helper.make_request(url, True)
        if 'media_id' in post_meta:
            soc_token = SocToken.query.get(token_id)
            request_url = "%s%s?access_token=%s" % (
                self.API_PATH, self.API_PARTS['media_likes'] %
                (post_meta['media_id']), user_token)

            likes_data = request_helper.make_request(request_url, True)

        return likes_data
Пример #2
0
    def get_media_likes(self, url, token_id):
        likes_data = {}

        url = "%s%s" (self.API_PARTS['oembed'], url)
        post_meta = request_helper.make_request(url, True)
        if 'media_id' in post_meta:
            soc_token = SocToken.query.get(token_id)
            request_url = "%s%s?access_token=%s" % (
                self.API_PATH,
                self.API_PARTS['media_likes'] % (post_meta['media_id']),
                user_token)

            likes_data = request_helper.make_request(request_url, True)

        return likes_data
Пример #3
0
    def likes_control_value(self, condition_id):
        answer = False

        condition = PaymentLoyaltySharing.query.get(condition_id)
        if not condition:
            return False

        data = json.loads(condition.data)
        if not ('owner_id' in data and 'type' in data and 'item_id' in data):
            return False

        api_url = self.API_PATH \
            + 'likes.getList' \
            + '?type=' \
            + str(data['type']) \
            + '&owner_id=' \
            + str(data['owner_id']) \
            + '&item_id=' \
            + str(data['item_id']) \
            + '&count=1'

        likes = request_helper.make_request(api_url, True)

        if not('response' in likes and 'count' in likes['response'] and 'users' in likes['response']):
            return False

        answer = int(likes['response']['count'])

        return answer
Пример #4
0
    def get_page(self, url, token_id, parse_json):
        socToken = SocToken.query.get(token_id)
        username = FacebookApi.parse_username(url)
        urlAPI = self.API_PATH + username + \
            '?access_token=' + socToken.user_token

        return request_helper.make_request(urlAPI, parse_json)
Пример #5
0
    def get_page(self, url, token_id, parse_json):
        socToken = SocToken.query.get(token_id)
        username = FacebookApi.parse_username(url)
        urlAPI = self.API_PATH + username + \
            '?access_token=' + socToken.user_token

        return request_helper.make_request(urlAPI, parse_json)
Пример #6
0
    def likes_control_value(self, condition_id):
        answer = False

        condition = PaymentLoyaltySharing.query.get(condition_id)
        if not condition:
            return False

        data = json.loads(condition.data)
        if not ('owner_id' in data and 'type' in data and 'item_id' in data):
            return False

        api_url = self.API_PATH \
            + 'likes.getList' \
            + '?type=' \
            + str(data['type']) \
            + '&owner_id=' \
            + str(data['owner_id']) \
            + '&item_id=' \
            + str(data['item_id']) \
            + '&count=1'

        likes = request_helper.make_request(api_url, True)

        if not ('response' in likes and 'count' in likes['response']
                and 'users' in likes['response']):
            return False

        answer = int(likes['response']['count'])

        return answer
Пример #7
0
    def check_subscription(self, url, token_id, sharing_id):
        """подпиcка на группу"""
        answer = self.CONDITION_ERROR

        condition = PaymentLoyaltySharing.query.get(sharing_id)
        if not condition:
            return self.CONDITION_ERROR

        soc_token = SocToken.query.get(token_id)
        if not soc_token:
            return self.CONDITION_FAILED

        api_url = self.API_PATH \
            + 'users.getSubscriptions?user_id=' \
            + soc_token.soc_id

        userSubs = request_helper.make_request(api_url, True)

        if not ('response' in userSubs and 'groups' in userSubs['response']):
            return self.CONDITION_ERROR

        answer = self.CONDITION_FAILED

        if not ('items' in userSubs['response']['groups']):
            return answer

        for item in userSubs['response']['groups']['items']:
            if str(item) == str(condition.data):
                answer = self.CONDITION_PASSED

        return answer
Пример #8
0
    def get_relation(self, url, token_id):
        relation = {}
        soc_token = SocToken.query.get(token_id)
        request_url = "%s%s%s&access_token=%s" % (
            self.API_PATH, self.API_PARTS['search_users'],
            request_helper.parse_get_param(
                url, self.API_PARTS['base']), soc_token.user_token)
        user = request_helper.make_request(request_url, True)
        if 'data' in user and len(
                user['data']) > 0 and 'id' in user['data'][0]:
            request_url = "%s%s?access_token=%s" % (
                self.API_PATH, self.API_PARTS['relationship'] %
                (user['data'][0]['id']), soc_token.user_token)
            relation = request_helper.make_request(request_url, True)

        return relation
Пример #9
0
    def check_subscription(self, url, token_id, sharing_id):
        """подпиcка на группу"""
        answer = self.CONDITION_ERROR

        condition = PaymentLoyaltySharing.query.get(sharing_id)
        if not condition:
            return self.CONDITION_ERROR

        soc_token = SocToken.query.get(token_id)
        if not soc_token:
            return self.CONDITION_FAILED

        api_url = self.API_PATH \
            + 'users.getSubscriptions?user_id=' \
            + soc_token.soc_id

        userSubs = request_helper.make_request(api_url, True)

        if not ('response' in userSubs and 'groups' in userSubs['response']):
            return self.CONDITION_ERROR

        answer = self.CONDITION_FAILED

        if not ('items' in userSubs['response']['groups']):
            return answer

        for item in userSubs['response']['groups']['items']:
            if str(item) == str(condition.data):
                answer = self.CONDITION_PASSED

        return answer
Пример #10
0
    def get_object_like(self, object_id, token_id, parse_json):
        socToken = SocToken.query.get(token_id)
        query = 'SELECT object_id,user_id FROM like WHERE user_id = me() and object_id = %s' % (
            object_id)

        return request_helper.make_request(
            self.FQL_PATH + query.replace(' ', '+') + '&access_token=' +
            socToken.user_token, parse_json)
Пример #11
0
    def get_external_like(self, url, token_id):
        socToken = SocToken.query.get(token_id)
        query = 'SELECT attachment ,created_time ,type ,description FROM stream WHERE source_id=me() and strpos(attachment.href,"%s")>=0 and strpos(attachment.href,"fb_action_types=og.likes") > 0' % (
            url)

        return request_helper.make_request(
            self.FQL_PATH + query.replace(' ', '+') + '&access_token=' +
            socToken.user_token, True)
Пример #12
0
    def get_sharing(self, url, token_id, parse_json):
        socToken = SocToken.query.get(token_id)
        query = 'SELECT attachment ,created_time ,type ,description FROM stream WHERE source_id=me() and actor_id=me() and type=80 and attachment.href="%s"' % (
            url)

        return request_helper.make_request(
            self.FQL_PATH + query.replace(' ', '+') + '&access_token=' +
            socToken.user_token, parse_json)
Пример #13
0
    def get_relation(self, url, token_id):
        relation = {}
        soc_token = SocToken.query.get(token_id)
        request_url = "%s%s%s&access_token=%s" % (
            self.API_PATH,
            self.API_PARTS['search_users'],
            request_helper.parse_get_param(url, self.API_PARTS['base']),
            soc_token.user_token
        )
        user = request_helper.make_request(request_url, True)
        if 'data' in user and len(user['data']) > 0 and 'id' in user['data'][0]:
            request_url = "%s%s?access_token=%s" % (
                self.API_PATH,
                self.API_PARTS['relationship'] % (user['data'][0]['id']),
                soc_token.user_token)
            relation = request_helper.make_request(request_url, True)

        return relation
Пример #14
0
    def get_app_token(self):
        """жетон приложения"""

        api_url = 'https://graph.facebook.com/' \
            + 'oauth/access_token?client_id=' \
            + SocConfig.FACEBOOK_ID \
            + '&client_secret=' \
            + SocConfig.FACEBOOK_SECRET \
            + '&grant_type=client_credentials'

        app_token = str(request_helper.make_request(api_url, False))
        app_token = request_helper.parse_get_param(app_token, 'access_token=')

        return app_token
Пример #15
0
    def get_app_token(self):
        """жетон приложения"""

        api_url = 'https://graph.facebook.com/' \
            + 'oauth/access_token?client_id=' \
            + SocConfig.FACEBOOK_ID \
            + '&client_secret=' \
            + SocConfig.FACEBOOK_SECRET \
            + '&grant_type=client_credentials'

        app_token = str(request_helper.make_request(api_url, False))
        app_token = request_helper.parse_get_param(app_token, 'access_token=')

        return app_token
Пример #16
0
    def check_views(self, url, token_id, loyalty_id):
        viewed = False
        self.refresh_token(token_id)
        soc_token = SocToken.query.get(token_id)

        url = "%s&access_token=%s" % (self.HISTORY_URL, soc_token.user_token)
        watch_history = request_helper.make_request(url, False)
        target_id = self.parse_video_id(url)

        watch_history = unicode(watch_history, 'latin1')
        if '/' + target_id in watch_history or self.API_PARTS['watch'] + target_id in watch_history:
            viewed = True

        return viewed
Пример #17
0
    def check_views(self, url, token_id, loyalty_id):
        viewed = False
        self.refresh_token(token_id)
        soc_token = SocToken.query.get(token_id)

        url = "%s&access_token=%s" % (self.HISTORY_URL, soc_token.user_token)
        watch_history = request_helper.make_request(url, False)
        target_id = self.parse_video_id(url)

        watch_history = unicode(watch_history, 'latin1')
        if '/' + target_id in watch_history or self.API_PARTS[
                'watch'] + target_id in watch_history:
            viewed = True

        return viewed
Пример #18
0
    def likes_count(self, url):
        object_id = False
        answer = -1
        if self.URLS_PARTS['base'] in url and (self.URLS_PARTS['photo'] in url or self.URLS_PARTS['posts'] in url):
            if self.URLS_PARTS['posts'] in url:
                # пост
                object_id = request_helper.parse_get_param(
                    url, self.URLS_PARTS['posts'])
            else:
                # фото
                object_id = request_helper.parse_get_param(
                    url, self.URLS_PARTS['photo'])
        elif self.URLS_PARTS['base'] in url:
            # страница
            object_id = request_helper.parse_get_param(
                url, self.URLS_PARTS['base'])

        if not object_id:
            return -1

        app_token = self.get_app_token()

        object_likes = request_helper.make_request(
            self.API_PATH
            + object_id
            + self.URLS_PARTS['object_likes']
            + '&access_token='
            + app_token, True)
        if not 'likes' in object_likes:
            return -1

        if type(object_likes['likes']) == type({}) \
                and 'summary' in object_likes['likes'] \
                and 'total_count' in object_likes['likes']['summary']:
            try:
                answer = int(object_likes['likes']['summary']['total_count'])
            except ValueError:
                return -1
        else:
            try:
                answer = int(object_likes['likes'])
            except ValueError:
                return -1

        return answer
Пример #19
0
    def check_in_circle(self, url, token_id, loyalty_id):
        in_circle = False
        self.refresh_token(token_id)

        soc_token = SocToken.query.get(token_id)
        username = self.parse_username(url)
        request_url = "%s%s%s?key=%s" % (
            self.API_PATH,
            self.API_PARTS['people_list'],
            username,
            SocConfig.GOOGLE_KEY)
        taget_user = request_helper.make_request(request_url, True)

        if 'id' in taget_user and taget_user['id']:
            user_id = taget_user['id']
            g = Grab()
            g.setup(headers={'Authorization': 'Bearer ' + soc_token.user_token})

            url_api = self.API_PATH + self.API_PARTS['peoples']

            while not in_circle:
                g.go(url_api)
                circle = json.loads(g.response.body)

                if 'items' not in circle:
                    break
                if len(circle['items']) <= 0:
                    break

                for friend in circle['items']:
                    if 'id' in friend and friend['id'] == user_id:
                        in_circle = True

                if 'nextPageToken' not in circle:
                    break
                if len(circle['nextPageToken']) <= 0:
                    break

                url_api = "%s%s&pageToken=%s" % (
                    self.API_PATH,
                    self.API_PARTS['peoples'],
                    circle['nextPageToken'])

        return in_circle
Пример #20
0
    def likes_count(self, url):
        object_id = False
        answer = -1
        if self.URLS_PARTS['base'] in url and (self.URLS_PARTS['photo'] in url
                                               or self.URLS_PARTS['posts']
                                               in url):
            if self.URLS_PARTS['posts'] in url:
                # пост
                object_id = request_helper.parse_get_param(
                    url, self.URLS_PARTS['posts'])
            else:
                # фото
                object_id = request_helper.parse_get_param(
                    url, self.URLS_PARTS['photo'])
        elif self.URLS_PARTS['base'] in url:
            # страница
            object_id = request_helper.parse_get_param(url,
                                                       self.URLS_PARTS['base'])

        if not object_id:
            return -1

        app_token = self.get_app_token()

        object_likes = request_helper.make_request(
            self.API_PATH + object_id + self.URLS_PARTS['object_likes'] +
            '&access_token=' + app_token, True)
        if not 'likes' in object_likes:
            return -1

        if type(object_likes['likes']) == type({}) \
                and 'summary' in object_likes['likes'] \
                and 'total_count' in object_likes['likes']['summary']:
            try:
                answer = int(object_likes['likes']['summary']['total_count'])
            except ValueError:
                return -1
        else:
            try:
                answer = int(object_likes['likes'])
            except ValueError:
                return -1

        return answer
Пример #21
0
    def subscription_control(self, condition_id):
        answer = False

        condition = PaymentLoyaltySharing.query.get(condition_id)
        if not condition:
            return False

        api_url = self.API_PATH \
            + 'groups.getById?group_id=' \
            + condition.data  \
            + '&fields=members_count'

        group = request_helper.make_request(api_url, True)

        if 'response' in group and len(group['response']) > 0 \
                and 'members_count' in group['response'][0]:
            answer = group['response'][0]['members_count']

        return answer
Пример #22
0
    def subscription_control(self, condition_id):
        answer = False

        condition = PaymentLoyaltySharing.query.get(condition_id)
        if not condition:
            return False

        api_url = self.API_PATH \
            + 'groups.getById?group_id=' \
            + condition.data  \
            + '&fields=members_count'

        group = request_helper.make_request(api_url, True)

        if 'response' in group and len(group['response']) > 0 \
                and 'members_count' in group['response'][0]:
            answer = group['response'][0]['members_count']

        return answer
Пример #23
0
    def check_like(self, url, token_id, sharing_id):
        """лайк объекта"""

        answer = self.CONDITION_ERROR
        condition = PaymentLoyaltySharing.query.get(sharing_id)
        if not condition:
            return self.CONDITION_FAILED

        data = json.loads(condition.data)
        if not ('owner_id' in data and 'type' in data and 'item_id' in data):
            return self.CONDITION_FAILED

        soc_token = SocToken.query.get(token_id)
        if not soc_token:
            return self.CONDITION_FAILED

        key = 'vk_likes_list_%s' % str(condition.id)

        if cache.get(key):
            likes_list = cache.get(key)
            answer = self.CONDITION_FAILED

            for user_id in likes_list:
                if str(user_id) == str(soc_token.soc_id):
                    answer = self.CONDITION_PASSED
                    break

            return answer

        offset = 0
        likes_count = self.MAX_LIKES_COUNT
        likes_list = []

        while not (offset > likes_count):

            api_url = self.API_PATH \
                + 'likes.getList' \
                + '?type=' \
                + str(data['type']) \
                + '&owner_id=' \
                + str(data['owner_id']) \
                + '&item_id=' \
                + str(data['item_id']) \
                + '&count=' \
                + str(self.MAX_LIKES_COUNT) \
                + '&offset=' \
                + str(offset)

            likes = request_helper.make_request(api_url, True)
            if not ('response' in likes and 'count' in likes['response']
                    and 'users' in likes['response']):
                return self.CONDITION_ERROR

            if answer == self.CONDITION_ERROR:
                answer = self.CONDITION_FAILED

            likes_count = int(likes['response']['count'])

            for user_id in likes['response']['users']:
                likes_list.append(user_id)
                if str(user_id) == str(soc_token.soc_id):
                    answer = self.CONDITION_PASSED

            offset += self.MAX_LIKES_COUNT

        cache.set(key, likes_list, 50)

        return answer
Пример #24
0
    def get_object_like(self, object_id, token_id, parse_json):
        socToken = SocToken.query.get(token_id)
        query = 'SELECT object_id,user_id FROM like WHERE user_id = me() and object_id = %s' % (
            object_id)

        return request_helper.make_request(self.FQL_PATH + query.replace(' ', '+') + '&access_token=' + socToken.user_token, parse_json)
Пример #25
0
    def get_external_like(self, url, token_id):
        socToken = SocToken.query.get(token_id)
        query = 'SELECT attachment ,created_time ,type ,description FROM stream WHERE source_id=me() and strpos(attachment.href,"%s")>=0 and strpos(attachment.href,"fb_action_types=og.likes") > 0' % (
            url)

        return request_helper.make_request(self.FQL_PATH + query.replace(' ', '+') + '&access_token=' + socToken.user_token, True)
Пример #26
0
    def get_sharing(self, url, token_id, parse_json):
        socToken = SocToken.query.get(token_id)
        query = 'SELECT attachment ,created_time ,type ,description FROM stream WHERE source_id=me() and actor_id=me() and type=80 and attachment.href="%s"' % (
            url)

        return request_helper.make_request(self.FQL_PATH + query.replace(' ', '+') + '&access_token=' + socToken.user_token, parse_json)
Пример #27
0
    def get_like(self, page_id, token_id, parse_json):
        socToken = SocToken.query.get(token_id)
        urlAPI = self.API_PATH + self.URLS_PARTS['my_likes'] + \
            page_id + '?access_token=' + socToken.user_token

        return request_helper.make_request(urlAPI, parse_json)
Пример #28
0
 def _get_oauth_request(self, token_id, method):
     soc_token = SocToken.query.get(token_id)
     url_api = "%s%s?oauth_token=%s%s" % (
         self.API_PATH, self.API_PARTS[method], soc_token.user_token,
         self.VERSION)
     return request_helper.make_request(url_api, True)
Пример #29
0
    def get_like(self, page_id, token_id, parse_json):
        socToken = SocToken.query.get(token_id)
        urlAPI = self.API_PATH + self.URLS_PARTS['my_likes'] + \
            page_id + '?access_token=' + socToken.user_token

        return request_helper.make_request(urlAPI, parse_json)
Пример #30
0
    def check_like(self, url, token_id, sharing_id):
        """лайк объекта"""

        answer = self.CONDITION_ERROR
        condition = PaymentLoyaltySharing.query.get(sharing_id)
        if not condition:
            return self.CONDITION_FAILED

        data = json.loads(condition.data)
        if not ('owner_id' in data and 'type' in data and 'item_id' in data):
            return self.CONDITION_FAILED

        soc_token = SocToken.query.get(token_id)
        if not soc_token:
            return self.CONDITION_FAILED

        key = 'vk_likes_list_%s' % str(condition.id)

        if cache.get(key):
            likes_list = cache.get(key)
            answer = self.CONDITION_FAILED

            for user_id in likes_list:
                if str(user_id) == str(soc_token.soc_id):
                    answer = self.CONDITION_PASSED
                    break

            return answer

        offset = 0
        likes_count = self.MAX_LIKES_COUNT
        likes_list = []

        while not (offset > likes_count):

            api_url = self.API_PATH \
                + 'likes.getList' \
                + '?type=' \
                + str(data['type']) \
                + '&owner_id=' \
                + str(data['owner_id']) \
                + '&item_id=' \
                + str(data['item_id']) \
                + '&count=' \
                + str(self.MAX_LIKES_COUNT) \
                + '&offset=' \
                + str(offset)

            likes = request_helper.make_request(api_url, True)
            if not('response' in likes and 'count' in likes['response'] and 'users' in likes['response']):
                return self.CONDITION_ERROR

            if answer == self.CONDITION_ERROR:
                answer = self.CONDITION_FAILED

            likes_count = int(likes['response']['count'])

            for user_id in likes['response']['users']:
                likes_list.append(user_id)
                if str(user_id) == str(soc_token.soc_id):
                    answer = self.CONDITION_PASSED

            offset += self.MAX_LIKES_COUNT

        cache.set(key, likes_list, 50)

        return answer