search.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """
  2. 搜索API
  3. """
  4. from flask import Blueprint
  5. from flask import request
  6. import api
  7. from huitun import HuiTunBrowser
  8. from util import lock_util
  9. from xhs import XhsBrowser
  10. from playwright.sync_api import Playwright, sync_playwright
  11. search_opt = Blueprint('search', __name__)
  12. @search_opt.route('/note', methods=["POST"])
  13. def search_note():
  14. """
  15. 根据关键字搜索抖音笔记
  16. :return:
  17. """
  18. request_body = request.json
  19. playwright = sync_playwright().start()
  20. browser = HuiTunBrowser(lock_util.get_idle_phone('huitun'), playwright)
  21. result = browser.search_note(request_body.get('tagName'), request_body.get('searchLimit'))
  22. xhs_browser = XhsBrowser(lock_util.get_idle_phone('xhs'), playwright)
  23. result = xhs_browser.polish_huitun_note(result)
  24. playwright.stop()
  25. return api.success(result)
  26. @search_opt.route('/hot-tag-note', methods=["POST"])
  27. def search_hot_tag_note():
  28. """
  29. 获取热门标签笔记
  30. :return:
  31. """
  32. request_body = request.json
  33. playwright = sync_playwright().start()
  34. browser = HuiTunBrowser(lock_util.get_idle_phone('huitun'), playwright)
  35. result = browser.search_note_by_hot_tag(int(request_body.get('searchLimit')))
  36. xhs_browser = XhsBrowser(lock_util.get_idle_phone('xhs'), playwright)
  37. note_list = [item for sublist in result.get('tagNotes').values() for item in sublist]
  38. xhs_browser.polish_huitun_note(note_list)
  39. playwright.stop()
  40. return api.success(result)
  41. @search_opt.route('/note-info', methods=["POST"])
  42. def search_note_info():
  43. """
  44. 根据笔记id获取笔记详情
  45. :return:
  46. """
  47. with sync_playwright() as playwright:
  48. request_body = request.json
  49. xhs_browser = XhsBrowser(lock_util.get_idle_phone('xhs'), playwright=playwright)
  50. xhs_browser.__init_browser__()
  51. note = xhs_browser.get_note(request_body.get('noteId'))
  52. return api.success(note)