描述

需求:登录禅道,通过账号获取未关闭的任务列表。
环境:python 3 +禅道版本9.0.1
思路:登录禅道,调用二次开发API方法。
这里介绍两种接口:

  1. 方法名get_story_list_by_projectID,根据projectID获取需求列表,返回数据json格式。
  2. 方法名get_task_list_by_account,根据账号获取任务列表,返回任务列表在html的select标签中。这里通过将bytes转化为字符串,再正则匹配出需要的数据进行处理。

    先在浏览器中试试接口请求与返回数据样式

脚本

#!/usr/bin/env python3
# coding=utf-8
import requests,json,re
class Zentao_cli(object):
session = None  # 用于实现单例类,避免多次申请sessionID
sid = None
def __init__(self, url, account, password, override=False):
self.url = url
self.account = account  # 账号
self.password = password  # 密码
self.session_override = override  # 是否覆盖原会话
self.pages = { 
"sid": "/index.php?m=api&f=getSessionID&t=json",  # 获取sid的接口
"login": "/index.php?t=json&m=user&f=login&account={0}&password={1}&sid={2}",  # 登录的接口
"get_story_list_by_projectID": "/index.php?t=json&m=story&f=ajaxGetProjectStories&projectID={0}",
"get_task_list_by_account": "/task-ajaxGetUserTasks-{0}-1-{1}.json",
"get_story_list_by_account": "/index.php?"
}
self.s = None
self.sid = None
def req(self, url):
# 请求并返回结果
web = self.s.get(url)
if web.status_code == 200:
resp = json.loads(web.content)
if resp.get("status") == "success":
return True, resp
else:
return False, resp
def login(self):
if self.s is None:
if not self.session_override and Zentao_cli.session is not None:
self.s = Zentao_cli.session
self.sid = Zentao_cli.sid
else:
# 新建会话
self.s = requests.session()
res, resp = self.req(self.url.rstrip("/") + self.pages["sid"])
if res:
print("获取sessionID成功")
self.sid = json.loads(resp["data"])["sessionID"]
Zentao_cli.sid = self.sid
login_res, login_resp = self.req(
self.url.rstrip("/") + self.pages["login"].format(self.account, self.password, self.sid))
if login_res:
print("登录成功")
Zentao_cli.session = self.s
def get_story_list_by_projectID(self, projectID):
# 根据projectID获取需求列表
req_url = self.url.rstrip("/") + self.pages["get_story_list_by_projectID"].format(str(projectID))
print(req_url)
web = self.s.get(req_url)
if web.status_code == 200:
resp = json.loads(web.content.decode())
for k, v in resp.items():
print(k, v)
def get_task_list_by_account(self, accounts):
# 根据账号获取任务列表
task_list='未关闭禅道任务如下:\n'
status = ['wait','doing','done']
statusname = ['未开始', '进行中', '已完成']
for account in accounts:
for i in range(0, len(status)):
# 获取不同状态的任务,参数是账号和状态
req_url = self.url.rstrip("/") + self.pages["get_task_list_by_account"].format(account,status[i])
web = self.s.get(req_url)
if web.status_code == 200:
resp = web.content.decode("utf8")
tasks = re.compile(r' /(.*)</option>').findall(resp)
tasksID = re.compile(r'value=\'(.*)\' data-keys').findall(resp)
for j in range(0,len(tasks)):
# 用户:任务id-任务名称[状态]
summary = account+':'+tasksID[j]+'-'+tasks[j].strip() +'['+statusname[i]+']\n'
task_list = task_list+ summary
return task_list
if __name__ == "__main__":
cli = Zentao_cli("http://192.168.***.***/zentao", "summer", "123123")
cli.login()
cli.get_story_list_by_projectID(13)
task_list = cli.get_task_list_by_account(['zhaopeng','liangjia'])
print(task_list)

结果

本文地址:https://blog.csdn.net/sinat_35630008/article/details/110871942