This repository has been archived by the owner on Aug 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
65 lines (55 loc) · 2.07 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# pylint: disable=C0103,C0111
from unicodedata import normalize
from datetime import datetime
from flask import request, url_for
import subprocess
import urllib.parse
import hashlib
import humanize
import re
_punct_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')
def slugify(text, delim=b'-'):
"""Generates an ASCII-only slug."""
result = []
for word in _punct_re.split(text.lower()):
word = normalize('NFKD', word).encode('ascii', 'ignore')
if word:
result.append(word)
return delim.join(result).decode("utf-8", "strict")
def naturaltime(ue):
if isinstance(ue, int):
return humanize.naturaltime(datetime.fromtimestamp(ue))
return humanize.naturaltime(ue)
def get_gravatar(email):
size = 69
base_url = 'http://www.gravatar.com/avatar/'
url = base_url + hashlib.md5(email.lower().encode('utf8')).hexdigest() + "?"
url += urllib.parse.urlencode({'d': 'identicon', 's': str(size)})
return url
def is_safe_url(target):
ref_url = urllib.parse.urlparse(request.host_url)
parsed_url = urllib.parse.urljoin(request.host_url, target)
test_url = urllib.parse.urlparse(parsed_url)
return test_url.scheme in ('http', 'https') and \
ref_url.netloc == test_url.netloc
def url_for_redirect_back(endpoint, **values):
target = request.form['next']
if not target or not is_safe_url(target):
target = url_for(endpoint, **values)
return target
def get_redirect_target():
for target in request.values.get('next'), request.referrer:
if not target:
continue
if is_safe_url(target):
return target
def downsample(directory, target, name, width, height):
command_str = "avconv -y -i %s/%s -s %sx%s -strict experimental" \
" -c:v libvpx -qmin 0 -qmax 50 -crf 5 -b:v 1M -c:a libvorbis" \
" %s/led-%s.webm"
command = command_str % (directory, name, width, height, target, name)
try:
subprocess.check_call(command.split(' '))
return True, ''
except subprocess.CalledProcessError as e:
return False, e