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
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
:electric_plug: Generating short urls with python has never been easier

pyshorteners A simple URL shortening API wrapper Python library. Installing pip install pyshorteners Documentation https://pyshorteners.readthedocs.i

Ellison 350 Dec 24, 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
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
🔗 Generate Phishing URLs 🔗

URLer ?? Generate Phishing URLs ?? URLer Table Of Contents General Information Preview Installation Disclaimer Credits Social Media Bug Report General

mrblackx 5 Feb 8, 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 7, 2022
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 9, 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
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
A python code for url redirect check

A python code for url redirect check

Fayas Noushad 1 Oct 24, 2021
A url redirect status check module for python

A url redirect status check module for python

Fayas Noushad 2 Oct 24, 2021
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 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
🌐 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 4, 2023
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 2, 2023
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
Customizable URL shortener written in Python3 for sniffing and spoofing

Customizable URL shortener written in Python3 for sniffing and spoofing

null 3 Nov 22, 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

null 13 Apr 14, 2022
Have you ever wondered: Where does this link go? The REDLI Tool follows the path of the URL.

Have you ever wondered: Where does this link go? The REDLI Tool follows the path of the URL. It allows you to see the complete path a redirected URL goes through. It will show you the full redirection path of URLs, shortened links, or tiny URLs.

JAYAKUMAR 28 Sep 11, 2022