用于与toggl api交互的python库。
Toggl的Python项目详细描述
togglpy
togglpy是一个用于与Toggl API交互的python库。
功能
- 对任何(toggl)api端点发出请求,并将请求数据作为字典
- 生成并保存摘要、每周或详细报告的PDF
- 以json格式获取报告
- 获取所有工作区或所有客户端
- 通过id或name获取特定的工作区或客户机
- 按客户或单个名称查询项目
- 添加自定义时间项
设置
- 使用pip: 安装项目
pip install -U TogglPy
- 导入内容:
fromtoggl.TogglPyimportToggl
- 创建toggl对象:
toggl=Toggl()
- 通过toggl凭据或使用your personal API token:
toggl.setAuthCredentials('<EMAIL>','<PASSWORD>')
或:
toggl.setAPIKey('<API-TOKEN>')
我通过示例学习得最好:
针对任何toggl端点的手动get请求:
fromtoggl.TogglPyimportToggl# create a Toggl object and set our API key toggl=Toggl()toggl.setAPIKey("mytogglapikey")response=toggl.request("https://www.toggl.com/api/v8/clients")# print the client name and id for each client in the response# list of returned values can be found in the Toggl docs:# https://github.com/toggl/toggl_api_docs/blob/master/chapters/clients.mdforclientinresponse:print"Client name: %s Client id: %s"%(client['name'],client['id'])
或者,如果您想在请求中添加一些数据:
data={'id':42,'some_key':'some_value','user_agent':'TogglPy_test',}response=toggl.request("https://www.toggl.com/api/v8/some/endpoint",parameters=data)
向任何toggl终结点发出post请求:
data={"project":{"name":"some project","wid":777,"template_id":10237,"is_private":true,"cid":123397}}response=toggl.postRequest("https://www.toggl.com/api/v8/projects",parameters=data)
生成pdf报告:
# specify that we want reports from this weekdata={'workspace_id':0000,# see the next example for getting a workspace id'since':'2015-04-27','until':'2015-05-03',}# download one of each type of report for this time periodtoggl.getWeeklyReportPDF(data,"weekly-report.pdf")toggl.getDetailedReportPDF(data,"detailed-report.pdf")toggl.getSummaryReportPDF(data,"summary-report.pdf")
寻找工作区和客户
这将打印一些原始数据,这些数据将为您提供快速识别客户机和工作区所需的所有信息:
printtoggl.getWorkspaces()printtoggl.getClients()
如果您想稍微清理一下,请用
forworkspaceintoggl.getWorkspaces():print"Workspace name: %s\tWorkspace id:%s"%(workspace['name'],workspace['id'])forclientintoggl.getClients():print"Client name: %s\tClient id:%s"%(client['name'],client['id'])
如果要查找特定的客户端或工作区:
john_doe=toggl.getClient(name="John Doe")personal=toggl.getWorkspace(name="Personal")print"John's client id is %s"%john_doe['id']print"The workspace id for 'Personal' is %s"%personal['id']
反之亦然;使用.getClient(id=0000)
或.getWorkspace(id=000)
按ID查找项目。
启动新计时器
# You can get your project pid in toggl.com->Projects->(select your project)# and copying the last number of the urlmyprojectpid=10959693toggl.startTimeEntry("my description",myprojectpid)
停止电流计时器
currentTimer=currentRunningTimeEntry()stopTimeEntry(currentTimer['data']['id'])
创建自定义时间项
# Create a custom entry for today, of a 9 hour duration, starting at 10 AM:toggl.createTimeEntry(hourduration=9,projectname='GoogleDrive',hour=10)# Or speed up the query process and provide the clien't name:toggl.createTimeEntry(hourduration=9,projectname='GoogleDrive',clientname='Google',hour=10)# Provide *month* and/or *day* too for specific dates:toggl.createTimeEntry(hourduration=9,projectname='GoogleDrive',clientname='Google',month=1,day=31,hour=10)# Automate missing time entries!fordayin(29,30,31):toggl.createTimeEntry(hourduration=9,projectname='someproject',day=day,hour=10)
自动化日常记录
# toggle_entry.pyimportdatetimeifdatetime.datetime.today().weekday()notin(4,5):toggl.createTimeEntry(hourduration=9,projectname='someproject',hour=10)
将每日记录添加为cron作业:
(crontab -l ;echo"0 22 * * * toggl_entry.py")| crontab -