Created
March 14, 2019 12:30
-
-
Save sumitmallick/b1dd1a6eaa91158ce19381f01ad2a465 to your computer and use it in GitHub Desktop.
This file contains 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 requests | |
import json | |
import urllib.request | |
from hello.models import ContactOutcomes | |
from django.core.management.base import BaseCommand | |
APIKEY_VALUE = "39752e30-2ade-426a-955b-8e6daf9de1af" | |
APIKEY = "/profile?hapikey=" + APIKEY_VALUE | |
HS_API_URL = "http://api.hubapi.com" | |
xurl = "/contacts/v1/contact/vid/" | |
max_results = 500 | |
hapikey = '39752e30-2ade-426a-955b-8e6daf9de1af' | |
count = 5 | |
contact_list = [] | |
property_list = [] | |
get_all_contacts_url = "https://api.hubapi.com/contacts/v1/lists/all/contacts/all?" | |
parameter_dict = {'hapikey': hapikey, 'count': count} | |
headers = {} | |
class Command(BaseCommand): | |
help = 'Displays current time' | |
def handle(self, *args, **kwargs): | |
has_more = True | |
while has_more: | |
parameters = urllib.parse.urlencode(parameter_dict) | |
get_url = get_all_contacts_url + parameters | |
r = requests.get(url=get_url, headers=headers) | |
response_dict = json.loads(r.text) | |
has_more = response_dict['has-more'] | |
contact_list.extend(response_dict['contacts']) | |
parameter_dict['vidOffset'] = response_dict['vid-offset'] | |
# Exit pagination, based on whatever value you've set your max results variable to. | |
# if len(contact_list) >= max_results: | |
# print('maximum number of results exceeded') | |
# break | |
for items in contact_list: | |
# print(items) | |
xid = items.get('vid') | |
firstname = items["properties"]["firstname"].get("value") | |
lastname = items["properties"]["lastname"].get("value") | |
url3 = HS_API_URL + xurl + str(xid) + APIKEY | |
# print("xid1", url3) | |
# for i in range(0, len(contact_list)): | |
# xid1 = str(xid) | |
# url = HS_API_URL + xurl + xid1 + APIKEY | |
# print(url) | |
myresponse = requests.get(url3) | |
data = myresponse.json() | |
# print(data) | |
if "recurring_billing_amount" not in data["properties"]: | |
# data1 = "na" #["properties"]["latest_client_communication_outcome"]["value"] | |
continue | |
# elif not data["properties"]["value"]: | |
# data1 = "default" #["properties"]["latest_client_communication_outcome"]["value"] | |
# elif "versions" not in data["properties"]["recurring_billing_amount"]: | |
# data2 = "na" | |
# continue | |
else: | |
# for i in range(0, len(data["properties"]["recurring_billing_amount"])): | |
if "value" not in data["properties"]["recurring_billing_amount"]: | |
# data2 = "na" #data["properties"]["latest_client_communication_outcome"]["versions"][0]["value"] | |
continue | |
# elif not data["properties"]["recurring_billing_amount"]["versions"][i]["value"]: | |
# data2 = "na" | |
else: | |
data2 = data["properties"]["recurring_billing_amount"]["value"] | |
if "hs_content_membership_status" not in data["properties"]: | |
# data1 = "na" #["properties"]["latest_client_communication_outcome"]["value"] | |
continue | |
# elif not data["properties"]["value"]: | |
# data1 = "default" #["properties"]["latest_client_communication_outcome"]["value"] | |
# elif "versions" not in data["properties"]["recurring_billing_amount"]: | |
# data2 = "na" | |
# continue | |
else: | |
# for i in range(0, len(data["properties"]["recurring_billing_amount"])): | |
if "value" not in data["properties"]["hs_content_membership_status"]: | |
# data2 = "na" #data["properties"]["latest_client_communication_outcome"]["versions"][0]["value"] | |
continue | |
# elif not data["properties"]["recurring_billing_amount"]["versions"][i]["value"]: | |
# data2 = "na" | |
else: | |
data3 = data["properties"]["hs_content_membership_status"]["value"] | |
dataff = { | |
'vid': xid, | |
'name': firstname + " " +lastname, | |
'value': data2, | |
'status': data3, | |
} | |
print(dataff) | |
# instance = ContactOutcomes(**dataff) | |
# print(instance) | |
# instance.save() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment