downloader.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # !/usr/bin/env python
  2. from selenium import webdriver
  3. from webdriver_manager.chrome import ChromeDriverManager
  4. import json
  5. import requests
  6. # article url
  7. # URL = "https://www.geeksforgeeks.org/what-can-i-do-with-python/"
  8. def get_driver():
  9. # chrome options settings
  10. chrome_options = webdriver.ChromeOptions()
  11. settings = {
  12. "recentDestinations": [
  13. {"id": "Save as PDF", "origin": "local", "account": ""}
  14. ],
  15. "selectedDestinationId": "Save as PDF",
  16. "version": 2,
  17. }
  18. prefs = {
  19. "printing.print_preview_sticky_settings.appState": json.dumps(settings)
  20. }
  21. chrome_options.add_experimental_option("prefs", prefs)
  22. chrome_options.add_argument("--kiosk-printing")
  23. # launch browser with predefined settings
  24. browser = webdriver.Chrome(
  25. executable_path=ChromeDriverManager().install(), options=chrome_options
  26. )
  27. return browser
  28. def download_article(URL):
  29. browser = get_driver()
  30. browser.get(URL)
  31. # launch print and save as pdf
  32. browser.execute_script("window.print();")
  33. browser.close()
  34. if __name__ == "__main__":
  35. URL = input("provide article URL: ")
  36. # check if the url is valid/reachable
  37. if requests.get(URL).status_code == 200:
  38. try:
  39. download_article(URL)
  40. print("Your article is successfully downloaded")
  41. except Exception as e:
  42. print(e)
  43. else:
  44. print("Enter a valid working URL")