Skip to content

Instantly share code, notes, and snippets.

@ojmccall
ojmccall / GCP - SFMC Data Extensions Rows section 1.py
Last active September 6, 2021 09:14
GCP - SFMC Data Extension Rows 1
def sfmc_api(event,context):
import requests
import json
from google.cloud import storage
import csv
import pandas as pd
#get the file info from the trigger event
filename = format(event['name'])
bucketname = format(event['bucket'])
@ojmccall
ojmccall / GCP - SFMC Data Extensions Rows section 2.py
Last active September 4, 2021 03:15
GCP - SFMC Data Extensions Rows section 2
# find config data values
client_id = config_data['client_id']
client_secret = config_data['client_secret']
baseURL = config_data['base_url']
#Find DE data in filename
var = filename[5:]
var = var.replace(".csv","")
split =var.split("_")
@ojmccall
ojmccall / GCP - SFMC Data Extensions Rows section 3.py
Last active September 4, 2021 03:08
GCP - SFMC Data Extension Rows 3
#get OAuth token
try:
url = 'https://'+baseURL+'.auth.marketingcloudapis.com/v2/Token'
data = {"grant_type":"client_credentials",
"client_id":client_id,
"client_secret":client_secret
}
r = requests.post(url, data=data)
print('posted')
@ojmccall
ojmccall / GCP - SFMC Data Extensions Rows section requirements.txt
Created September 4, 2021 03:17
GCP - SFMC Data Extensions Rows section requirements
google-cloud-storage==1.7.0
requests==2.22.0
requests-html==0.6.6
pandas>=1
gcsfs>=0.6
@ojmccall
ojmccall / GCP - SFMC Data Extension Builder 1.py
Created September 4, 2021 03:23
GCP - SFMC Data Extension Builder 1
#Additional imports for building a DE in addition to just rows
import base64
import pandas as pd
import xml.etree.ElementTree as ET
#Set Values for DE structure
SOAPEndpoint = "https://"+baseURL+".soap.marketingcloudapis.com/Service.asmx"
#get DE Name & DE Folder ID
DEName = split[0]
@ojmccall
ojmccall / GCP - SFMC Data Extension Builder 2.py
Last active September 4, 2021 03:25
GCP - SFMC Data Extension Builder 2
#get csv values to create DE
try:
df = pd.read_csv(fullpath)
df = df.fillna("")
df_dict = df.to_dict(orient='records')
print("df_array: "+str(df_dict))
print("length: "+str(len(df_dict)))
@ojmccall
ojmccall / GCP - SFMC Data Extension Builder 3.py
Created September 4, 2021 03:28
GCP - SFMC Data Extension Builder 3
#Post XML string to the SOAP Endpoint
try:
header = {"Content-Type": "text/xml" }
post = requests.post(SOAPEndpoint, data = XML ,headers=header)
print("posted")
body = post.content
except Exception as e: print("error from DE Build: "+e)
import xml.etree.ElementTree as ET
import requests
client_id =
client_secret =
baseURL =
SOAPEndpoint = "https://"+baseURL+".soap.marketingcloudapis.com/Service.asmx"
#compile XML
@ojmccall
ojmccall / GCP - SFMC Find & Replace 1.py
Created September 25, 2021 05:50
GCP - SFMC Find & Replace section 1
def sfmc_replace(event,context):
import requests
import json
import uuid
from google.cloud import storage
filename = format(event['name'])
bucketname = format(event['bucket'])
if filename.find("find_replace") >-1:
@ojmccall
ojmccall / GCP - SFMC Find & Replace 2.py
Created September 25, 2021 05:54
GCP- SFMC Find & Replace Section 2
contentChanged = 0
SQLChanged = 0
SSJSChanged = 0
#get Token Here
try:
url = "https://"+baseURL+".auth.marketingcloudapis.com/v2/Token"
data = {"grant_type":"client_credentials",
"client_id":client_id,