예제 #1
0
파일: views.py 프로젝트: songxtianx/JuWenDa
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})
예제 #2
0
파일: web_views.py 프로젝트: ashang/JuWenDa
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)