Last active
January 6, 2021 11:19
-
-
Save yves0003/76d6aa5a36682d350ef6ec0f7476e2e7 to your computer and use it in GitHub Desktop.
Main python code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import json | |
import config | |
from google.cloud import bigquery | |
import requests | |
def transform_data(ligne,data): | |
contact = { | |
"email": data.at[ligne,"email"], | |
"name": data.at[ligne,"first_name"] + " " + data.at[ligne,"last_name"], | |
"properties": [ | |
{"property": "firstname", "value": data.at[ligne,"first_name"]}, | |
{"property": "lastname", "value": data.at[ligne,"last_name"]}, | |
{"property": "company", "value": ""}, | |
{"property": "associatedcompanyid", "value": ""} | |
] | |
} | |
return contact | |
def create_contacts_to_hs(): | |
#global blob | |
global contacts | |
sql = "SELECT * FROM exampleDataset.exampleData" | |
bq_client = bigquery.Client(project=config.config_vars['project_id']) | |
rows_df = bq_client.query(sql).to_dataframe() | |
rows_df_length = len(rows_df.index) | |
print('Create Contacts to HubSpot.') | |
for x in range(rows_df_length): | |
try: | |
contact = transform_data(x, rows_df) | |
email = contact['email'] | |
#get api key | |
url = f"https://api.hubapi.com/contacts/v1/contact/createOrUpdate/email/{email}/?hapikey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | |
r = requests.post(url, json= contact) | |
name = contact["name"] | |
status_code = r.status_code | |
print(str(status_code) + ' - Contact: '+ name) | |
if status_code != 200: | |
logger.info(r.json()) | |
contact['vid'] = r.json().get('vid') | |
except Exception as e: | |
print(e.args) | |
pass | |
def get_data(event, context): | |
message = base64.b64decode(event['data']).decode('utf-8') | |
pubsub_message = json.loads(message) | |
if pubsub_message["protoPayload"]["serviceData"]["jobCompletedEvent"]["job"]["jobConfiguration"]["query"]["statementType"]=="UPDATE": | |
print("Update") | |
create_contacts_to_hs() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment