B /Wb,@s<ddlZddlZddlmZmZedZGdddZdS)N)BadURLExceptionExpandingErrorExceptionu(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))c@sHeZdZdZddZdddZdddZd d Zd d Ze d dZ dS) BaseShorteneraBase Class for all shorteners. Keyword Args: proxies (dict, optional): Web proxy configuration for :ref:`Requests Proxies `. timeout (int, optional): Seconds before request is killed. verify (bool, str, optional): SSL Certificate verification for :ref:`Requests Verification `. Example: >>> class NewShortener(BaseShortener): ... api_url = 'http://the/link/for/the/api' ... def short(self, url): ... pass ... def expand(self, url): ... pass ... def custom_method(self): ... pass cKsTx$t|D]\}}t|||qWt|dd|_t|dd|_t|di|_dS)NtimeoutverifyTproxies)listitemssetattrgetattrrrr )selfkwargskeyitemr8/tmp/pip-target-ju3w756n/lib/python/pyshorteners/base.py__init__$s zBaseShortener.__init__NcCs*||}tj|||j|j||jd}|S)a;Wrap a GET request with a url check. Args: url (str): URL shortener address. Keyword Args: headers (dict): HTTP headers to add, `Requests Custom Headers`_. params (dict): URL parameters, `Requests Parameters`_. .. _Requests Custom Headers: http://requests.kennethreitz.org/en/master/user/quickstart/#custom-headers .. _Requests Parameters: http://requests.kennethreitz.org/en/master/user/quickstart/#passing-parameters-in-urls Returns: requests.Response: HTTP response. )paramsrrheadersr ) clean_urlrequestsgetrrr )rurlrrresponserrr_get-s  zBaseShortener._getc Cs.||}tj||||||j|j|jd}|S)afWrap a POST request with a url check. Args: url (str): URL shortener address. Keyword Args: data (dict, str): Form-encoded data, `Requests POST Data`_. headers (dict): HTTP headers to add, `Requests Custom Headers`_. json (dict): Python object to JSON encode for data, `Requests POST Data`_. params (dict): URL parameters, `Requests Parameters`_. .. _Requests Custom Headers: http://requests.kennethreitz.org/en/master/user/quickstart/#custom-headers .. _Requests Parameters: http://requests.kennethreitz.org/en/master/user/quickstart/#passing-parameters-in-urls .. _Requests POST Data: http://requests.kennethreitz.org/en/master/user/quickstart/#more-complicated-post-requests Returns: requests.Response: HTTP response. )datajsonrrrrr )rrpostrrr )rrrrrrrrrr_postIs  zBaseShortener._postcCstdS)zShorten URL using a shortening service. Args: url (str): URL to shorten. Raises: NotImplementedError: Subclass must override. N)NotImplementedError)rrrrrshortks zBaseShortener.shortcCs(||}||}|jr |jStdS)zExpand URL using a shortening service. Only visits the link, and returns the response url. Args: url (str): URL to shorten. Raises: ExpandingErrorException: URL failed to expand. N)rrokrr)rrrrrrexpandws  zBaseShortener.expandcCs0|dsd|}t|s,t|d|S)zURL validation. Args: url (str): URL to shorten. Raises: BadURLException: URL is not valid. )zhttp://zhttps://zhttp://z is not valid) startswithURL_REmatchr)rrrrrs   zBaseShortener.clean_url)NN)NNNN) __name__ __module__ __qualname____doc__rrr r"r$ staticmethodrrrrrrs  " r)rre exceptionsrrcompiler&rrrrrs