__init__.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 facebook.data_handler import *
  10. FACEBOOK_URL = 'https://www.facebook.com'
  11. lock_manager = LockManager()
  12. class FackBookBrowser(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 'facebook'
  18. def invoke(self, lambda_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 lambda_func(*args, **kwargs)
  24. finally:
  25. lock_manager.release_lock(self.account)
  26. def login(self):
  27. self.page.goto(FACEBOOK_URL)
  28. self.page.wait_for_timeout(80000)
  29. def search_post(self, url):
  30. api.assert_not_none(url, 'url不能为空')
  31. self.result = None
  32. self.map_result = {}
  33. self.id = url.lstrip('/').split('/')[-1]
  34. self.browser.on('response', self.post_info_handler)
  35. self.page.goto(url)
  36. self.page.wait_for_timeout(2000)
  37. # if self.result is not None:
  38. # # 将鼠标光标放到头像上,获取粉丝等数据信息
  39. # username = self.result['actor'].get('name')
  40. # head_ele = self.page.locator(f'img[alt="{username}的头像"]')
  41. # head_ele.nth(0).hover()
  42. # self.page.wait_for_timeout(1000)
  43. return self.result
  44. def post_info_handler(self, response):
  45. if response is None or response.status != 200:
  46. return
  47. if self.id in response.url and response.request.resource_type == 'document':
  48. logging.info(f'get {self.id} post response')
  49. self.result = get_post_by_doc(response)
  50. def user_info_handler(self, response):
  51. if response is None or response.status != 200:
  52. return
  53. if '/graphql/query' in response.url:
  54. req_params = response.request.post_data_json.get('variables')
  55. if req_params is not None:
  56. req_body = json.loads(req_params)
  57. if 'userID' in req_body:
  58. self.map_result['author'] = get_user_by_request(response)