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
|
|
|
|
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
|
1 |
|
kwargs = cache.get(index - 1) |
34
|
|
|
|
35
|
1 |
|
if kwargs: |
36
|
1 |
|
kwargs['preview'] = True |
37
|
|
|
else: |
38
|
1 |
|
kwargs['key'] = 'custom' |
39
|
1 |
|
kwargs['path'] = "your_meme/goes_here" |
40
|
1 |
|
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
|
|
|
|
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
|
1 |
|
text = domain.Text(template.default_path) |
53
|
|
|
|
54
|
1 |
|
return redirect(route('.get', key=key, path=text.path, **options)) |
55
|
|
|
|
56
|
|
|
|
57
|
1 |
|
@blueprint.route("/<key>.jpeg") |
58
|
|
|
def get_without_text_jpeg(key): |
59
|
1 |
|
return redirect(route('.get_without_text', key=key)) |
60
|
|
|
|
61
|
|
|
|
62
|
1 |
|
@blueprint.route("/<key>/<path:path>.jpg", endpoint='get') |
63
|
1 |
|
@flaskparser.use_kwargs(OPTIONS) |
64
|
|
|
def get_with_text(key, path, alt, font, watermark, preview, share, **size): |
65
|
1 |
|
options = dict(key=key, path=path, |
66
|
|
|
alt=alt, font=font, watermark=watermark, **size) |
67
|
1 |
|
if preview: |
68
|
1 |
|
options['preview'] = True |
69
|
1 |
|
if share: |
70
|
|
|
options['share'] = True |
71
|
|
|
|
72
|
1 |
|
text = domain.Text(path) |
73
|
1 |
|
fontfile = current_app.font_service.find(font) |
74
|
|
|
|
75
|
1 |
|
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
|
1 |
|
options.pop('font') |
90
|
1 |
|
return redirect(route('.get', **options)) |
|
|
|
|
91
|
|
|
|
92
|
1 |
|
options['watermark'] = watermark = _get_watermark(request, watermark) |
93
|
1 |
|
if watermark and watermark not in current_app.config['WATERMARK_OPTIONS']: |
94
|
1 |
|
options.pop('watermark') |
95
|
1 |
|
return redirect(route('.get', **options)) |
|
|
|
|
96
|
|
|
|
97
|
1 |
|
image = current_app.image_service.create( |
98
|
|
|
template, text, |
99
|
|
|
style=alt, font=fontfile, size=size, watermark=watermark, |
100
|
|
|
) |
101
|
|
|
|
102
|
1 |
|
if not preview: |
103
|
1 |
|
cache_filtered.add(key=key, path=path, alt=alt, font=font) |
104
|
1 |
|
cache_unfiltered.add(key=key, path=path, alt=alt, font=font) |
105
|
1 |
|
track(image.text) |
106
|
|
|
|
107
|
1 |
|
return display(image.text, image.path, share=share) |
108
|
|
|
|
109
|
|
|
|
110
|
1 |
|
@blueprint.route("/<key>/<path:path>.jpeg") |
111
|
|
|
def get_with_text_jpeg(key, path): |
112
|
1 |
|
return redirect(route('.get', key=key, path=path)) |
113
|
|
|
|
114
|
|
|
|
115
|
1 |
|
@blueprint.route("/_<code>.jpg") |
116
|
|
|
def get_encoded(code): |
117
|
|
|
|
118
|
1 |
|
key, path = current_app.link_service.decode(code) |
119
|
1 |
|
template = current_app.template_service.find(key) |
120
|
1 |
|
text = domain.Text(path) |
121
|
1 |
|
image = current_app.image_service.create(template, text) |
122
|
|
|
|
123
|
1 |
|
track(image.text) |
124
|
|
|
|
125
|
1 |
|
return display(image.text, image.path) |
126
|
|
|
|
127
|
|
|
|
128
|
1 |
|
def _get_watermark(_request, watermark): |
129
|
1 |
|
if watermark: |
130
|
1 |
|
return watermark |
131
|
|
|
|
132
|
1 |
|
referer = _request.environ.get('HTTP_REFERER', "") |
133
|
1 |
|
for option in current_app.config['WATERMARK_OPTIONS']: |
134
|
1 |
|
if option in referer: |
135
|
|
|
return None |
136
|
|
|
|
137
|
|
|
return current_app.config['WATERMARK_OPTIONS'][0] |
138
|
|
|
|
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__.py
files in your module folders. Make sure that you place one file in each sub-folder.