Ejemplo n.º 1
0
def retrieveFbokPageStats(user_id=None, company_id=None, job_id=None, run_type=None, sinceDateTime=None):
    try:
        print 'starting retrieveFbokPageStats for company ' + str(company_id)
        #print 'timestamp ' + str(sinceDateTime)
        if (datetime.now() - sinceDateTime).days > 89:
            print 'FB does not allow for more than 89 days of data'
            sinceDateTime = datetime.now() - timedelta(days=89)
        sinceTimestamp = calendar.timegm(sinceDateTime.timetuple())
        sinceTimestamp = str('{0:f}'.format(sinceTimestamp).rstrip('0').rstrip('.'))
        untilTimestamp = time.mktime(datetime.now().timetuple())
        untilTimestamp = str('{0:f}'.format(untilTimestamp).rstrip('0').rstrip('.'))
        #print 'since ' + str(sinceTimestamp) + ' until ' + str(untilTimestamp)
    
        existingIntegration = CompanyIntegration.objects(company_id = company_id).first()
        if 'fbok' not in existingIntegration['integrations']: # if Buffer is present and configured
            print 'did not find fbok'
            raise Exception('Facebook integration not found')
        integration = existingIntegration.integrations['fbok']
        if integration['access_token'] is None:
            raise Exception('Facebook access token not found')
        fbok = FacebookPage(integration['access_token'])
        if fbok is None:
            raise Exception('Facebook Page object could not be created')
        definedPages = integration.get('pages', None)
        
        print 'calling pages'
        pages = fbok.get_pages()['data']
        print 'found FB pages: ' + str(pages)
        for page in pages:
            page_token = page['access_token']
            page_id = page['id']
            if not any(d['id'] == page_id for d in definedPages): #if page not defined in company instance, skip it
                continue
            print 'page id is ' + str(page_id) + ' and token is ' + str(page_token)
            page_insights = fbok.get_page_insights(page_id, page_token, sinceTimestamp, untilTimestamp)
            #print 'page insights for ' + page['name'] + ': ' + str(page_insights)
            page_insights_cleaned = json.loads(json.dumps(page_insights['data']), object_hook = replace_dots)
            results = {'page_id': page_id, 'insights': page_insights_cleaned}
            saveFbokPageStats(user_id=user_id, company_id=company_id, results=results, job_id=job_id, run_type=run_type)
        #saveFbokAdStats(user_id=user_id, company_id=company_id, results=campaigns, job_id=job_id, run_type=run_type)
                
        
    except Exception as e:
        print 'exception was ' + str(e)
        send_notification(dict(
             type='error',
             success=False,
             message=str(e)
            ))      
Ejemplo n.º 2
0
def retrieveFbokPostStats(user_id=None, company_id=None, job_id=None, run_type=None, sinceDateTime=None):
    try:
        print 'starting retrieveFbokPostStats for company ' + str(company_id)
        existingIntegration = CompanyIntegration.objects(company_id = company_id).first()
        if 'fbok' not in existingIntegration['integrations']: # if Buffer is present and configured
            print 'did not find fbok'
            raise Exception('Facebook integration not found')
        integration = existingIntegration.integrations['fbok']
        if integration['access_token'] is None:
            raise Exception('Facebook access token not found')
        fbok = FacebookPage(integration['access_token'])
        if fbok is None:
            raise Exception('Facebook Page object could not be created')
        
        print 'calling pages'
        pages = fbok.get_pages()['data']
        print 'found FB pages: ' + str(pages)
        for page in pages:
            page_token = page['access_token']
            page_id = page['id']
            posts = fbok.get_posts(page_id, page_token)['data']
            print 'got back #posts ' + str(len(posts))
            for post in posts:
                post_insights = fbok.get_post_insights(post['id'], page_token)
                #print 'post insights for ' + post['id'] + ': ' + str(post_insights)
                post_insights_cleaned = json.loads(json.dumps(post_insights['data']), object_hook = replace_dots)
                results = {'page_id': page_id, 'post_id' : post['id'], 'created_time': post['created_time'], 'insights': post_insights_cleaned}
                saveFbokPostStats(user_id=user_id, company_id=company_id, results=results, job_id=job_id, run_type=run_type)
        #saveFbokAdStats(user_id=user_id, company_id=company_id, results=campaigns, job_id=job_id, run_type=run_type)
                
        
    except Exception as e:
        print 'exception was ' + str(e)
        send_notification(dict(
             type='error',
             success=False,
             message=str(e)
            ))