当前位置: 代码迷 >> python >> 使用 Python3 的 Pipedrive API 将交易输出限制为 1000 笔交易
  详细解决方案

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

热度:157   发布时间:2023-06-27 21:18:01.0

我编写了一个 Python3 脚本来从 Pipedrive 检索所有 ldeal 信息并将其存入 Excel csv 文件。 当数据库中有超过 2,500 笔交易时,我无法弄清楚为什么我只能检索前 1,000 笔交易。 任何建议将不胜感激。

脚本是: import requests import json import 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()

不知道你是否曾经在其他地方得到过这个答案,但认为 pipedrive api 限制是 500 而不是 5000 所以改变它

  相关解决方案