class Toggl2GSuiteTest(unittest.TestCase): def setUp(self): self.api_key = os.environ["TOGGL_API_KEY"] self.toggl = Toggl() self.toggl.setAPIKey(self.api_key) # see https://stackoverflow.com/questions/19153462/get-excel-style-column-names-from-column-number LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" @staticmethod def excel_style(row, col): """ Convert given row and column number to an Excel-style cell name. """ result = [] while col: col, rem = divmod(col - 1, 26) result[:0] = Toggl2GSuiteTest.LETTERS[rem] return "".join(result) + str(row) def test_toggl2gsuite(self): # have to do this year by year data = { "workspace_id": os.environ["WORKSPACE_ID"], } y = self.toggl.getDetailedReport(data) credentials = ServiceAccountCredentials.from_json_keyfile_name( os.environ["KEYFILE"], ["https://spreadsheets.google.com/feeds"] ) client = gspread.authorize(credentials) sheet = client.open_by_url(os.environ["SHEET_URL"]) worksheet = sheet.get_worksheet(0) wrote_header = False columns_to_write = [ "user", "updated", "start", "end", "client", "project", "description", "is_billable", "billable", ] cell_row = 0 for row_idx, rec in enumerate(y["data"]): if wrote_header == False: for col_idx, header in enumerate(columns_to_write): worksheet.update_acell( Toggl2GSuiteTest.excel_style(row_idx + 1, col_idx + 1), header ) wrote_header = True for col_idx, header in enumerate(columns_to_write): worksheet.update_acell( Toggl2GSuiteTest.excel_style(row_idx + 2, col_idx + 1), rec[header] )
class Toggl2GSuiteTest(unittest.TestCase): def setUp(self): self.api_key = os.environ['TOGGL_API_KEY'] self.toggl = Toggl() self.toggl.setAPIKey(self.api_key) # see https://stackoverflow.com/questions/19153462/get-excel-style-column-names-from-column-number LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' @staticmethod def excel_style(row, col): """ Convert given row and column number to an Excel-style cell name. """ result = [] while col: col, rem = divmod(col - 1, 26) result[:0] = Toggl2GSuiteTest.LETTERS[rem] return ''.join(result) + str(row) def test_toggl2gsuite(self): # have to do this year by year data = { 'workspace_id': os.environ['WORKSPACE_ID'], } y = self.toggl.getDetailedReport(data) credentials = ServiceAccountCredentials.from_json_keyfile_name( os.environ['KEYFILE'], ['https://spreadsheets.google.com/feeds']) client = gspread.authorize(credentials) sheet = client.open_by_url(os.environ['SHEET_URL']) worksheet = sheet.get_worksheet(0) wrote_header = False columns_to_write = ['user', 'updated', 'start', 'end', 'client', 'project', 'description', 'is_billable', 'billable'] cell_row = 0 for row_idx, rec in enumerate(y['data']): if wrote_header == False: for col_idx, header in enumerate(columns_to_write): worksheet.update_acell(Toggl2GSuiteTest.excel_style(row_idx + 1, col_idx + 1), header) wrote_header = True for col_idx, header in enumerate(columns_to_write): worksheet.update_acell(Toggl2GSuiteTest.excel_style(row_idx + 2, col_idx + 1), rec[header])
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://api.track.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)
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)
def run(): # Connect with Google Calendar from google.auth import app_engine import googleapiclient.discovery # Second method using OAuth 2.0 from google.oauth2 import service_account project = "toggltocalendar" SCOPES = ['https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/calendar.events'] credentials = service_account.Credentials.from_service_account_file(os.environ['GOOGLE_APPLICATION_CREDENTIALS'], scopes=SCOPES) # Explicitly use App Engine credentials. These credentials are # only available when running on App Engine Standard. # credentials = app_engine.Credentials(scopes=SCOPES) # Explicitly pass the credentials to the client library. # storage_client = googleapiclient.discovery.build('storage', 'v1', credentials=credentials) # Make an authenticated API request # buckets = storage_client.buckets().list(project=project).execute() # print(buckets) # Connect with Google Calendar to read and create events service = googleapiclient.discovery.build('calendar', 'v3', credentials=credentials) calendars = service.calendarList().list().execute() print(calendars) # Create toggl object toggl = Toggl() toggl.setAPIKey('2e8cb0eb95b4d98c98564e4ba8ee26ee') duration = 1 # day(s) until = datetime.datetime.now() since = until - datetime.timedelta(days=duration) page = 1 more_results = True # Run while we have more results on the next page while (more_results): data = { 'workspace_id': 2705063, 'since': since.strftime("%Y-%m-%d"), 'until': until.strftime("%Y-%m-%d"), 'page': page } response = toggl.getDetailedReport(data) df = pd.DataFrame(response['data']) # For each new entry, create a new Calendar entry for i, row in df.iterrows: createNewCalendarEntry(i) page += 1 total_results = response['total_count'] more_results = ((page-1) * 50 < total_results)