|
1
|
1 |
|
import logging |
|
2
|
|
|
|
|
3
|
1 |
|
from flask import Blueprint, current_app, request, redirect |
|
|
|
|
|
|
4
|
1 |
|
from webargs import fields, flaskparser |
|
|
|
|
|
|
5
|
|
|
|
|
6
|
1 |
|
from .. import domain |
|
7
|
|
|
|
|
8
|
1 |
|
from ._cache import Cache |
|
9
|
1 |
|
from ._utils import route, track, display |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
1 |
|
blueprint = Blueprint('image', __name__) |
|
13
|
1 |
|
log = logging.getLogger(__name__) |
|
14
|
1 |
|
cache_filtered = Cache() |
|
15
|
1 |
|
cache_unfiltered = Cache(filtered=False) |
|
16
|
|
|
|
|
17
|
1 |
|
OPTIONS = { |
|
18
|
|
|
'alt': fields.Str(missing=None), |
|
19
|
|
|
'font': fields.Str(missing=None), |
|
20
|
|
|
'preview': fields.Bool(missing=False), |
|
21
|
|
|
'share': fields.Bool(missing=False), |
|
22
|
|
|
'width': fields.Int(missing=None), |
|
23
|
|
|
'height': fields.Int(missing=None), |
|
24
|
|
|
'watermark': fields.Str(missing=None), |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
1 |
|
|
|
28
|
1 |
|
@blueprint.route("/latest.jpg") |
|
29
|
1 |
|
@blueprint.route("/latest<int:index>.jpg") |
|
30
|
1 |
|
@flaskparser.use_kwargs({'filtered': fields.Bool(missing=True)}) |
|
31
|
1 |
|
def get_latest(index=1, filtered=True): |
|
32
|
1 |
|
cache = cache_filtered if filtered else cache_unfiltered |
|
33
|
|
|
kwargs = cache.get(index - 1) |
|
34
|
1 |
|
|
|
35
|
1 |
|
if kwargs: |
|
36
|
1 |
|
kwargs['preview'] = True |
|
37
|
1 |
|
else: |
|
38
|
|
|
kwargs['key'] = 'custom' |
|
39
|
1 |
|
kwargs['path'] = "your_meme/goes_here" |
|
40
|
|
|
kwargs['alt'] = "https://raw.githubusercontent.com/jacebrowning/memegen/master/memegen/static/images/missing.png" |
|
41
|
|
|
|
|
42
|
1 |
|
return redirect(route('.get', _external=True, **kwargs)) |
|
|
|
|
|
|
43
|
1 |
|
|
|
44
|
|
|
|
|
45
|
1 |
|
@blueprint.route("/<key>.jpg") |
|
46
|
1 |
|
@flaskparser.use_kwargs(OPTIONS) |
|
47
|
|
|
def get_without_text(key, **options): |
|
48
|
1 |
|
options.pop('preview') |
|
49
|
1 |
|
options.pop('share') |
|
50
|
|
|
|
|
51
|
1 |
|
template = current_app.template_service.find(key) |
|
52
|
|
|
text = domain.Text(template.default_path) |
|
53
|
|
|
|
|
54
|
1 |
|
return redirect(route('.get', key=key, path=text.path, **options)) |
|
55
|
|
|
|
|
56
|
1 |
|
|
|
57
|
|
|
@blueprint.route("/<key>.jpeg") |
|
58
|
|
|
def get_without_text_jpeg(key): |
|
59
|
1 |
|
return redirect(route('.get_without_text', key=key)) |
|
60
|
1 |
|
|
|
61
|
|
|
|
|
62
|
1 |
|
@blueprint.route("/<key>/<path:path>.jpg", endpoint='get') |
|
63
|
1 |
|
@flaskparser.use_kwargs(OPTIONS) |
|
64
|
1 |
|
def get_with_text(key, path, alt, font, watermark, preview, share, **size): |
|
65
|
1 |
|
options = dict(key=key, path=path, alt=alt, font=font, **size) |
|
66
|
|
|
if preview: |
|
67
|
|
|
options['preview'] = True |
|
68
|
1 |
|
if share: |
|
69
|
1 |
|
options['share'] = True |
|
70
|
|
|
options['watermark'] = watermark = _get_watermark(request, watermark) |
|
71
|
1 |
|
|
|
72
|
1 |
|
text = domain.Text(path) |
|
73
|
1 |
|
fontfile = current_app.font_service.find(font) |
|
74
|
1 |
|
|
|
75
|
|
|
template = current_app.template_service.find(key, allow_missing=True) |
|
76
|
1 |
|
if template.key != key: |
|
77
|
1 |
|
options['key'] = template.key |
|
78
|
1 |
|
return redirect(route('.get', **options)) |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
1 |
|
if alt and template.path == template.get_path(alt): |
|
81
|
1 |
|
options.pop('alt') |
|
82
|
1 |
|
return redirect(route('.get', **options)) |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
1 |
|
if path != text.path: |
|
85
|
1 |
|
options['path'] = text.path |
|
86
|
1 |
|
return redirect(route('.get', **options)) |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
1 |
|
if font and not fontfile: |
|
89
|
|
|
options.pop('font') |
|
90
|
|
|
return redirect(route('.get', **options)) |
|
|
|
|
|
|
91
|
1 |
|
|
|
92
|
1 |
|
if watermark and watermark not in current_app.config['WATERMARK_OPTIONS']: |
|
93
|
1 |
|
options.pop('watermark') |
|
94
|
1 |
|
return redirect(route('.get', **options)) |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
1 |
|
image = current_app.image_service.create( |
|
97
|
|
|
template, text, |
|
98
|
|
|
style=alt, font=fontfile, size=size, watermark=watermark, |
|
99
|
1 |
|
) |
|
100
|
|
|
|
|
101
|
1 |
|
if not preview: |
|
102
|
|
|
cache_filtered.add(key=key, path=path, alt=alt, font=font) |
|
103
|
|
|
cache_unfiltered.add(key=key, path=path, alt=alt, font=font) |
|
104
|
1 |
|
track(image.text) |
|
105
|
|
|
|
|
106
|
|
|
return display(image.text, image.path, share=share) |
|
107
|
1 |
|
|
|
108
|
1 |
|
|
|
109
|
1 |
|
@blueprint.route("/<key>/<path:path>.jpeg") |
|
110
|
1 |
|
def get_with_text_jpeg(key, path): |
|
111
|
|
|
return redirect(route('.get', key=key, path=path)) |
|
112
|
1 |
|
|
|
113
|
|
|
|
|
114
|
1 |
|
@blueprint.route("/_<code>.jpg") |
|
115
|
|
|
def get_encoded(code): |
|
116
|
|
|
|
|
117
|
|
|
key, path = current_app.link_service.decode(code) |
|
118
|
|
|
template = current_app.template_service.find(key) |
|
119
|
|
|
text = domain.Text(path) |
|
120
|
|
|
image = current_app.image_service.create(template, text) |
|
121
|
|
|
|
|
122
|
|
|
track(image.text) |
|
123
|
|
|
|
|
124
|
|
|
return display(image.text, image.path) |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
def _get_watermark(_request, watermark): |
|
128
|
|
|
if watermark: |
|
129
|
|
|
return watermark |
|
130
|
|
|
|
|
131
|
|
|
referer = _request.environ.get('HTTP_REFERER', "") |
|
132
|
|
|
for option in current_app.config['WATERMARK_OPTIONS']: |
|
133
|
|
|
if option in referer: |
|
134
|
|
|
return None |
|
135
|
|
|
|
|
136
|
|
|
return current_app.config['WATERMARK_OPTIONS'][0] |
|
137
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.