def parse_username(self, url): user_id = url if self.URLS_PARTS['user'] in url: user_id = request_helper.parse_get_param( url, self.URLS_PARTS['user']) elif self.URLS_PARTS['plus'] in url: user_id = request_helper.parse_get_param( url, self.URLS_PARTS['plus']) elif self.URLS_PARTS['base'] in url: user_id = request_helper.parse_get_param(url, self.URLS_PARTS['base']) return user_id
def parse_place(self, placeStr): place = {} place['name'] = request_helper.parse_get_param(placeStr, '"') if '"' in place['name']: place['address'] = place['name'][place['name'].find('"') + 1:] place['name'] = place['name'][0:place['name'].find('"')] place['address'] = request_helper.parse_get_param( place['address'], '"') if '"' in place['address']: place['address'] = place['address'][0:place['address'].find('"' )] if 'address' in place and 0 == len(place['address']): place.pop('address') return place
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
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
def search_hashtag(token_id, url): hashtag = '#' + request_helper.parse_get_param(url, '#') soc_token = SocToken.query.get(token_id) twitter = Twython(SocConfig.TWITTER_KEY, SocConfig.TWITTER_SECRET, soc_token.user_token, soc_token.token_secret) params = {'q': hashtag, 'count': 1, 'from': soc_token.soc_username} searchHashtag = twitter.get('search/tweets', params=params) return searchHashtag
def parse_video_id(self, url): video_id = url for video_url in self.VIDEO_URLS: if video_url not in url: break video_id = request_helper.parse_get_param(url, video_url) return video_id
def check_like(self, url, token_id, sharing_id): pageLiked = self.CONDITION_ERROR object_id = 0 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']) like = self.get_object_like(object_id, token_id, True) if 'data' in like and len(like['data']) > 0 \ and 'object_id' in like['data'][0] \ and like['data'][0]['object_id'] == object_id: pageLiked = self.CONDITION_PASSED elif self.URLS_PARTS['base'] in url: page = self.get_page(url, token_id, True) if page.get('id'): like = self.get_like(page['id'], token_id, True) if 'data' in like and len( like['data']) > 0 and like['data'][0].get('id'): pageLiked = self.CONDITION_PASSED elif 'data' in like: pageLiked = self.CONDITION_FAILED else: # like внешней ссылки like = self.get_external_like(url, token_id) if 'data' in like and len(like['data']) > 0 \ and 'attachment' in like['data'][0] \ and 'href' in like['data'][0]['attachment']: pageLiked = self.CONDITION_PASSED return pageLiked
def search_hashtag(token_id, url): hashtag = '#' + request_helper.parse_get_param(url, '#') soc_token = SocToken.query.get(token_id) twitter = Twython( SocConfig.TWITTER_KEY, SocConfig.TWITTER_SECRET, soc_token.user_token, soc_token.token_secret) params = { 'q': hashtag, 'count': 1, 'from': soc_token.soc_username} searchHashtag = twitter.get('search/tweets', params=params) return searchHashtag
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
def check_like(self, url, token_id, sharing_id): pageLiked = self.CONDITION_ERROR object_id = 0 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']) like = self.get_object_like(object_id, token_id, True) if 'data' in like and len(like['data']) > 0 \ and 'object_id' in like['data'][0] \ and like['data'][0]['object_id'] == object_id: pageLiked = self.CONDITION_PASSED elif self.URLS_PARTS['base'] in url: page = self.get_page(url, token_id, True) if page.get('id'): like = self.get_like(page['id'], token_id, True) if 'data' in like and len(like['data']) > 0 and like['data'][0].get('id'): pageLiked = self.CONDITION_PASSED elif 'data' in like: pageLiked = self.CONDITION_FAILED else: # like внешней ссылки like = self.get_external_like(url, token_id) if 'data' in like and len(like['data']) > 0 \ and 'attachment' in like['data'][0] \ and 'href' in like['data'][0]['attachment']: pageLiked = self.CONDITION_PASSED return pageLiked
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
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
def parse_status_id(url): return request_helper.parse_get_param(url, TwitterApi.URLS_PARTS['status'])
def parse_screen_name(url): return request_helper.parse_get_param(url, TwitterApi.URLS_PARTS['base'])