# Path: plumbing/scraping/browser.py
# def download_via_browser(url,
#                          destination = None,
#                          uncompress  = True,
#                          timeout     = 120):
#     """
#     Save the resource as a file on disk by running a full browser to avoid
#     being blocked by the server.
#     Note: While the file is downloading, it will have ".crdownload" appended
#     to it.
#     """
#     # Choose the right option for destination #
#     destination = handle_destination(url, destination)
#     # Make a driver if we don't already have one #
#     driver, download_dir = make_driver()
#     # Get resource #
#     driver.get(url)
#     # This should remove the query part of the URL #
#     predicted_name = url.split("/")[-1].split("?")[0]
#     # But we need to convert things like %20 to a space #
#     predicted_name = urllib.parse.unquote(predicted_name)
#     # It should land in the downloads directory #
#     result = download_dir + predicted_name
#     # We have no way to know when it finishes #
#     start_time = time.time()
#     while True:
#         time.sleep(0.5)
#         if result.exists: break
#         if time.time() - start_time > timeout:
#             msg = "The file should have been at '%s'." % result
#             raise Exception("The timeout was exceeded. " + msg)
#     # Let's move it #
#     result.move_to(destination, overwrite=True)
#     # Uncompress #
#     if uncompress:
#         with open(destination, 'rb') as f: header = f.read(4)
#         if header == b"PK\x03\x04": destination.unzip_to(inplace=True)
#     # Return #
#     return destination


from plumbing.scraping.browser import download_via_browser
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Script to test the functionality of the scraping module.

Written by Lucas Sinclair. MIT Licensed.

Call it like this:

    $ ipython3 -i ~/repos/plumbing/test/scraping/test_user_agent.py
"""

# Internal modules #

# Constants #
url = "http://speedtest.tele2.net/1MB.zip"
destination = '~/test2/'

###############################################################################

# Path: plumbing/scraping/browser.py
# def download_via_browser(url,
#                          destination = None,
#                          uncompress  = True,
#                          timeout     = 120):
#     """
#     Save the resource as a file on disk by running a full browser to avoid
#     being blocked by the server.
#     Note: While the file is downloading, it will have ".crdownload" appended
#     to it.
#     """
#     # Choose the right option for destination #
#     destination = handle_destination(url, destination)
#     # Make a driver if we don't already have one #
#     driver, download_dir = make_driver()
#     # Get resource #
#     driver.get(url)
#     # This should remove the query part of the URL #
#     predicted_name = url.split("/")[-1].split("?")[0]
#     # But we need to convert things like %20 to a space #
#     predicted_name = urllib.parse.unquote(predicted_name)
#     # It should land in the downloads directory #
#     result = download_dir + predicted_name
#     # We have no way to know when it finishes #
#     start_time = time.time()
#     while True:
#         time.sleep(0.5)
#         if result.exists: break
#         if time.time() - start_time > timeout:
#             msg = "The file should have been at '%s'." % result
#             raise Exception("The timeout was exceeded. " + msg)
#     # Let's move it #
#     result.move_to(destination, overwrite=True)
#     # Uncompress #
#     if uncompress:
#         with open(destination, 'rb') as f: header = f.read(4)
#         if header == b"PK\x03\x04": destination.unzip_to(inplace=True)
#     # Return #
#     return destination


# Path: testing/scraping/test_download_driver.py
from plumbing.scraping.browser import download_via_browser

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Script to test the functionality of the scraping module.

Written by Lucas Sinclair. MIT Licensed.

Call it like this:

    $ ipython3 -i ~/repos/plumbing/test/scraping/test_user_agent.py
"""

# Internal modules #

# Constants #
url = "http://speedtest.tele2.net/1MB.zip"
destination = '~/test2/'

###############################################################################

download_via_browser(url, destination)