"""
This module contains useful utility methods.
"""
import requests


def jq_format(code):
    """
    Use before throwing raw code such as 'div[tab="advanced"]' into jQuery.
    Selectors with quotes inside of quotes would otherwise break jQuery.
    This is similar to "json.dumps(value)", but with one less layer of quotes.
    """
    code = code.replace('\\', '\\\\').replace('\t', '\\t').replace('\n', '\\n')
    code = code.replace('\"', '\\\"').replace('\'', '\\\'')
    code = code.replace('\v', '\\v').replace('\a', '\\a').replace('\f', '\\f')
    code = code.replace('\b', '\\b').replace(r'\u', '\\u').replace('\r', '\\r')
    return code


def get_domain_url(url):
    """
    Use this to convert a url like this:
    https://blog.xkcd.com/2014/07/22/what-if-book-tour/
    Into this:
    https://blog.xkcd.com
    """
    url_header = url.split('://')[0]
    simple_url = url.split('://')[1]
    base_url = simple_url.split('/')[0]
    domain_url = url_header + '://' + base_url
    return domain_url


def is_xpath_selector(selector):
    """
    A basic method to determine if a selector is an xpath selector.
    """
    if (selector.startswith('/') or
            selector.startswith('./') or
            selector.startswith('(')):
        return True
    return False


def _download_file_to(file_url, destination_folder, new_file_name=None):
    if new_file_name:
        file_name = new_file_name
    else:
        file_name = file_url.split('/')[-1]
    r = requests.get(file_url)
    with open(destination_folder + '/' + file_name, "wb") as code:
        code.write(r.content)

@@ -46,16 +46,26 @@
 ds.%0A%22%22%22%0A
+import re%0A
 import r
@@ -1288,24 +1288,494 @@
 urn False%0A%0A%0A
+def is_valid_url(url):%0A    regex = re.compile(%0A        r'%5E(?:http)s?://'  # http:// or https://%0A        r'(?:(?:%5BA-Z0-9%5D(?:%5BA-Z0-9-%5D%7B0,61%7D%5BA-Z0-9%5D)?%5C.)+'%0A        r'(?:%5BA-Z%5D%7B2,6%7D%5C.?%7C%5BA-Z0-9-%5D%7B2,%7D%5C.?)%7C'  # domain...%0A        r'localhost%7C'  # localhost...%0A        r'%5Cd%7B1,3%7D%5C.%5Cd%7B1,3%7D%5C.%5Cd%7B1,3%7D%5C.%5Cd%7B1,3%7D)'  # ...or ip%0A        r'(?::%5Cd+)?'  # optional port%0A        r'(?:/?%7C%5B/?%5D%5CS+)$', re.IGNORECASE)%0A    if regex.match(url):%0A        return True%0A    else:%0A        return False%0A%0A%0A
 def _downloa

Add a method to determine if a URL is valid