-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
129 lines (95 loc) · 3.41 KB
/
manage.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python2.6
# -*- coding: utf-8 -*-
"""
rdreiflask.manage
~~~~~~~~~~~~~~~~~
Management script.
:copyright: 2010, Pascal Hartig <phartig@rdrei.net>
:license: GPL v3, see doc/LICENSE for more details.
"""
from contextlib import contextmanager
from flaskext.script import Manager
from rdrei.application import app
manager = Manager(app)
@contextmanager
def _prepared_context(app):
ctx = app.test_request_context()
ctx.push()
app.preprocess_request()
yield
ctx.pop()
@manager.command
def flickr_import(app):
"""Import the latest photos to redis."""
from flask import g
from rdrei.utils import flickr
with _prepared_context(app):
for photo in flickr.get_recent_profile_photos():
# Check if the entry exists.
key = 'photo:' + photo['id']
print("Current Key: ", key)
if g.db.exists(key):
print("Came across already known photo.")
break
for subkey in ('title', 'farm', 'server', 'secret', 'id',
'original_secret', 'height_o', 'width_o',
'height_m', 'width_m'):
# We need to double-check, because flickr is very picky about
# what and when to include attributes.
if subkey in photo:
g.db.hset(key, subkey, photo[subkey])
# Save the tags to subsets
for tag in photo['tags'].split(' '):
g.db.sadd('phototags:' + tag, photo['id'])
# And don't forget to keep track of the tags itself.
g.db.sadd('phototags', tag)
g.db.sadd('photos', photo['id'])
@manager.command
def dump_photos(app):
"""
Dumps the photos from redis to stdout.
"""
import simplejson
from flask import g
from rdrei.utils.redis_fixtures import dump_fixture
# As 'closure' to not load simplejson in global space.
class _SetSerializer(simplejson.JSONEncoder):
def default(self, obj):
if isinstance(obj, set):
return list(obj)
return simplejson.JSONEncoder.default(self, obj)
with _prepared_context(app):
result = []
result.append(dump_fixture("photos"))
for photo in g.db.smembers("photos"):
result.append(dump_fixture("photo:" + photo))
result.append(dump_fixture("phototags"))
for phototag in g.db.smembers("phototags"):
result.append(dump_fixture("phototags:" + phototag))
print(simplejson.dumps(result, cls=_SetSerializer))
@manager.command
def dump_albums(app):
"""Dumps the albums to stdout."""
import simplejson
from flask import g
from rdrei.utils.redis_fixtures import dump_fixture
with _prepared_context(app):
result = []
result.append(dump_fixture("photoalbum"))
for i in xrange(1, int(g.db.get("photoalbum"))):
key = "photoalbum:" + str(i)
if g.db.exists(key):
result.append(dump_fixture(key))
print(simplejson.dumps(result))
@manager.command
def load_dump(app, filename="data.json"):
"""
Loads a fixture from a file.
:param filename: Filename to load the JSON dump from.
"""
from rdrei.utils.redis_fixtures import load_fixture
with _prepared_context(app):
load_fixture(filename)
if __name__ == '__main__':
manager.run()
# vim: set ts=8 sw=4 tw=78 ft=python: