Untitled

 avatar
unknown
plain_text
4 years ago
3.9 kB
5
Indexable
import datorama
import requests
import pandas as pd
import xml.etree.ElementTree as et
import datetime
from datetime import timedelta
import dateutil.parser

# SOAP request URL
url = "https://soap.flexmail.eu/3.0.0/flexmail.php"


def extract_campaign_ids():
    # dataframe for IDs and Date
    df = pd.DataFrame(columns=['CampaignDate', 'CampaignId'])

    # structured XML
    payload = """<x:Envelope
        xmlns:x="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:fle="https://soap.flexmail.eu/3.0.0/flexmail.wsdl">
        <x:Header/>
        <x:Body>
            <fle:GetCampaigns>
                <fle:GetCampaignsReq>
                    <fle:header>
                        <fle:userId>25531</fle:userId>
                        <fle:userToken>F03B7821-50603459-8331E419-24BD8DE7-3769D60DE5133353532</fle:userToken>
                    </fle:header>
                    <fle:type>Campaign</fle:type>
                </fle:GetCampaignsReq>
            </fle:GetCampaigns>
        </x:Body>
    </x:Envelope>"""

    # headers
    headers = {
        'Content-Type': 'text/xml; charset=utf-8'
    }
    # POST request
    response = requests.request("POST", url, headers=headers, data=payload)
    data = """{}""".format(response.text)
    response_root = et.fromstring(data)

    for item in response_root.iter('item'):
        if item.find('campaignId') is not None:
            try:
                campaign_id = item.find('campaignId').text
                campaign_date = dateutil.parser.isoparse(item.find('campaignSendDate').text)
                df = df.append([{'CampaignDate': campaign_date, 'CampaignId': campaign_id}], ignore_index=True)
            except ValueError as ve:
                print(ve)
    return df


def narrow_down_campaigns(dfs):
    end_date = datetime.datetime.today()
    begin_date = end_date - timedelta(days=30)
    narrowed_campaigns = dfs[(dfs['CampaignDate'] <= end_date) & (dfs['CampaignDate'] >= begin_date)]
    return narrowed_campaigns


def get_campaigns_report(row):
    # dataframe for IDs and Date

    # structured XML
    payload = """<x:Envelope
    xmlns:x="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:fle="https://soap.flexmail.eu/3.0.0/flexmail.wsdl">
    <x:Header/>
    <x:Body>
        <fle:GetCampaignReport>
            <fle:GetCampaignReportReq>
                <fle:header>
                    <fle:userId>25531</fle:userId>
                    <fle:userToken>F03B7821-50603459-8331E419-24BD8DE7-3769D60DE5133353532</fle:userToken>
                </fle:header>
                <fle:campaignId>{}</fle:campaignId>
            </fle:GetCampaignReportReq>
        </fle:GetCampaignReport>
    </x:Body>
</x:Envelope>""".format(row['CampaignId'])

    # headers
    headers = {
        'Content-Type': 'text/xml; charset=utf-8'
    }
    # POST request
    response = requests.request("POST", url, headers=headers, data=payload)
    data = """{}""".format(response.text)
    response_root = et.fromstring(data)

    for item in response_root.iter('campaignReportType'):
        if item.find('campaignTotalEmailAddressesClicked') is not None:
            all_clicked = item.find('campaignTotalEmailAddressesClicked').text
            all_opened = item.find('campaignTotalEmailAddressesRead').text
            return [row['CampaignDate'], row['CampaignId'], all_opened, all_clicked]


def expand_campaign_info(df):
    test = df.apply(get_campaigns_report, axis=1, result_type='expand')
    test.columns = ['campaign_date', 'campaign_id', 'opened', 'clicked']
   
    return test



df = extract_campaign_ids()
campaign_set = narrow_down_campaigns(df)
all_info = expand_campaign_info(campaign_set)
datorama.add_row(['campaign_date', 'campaign_id', 'opened', 'clicked'])
datorama.add_rows(all_info.values.tolist())
datorama.save()
Editor is loading...