# Retrieve data from toggl API # ---------------------------- # for workspace in toggl.getWorkspaces(): # print("Workspace: {}; id: {}".format(workspace["name"],workspace["id"])) request_params = { "workspace_id": id_vals["id"], "user_agent": id_vals["uagent"], "since": start_date_str, "until": end_date_str, } # fetch data via API call response = toggl.request("https://toggl.com/reports/api/v2/details", parameters=request_params) # temporary: read data from local file # infile = open("{}wochenbericht_temp_input.txt".format(id_vals["outpath"]), # "r", encoding="utf-8") # response = infile.read() # infile.close() # Extract data # ------------ # totalTime = response["total_grand"] # totalTime = timedelta(milliseconds=response["total_grand"]) taskData = response["data"]
# create dict entry # ----------------- curWeek = {} curWeek["Mon"] = curMonday curWeek["Sun"] = curSunday curWeek["times"] = {} requestParams = { "workspace_id": idVals["id"], "user_agent": idVals["uagent"], "grouping": "projects", "since": curMonday, "until": curSunday, } response = toggl.request("https://toggl.com/reports/api/v2/summary", parameters=requestParams) print("retrieved {} - {}. Sleeping...".format(curMonday,curSunday)) #testOutput.append(response["data"]) #testOutput.append("="*50) sleep(2) #testOutput.append(curMonday) responseData = response["data"] for item in responseData: curProject = item["title"]["project"] curTime = item["time"] #testOutput.append(item["time"]) #testOutput.append(item["title"]["project"]) #testOutput.append(curProject) #testOutput.append(curTime) curTime = timedelta(milliseconds = curTime)
class TogglPyTests(unittest.TestCase): def setUp(self): self.api_key = os.environ['TOGGL_API_KEY'] if self.api_key is None: raise Exception("Unable to execute api tests without an api key") self.workspace_id = os.environ['WORKSPACE_ID'] if self.workspace_id is None: raise Exception( "Unable to execute api tests without a workspace key to query") self.toggl = Toggl() self.toggl.setAPIKey(self.api_key) def test_connect(self): response = self.toggl.request("https://www.toggl.com/api/v8/clients") self.assertTrue(response is not None) def test_putTimeEntry(self): request_args = { 'workspace_id': self.workspace_id, } entries = self.toggl.getDetailedReport(request_args) #for this tests I'm tagging my Pomodoro Entries missing_projects = [ r for r in entries['data'] if r['project'] is None and 'Pomodoro' in r['description'] ] me = missing_projects[0] me_id = me['id'] #remember for later #I've tagged my pomodoro entries as Self/Self cp = self.toggl.getClientProject("Self", "Self") project_id = cp['data']['id'] me['pid'] = project_id #his is the new stuff response = self.toggl.putTimeEntry({"id": me_id, "pid": project_id}) self.assertTrue(response is not None) self.assertTrue('data' in response) self.assertTrue(response['data']['pid'] == project_id) def test_getDetailedReportCSV(self): data = { 'workspace_id': self.workspace_id, } csvfile = 'data.csv' self.toggl.getDetailedReportCSV(data, csvfile) self.assertTrue(os.path.isfile(csvfile)) os.remove(csvfile) data = self.toggl.getDetailedReportCSV(data) self.assertTrue(data is not None) def test_getDetailedReport(self): data = { 'workspace_id': self.workspace_id, } d = self.toggl.getDetailedReport(data) self.assertTrue(d is not None) self.assertTrue(len(d.keys()) > 0) fields = ['total_count', 'total_currencies', 'total_billable', 'data'] for f in fields: self.assertTrue(f in d.keys()) data = d['data'] self.assertTrue(len(data) > 0) dr = data[0] self.assertTrue('client' in dr) self.assertTrue('start' in dr) self.assertTrue('end' in dr) self.assertTrue('task' in dr) self.assertTrue('user' in dr) self.assertTrue('project' in dr)
config = configparser.ConfigParser() config.read(settings_ini_file) start_date = config.get('Time', 'StartDate') end_date = config.get('Time', 'EndDate') internal_time = config.get('Time', 'InternalTime') display_time = config.get('Time', 'DisplayTime') dump_path = config.get('IO', 'DumpPath') print('starting API') # Initial setup toggl = Toggl() toggl.setAPIKey(api_key) params = {'start_date': start_date, 'end_date': end_date} print('Making request') # Get the raw data on time entries response = toggl.request(time_entry_url, parameters=params) print('%d records' % len(response)) df = pd.DataFrame(response) # Get a list of project ids pids = df['pid'].unique() pids = pids[np.isfinite(pids)].astype(int) # Add column with name of project, so not just project id in df print('Making project request') response = [toggl.request(pid_req_fmt % pp)['data'] for pp in pids] print('%d records' % len(response)) pid_df = pd.DataFrame(response) # Note: id in this data struct is the same as pid in df! pid_df.set_index('id', drop=True, inplace=True) df['proj_name'] = df['pid'].map(pid_df['name'])