zn0

a

zn0
Jan 2nd, 2019
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.32 KB | None | 0 0
  1. import requests, json
  2. from datetime import datetime
  3. from tqdm import tqdm
  4.  
  5. # Byt nyckel-variablerna till dina egna API-nycklar.
  6. # https://app.hubspot.com/integrations-settings/xxxxxxx/api-key
  7. # https://xxxx-xxxxxx.pipedrive.com/settings/personal/api
  8. pipedrive_key = "cff11b771bc5abd981a16cdaaf0079795d346713"
  9. hubspot_key = "6098f387-4b65-41ab-8151-1f3556fee1a4"
  10.  
  11. if __name__ == "__main__":
  12. error = 0
  13.  
  14. headers = {
  15. 'Host': 'api.hubapi.com',
  16. 'Content-Type': 'application/json',
  17. 'Accept': 'application/json',
  18. 'User-Agent': 'pythonrequests/2.10.0',
  19. 'Cache-Control': 'no-cache'
  20. }
  21.  
  22. # Funkar Pipedrive-nyckeln?
  23. if json.loads(requests.get('https://api.pipedrive.com/v1/currencies?api_token=' + pipedrive_key).text)['success'] != False:
  24.  
  25. # Funkar Hubspot-nyckeln?
  26. if "status" not in json.loads(requests.get('https://api.hubapi.com/contacts/v1/lists/all/contacts/all?hapikey=' + hubspot_key).text).keys():
  27.  
  28. # Alla contacts
  29. resp = requests.get('https://api.pipedrive.com/v1/persons?api_token=' + pipedrive_key)
  30. if resp.status_code != 200:
  31.  
  32. # Något gick fel
  33. print(resp.status_code)
  34. error += 1
  35.  
  36. else:
  37. contacts = json.loads(resp.text)
  38. for contact in tqdm(contacts['data'], dynamic_ncols=100, desc="Importing contacts..."):
  39.  
  40. # Variablerna uppdateras efter varje iteration.
  41. if "email" in contact.keys():
  42. if str(contact['email'][0]['value']) != "None":
  43. contactsEmail = contact['email'][0]['value']
  44. else:
  45. contactsEmail = ""
  46.  
  47. if "name" in contact.keys():
  48. contactsFirstname = contact['name'].split()[0]
  49. contactsLastname = contact['name'].split()[1]
  50.  
  51. if "org_name" in contact.keys():
  52. if str(contact['org_name']) != "None":
  53. contactsOrg = contact['org_name']
  54. else:
  55. contactsOrg = ""
  56.  
  57. if "add_time" in contact.keys():
  58. if str(contact['add_time']) != "None":
  59. epochDate = datetime.strptime(str(contact['add_time']), '%Y-%m-%d %H:%M:%S').timestamp()
  60. else:
  61. epochDate = 0 # Kan behövas uppdatera, 0 i epoch är Jan 1970
  62.  
  63. if "org_id" in contact.keys():
  64. if str(contact['org_id']['address']) != "None":
  65. contactsOrgAddress = contact['org_id']['address']
  66. else:
  67. contactsOrgAddress = ""
  68.  
  69. if "phone" in contact.keys():
  70. if str(contact['phone'][0]['value']) != "None":
  71. contactsPhone = contact['phone'][0]['value']
  72. else:
  73. contactsPhone = ""
  74.  
  75. # Stoppa in kontakt i Hubspot.
  76. # Alternativt byta till /contacts/v1/contact/batch/ ifall /contacts/v1/contact
  77. # blir rate-limited.
  78. 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}]}
  79. requests.post('https://api.hubapi.com/contacts/v1/contact/?hapikey=' + hubspot_key, data=json.dumps(post), headers=headers)
  80.  
  81. # Alla companies
  82. resp = requests.get('https://api.pipedrive.com/v1/organizations?api_token=' + pipedrive_key)
  83. if resp.status_code != 200:
  84.  
  85. # Något gick fel
  86. print(resp.status_code)
  87. error += 1
  88.  
  89. else:
  90. companies = json.loads(resp.text)
  91. for company in tqdm(companies['data'], dynamic_ncols=100, desc="Importing companies..."):
  92. if "name" in company.keys():
  93. if str(company['name']) != "None":
  94. companyName = company['name']
  95. else:
  96. companyName = ""
  97.  
  98. post = {"properties": [{"name": "name","value": companyName},{"name": "description","value": ""}]}
  99. requests.post('https://api.hubapi.com/companies/v2/companies?hapikey=' + hubspot_key, data=json.dumps(post), headers=headers)
  100.  
  101. # Alla deals
  102. resp = requests.get('https://api.pipedrive.com/v1/deals?api_token=' + pipedrive_key)
  103. if resp.status_code != 200:
  104. # Något gick fel
  105. print(resp.status_code)
  106. error += 1
  107.  
  108. else:
  109. deals = json.loads(resp.text)
  110. for deal in tqdm(deals['data'], dynamic_ncols=100, desc="Importing deals..."):
  111. if "title" in deal.keys():
  112. if str(deal['title']) != "None":
  113. dealName = deal['title']
  114. else:
  115. dealName = ""
  116.  
  117. if "value" in deal.keys():
  118. if str(deal['value'] != "None"):
  119. dealValue = deal['value']
  120. else:
  121. dealValue = 0
  122.  
  123. if "add_time" in deal.keys():
  124. if str(deal['add_time'] != "None"):
  125. dealAdded = datetime.strptime(str(deal['add_time']), '%Y-%m-%d %H:%M:%S').timestamp()
  126. else:
  127. dealAdded = 0 # Jan 1970 eftersom det är i epoch format
  128.  
  129. if "expected_close_date" in deal.keys():
  130. if str(deal['expected_close_date']) != "None":
  131. dealCloseDate = datetime.strptime(str(deal['expected_close_date']), '%Y-%m-%d').timestamp()
  132. else:
  133. dealCloseDate = 0
  134.  
  135. # Add deal
  136. post = {"properties": [{"name": "dealname","value": dealName},{"name": "amount","value": dealValue},{"name": "closedate","value": int(dealCloseDate*1000)},{"name": "createdate","value": int(dealAdded*1000)}]}
  137. resp = requests.post('https://api.hubapi.com/deals/v1/deal?hapikey=' + hubspot_key, data=json.dumps(post), headers=headers)
  138.  
  139. if ("person_name" in deal.keys()) or ("org_name" in deal.keys()):
  140. resp = requests.get('https://api.pipedrive.com/v1/deals?status=all_not_deleted&start=0&api_token=' + pipedrive_key)
  141. if resp.status_code != 200:
  142. # Något gick fel
  143. print(resp.status_code)
  144. error += 1
  145.  
  146. else:
  147.  
  148. deals = json.loads(resp.text)
  149. for deal in tqdm(deals['data'], dynamic_ncols=100, desc="Associating customers to deals..."):
  150.  
  151. pipeDealName = deal['']
  152. pipePersonEmail = deal['person_id']['email'][0]['value']
  153.  
  154. vid = json.loads(requests.get('https://api.hubapi.com/contacts/v1/contact/email/' + pipePersonEmail + '/profile?hapikey=' + hubspot_key).text)['vid']
  155. resp = requests.get('https://api.hubapi.com/deals/v1/deal/paged?hapikey=' + hubspot_key)
  156.  
  157. # Associera deal
  158. requests.put('https://api.hubapi.com/deals/v1/deal/' + dealId + '/associations/CONTACT?id=' + str(vid) + '&hapikey=' + hubspot_key)
  159.  
  160. '''
  161. # Alla tickets
  162. resp = requests.get('https://api.hubapi.com/crm-objects/v1/objects/tickets/paged?hapikey=' + hubspot_key)
  163. if resp.status_code != 200:
  164. # Något gick fel
  165. print(resp.status_code)
  166. error += 1
  167. else:
  168. '''
  169.  
  170. '''
  171. # Timeline
  172. resp = requests.get('https://api.pipedrive.com/v1/activities?api_token=' + pipedrive_key)
  173. if resp.status_code != 200:
  174. # Något gick fel
  175. print(resp.status_code)
  176. error += 1
  177. else:
  178. timeline = json.loads(resp.text)
  179. for event in tqdm(timeline['data'], dynamic_ncols=100, desc="Importing timeline..."):
  180. if "person_name" in event.keys():
  181. personId = requests.get('')
  182. '''
  183.  
  184. if error > 0:
  185. print("ERROR: " + str(error) + " errors occured")
  186.  
  187. else:
  188. print("Hubspot API key doesn't exist! (" + hubspot_key + ")")
  189. else:
  190. print("Pipedrive API key doesn't exist! (" + pipedrive_key + ")")
Advertisement
Add Comment
Please, Sign In to add comment