-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackburner.py
More file actions
64 lines (49 loc) · 2.19 KB
/
backburner.py
File metadata and controls
64 lines (49 loc) · 2.19 KB
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
import logging
from google.appengine.api import urlfetch, files, taskqueue
from google.appengine.api.images import Image, PNG, JPEG, get_serving_url
from google.appengine.ext.webapp import RequestHandler, WSGIApplication
from google.appengine.ext.ndb import Key
from models import Post
class MediaDownloader(RequestHandler):
def get(self):
self.response.out.write("No, yo no sirvo para peticiones get")
def post(self):
# Upload as a blob
post_safekey = self.request.get('post')
logging.info("Downloading media from post url")
post = Key(urlsafe=post_safekey).get()
if post:
result = urlfetch.fetch(post.media_url)
if result.status_code == 200:
post.media_url, post.media_key = store_picture_from_content(result.content, result.headers['content-type'])
post.put()
logging.info("Post media uploaded to url {}".format(post.media_url))
else:
logging.error("Error downloading {} - {}".format(post.media_url, result.status_code))
else:
logging.info("Post not found: {}".format(post_safekey))
class DownloadMissingMediaJob(RequestHandler):
def get(self):
query = Post.query(Post.media_key == None)
for post_key in query.iter(keys_only=True):
taskqueue.add(url='/backburner/download_post_media',
params={'post': post_key.urlsafe()})
def store_picture_from_content(content, mime_type='application/octet-stream'):
img = Image(content)
img.resize(width=800, height=800)
img.im_feeling_lucky()
img_type = PNG if 'png' in mime_type else JPEG
img = img.execute_transforms(output_encoding=img_type)
# create file
file_name = files.blobstore.create(mime_type=mime_type)
with files.open(file_name, 'a') as f:
f.write(img)
# Finalize the file
files.finalize(file_name)
# Get the file's blob key
blob_key = files.blobstore.get_blob_key(file_name)
return get_serving_url(blob_key), blob_key
app = WSGIApplication([
('/backburner/download_post_media', MediaDownloader),
('/backburner/download_missing', DownloadMissingMediaJob),
], debug=True)