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
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
🌐 URL parsing and manipulation made easy.

furl is a small Python library that makes parsing and manipulating URLs easy. Python's standard urllib and urlparse modules provide a number of URL re

Ansgar Grunseid 2.4k Jan 04, 2023
A tool programmed to shorten links/mask links

A tool programmed to shorten links/mask links

Anontemitayo 6 Dec 02, 2022
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 url shortner written in Flask.

url-shortener-elitmus This is a simple flask app which takes an URL and shortens it. This shortened verion of the URL redirects to the user to the lon

2 Nov 23, 2021
Simple python library to deal with URI Templates.

uritemplate Documentation -- GitHub -- Travis-CI Simple python library to deal with URI Templates. The API looks like from uritemplate import URITempl

Hyper 210 Dec 19, 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
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
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
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
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
A url redirect status check module for python

A url redirect status check module for python

Fayas Noushad 2 Oct 24, 2021
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 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
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
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
Use this module to detect if a URL is on discord's phishing list.

PhishDetector This module was made so you can check a URL and see if it's in discord's official list of phishing and suspicious URLs. Installation pip

Elijah 4 Mar 25, 2022
Customizable URL shortener written in Python3 for sniffing and spoofing

Customizable URL shortener written in Python3 for sniffing and spoofing

3 Nov 22, 2022
URL Shortener in Flask - Web service using Flask framework for Shortener URLs

URL Shortener in Flask Web service using Flask framework for Shortener URLs Install Create Virtual env $ python3 -m venv env Install requirements.txt

Rafnix Guzman 1 Sep 21, 2021
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