Ejemplo n.º 1
0
def short(url, custom_string=None):
    """
    Perform the shortening of given url. Optionally use a custom string
    instead of using internal algorithm provided by core module
    
    Params
        url - the url to shorten: url
        custom_string - the string to use to short the link: string
    
    Return
        shorted url: url string
    
    """    
    domain_name = parse_domain(url)
    
    if is_blacklisted(domain_name):
        raise DomainBlacklistedError(domain_name)
    
    if custom_string and shorting_frag_already_exists(custom_string, False):
        raise URLExistsError(custom_string)
    
    domain = get_or_create_domain(domain_name)
    urlbox = None
    
    if custom_string:
        urlbox = UrlBox()
        urlbox.shorting_frag = custom_string
        urlbox.custom = True
    else:
        urlbox = respawn_urlbox(domain)
        if not urlbox:
            urlbox = UrlBox()
            short = compute_next(domain.last_shorting_frag_assigned)
            while shorting_frag_already_exists(domain, short):
                short = compute_next(short)
            urlbox.shorting_frag = short
            urlbox.custom = False
    
    urlbox.domain = domain
    urlbox.url = url
    urlbox.put()
    
    return urlbox.shorted_url
Ejemplo n.º 2
0
def num_of_shorts(domain=None):
    '''Count how many shorts we've got in the datastore
    
    Params
        domain - if not None count shorts for that domain, count all otherwise
    
    Return
        number of shorts: integer
    '''
    
    query = UrlBox.all()
    if domain:
        query.filter(' domain = ', domain)
    return query.count()