Instantly share code, notes, and snippets.

Embed
What would you like to do?
Records number of Streak CRM emails as Beeminder datapoint
"""
cron_post_streaks.py ~ Records number of Streak CRM emails as Beeminder datapoint
Usage:
patrick@lucca:~$ crontab -e
>> @midnight /home/patrick/.virtualenvs/bin/python /home/patrick/cron/fs2bm/cron_post_weights.py
License:
http://www.wtfpl.net/
"""
import re
from utils import get_epoch_time_in_seconds, get_date_string
# Get streak client
from streak_client import StreakClient
streak = StreakClient()
# Get beeminder client
from beeminder_client import BeeminderClient
beeminder = BeeminderClient()
# Goal metric
total_touches = 0
sales_pipeline_regex = re.compile('^\d*[\.\,]') # Optional regex for filtering which pipelines to include
# Get pipelines
pipelines = streak.get("pipelines").body
for pipeline in pipelines:
n_touches = 0
# Get boxes
boxes = streak.get("pipelines/%s/boxes" % pipeline['pipelineKey']).body
for box in boxes:
n_emails = int(box['gmailThreadCount'])
n_comments = int(box['commentCount'])
n_touches += n_emails
n_touches += n_comments
if sales_pipeline_regex.match(pipeline['name']):
total_touches += n_touches
# Post to Beeminder
timestamp = get_epoch_time_in_seconds()
params = {
'timestamp': timestamp,
'comment': '%s sales touches (Cron entry on %s)' % (total_touches, get_date_string()),
'value': total_touches,
'requestid': 'cron-streak-%s' % (timestamp, )
}
response = beeminder.post('streak', params=params)
print response.body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment