示例#1
0
文件: tasks.py 项目: woostersoz/3m
def retrieveHsptCampaigns(user_id=None, company_id=None, job_id=None, run_type=None, sinceDateTime=None):
    '''remember that campaigns in HSPT API terms refers to email templates! here, we use actualCampaign to represent the Campaign and campaign to refer to email templates '''
    try:
        actualCampaigns = []
        actualCampaignsDict = {}
        actualCampaignsTempList = []
        
        utc_day_start_epoch = calendar.timegm(sinceDateTime.timetuple()) * 1000 #use calendar.timegm and not mktime because of UTC
        utc_day_start_epoch = str('{0:f}'.format(utc_day_start_epoch).rstrip('0').rstrip('.'))
        print 'utc start epoch is ' + str(utc_day_start_epoch)
        utc_day_end_epoch = calendar.timegm(datetime.now().timetuple()) * 1000
        utc_day_end_epoch = str('{0:f}'.format(utc_day_end_epoch).rstrip('0').rstrip('.'))
        print 'utc end epoch is ' + str(utc_day_end_epoch)
        
        hspt = Hubspot(company_id)
        if run_type == 'initial':
            campaignList = hspt.get_all_campaigns()
            #campaignList = hspt.get_recent_campaigns()
        else:
            campaignList = hspt.get_recent_campaigns()
        
        #print 'campaign list has entries' + str(len(campaignList['results']))
        #print 'campaign list is ' + str(campaignList)
        #campaignIds = [i['id'] for i in campaignList['results']]
        for i in campaignList['results']:
            #if i['id'] != 23202074:
            #    continue
            campaignStats = hspt.get_campaign_stats(i['id'])
            i['stats'] = campaignStats.get('stats', None)
            
            campaignDetails = hspt.get_campaign_details(campaignId=i['id'], appId=i['appId'])
            i['details'] = campaignDetails
            
            if 'contentId' not in campaignDetails:
                continue
            contentId = campaignDetails['contentId']
            email_content = hspt.get_email_by_content_id(contentId)
            #get events
            #i['events'] = {}
            
            email_events = hspt.get_campaign_events(i['id'], i['appId'], utc_day_start_epoch, utc_day_end_epoch)
           
            #i['events'] = email_events
            #print 'email events are ' + str(email_events)
            #process each event to add to lead record
            #for event in i['events']:
          
            #set other variables
            i['name'] = email_content['name']
            i['created'] = email_content['created']
            i['last_event_timestamp'] = 0
            #print 'guid is ' + str(email_content['campaign'])
            #if campaign GUID found, do actualCampaign stuff
            if 'campaign' in email_content and email_content['campaign'] is not None and email_content['campaign'] != "":
                if email_content['campaign'] not in actualCampaignsTempList: #first time this actualcampaign is being encountered
                    #print 'campaign not found'
                    actualCampaignsTempList.append(email_content['campaign'])
                    #print '1'
                    actualCampaignsDict[email_content['campaign']] = {}
                    actualCampaignsDict[email_content['campaign']]['guid'] = email_content['campaign']
                    actualCampaignsDict[email_content['campaign']]['name'] = email_content['campaign_name']
                    #print '2'
                    actualCampaignsDict[email_content['campaign']]['emails'] = []
                    actualCampaignsDict[email_content['campaign']]['emails'].append(i)
                    #print '3'
                    #print '1st dict is ' + str(actualCampaignsDict)
                else: #this actualcampaign has already been found before so add this email template to it
                    #print 'campaign exists ' + str(actualCampaignsDict)
                    actualCampaignsDict[email_content['campaign']]['emails'].append(i)
                #save email events separately to prevent Mongo size error
                #print 'email events is ' + str(email_events)
                if email_events is None:
                    continue
                for eventType, events in email_events.iteritems():
                    for event in events:
                        event_record = {'company_id': company_id, 'source_system':'hspt', 'campaign_guid': email_content['campaign'], 'email_id': i['id'], 'event_id': event['id'], 'event_type': eventType, 'created': event['created'], 'recipient': event['recipient'], 'details': event}
                        saveHsptCampaignEmailEvent(user_id=user_id, company_id=company_id, event=event_record, job_id=job_id, run_type=run_type)
                #break       
        #now that all email templates have been processed, iterate through the dict to fill the final array
        #print 'dict is ' + str(actualCampaignsDict)
        for key, value in actualCampaignsDict.iteritems():
            actualCampaigns.append(value)
            
        #print 'campaigns result is ' + str(actualCampaigns)
                        
            #with the content id, find the actual Campaign GUID and Name
            #campaignEventsTemp = hspt.get_campaign_events(campaignId=i['id'], appId=i['appId'])
            #i['events'] = campaignEventsTemp.get('results', None)
            
#             #campaignContacts = hspt.get_campaign_contacts(i['id'])
#             #i['contacts] = 
#             #print 'campaign contacts are ' + str(campaignContacts)
        for campaign in actualCampaigns: 
            saveHsptCampaigns(user_id=user_id, company_id=company_id, campaign=campaign, job_id=job_id, run_type=run_type)
        try:
            message = 'Email templates (Campaigns) retrieved from Hubspot'
            notification = Notification()
            #notification.company_id = company_id
            notification.owner = user_id
            notification.module = 'Campaigns'
            notification.type = 'Background task' 
            notification.method = os.path.basename(__file__)
            notification.message = message
            notification.success = True
            notification.read = False
            notification.save()
        except Exception as e:
            send_notification(dict(
                 type='error',
                 success=False,
                 message=str(e)
                ))    
        return campaignList
    except Exception as e:
        print 'error while retrieving hspt campaigns: ' + str(e)
        send_notification(dict(
             type='error',
             success=False,
             message=str(e)
            ))