Example #1
0
def expand(text):
    """<url> - unshortens <url>"""
    args = text.split()
    url = args[0]

    try:
        return web.expand(url)
    except web.ServiceError as e:
        return e.message
Example #2
0
def expand(text, reply):
    """<url> - unshortens <url>"""
    args = text.split()
    url = args[0]

    try:
        return web.expand(url)
    except web.ServiceError as e:
        reply(str(e))
        raise
Example #3
0
def amazon_short_url(match, reply):
    '''Expand redirect from Amazon URL shorteners'''
    try:
        loc = web.expand(match.group(0))
        match = AMAZON_RE.search(loc)
        if match:
            return amazon_url(match, reply)
        else:
            return url + " didn't redirect to an Amazon product"
    except Exception as ex:
        reply('Failed to get redirect: {}'.format(ex))
        raise
Example #4
0
def gitio(text):
    """<url> [custom] - shortens a github URL <url> using git.io with [custom] as an optional custom shortlink, or unshortens <url> if already short"""
    args = text.split()
    url = args[0]
    custom = args[1] if len(args) > 1 else None

    try:
        if 'git.io' in url:
            return web.expand(url, 'git.io')
        else:
            return web.shorten(url, custom, 'git.io')
    except web.ServiceError as e:
        return e.message
Example #5
0
def isgd(text):
    """<url> [custom] - shortens a url using is.gd with [custom] as an optional custom shortlink, or unshortens <url> if already short"""
    args = text.split()
    url = args[0]
    custom = args[1] if len(args) > 1 else None

    try:
        if 'is.gd' in url:
            return web.expand(url, 'is.gd')
        else:
            return web.shorten(url, custom, 'is.gd')
    except web.ServiceError as e:
        return e.message
Example #6
0
def gitio(text):
    """<url> [custom] - shortens a github URL <url> using git.io with [custom] as an optional custom shortlink,
    or unshortens <url> if already short"""
    args = text.split()
    url = args[0]
    custom = args[1] if len(args) > 1 else None

    try:
        if 'git.io' in url:
            return web.expand(url, 'git.io')
        else:
            return web.shorten(url, custom, 'git.io')
    except web.ServiceError as e:
        return e.message
Example #7
0
def isgd(text):
    """<url> [custom] - shortens a url using is.gd with [custom] as an optional custom shortlink,
    or unshortens <url> if already short"""
    args = text.split()
    url = args[0]
    custom = args[1] if len(args) > 1 else None

    try:
        if 'is.gd' in url:
            return web.expand(url, 'is.gd')
        else:
            return web.shorten(url, custom, 'is.gd')
    except web.ServiceError as e:
        return e.message
Example #8
0
def googl(text, reply):
    """<url> [custom] - shorten <url> using goo.gl with [custom] as an option custom shortlink,
    or unshortens <url> if already short"""
    args = text.split()
    url = args[0]
    custom = args[1] if len(args) > 1 else None

    try:
        if 'goo.gl' in url:
            return web.expand(url, 'goo.gl')
        else:
            return web.shorten(url, custom, 'goo.gl')
    except web.ServiceError as e:
        reply(e.message)
        raise
Example #9
0
def test_expand(mock_requests):
    mock_requests.add(
        mock_requests.GET,
        'http://is.gd/forward.php?shorturl=https%3A%2F%2Fis.gd%2Ffoobar&format=json',
        json={'url': 'https://example.com'})
    from cloudbot.util import web
    assert web.expand('https://is.gd/foobar',
                      service='is.gd') == 'https://example.com'
    assert web.expand('https://is.gd/foobar') == 'https://example.com'

    mock_requests.replace(
        mock_requests.GET,
        'http://is.gd/forward.php?shorturl=https%3A%2F%2Fis.gd%2Ffoobar&format=json',
        status=404)

    with pytest.raises(web.ServiceHTTPError):
        web.expand('https://is.gd/foobar')

    mock_requests.replace(
        mock_requests.GET,
        'http://is.gd/forward.php?shorturl=https%3A%2F%2Fis.gd%2Ffoobar&format=json',
        json={'errormessage': 'Error'})

    with pytest.raises(web.ServiceHTTPError):
        web.expand('https://is.gd/foobar')

    mock_requests.reset()

    with pytest.raises(web.ServiceError):
        web.expand('https://is.gd/foobar')

    with pytest.raises(web.ServiceError):
        web.expand('http://my.shortener/foobar')

    mock_requests.add(mock_requests.GET,
                      'http://my.shortener/foobar',
                      headers={'Location': 'https://example.com'})
    assert web.expand('http://my.shortener/foobar') == 'https://example.com'

    mock_requests.replace(mock_requests.GET,
                          'http://my.shortener/foobar',
                          status=404)
    with pytest.raises(web.ServiceHTTPError):
        web.expand('http://my.shortener/foobar')

    mock_requests.replace(mock_requests.GET, 'http://my.shortener/foobar')
    with pytest.raises(web.ServiceHTTPError):
        web.expand('http://my.shortener/foobar')

    with pytest.raises(web.ServiceError):
        web.expand('http://goo.gl/foobar')

    mock_requests.add(mock_requests.GET,
                      'https://www.googleapis.com/urlshortener/v1/url',
                      status=404)

    with pytest.raises(web.ServiceHTTPError):
        web.expand('http://goo.gl/foobar')

    mock_requests.replace(mock_requests.GET,
                          'https://www.googleapis.com/urlshortener/v1/url',
                          json={'error': {
                              'message': 'Error'
                          }})

    with pytest.raises(web.ServiceHTTPError):
        web.expand('http://goo.gl/foobar')

    mock_requests.replace(mock_requests.GET,
                          'https://www.googleapis.com/urlshortener/v1/url',
                          json={'longUrl': 'https://example.com'})

    assert web.expand('http://goo.gl/foobar') == 'https://example.com'
Example #10
0
def test_expand(mock_requests):
    mock_requests.add(
        mock_requests.GET,
        "http://is.gd/forward.php?shorturl=https%3A%2F%2Fis.gd%2Ffoobar&format=json",
        json={"url": "https://example.com"},
    )
    from cloudbot.util import web

    assert (web.expand("https://is.gd/foobar",
                       service="is.gd") == "https://example.com")
    assert web.expand("https://is.gd/foobar") == "https://example.com"

    mock_requests.replace(
        mock_requests.GET,
        "http://is.gd/forward.php?shorturl=https%3A%2F%2Fis.gd%2Ffoobar&format=json",
        status=404,
    )

    with pytest.raises(web.ServiceHTTPError):
        web.expand("https://is.gd/foobar")

    mock_requests.replace(
        mock_requests.GET,
        "http://is.gd/forward.php?shorturl=https%3A%2F%2Fis.gd%2Ffoobar&format=json",
        json={"errormessage": "Error"},
    )

    with pytest.raises(web.ServiceHTTPError):
        web.expand("https://is.gd/foobar")

    mock_requests.reset()

    with pytest.raises(web.ServiceError):
        web.expand("https://is.gd/foobar")

    with pytest.raises(web.ServiceError):
        web.expand("http://my.shortener/foobar")

    mock_requests.add(
        mock_requests.GET,
        "http://my.shortener/foobar",
        headers={"Location": "https://example.com"},
    )
    assert web.expand("http://my.shortener/foobar") == "https://example.com"

    mock_requests.replace(mock_requests.GET,
                          "http://my.shortener/foobar",
                          status=404)
    with pytest.raises(web.ServiceHTTPError):
        web.expand("http://my.shortener/foobar")

    mock_requests.replace(mock_requests.GET, "http://my.shortener/foobar")
    with pytest.raises(web.ServiceHTTPError):
        web.expand("http://my.shortener/foobar")

    with pytest.raises(web.ServiceError):
        web.expand("http://goo.gl/foobar")

    mock_requests.add(
        mock_requests.GET,
        "https://www.googleapis.com/urlshortener/v1/url",
        status=404,
    )

    with pytest.raises(web.ServiceHTTPError):
        web.expand("http://goo.gl/foobar")

    mock_requests.replace(
        mock_requests.GET,
        "https://www.googleapis.com/urlshortener/v1/url",
        json={"error": {
            "message": "Error"
        }},
    )

    with pytest.raises(web.ServiceHTTPError):
        web.expand("http://goo.gl/foobar")

    mock_requests.replace(
        mock_requests.GET,
        "https://www.googleapis.com/urlshortener/v1/url",
        json={"longUrl": "https://example.com"},
    )

    assert web.expand("http://goo.gl/foobar") == "https://example.com"