def get_page_posts(token, limit, since, until, page='me'): page_auth = SMAuth.set_token(token) try: profile = page_auth.get_object(page) except Exception, err: if err.message == ('Error validating access token: This may be because the user logged out or may be due to a system error.') \ or ('Error validating access token: The session is invalid because the user logged out.'): raise ValueError(err)
def get_page_fans_city(token): page_auth = SMAuth.set_token(token) try: request_result = page_auth.request('me/insights/page_fans_city')['data'][0]['values'][0] summation = page_auth.request('me/insights/page_fans_country')['data'][0]['values'][0]['value'] except Exception, err: logger.error("Error fetching data from API %s" % err) if err.message == ('Error validating access token: This may be because the user logged out or may be due to a system error.') \ or ('Error validating access token: The session is invalid because the user logged out.'): raise ValueError(err) raise
def get_overview(token, insight_nodes, since=None, until=None): """ :param token: access_token for the page :param insight_nodes: Any node mentioned in https://developers.facebook.com/docs/graph-api/reference/v2.5/insights#page_users :param since: From date [Optional] :param until: To Date [Optional] :return: json """ if insight_nodes is None: metrics = ["page_views", "page_stories", "page_fan_adds"] else: metrics = insight_nodes page_auth = SMAuth.set_token(token) output = [] def dictionary_builder(ori_list, new_dict, name): data = [] for line in new_dict: # append the new number to the existing array at this slot date_counts = [line['end_time'], 0 if 'value' not in line else line['value']] data.append(date_counts) ori_list.append({'name': name, 'data': data}) return ori_list for i in metrics: try: if since is None or until is None: request_result = page_auth.request('me/insights/%s' % i)['data'][0]['values'] else: request_result = page_auth.request('me/insights/%s' % i, args={'since': since, 'until': until} )['data'][0]['values'] except IndexError: request_result = [{'end_time': strftime("%Y-%m-%d %H:%M:%S", gmtime()), 'value': None}] pass except Exception, err: if err.message == ('Error validating access token: This may be because the user logged out or may be due to a system error.') \ or ('Error validating access token: The session is invalid because the user logged out.'): raise ValueError(err) print request_result output = dictionary_builder(output, request_result, i)
def get_promotional_info(token, promotion_node): page_auth = SMAuth.set_token(token) data = page_auth.get_connections("me", "Comments") return data