|
1
|
1 |
|
import pprint |
|
2
|
1 |
|
import logging |
|
3
|
1 |
|
from urllib.parse import unquote |
|
4
|
|
|
|
|
5
|
1 |
|
import requests |
|
6
|
1 |
|
from flask import (Response, url_for as _url_for, render_template, send_file, |
|
|
|
|
|
|
7
|
|
|
current_app, request) |
|
8
|
|
|
|
|
9
|
1 |
|
GITHUB_BASE = "https://raw.githubusercontent.com/jacebrowning/memegen/master/" |
|
10
|
1 |
|
CONTRIBUTING_URL = GITHUB_BASE + "CONTRIBUTING.md" |
|
11
|
1 |
|
CHANGES_URL = GITHUB_BASE + "CHANGES.md" |
|
12
|
|
|
|
|
13
|
1 |
|
log = logging.getLogger(__name__) |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
1 |
|
def route(*args, **kwargs): |
|
17
|
|
|
"""Unquoted version of Flask's `url_for`.""" |
|
18
|
1 |
|
return unquote(_url_for(*args, **kwargs)) |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
1 |
|
def samples(): |
|
22
|
|
|
"""Generate dictionaries of sample image data for template rendering.""" |
|
23
|
1 |
|
for template in sorted(current_app.template_service.all()): |
|
24
|
1 |
|
path = template.sample_path |
|
25
|
1 |
|
url = route('image.get', key=template.key, path=path, _external=True) |
|
26
|
1 |
|
link = route('links.get', key=template.key, path=path) |
|
27
|
1 |
|
yield { |
|
28
|
|
|
'key': template.key, |
|
29
|
|
|
'name': template.name, |
|
30
|
|
|
'url': url, |
|
31
|
|
|
'link': link |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
1 |
|
def display(title, path, mimetype='image/jpeg'): |
|
36
|
|
|
"""Render a webpage or raw image based on request.""" |
|
37
|
1 |
|
mimetypes = request.headers.get('Accept', "").split(',') |
|
38
|
1 |
|
browser = 'text/html' in mimetypes |
|
39
|
|
|
|
|
40
|
1 |
|
if browser: |
|
41
|
|
|
log.info("Rending image on page: %s", request.path) |
|
42
|
|
|
|
|
43
|
|
|
html = render_template( |
|
44
|
|
|
'image.html', |
|
45
|
|
|
src=request.path, |
|
46
|
|
|
title=title, |
|
47
|
|
|
ga_tid=get_tid(), |
|
48
|
|
|
) |
|
49
|
|
|
|
|
50
|
|
|
return Response(html) |
|
51
|
|
|
|
|
52
|
|
|
else: |
|
53
|
1 |
|
log.info("Sending image: %s", path) |
|
54
|
|
|
|
|
55
|
1 |
|
_track(title) |
|
56
|
|
|
|
|
57
|
1 |
|
return send_file(path, mimetype=mimetype) |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
1 |
|
def _track(title): |
|
61
|
|
|
"""Log the requested content, server-side.""" |
|
62
|
1 |
|
data = dict( |
|
63
|
|
|
v=1, |
|
64
|
|
|
tid=get_tid(), |
|
65
|
|
|
cid=request.remote_addr, |
|
66
|
|
|
|
|
67
|
|
|
t='pageview', |
|
68
|
|
|
dh='memegen.link', |
|
69
|
|
|
dp=request.path, |
|
70
|
|
|
dt=str(title), |
|
71
|
|
|
|
|
72
|
|
|
uip=request.remote_addr, |
|
73
|
|
|
ua=request.user_agent.string, |
|
74
|
|
|
dr=request.referrer, |
|
75
|
|
|
) |
|
76
|
1 |
|
if get_tid(default=None): |
|
77
|
|
|
requests.post("http://www.google-analytics.com/collect", data=data) |
|
78
|
|
|
else: |
|
79
|
1 |
|
log.debug("Analytics data:\n%s", pprint.pformat(data)) |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
1 |
|
def get_tid(*, default='local'): |
|
83
|
|
|
"""Get the analtyics tracking identifier.""" |
|
84
|
|
|
return current_app.config['GOOGLE_ANALYTICS_TID'] or default |
|
85
|
|
|
|
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.