使用Python3的Pipedrive API将交易输出限制为1000个交易

时间:2018-09-24 00:16:56

标签: python api

我已经编写了Python3脚本来从Pipedrive检索所有信息,并将其存入Excel csv文件。 当数据库中有2500多个时,我无法弄清为什么我只能检索前1000笔交易。任何建议将不胜感激。

脚本是: 汇入要求 导入json 导入csv

API_TOKEN = "<SECRET API KEY>"
FILTER = False # Apply Filtering.
FILTER_READABLE = True # Filter uses human-readable field names, not just ids/hashes.
FILTER_FIELDS = [ # Fields to Include, separated by commas, example below. Make sure spelling is exact. Run without filter enabled to see exact names.

]

EXAMPLE_FILTER = [
    "ID",
    "Expected close date",
    "Email messages count",
]


DEALS_BASE_URL = "https://api.pipedrive.com/v1/deals/"
DEALFIELDS_BASE_URL = "https://api.pipedrive.com/v1/dealFields/"

DEALFIELDS = {}
DEALFIELDS_OPTIONS = {}

end_last = 0
while True:
    req = requests.get(DEALFIELDS_BASE_URL, params={"api_token": API_TOKEN, "start": end_last, "limit": 5000})
    response = json.loads(req.text)
    if not response.get("data", None):
        print("Invalid Request. Check API Token!")
        quit(1)

    for field in response.get("data", []):
        DEALFIELDS[field["key"]] = field["name"]
        if field["field_type"] == "enum":
            DEALFIELDS_OPTIONS[field["key"]] = ("enum", {str(item["id"]): item["label"] for item in field["options"]})
        elif field["field_type"] == "set":
            DEALFIELDS_OPTIONS[field["key"]] = ("set", {str(item["id"]): item["label"] for item in field["options"]})

    if not response.get("additional_info", {}).get("pagination", {}).get("more_items_in_collection", False):
        break

    end_last = end_last + 5000


DEALS = []

end_last = 0
while True:
    req = requests.get(DEALS_BASE_URL, params={"api_token": API_TOKEN, "start": end_last, "limit": 5000})
    response = json.loads(req.text)
    if not response.get("data", None):
        quit()

    for deal in response.get("data", []):
        new_deal = {}
        for key, item in deal.items():
            if type(item) is dict:
                item = item["name"]

            if key in DEALFIELDS:
                if FILTER and FILTER_READABLE and DEALFIELDS[key] not in FILTER_FIELDS:
                    continue
                if FILTER and not FILTER_READABLE and key not in FILTER_FIELDS:
                    continue
                if key in DEALFIELDS_OPTIONS:
                    if DEALFIELDS_OPTIONS[key][0] == "enum":
                        if item is None:
                            continue
                        new_deal[DEALFIELDS[key]] = DEALFIELDS_OPTIONS[key][1][str(item)]
                    elif DEALFIELDS_OPTIONS[key][0] == "set":
                        if item is None:
                            continue
                        new_deal[DEALFIELDS[key]] = str(",".join([DEALFIELDS_OPTIONS[key][1][i] for i in item.split(",")]))
                    continue
                new_deal[DEALFIELDS[key]] = item
            else:
                if FILTER and key not in FILTER_FIELDS:
                    continue
                new_deal[key] = item
        DEALS.append(new_deal)

    if not response.get("additional_info", {}).get("pagination", {}).get("more_items_in_collection", False):
        break

    end_last = end_last + 5000

fieldnames = []
for deal in DEALS:
    for key, item in deal.items():
        if key not in fieldnames:
            fieldnames.append(key)

output_csv = open("out.csv", "w", encoding="utf-8")
out = csv.DictWriter(output_csv, fieldnames=fieldnames)
out.writeheader()
for deal in DEALS:
    out.writerow({str(key).strip(): str(item).strip() for key, item in deal.items()})
output_csv.close()

1 个答案:

答案 0 :(得分:-1)

不知道您是否曾经在其他地方找到答案,但是认为pipedrive api的限制是500而不是5000,请更改