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
ShortenURL-model - The model layer class for shorten url service

ShortenURL Model The model layer class for shorten URL service Usage Complete th

TwinIsland 1 Jan 07, 2022
🔗 FusiShort is a URL shortener built with Python, Redis, Docker and Kubernetes

This is a playground application created with goal of applying full cycle software development using popular technologies like Python, Redis, Docker and Kubernetes.

Lucas Fusinato Zanis 7 Nov 10, 2022
find all the URL of a site with a specific Regex

href this program will find all the link with a spesfic Regex pattern from a site. what it will do in any site there are a lots of url that may you ne

Arya Shabane 12 Dec 05, 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
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
Python implementation for generating Tiny URL- and bit.ly-like URLs.

Short URL Generator Python implementation for generating Tiny URL- and bit.ly-like URLs. A bit-shuffling approach is used to avoid generating consecut

Alireza Savand 170 Dec 28, 2022
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
python3 flask based python-url-shortener microservice.

python-url-shortener This repository is for managing all public/private entity specific api endpoints for an organisation. In this case we have entity

Asutosh Parida 1 Oct 18, 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
Ukiyo - A simple, minimalist and efficient discord vanity URL sniper

Ukiyo - a simple, minimalist and efficient discord vanity URL sniper. Ukiyo is easy to use, has a very visually pleasing interface, and has great spee

13 Apr 14, 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
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
A URL builder for genius :D

genius-url A URL builder for genius :D Usage from gurl import genius_url

ꌗᖘ꒒ꀤ꓄꒒ꀤꈤꍟ 12 Aug 14, 2021
Yet another URL library

Yet another URL library

aio-libs 884 Jan 03, 2023
A python code for url redirect check

A python code for url redirect check

Fayas Noushad 1 Oct 24, 2021
A teeny Tiny module to check URLs against discord's list of phishing domains

A teeny Tiny module to check URLs against discord's list of phishing domains

kaj 1 Aug 29, 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
A url redirect status check module for python

A url redirect status check module for python

Fayas Noushad 2 Oct 24, 2021
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
A tool programmed to shorten links/mask links

A tool programmed to shorten links/mask links

Anontemitayo 6 Dec 02, 2022