__init__.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """
  2. """
  3. import json
  4. import logging
  5. from playwright.sync_api import sync_playwright, Page, Playwright
  6. import api
  7. from browser import BaseBrowser
  8. from util.lock_util import LockManager
  9. from tiktok.data_handler import *
  10. IG_URL = 'https://www.tiktok.com/'
  11. lock_manager = LockManager()
  12. class TikTokBrowser(BaseBrowser):
  13. def __init__(self, account: str, playwright=None):
  14. super().__init__(account, playwright)
  15. self.id = None
  16. def __get_name__(self):
  17. return 'tiktok'
  18. def invoke(self, func, *args, **kwargs):
  19. lock_manager.acquire_lock(self.account)
  20. try:
  21. with sync_playwright() as playwright:
  22. self.__init_browser__(playwright)
  23. return func(*args, **kwargs)
  24. finally:
  25. lock_manager.release_lock(self.account)
  26. def search_item(self, url):
  27. api.assert_not_none(url, 'url不能为空')
  28. self.result = None
  29. self.map_result = {}
  30. self.id = url.lstrip('/').split('/')[-1]
  31. self.browser.on('response', self.item_handler)
  32. self.page.goto(url=url, wait_until='commit')
  33. self.page.wait_for_timeout(2000)
  34. return self.result
  35. def item_handler(self, response):
  36. if response is None or response.status != 200 or self.result is not None:
  37. return
  38. if self.id in response.url:
  39. if '/api/item/detail/' in response.url:
  40. self.result = get_detail_by_response(response)
  41. elif response.request.resource_type == 'document':
  42. logging.info(f'get {self.id} item response')
  43. self.result = get_video_by_response(response)