def ask_question_at_csdn(title, description): """ print("getlt") # 获取lt login_lt = call_api({ "iw-apikey": 123, "iw-cmd": "getloginlt" }) login_lt = json.loads(login_lt) login_lt = login_lt["iw-response"]["iw-object"]["lt"] # 登录 result = call_api({ "iw-apikey": 123, "iw-cmd": "login", "username": "******", "password": "******", "lt": login_lt }) print("login result: " + result) """ # 提问 result = call_api({ "iw-apikey": 123, "iw-cmd": "question", "question[is_reward]": "false", "question[tag_list]": "", "question[body]": description, "question[title]": title, "question[from_type]": "ask.csdn.net" }) print("Ask result: " + result) data = json.loads(result) data = data["iw-response"]["iw-object"]["responseResultStr"] return "添加问题成功" in data
def search_answer(request): title = request.POST["title"] if request.user.is_authenticated(): user = request.user search = Search(title=title, user=user) search.save() answers = [] page = 1 while len(answers) <= 20 and page <= 10: result = call_api({ "iw-apikey": 123, "iw-cmd": "search", "p": page, "q": title }) data = json.loads(result) data = data["iw-response"]["iw-object"]["list"] for answer in data: print(answer) if not answer["link"].startswith("http://blog.csdn.net"): continue rep = [x for x in answers if x["summary"].count(answer["summary"][:-12]) != 0 or answer["summary"].count(x["summary"][:-12]) != 0] print(len(rep)) if len(rep) == 0: answers.append(answer) page += 1 return render(request, "answerlist.html", {"answers": answers})
def get_detail(request, link): link = link.replace("http://blog.csdn.net","") print(link) result = call_api({ "iw-apikey": 123, "iw-cmd": "resultcontent", "iw_ir_1": link }) data = json.loads(result) print(data) data = data["iw-response"]["iw-object"]["content"] return render(request, "detailpage.html", {"content": data})
def get_detail(request): """ 获得详细结果页面 前置条件 该接口要求POST请求 参数 "link": 详细页面URL 返回数据 见燕风API """ link = request.POST["link"] result = call_api({ "iw-apikey": 123, "iw-cmd": "resultcontent", "iw_ir_1": link }) data = json.loads(result) print(data) return JsonResponse(data)
def search_answer(request): """ 搜索问题 前置条件 该接口要求POST请求 参数 "title": 问题标题 "username": 用户名 返回数据 见燕风API """ title = request.POST["title"] username = request.POST["username"] user = User.objects.get(username=username) search = Search(title=title, user=user) search.save() answers = [] page = 1 while len(answers) <= 20 and page <= 10: result = call_api({ "iw-apikey": 123, "iw-cmd": "search", "p": page, "q": title }) data = json.loads(result) data = data["iw-response"]["iw-object"]["list"] for answer in data: print(answer) rep = [x for x in answers if x["summary"].count(answer["summary"][:-12]) != 0 or answer["summary"].count(x["summary"][:-12]) != 0] print(len(rep)) if len(rep) == 0: answers.append(answer) page += 1 return JsonResponse(answers, safe=False)