-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a7ec861
commit 2e1e887
Showing
3 changed files
with
192 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#!/usr/bin/env python | ||
# coding=utf-8 | ||
# | ||
# Copyright 2011 Friday Film Club. All Rights Reserved. | ||
|
||
"""Migrate images from blobstore to cloud storage | ||
You'll need to `pip install python-magic` | ||
""" | ||
|
||
import os | ||
import sys | ||
sys.path.append('/usr/local/google_appengine') | ||
import dev_appserver | ||
import posixpath | ||
import cloudstorage | ||
import magic | ||
import re | ||
import getpass | ||
|
||
dev_appserver.fix_sys_path() | ||
|
||
dir = os.path.dirname(__file__) | ||
sys.path.insert(0, os.path.join(dir, '../src')) | ||
|
||
from google.appengine.ext.remote_api import remote_api_stub | ||
from google.appengine.ext import blobstore, ndb | ||
|
||
GCS_ROOT = '/ffcapp.appspot.com/images/' | ||
|
||
os.environ['SERVER_SOFTWARE'] = 'Development (remote_api)/1.0' | ||
|
||
import models | ||
|
||
APP_NAME = 's~ffcapp' | ||
RE_SPECIAL_CHARS_ = re.compile(r'[^a-zA-Z0-9 ]') | ||
os.environ['AUTH_DOMAIN'] = 'gmail.com' | ||
os.environ['USER_EMAIL'] = 'adamjmcgrath@gmail.com' | ||
|
||
def slugify(my_string): | ||
"""Remove special characters and replace spaces with hyphens.""" | ||
return '-'.join(re.sub(RE_SPECIAL_CHARS_, '', my_string).lower().split(' ')) | ||
|
||
def auth_func(): | ||
return (os.environ['USER_EMAIL'], getpass.getpass()) | ||
|
||
def save_image(blob_key, folder_name, file_name): | ||
img_file = blobstore.BlobInfo(blob_key) | ||
img_file_o = img_file.open() | ||
img_data = img_file_o.read() | ||
type = magic.from_buffer(img_data, mime=True) | ||
|
||
file_path = posixpath.join( | ||
GCS_ROOT, folder_name, file_name) | ||
gcs_file = cloudstorage.open(file_path, 'w', content_type=type) | ||
print 'Saving file: %s' % file_path | ||
gcs_file.write(img_data) | ||
gcs_file.close() | ||
img_file_o.close() | ||
return blobstore.BlobKey(blobstore.create_gs_key('/gs' + file_path)) | ||
|
||
|
||
def migrate_screenshot(q): | ||
clue = q.clues[0] | ||
if not clue: | ||
return | ||
title = q.answer_title | ||
clue_entity = clue.get() | ||
old_key = str(clue_entity.image) | ||
clue_entity.image = save_image( | ||
clue_entity.image, 'questions', slugify(title) + '-screenshot') | ||
clue_entity.put() | ||
print ('screenshot: %s | old: %s | new: %s' % (title, old_key, str(clue_entity.image))) | ||
|
||
|
||
def migrate_packshot(q): | ||
title = q.answer_title | ||
old_key = str(q.packshot) | ||
q.packshot = save_image( | ||
q.packshot, 'questions', slugify(title) + '-packshot') | ||
q.put() | ||
print ('packshot: %s | old: %s | new: %s' % (title, old_key, str(q.packshot))) | ||
|
||
|
||
def migrate_questions(): | ||
for q in models.Question.query().fetch(2): | ||
migrate_packshot(q) | ||
migrate_screenshot(q) | ||
|
||
|
||
def main(): | ||
# Use 'localhost:8080' for dev server. | ||
remote_api_stub.ConfigureRemoteApi(APP_NAME, '/_ah/remote_api', | ||
auth_func, servername='ffcapp.appspot.com') | ||
|
||
# migrate_questions() | ||
# TODO leagues, users | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters