-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgur.py
49 lines (39 loc) · 1.78 KB
/
imgur.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
# -*- coding: utf-8 -*-
from jinja2 import nodes
from jinja2.ext import Extension
from google.appengine.api import urlfetch
from google.appengine.ext import db
class ImgurExtension(Extension):
tags = set(['imgur'])
def parse(self, parser):
stream = parser.stream
tag = stream.next()
args = []
while not stream.current.test_any('block_end', 'name:as'):
if stream.current.test('name') or stream.look().test('assign'):
key = nodes.Const(stream.next().value)
stream.skip()
value = parser.parse_expression()
args.append(nodes.Pair(key, value, lineno=key.lineno))
else:
parser.parse_expression()
def make_call_node(*kw):
return self.call_method('_render_tag', args=[nodes.Dict(args)], kwargs=kw)
return nodes.Output([make_call_node()]).set_lineno(tag.lineno)
@classmethod
def _render_tag(self, args):
import urllib
from settings import IMGUR_KEY, IMGUR_API_UPLOAD
from utils import build_url
filename = args.get('src')
alt = args.get('alt', '')
params = urllib.urlencode({'key' : IMGUR_KEY, 'image' : build_url(filename)})
result = urlfetch.fetch(IMGUR_API_UPLOAD, method=urlfetch.POST, payload=params)
if result.status_code == 200:
from json import loads
data = loads(result.content)
upload = data["upload"]
image = upload["image"]
links = upload["links"]
html = '<div class="centered"><img class="img-thumbnail center-block" width="{width}" height="{height}" src="{src}" alt="{alt}" /></div>'
return html.format(width=image["width"], height=image["height"], src=links["original"], alt=alt)