Advertisement
Guest User

Untitled

a guest
Jan 15th, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.27 KB | None | 0 0
  1. import asyncio
  2. from concurrent.futures import ThreadPoolExecutor
  3. import sys
  4.  
  5. from playwright.sync_api import BrowserContext, sync_playwright
  6. from playwright.sync_api._generated import ConsoleMessage
  7.  
  8.  
  9. class BaseBrowser:
  10.     DEFAULT_CONTEXT = 'Desktop Safari'
  11.  
  12.     def __init__(
  13.         self,
  14.         /,
  15.         device_name: str | None = None,
  16.         headless: bool = True,
  17.     ) -> None:
  18.         self._device_name = device_name or self.DEFAULT_CONTEXT
  19.         self._headless = headless
  20.    
  21.     def start(self) -> BrowserContext:
  22.         self._playwright = sync_playwright().start()
  23.         self._browser = self._playwright.webkit.launch(
  24.             headless=self._headless,
  25.         )
  26.         device = self._playwright.devices[self._device_name]
  27.         self._context = self._browser.new_context(**device)
  28.         return self._context
  29.    
  30.     def close(self) -> None:
  31.         self._context.close()
  32.         self._browser.close()
  33.         self._playwright.stop()
  34.         self._set_asyncio_policy(asyncio.WindowsSelectorEventLoopPolicy())
  35.  
  36.     def _set_asyncio_policy(
  37.         self,
  38.         policy: asyncio.WindowsSelectorEventLoopPolicy | asyncio.DefaultEventLoopPolicy | None
  39.     ) -> None:
  40.         if sys.version_info >= (3, 8) and sys.platform.lower().startswith('win'):
  41.             asyncio.set_event_loop_policy(policy)
  42.  
  43.  
  44. class Hsw(BaseBrowser):
  45.     def __init__(
  46.         self,
  47.         token: str
  48.     ) -> None:
  49.         super().__init__()
  50.         self._token = token
  51.  
  52.     async def get(self) -> str:
  53.         with ThreadPoolExecutor() as pool:
  54.             self._set_asyncio_policy(asyncio.DefaultEventLoopPolicy())
  55.             loop = asyncio.get_running_loop()
  56.             token = await loop.run_in_executor(pool, self._get_hsw, 'https://newassets.hcaptcha.com/c/2458d9b/hsw.js')
  57.         return token
  58.    
  59.     def _on_token(self, message: ConsoleMessage) -> None:
  60.         self._hsw_token = message.text
  61.  
  62.     def _get_hsw(self, url: str) -> str:
  63.         self._hsw_token = None
  64.         context = self.start()
  65.         page = context.new_page()
  66.         page.on('console', self._on_token)
  67.         page.add_script_tag(url=url)
  68.         # how to get function result?
  69.         page.evaluate('hsw("%s").then(res => console.log(res))' % self._token)
  70.         self.close()
  71.         return self._hsw_token
  72.  
  73.  
  74.  
  75. async def main():
  76.     token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmIjowLCJzIjoyLCJ0IjoidyIsImQiOiJuZVZwYWZrT0FRNnhNNUJCV3Vwa01HdWM0ZFVxWGVRS0YwY0xiRlA5L2MrTVo0WlkxaWlvT3hvY3lqRFE4RGJYT2R6MGFsVmFmeEhENVBIWHUyYWFjUHBNUXRIL0t1RUEvNndDRVVGNWhIQnJEMUNuNUZnd2JwdkdOUVJ1WkdXVXZrTlVFRnJhS0lnaW9uOTg1bHl2eW1jQmU4SmlES25oc3MrcklsdkVGU2lPSEpJQ1JpNVo3Mzd3RzM4eDlKZC9adGNBTngyaEdFYy91THpSaUE4OUNPdmU2RVdXTG4rTWNTTmxwVVE4WHVDTmt4akFwWmlodTJzN1B3L3daUzNRIiwibCI6Imh0dHBzOi8vbmV3YXNzZXRzLmhjYXB0Y2hhLmNvbS9jLzNhODRjMTUiLCJpIjoic2hhMjU2LXV4L0ZDV2M4ZS9vWTN1cGFINnRVeWpQN2hMbGhwR295OG45ZVdWdVdNS009IiwiZSI6MTcwNTMyOTA3MywibiI6ImhzdyIsImMiOjEwMDB9.lZsCbqodE16TzXJclh4oYIVHmJH12zV23BvzJEzFo8c'
  77.     hsw = Hsw(token)
  78.     hsw_token = await hsw.get()
  79.     print(hsw_token)
  80.  
  81.  
  82. if __name__ == '__main__':
  83.     if sys.version_info >= (3, 8) and sys.platform.lower().startswith('win'):
  84.         asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
  85.     asyncio.run(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement