class Encoder: def __init__(self, alphabet): self.encoder = UrlEncoder(alphabet=alphabet) def int2string(self, value): return self.encoder.encode_url(value) def string2int(self, value): return self.encoder.decode_url(value)
class Encoder: def __init__(self): self.encoder = UrlEncoder(alphabet=settings.DEFAULT_ALPHABET) def int2string(self, value): return self.encoder.encode_url(value) def string2int(self, value): return self.encoder.decode_url(value)
def __init__(self): self.encoder = UrlEncoder(alphabet=settings.DEFAULT_ALPHABET)
from flask import Flask, request, url_for, redirect from flask_sqlalchemy import SQLAlchemy from short_url import UrlEncoder import os BASE_DIR = os.path.abspath(os.path.dirname(__file__)) DB_URI = "sqlite:///" + os.path.join(BASE_DIR, "database.db") app = Flask(__name__) app.config["SQLALCHEMY_DATABASE_URI"] = DB_URI app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False db = SQLAlchemy(app) su = UrlEncoder(alphabet='DEQhd2uFteibPwq0SWBInTpA_jcZL5GKz3YCR14Ulk87Jors9vNHgfaOmMXy6Vx-', block_size=16) class URL(db.Model): id = db.Column(db.Integer, primary_key=True) url = db.Column(db.UnicodeText, unique=True) def __init__(self, url): self.url = url def getname(self): return su.enbase(self.id, 1) def geturl(self): return url_for("get", path=self.getname(), _external=True) + "\n"
if app.config["NSFW_DETECT"]: from nsfw_detect import NSFWDetector nsfw = NSFWDetector() try: mimedetect = Magic(mime=True, mime_encoding=False) except: print("""Error: You have installed the wrong version of the 'magic' module. Please install python-magic.""") sys.exit(1) db = SQLAlchemy(app) migrate = Migrate(app, db) su = UrlEncoder(alphabet=app.config["URL_ALPHABET"], block_size=16) class URL(db.Model): id = db.Column(db.Integer, primary_key=True) url = db.Column(db.UnicodeText, unique=True) def __init__(self, url): self.url = url def getname(self): return su.enbase(self.id, 1) def geturl(self): return url_for("get", path=self.getname(), _external=True) + "\n"
def __init__(self, alphabet): self.encoder = UrlEncoder(alphabet=alphabet)
#!/usr/bin/env python import sys from short_url import UrlEncoder BLOCK_SIZE = 24 MIN_LENGTH = 2 print 'Testing block size %d' % BLOCK_SIZE url_encoder = UrlEncoder(block_size=BLOCK_SIZE) urls = set() for n in xrange(0,3000000): url = url_encoder.encode_url(n, min_length=2) if url in urls: print 'BREAK: duplicate url found at n=%d' % n break else: urls.add(url) sys.stdout.write('%d: %s\t' % (n, url)) sys.stdout.flush()
def encode_uri(numeric_id): encoder = UrlEncoder(alphabet=SHORT_URL_ALPHABET) return encoder.encode_url(numeric_id)