You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.7 KiB
66 lines
1.7 KiB
from flask import Flask, request, redirect
|
|
from werkzeug.routing import BaseConverter
|
|
from datetime import datetime
|
|
import os
|
|
|
|
BASE_DIR = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
|
|
|
|
app = Flask(__name__, instance_relative_config=True)
|
|
app.config.from_pyfile('config.py')
|
|
|
|
class RegexConverter(BaseConverter):
|
|
def __init__(self, url_map, *items):
|
|
super(RegexConverter, self).__init__(url_map)
|
|
self.regex = items[0]
|
|
|
|
app.url_map.converters['regex'] = RegexConverter
|
|
|
|
@app.route('/<regex("[_0-9A-Za-z]{6}"):slug>', methods=['GET', ])
|
|
def slug_redirect(slug):
|
|
post_date = slugtodate(slug)
|
|
# TODO: check if valid and just redir if not.
|
|
redir_path = post_date.strftime("/%Y/%m/%d/%H%M%S/")
|
|
return redirect(app.config['REDIRECT_BASE'] + redir_path)
|
|
|
|
# catchall route - just redir
|
|
@app.route('/', defaults={'path': ''})
|
|
@app.route('/<path:path>')
|
|
def catch_all(path):
|
|
return redirect(app.config['REDIRECT_BASE'] + '/' + path, code=301)
|
|
|
|
def slugtodate(s):
|
|
epoch_days = sxgtonum(s[:3])
|
|
day_secs = sxgtonum(s[3:])
|
|
unixtime = (epoch_days * 60*60*24) + day_secs
|
|
return datetime.fromtimestamp(unixtime)
|
|
|
|
def sxgtonum(s):
|
|
n = 0
|
|
j = len(s)
|
|
|
|
for i in range(0, j):
|
|
c = ord(s[i])
|
|
if(c >= 48 and c <= 57):
|
|
c -= 48
|
|
elif (c >= 65 and c <= 72):
|
|
c -=55
|
|
elif (c == 73 or c == 108):
|
|
c = 1
|
|
elif (c >= 74 and c <= 78):
|
|
c -= 56
|
|
elif (c == 79):
|
|
c = 0
|
|
elif (c >= 80 and c <= 90):
|
|
c -= 57
|
|
elif (c == 95):
|
|
c = 34
|
|
elif (c >= 97 and c <= 107):
|
|
c -= 62
|
|
elif (c >= 109 and c <= 122):
|
|
c -= 63
|
|
else:
|
|
c = 0
|
|
n = 60 * n + c
|
|
|
|
return n
|