Last active
June 9, 2018 22:40
-
-
Save amanahuja/f084492027822e28ba2e3131badf7a3b to your computer and use it in GitHub Desktop.
Shoutbase API helper utils
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
# From Shoutbase team | |
# 2018 June 04 | |
import requests | |
import time | |
import urllib | |
import csv | |
try: | |
# for Python 2.x | |
from StringIO import StringIO | |
except ImportError: | |
# for Python 3.x | |
from io import StringIO | |
### EDIT THIS #################################################################################### | |
username = 'user@domain.com' | |
password = 'PASSWORD' | |
startDate = '2018-05-21' | |
endDate = '2018-05-23' | |
tagList = [] | |
tagFilterType = "ANY" # "ANY | ALL" | |
teamName = "" | |
################################################################################################## | |
hostName = 'https://api.shoutbase.com' | |
def toEpoch(date): | |
pattern = '%Y-%m-%d' | |
epoch = int(time.mktime(time.strptime(date, pattern))) | |
return str(epoch * 1000) | |
def toTagId(tagName): | |
tag_url = hostName + '/v1/tags?name=' + urllib.quote_plus(tagName) | |
tag_response = requests.get(tag_url, auth=(username, password)) | |
tag_json = tag_response.json() | |
if len(tag_json["data"]) > 0: | |
return tag_json["data"][0]["id"] | |
else: | |
return "" | |
def toTeamId(name): | |
team_url = hostName + '/v1/teams?name=' + urllib.quote_plus(name) | |
team_response = requests.get(team_url, auth=(username, password)) | |
team_json = team_response.json() | |
if len(team_json["data"]) > 0: | |
return team_json["data"][0]["id"] | |
else: | |
return "" | |
def toTagIds(tagNames): | |
ids = list(map(lambda x: toTagId(x), tagNames)) | |
return filter(lambda x: x != "", ids) | |
def toCommas(ids): | |
return ",".join(ids) | |
tagIds = toCommas(toTagIds(tagList)) | |
teamId = toTeamId(teamName) | |
url = hostName + '/v1/export/timerecords?teamId=' + teamId + '&closedOnly=false&startsBy=' + toEpoch(startDate) + '&endsBy=' + toEpoch(endDate) + '&tagIds=' + tagIds + '&tagFilterType=' + tagFilterType | |
print(url) | |
response = requests.get(url, auth=(username, password)) | |
f = StringIO(response.text) | |
reader = csv.reader(f, delimiter=',') | |
for row in reader: | |
print('\t'.join(row)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment