SHARE
TWEET

Untitled

a guest Aug 24th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import time
  3. import hashlib
  4.  
  5. from urllib3 import PoolManager
  6. from selenium import webdriver
  7.  
  8. from selenium.common.exceptions import UnexpectedAlertPresentException
  9.  
  10. from dotenv import load_dotenv
  11.  
  12. from src.utils import get_valid_filename, create_temp_folder, write_to_file
  13.  
  14. load_dotenv()
  15.  
  16. http = PoolManager()
  17.  
  18. if not os.path.exists('temp'):
  19.     os.makedirs('temp')
  20.  
  21. options = webdriver.ChromeOptions()
  22. options.add_argument('--headless')
  23. options.add_argument('--disable-gpu')
  24. options.add_argument('--disable-dev-shm-usage')
  25. options.add_argument('--no-sandbox')
  26. options.add_argument('--disable-application-cache')
  27.  
  28. base_url = os.getenv('PREVIEW_URL')
  29. embed_js = os.getenv('EMBED_JS')
  30. embed_css = os.getenv('EMBED_CSS')
  31.  
  32. chrome_driver = webdriver.Chrome(
  33.     os.getenv("CHROME_DRIVER_PATH"),
  34.     options=options
  35. )
  36.  
  37. chrome_driver.set_window_size(1428, 1280)
  38.  
  39.  
  40. def take_screenshot(url):
  41.     file_name = f'scr_0_{int(time.time())}.png'
  42.     path = f'temp/{file_name}'
  43.  
  44.     chrome_driver.get(url)
  45.  
  46.     exep = True
  47.     exep_count = 0
  48.  
  49.     while exep and exep_count < 10:
  50.         try:
  51.             chrome_driver.save_screenshot(path)
  52.             exep = False
  53.         except UnexpectedAlertPresentException:
  54.             chrome_driver.switch_to.alert.dismiss()
  55.             exep_count += 1
  56.             exep = True
  57.  
  58.     if exep_count == 10:
  59.         return 'assets/preview-failed.png'
  60.  
  61.     return path
  62.  
  63.  
  64. def download_page(id):
  65.     fpath = download_assets_for('page', id)
  66.     source = driver_get_page(id)
  67.     write_to_file(source, f'{fpath}/page-{id}.html')
  68.  
  69.     return fpath
  70.  
  71.  
  72. def download_funnel(id, pages):
  73.     fpath = download_assets_for('funnel', id)
  74.  
  75.     for page in pages:
  76.         source = driver_get_page(page.id)
  77.         file_path = f'{fpath}/{get_valid_filename(page.name)}_{page.id}.html'
  78.         write_to_file(source, file_path)
  79.  
  80.     return fpath
  81.  
  82.  
  83. def driver_get_page(id):
  84.     m = hashlib.md5()
  85.     m.update(f'{id}igloo-preview'.encode('utf-8'))
  86.     code = m.hexdigest()
  87.     chrome_driver.get(f'{base_url}{id}?code={code}')
  88.     source = chrome_driver.page_source
  89.     source = source.replace(embed_js, 'assets/embed.js')
  90.     source = source.replace(embed_css, 'assets/embed.css')
  91.     return source
  92.  
  93.  
  94. def driver_download_asset(fpath, path):
  95.     r = http.request('GET', path)
  96.     with open(fpath, 'wb') as f:
  97.         f.write(r.data)
  98.         f.close()
  99.  
  100.  
  101. def download_assets_for(prefix, id):
  102.     fpath = create_temp_folder(prefix, id)
  103.     driver_download_asset(f'{fpath}/embed.js', embed_js)
  104.     driver_download_asset(f'{fpath}/embed.css', embed_css)
  105.  
  106.     return fpath
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top