Python implementation for generating Tiny URL- and bit.ly-like URLs.

Overview

Short URL Generator

travis-cli tests status for short_url PyPI Version PyPI Status PyPI License PyPI Format PyPI Py_versions PyPI Implementation PyPI Egg

Python implementation for generating Tiny URL- and bit.ly-like URLs.

A bit-shuffling approach is used to avoid generating consecutive, predictable URLs. However, the algorithm is deterministic and will guarantee that no collisions will occur.

The URL alphabet is fully customizable and may contain any number of characters. By default, digits and lower-case letters are used, with some removed to avoid confusion between characters like o, O and 0. The default alphabet is shuffled and has a prime number of characters to further improve the results of the algorithm.

The block size specifies how many bits will be shuffled. The lower BLOCK_SIZE bits are reversed. Any bits higher than BLOCK_SIZE will remain as is. BLOCK_SIZE of 0 will leave all bits unaffected and the algorithm will simply be converting your integer to a different base.

The intended use is that incrementing, consecutive integers will be used as keys to generate the short URLs. For example, when creating a new URL, the unique integer ID assigned by a database could be used to generate the URL by using this module. Or a simple counter may be used. As long as the same integer is not used twice, the same short URL will not be generated twice.

The module supports both encoding and decoding of URLs. The min_length parameter allows you to pad the URL if you want it to be a specific length.

Sample Usage:

>>> import short_url
>>> url = short_url.encode_url(12)
>>> print url
LhKA
>>> key = short_url.decode_url(url)
>>> print key
12

Use the functions in the top-level of the module to use the default encoder. Otherwise, you may create your own UrlEncoder object and use its encode_url and decode_url methods.

Install

short_url is also available at pypi:

http://pypi.python.org/pypi/short_url

Give a try to your finger:

$ pip install short_url

And done ;)

Tests

short_url is tested on both python2 and python3, to run the tests:

$ tox

Source https://github.com/Alir3z4/short_url
Website http://alir3z4.github.com/short_url
Issues https://github.com/Alir3z4/short_url/issues
PyPi http://pypi.python.org/pypi/short_url
Author Michael Fogleman
Maintainer Alireza Savand
License MIT
Link http://code.activestate.com/recipes/576918/
Comments
  • Make Python 3 compatible

    Make Python 3 compatible

    Hi, could you try to make short_url compatible with Python 3,

    The statment """ n & ~self.mask """ returns an """ TypeError: unsupported operand type(s) for &: 'str' and 'int' """ on Python 3

    opened by loechel 3
  • Not generating alpha strings?

    Not generating alpha strings?

    I'm getting int responses from short_url.encode() Possibly an environment issue?

    Reproduction

    $ pip install short_url
       Collecting short_url
       Installing collected packages: short-url
       Successfully installed short-url-1.2.2
    
    Python 2.7.11 (default, Feb 19 2017, 23:20:53)
    IPython 4.1.1 -- An enhanced Interactive Python.
    In [1]: import short_url
    In [2]: print short_url.encode(12)
    3145728
    

    Expected LhKA instead of 3145728

    opened by perrierism 2
  • As there a way to predict the maximum length?

    As there a way to predict the maximum length?

    I would like to save URLs in a database field. What is the maximum number of characters I will need for a given range of integer numbers or: Is there a safe length which will always suffice for 32-bit integers as input?

    Thanks for publishing this project!

    opened by mcrot 1
  • Make it work with a real URL

    Make it work with a real URL

    Re-Issue an old issue from http://code.activestate.com/recipes/576918/

    is there a way to make it work with a url? i can shorten integers all day long, but when i actually enter a url i get this:

    >> import short_url
    >> link = '/home/gmilby/public_html/syrbotwebdesign.com/scripts/playground'
    >> url = short_url.encode_url(link)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "short_url.py", line 66, in encode_url
        return DEFAULT_ENCODER.encode_url(n, min_length)
      File "short_url.py", line 15, in encode_url
        return self.enbase(self.encode(n), min_length)
      File "short_url.py", line 19, in encode
        return (n & ~self.mask) | self._encode(n & self.mask)
    TypeError: unsupported operand type(s) for &: 'str' and 'long'
    

    i kept shaving off the beginning (http:, then // then...) same error everytime.

    is there a way to short a real URL? (Protocoll://hostname:port/path) not only Integer values, if not the package name is misleading.

    opened by loechel 1
  • Torrent magnet links

    Torrent magnet links

    Hi, thank you for the amazing work! I tried many APIs to short a magnet link, for example

    import pyshorteners
    s = pyshorteners.Shortener()
    s.tinyurl.short('magnet:?xt=urn:btih:44EA47CF8DF204C8E5DF79A255809D47AC94C21B')
    

    and I got https://tinyurl.com/y8ykwlpb which however redirect to http://magnet/?xt=urn:btih:44EA47CF8DF204C8E5DF79A255809D47AC94C21B which doesn't work since it does not open the bittorrent client. Is there a way to short a magnet link? Thank you

    opened by Rabelaiss 0
  • Missing tests in PyPI release

    Missing tests in PyPI release

    The PyPI release tarball should contain the test cases. These are usually ran by distributions that package Python modules in order to ensure their basic usability.

    opened by mia-0 1
Releases(1.2.2)
Owner
Alireza Savand
I am Alireza Savand, a Software Architect.
Alireza Savand
A simple URL shortener app using Python AWS Chalice, AWS Lambda and AWS Dynamodb.

url-shortener-chalice A simple URL shortener app using AWS Chalice. Please make sure you configure your AWS credentials using AWS CLI before starting

Ranadeep Ghosh 2 Dec 09, 2022
coURLan: Clean, filter, normalize, and sample URLs

coURLan: Clean, filter, normalize, and sample URLs Why coURLan? “Given that the bandwidth for conducting crawls is neither infinite nor free, it is be

Adrien Barbaresi 20 Dec 14, 2022
Qysqa - URL shortener website with python

Qysqa - shorten your URL. ~ A simple URL-shortening website. how do you pronounc

Dastan Ozgeldi 0 Nov 18, 2022
A simple, immutable URL class with a clean API for interrogation and manipulation.

purl - A simple Python URL class A simple, immutable URL class with a clean API for interrogation and manipulation. Supports Pythons 2.7, 3.3, 3.4, 3.

David Winterbottom 286 Jan 02, 2023
Simple Version of ouo.io. shorten any link on the web easily

OUO.IO LINK SHORTENER This is a simple python script that made to short links. currently ouo.io doesn't have Application Programming Interface so i de

Danushka-Madushan 1 Dec 11, 2021
Fast pattern fetcher, Takes a URLs list and outputs the URLs which contains the parameters according to the specified pattern.

Fast Pattern Fetcher (fpf) Coded with 3 by HS Devansh Raghav Fast Pattern Fetcher, Takes a URLs list and outputs the URLs which contains the paramete

whoami security 5 Feb 20, 2022
declutters url lists for crawling/pentesting

uro Using a URL list for security testing can be painful as there are a lot of URLs that have uninteresting/duplicate content; uro aims to solve that.

Somdev Sangwan 677 Jan 07, 2023
encurtador de links feito com python

curt-link encurtador de links feito com python! instalação Linux: $ git clone https://github.com/bydeathlxncer/curt-link $ cd curt-link $ python3 url.

bydeathlxncer 5 Dec 29, 2021
Shorten-Link - Make shorten URL with Cuttly API

Shorten-Link This Script make shorten URL with custom slashtag The script take f

Ahmed Hossam 3 Feb 13, 2022
A tool programmed to shorten links/mask links

A tool programmed to shorten links/mask links

Anontemitayo 6 Dec 02, 2022
This is a no-bullshit file hosting and URL shortening service that also runs 0x0.st. Use with uWSGI.

This is a no-bullshit file hosting and URL shortening service that also runs 0x0.st. Use with uWSGI.

mia 1.6k Dec 31, 2022
A tool to manage the base URL of the Python package index.

chpip A tool to manage the base URL of the Python package index. Installation $ pip install chpip Usage Set pip index URL Set the base URL of the Pyth

Prodesire 4 Dec 20, 2022
A python code for url redirect check

A python code for url redirect check

Fayas Noushad 1 Oct 24, 2021
a little project to make custom discord invites over a url

custom-dc-invite a little project to make custom discord invites over a url how it works you create a account for

baum1810 2 Oct 03, 2022
Temporary-shortner - A webapp that shortner URLs but for limited time

temporary-shortner A webapp that shortens URLs but for a limited time Demo site

Vitor 2 Jan 07, 2022
Extract countries, regions and cities from a URL or text

This project is no longer being maintained and has been archived. Please check the Forks list for newer versions. Forks We are aware of two 3rd party

Ushahidi 216 Nov 18, 2022
UDdup - URLs Deduplication Tool

UDdup - URLs Deduplication Tool The tool gets a list of URLs, and removes "duplicate" pages in the sense of URL patterns that are probably repetitive

Rotem Reiss 128 Dec 21, 2022
Astra is a tool to find URLs and secrets.

Astra finds urls, endpoints, aws buckets, api keys, tokens, etc from a given url/s. It combines the paths and endpoints with the given domain and give

Stinger 198 Dec 27, 2022
A friendly library for parsing HTTP request arguments, with built-in support for popular web frameworks, including Flask, Django, Bottle, Tornado, Pyramid, webapp2, Falcon, and aiohttp.

webargs Homepage: https://webargs.readthedocs.io/ webargs is a Python library for parsing and validating HTTP request objects, with built-in support f

marshmallow-code 1.3k Jan 01, 2023
C++ library for urlencode.

liburlencode C library for urlencode.

Khaidi Chu 6 Oct 31, 2022