query.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:dbas 作者: hhucn 项目源码 文件源码
def get_short_url(url, nickname, ui_locales) -> dict:
    """
    Shortens the url via external service.

    :param url: Url as string, which should be shortened
    :param nickname: current users nickname
    :param ui_locales: language of the discussion
    :rtype: dict
    :return: dictionary with the url, services name and the url of the service or an error
    """
    user.update_last_action(nickname)

    try:
        service = Shorteners.TINYURL
        service_url = 'http://tinyurl.com/'
        shortener = Shortener(service)
        short_url = format(shortener.short(url))
    except (ReadTimeout, ConnectionError, NewConnectionError) as e:
        logger('getter', 'get_short_url', repr(e), error=True)
        _tn = Translator(ui_locales)
        prepared_dict = {'error': _tn.get(_.serviceNotAvailable)}
        return prepared_dict

    prepared_dict = dict()
    prepared_dict['url'] = short_url
    prepared_dict['service'] = service
    prepared_dict['service_url'] = service_url
    prepared_dict['error'] = ''

    return prepared_dict
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号