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
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 URL builder for genius :D

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

ꌗᖘ꒒ꀤ꓄꒒ꀤꈤꍟ 12 Aug 14, 2021
A python code for url redirect check

A python code for url redirect check

Fayas Noushad 1 Oct 24, 2021
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
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
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
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
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
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
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
hugeURLer 是一个基于 Python 和 GitHub action 的短链接服务

hugeURLer 是一个基于 Python 和 GitHub action 的短链接服务 如何使用 您需要把库 clone 到本地,然后在终端执行 python3 .\src\addNewRedirection.py url ,就能创建一个指向你设置的 url 的跳转页面。

安东尼洪 2 Dec 22, 2021
A simple URL shortener built with Flask

A simple URL shortener built with Flask and MongoDB.

Mike Lowe 2 Feb 05, 2022
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
C++ library for urlencode.

liburlencode C library for urlencode.

Khaidi Chu 6 Oct 31, 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 08, 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
🌐 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
🔗 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
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
: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