Exemplo n.º 1
0
def _get_hspt_email_details(email_id, existingCampaign, company_id, job_id, run_type):
    try:
        if run_type == 'initial':
            campaigns = TempData.objects(Q(company_id=company_id) & Q(record_type='campaign') & Q(source_system='hspt') & Q(job_id=job_id) ).only('source_record') #& Q(job_id=job_id) 
        else:
            campaigns = TempDataDelta.objects(Q(company_id=company_id) & Q(record_type='campaign') & Q(source_system='hspt') & Q(job_id=job_id) ).only('source_record') #& Q(job_id=job_id) 
    
        campaignListTemp = list(campaigns)
        campaignList = [i['source_record'] for i in campaignListTemp]
        #print 'temp recs found ' + str(len(campaignList))
        #now get the appId for this email Id
        app_id = None
        int_email_id = 0
        for campaign in campaignList:
            #print 'campaign in temp ' + str(campaign)
            if str(campaign['id']) == email_id:
                print 'campaign id is ' + str(campaign['id']) + ' and email id is ' + email_id
                app_id = campaign['appId']
                int_email_id = campaign['id']
                break
        #if appId found, get details
        emailDetails = None
        if app_id is not None:
            hspt = Hubspot(company_id)
            emailDetails = hspt.get_campaign_details(campaignId=int_email_id, appId=app_id) 
        return emailDetails   
    except Exception as e:
        print 'error while getting email details with Hspt campaign from traffic ' + str(e)
        send_notification(dict(
             type='error',
             success=False,
             message=str(e)
            ))  
Exemplo n.º 2
0
def retrieveHsptOpportunities(user_id=None, company_id=None, job_id=None, run_type=None, sinceDateTime=None):
    try:
        print 'retrieving Hspt deals'
        hspt = Hubspot(company_id)
        oppList = []
#         if run_type == 'initial':
#             oppList = hspt.get_deals(count=10000)
#         else:
#             oppList = hspt.get_deals(count=1000)
#         print 'got opps ' + str(len(oppList['results']))
#         saveHsptOpportunities(user_id=user_id, company_id=company_id, oppList=oppList, job_id=job_id, run_type=run_type)
        #print 'job id is ' + str(job_id)
        if run_type == 'initial':
            print 'initial run for opps'
            try:
                for opp in hspt.get_deals(count=10000)['results']:
                    oppList.append(opp)
                    if len(oppList) == 100:
                        #print 'going to save'
                        saveHsptOpportunities(user_id=user_id, company_id=company_id, oppList=oppList, job_id=job_id, run_type=run_type)
                        oppList = []
            except Exception as e:
                print 'exception: ' + str(e)
        else:
            try:
                for opp in hspt.get_deals(count=1000)['results']:
                    oppList.append(opp)
                    if len(oppList) == 100:
                        saveHsptOpportunities(user_id=user_id, company_id=company_id, oppList=oppList, job_id=job_id, run_type=run_type)
                        oppList = []
            except Exception as e:
                print 'exception: ' + str(e)
        
        
        try:
            message = 'Opportunities retrieved from Hubspot'
            notification = Notification()
            #notification.company_id = company_id
            notification.owner = user_id
            notification.module = 'Opportunities'
            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 oppList
    except Exception as e:
        send_notification(dict(type='error', success=False, message=str(e)))      
Exemplo n.º 3
0
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)
            ))