Skip to content

Instantly share code, notes, and snippets.

@lancejohnson
Created August 26, 2020 17:55
Show Gist options
  • Save lancejohnson/5cccd5d484d3fe1653e43829e7aae926 to your computer and use it in GitHub Desktop.
Save lancejohnson/5cccd5d484d3fe1653e43829e7aae926 to your computer and use it in GitHub Desktop.
Generate the counts for a given query over a period of months. Not beautiful, but pretty quick.
from os import environ
import requests
from requests.auth import HTTPBasicAuth
def create_close_params(starting_year, ending_year):
def generate_first_day_of_the_month(starting_year, ending_year):
first_day_of_the_month = []
for year in range(starting_year, ending_year + 1):
for month in range(1, 13):
month_formatted = format(month, "02d")
date = f"{year}-{month_formatted}-01"
# 2016-01-01
first_day_of_the_month.append(date)
return first_day_of_the_month
close_params = {}
start_dates = generate_first_day_of_the_month(starting_year, ending_year)
for i in range(0, len(start_dates) - 1):
start_date = start_dates[i]
end_date = start_dates[i + 1]
param = {"query": f"""(original_lead_status:"stat_pdBqiRfX6DrpDrE8v320qT2tCSSNURza5cv8aSWxdcp" and created >= "{start_date} 01:00:00" and created <= "{end_date} 00:59:59") or lead_status_change(new_status:"stat_pdBqiRfX6DrpDrE8v320qT2tCSSNURza5cv8aSWxdcp" and date >= "{start_date} 01:00:00" and date <= "{end_date} 00:59:59")"""}
close_params[start_date] = param
return close_params
def close_queries(close_params):
def query_close_for_results_count(query_params):
CLOSE_API_KEY = environ.get("CLOSE_API_KEY", "")
BASE_URL = "https://api.close.com/api/v1/lead/"
resp = requests.get(BASE_URL, params=query_params, auth=HTTPBasicAuth(CLOSE_API_KEY, ""))
num_of_results = resp.json()["total_results"]
return num_of_results
results = {}
for start_date, query_params in close_params.items():
query_result_count = query_close_for_results_count(query_params)
results[start_date] = query_result_count
return results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment