Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests, json
- from datetime import datetime
- from tqdm import tqdm
- # Byt nyckel-variablerna till dina egna API-nycklar.
- # https://app.hubspot.com/integrations-settings/xxxxxxx/api-key
- # https://xxxx-xxxxxx.pipedrive.com/settings/personal/api
- pipedrive_key = "cff11b771bc5abd981a16cdaaf0079795d346713"
- hubspot_key = "6098f387-4b65-41ab-8151-1f3556fee1a4"
- if __name__ == "__main__":
- error = 0
- headers = {
- 'Host': 'api.hubapi.com',
- 'Content-Type': 'application/json',
- 'Accept': 'application/json',
- 'User-Agent': 'pythonrequests/2.10.0',
- 'Cache-Control': 'no-cache'
- }
- # Funkar Pipedrive-nyckeln?
- if json.loads(requests.get('https://api.pipedrive.com/v1/currencies?api_token=' + pipedrive_key).text)['success'] != False:
- # Funkar Hubspot-nyckeln?
- if "status" not in json.loads(requests.get('https://api.hubapi.com/contacts/v1/lists/all/contacts/all?hapikey=' + hubspot_key).text).keys():
- # Alla contacts
- resp = requests.get('https://api.pipedrive.com/v1/persons?api_token=' + pipedrive_key)
- if resp.status_code != 200:
- # Något gick fel
- print(resp.status_code)
- error += 1
- else:
- contacts = json.loads(resp.text)
- for contact in tqdm(contacts['data'], dynamic_ncols=100, desc="Importing contacts..."):
- # Variablerna uppdateras efter varje iteration.
- if "email" in contact.keys():
- if str(contact['email'][0]['value']) != "None":
- contactsEmail = contact['email'][0]['value']
- else:
- contactsEmail = ""
- if "name" in contact.keys():
- contactsFirstname = contact['name'].split()[0]
- contactsLastname = contact['name'].split()[1]
- if "org_name" in contact.keys():
- if str(contact['org_name']) != "None":
- contactsOrg = contact['org_name']
- else:
- contactsOrg = ""
- if "add_time" in contact.keys():
- if str(contact['add_time']) != "None":
- epochDate = datetime.strptime(str(contact['add_time']), '%Y-%m-%d %H:%M:%S').timestamp()
- else:
- epochDate = 0 # Kan behövas uppdatera, 0 i epoch är Jan 1970
- if "org_id" in contact.keys():
- if str(contact['org_id']['address']) != "None":
- contactsOrgAddress = contact['org_id']['address']
- else:
- contactsOrgAddress = ""
- if "phone" in contact.keys():
- if str(contact['phone'][0]['value']) != "None":
- contactsPhone = contact['phone'][0]['value']
- else:
- contactsPhone = ""
- # Stoppa in kontakt i Hubspot.
- # Alternativt byta till /contacts/v1/contact/batch/ ifall /contacts/v1/contact
- # blir rate-limited.
- post = {"properties":[{"property":"email","value":contactsEmail},{"property":"firstname","value":contactsFirstname},{"property":"lastname","value":contactsLastname},{"property":"company","value":contactsOrg},{"property":"phone","value":contactsPhone},{"property":"address","value":contactsOrgAddress}]}
- requests.post('https://api.hubapi.com/contacts/v1/contact/?hapikey=' + hubspot_key, data=json.dumps(post), headers=headers)
- # Alla companies
- resp = requests.get('https://api.pipedrive.com/v1/organizations?api_token=' + pipedrive_key)
- if resp.status_code != 200:
- # Något gick fel
- print(resp.status_code)
- error += 1
- else:
- companies = json.loads(resp.text)
- for company in tqdm(companies['data'], dynamic_ncols=100, desc="Importing companies..."):
- if "name" in company.keys():
- if str(company['name']) != "None":
- companyName = company['name']
- else:
- companyName = ""
- post = {"properties": [{"name": "name","value": companyName},{"name": "description","value": ""}]}
- requests.post('https://api.hubapi.com/companies/v2/companies?hapikey=' + hubspot_key, data=json.dumps(post), headers=headers)
- # Alla deals
- resp = requests.get('https://api.pipedrive.com/v1/deals?api_token=' + pipedrive_key)
- if resp.status_code != 200:
- # Något gick fel
- print(resp.status_code)
- error += 1
- else:
- deals = json.loads(resp.text)
- for deal in tqdm(deals['data'], dynamic_ncols=100, desc="Importing deals..."):
- if "title" in deal.keys():
- if str(deal['title']) != "None":
- dealName = deal['title']
- else:
- dealName = ""
- if "value" in deal.keys():
- if str(deal['value'] != "None"):
- dealValue = deal['value']
- else:
- dealValue = 0
- if "add_time" in deal.keys():
- if str(deal['add_time'] != "None"):
- dealAdded = datetime.strptime(str(deal['add_time']), '%Y-%m-%d %H:%M:%S').timestamp()
- else:
- dealAdded = 0 # Jan 1970 eftersom det är i epoch format
- if "expected_close_date" in deal.keys():
- if str(deal['expected_close_date']) != "None":
- dealCloseDate = datetime.strptime(str(deal['expected_close_date']), '%Y-%m-%d').timestamp()
- else:
- dealCloseDate = 0
- # Add deal
- post = {"properties": [{"name": "dealname","value": dealName},{"name": "amount","value": dealValue},{"name": "closedate","value": int(dealCloseDate*1000)},{"name": "createdate","value": int(dealAdded*1000)}]}
- resp = requests.post('https://api.hubapi.com/deals/v1/deal?hapikey=' + hubspot_key, data=json.dumps(post), headers=headers)
- if ("person_name" in deal.keys()) or ("org_name" in deal.keys()):
- resp = requests.get('https://api.pipedrive.com/v1/deals?status=all_not_deleted&start=0&api_token=' + pipedrive_key)
- if resp.status_code != 200:
- # Något gick fel
- print(resp.status_code)
- error += 1
- else:
- deals = json.loads(resp.text)
- for deal in tqdm(deals['data'], dynamic_ncols=100, desc="Associating customers to deals..."):
- pipeDealName = deal['']
- pipePersonEmail = deal['person_id']['email'][0]['value']
- vid = json.loads(requests.get('https://api.hubapi.com/contacts/v1/contact/email/' + pipePersonEmail + '/profile?hapikey=' + hubspot_key).text)['vid']
- resp = requests.get('https://api.hubapi.com/deals/v1/deal/paged?hapikey=' + hubspot_key)
- # Associera deal
- requests.put('https://api.hubapi.com/deals/v1/deal/' + dealId + '/associations/CONTACT?id=' + str(vid) + '&hapikey=' + hubspot_key)
- '''
- # Alla tickets
- resp = requests.get('https://api.hubapi.com/crm-objects/v1/objects/tickets/paged?hapikey=' + hubspot_key)
- if resp.status_code != 200:
- # Något gick fel
- print(resp.status_code)
- error += 1
- else:
- '''
- '''
- # Timeline
- resp = requests.get('https://api.pipedrive.com/v1/activities?api_token=' + pipedrive_key)
- if resp.status_code != 200:
- # Något gick fel
- print(resp.status_code)
- error += 1
- else:
- timeline = json.loads(resp.text)
- for event in tqdm(timeline['data'], dynamic_ncols=100, desc="Importing timeline..."):
- if "person_name" in event.keys():
- personId = requests.get('')
- '''
- if error > 0:
- print("ERROR: " + str(error) + " errors occured")
- else:
- print("Hubspot API key doesn't exist! (" + hubspot_key + ")")
- else:
- print("Pipedrive API key doesn't exist! (" + pipedrive_key + ")")
Advertisement
Add Comment
Please, Sign In to add comment