padma_migration/padma_dump.py

133 lines
3.7 KiB
Python
Raw Normal View History

2011-12-04 13:33:48 +00:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2011-12-04 13:33:48 +00:00
# GPL written 2008 by j@pad.ma
import pkg_resources
pkg_resources.require("TurboGears")
from turbogears import config, update_config, start_server
import cherrypy
cherrypy.lowercase_api = True
from os.path import *
import sys
# first look on the command line for a desired config file,
# if it's not on the command line, then
# look for setup.py in this directory. If it's not there, this script is
# probably installed
if len(sys.argv) > 1:
update_config(configfile=sys.argv[1],
modulename="padma.config")
elif exists(join(dirname(__file__), "setup.py")):
update_config(configfile="dev.cfg",modulename="padma.config")
else:
update_config(configfile="prod.cfg",modulename="padma.config")
config.update(dict(package="padma"))
from padma.model import *
import os
import ox
import simplejson as json
prefix = '/tmp/padma'
os.makedirs(prefix)
data = {}
for v in Video.select():
data[v.hid] = v.jsondump()
2011-12-05 13:49:34 +00:00
with open(os.path.join(prefix, 'data.json'), 'w') as f:
2011-12-04 17:05:48 +00:00
json.dump(data, f, indent=2)
2011-12-04 13:33:48 +00:00
users = []
for u in User.select().orderBy('id'):
users.append({
'id': u.id,
'username': u.user_name.strip(),
'email': u.email_address,
'password': 'sha1$$' + u.password,
'created': u.created.strftime('%Y-%m-%dT%H:%M:%SZ'),
'groups': [g.name for g in u.groups],
})
with open(os.path.join(prefix, 'users.json'), 'w') as f:
json.dump(users, f, indent=2)
2011-12-04 17:05:48 +00:00
files = {}
2011-12-04 13:33:48 +00:00
for v in Video.select().orderBy('id'):
f = {
'sha1sum': v.source_hash,
'ogg': v.filename,
'created': int(v.created.strftime('%s'))
}
info = ox.avinfo(v.filename)
f['oshash'] = info.get('metadata', {}).get('SOURCE_OSHASH', '')
f['ogg_oshash'] = info['oshash']
2011-12-04 17:05:48 +00:00
files[v.hid] = f
2011-12-04 13:33:48 +00:00
2011-12-05 13:49:34 +00:00
with open(os.path.join(prefix, 'files.json'), 'w') as f:
2011-12-04 13:33:48 +00:00
json.dump(files, f, indent=2)
2011-12-04 17:05:48 +00:00
lists = []
for l in List.select().orderBy('id'):
data = {
'id': l.hid,
'user': l.creator.user_name.strip(),
'title': l.title.strip(),
'created': int(l.created.strftime('%s')),
'modified': int(l.modified.strftime('%s')),
'public': l.public,
'description': l.description.strip(),
'type': l.type,
}
if data['type'] == 'dynamic':
data['query'] = {'value': l.query, 'key': l.field}
else:
data['items'] = [v.hid for v in l.videos]
lists.append(data)
2011-12-05 13:49:34 +00:00
with open(os.path.join(prefix, 'lists.json'), 'w') as f:
2011-12-04 17:05:48 +00:00
json.dump(lists, f, indent=2)
locations = []
for l in Location.select().orderBy('id'):
if l.lat_center != 0 and l.lng_center != 0 and l.area != 0:
data = {}
data['id'] = l.hid
data['name'] = l.name
data['south'] = l.lat_sw
data['west'] = l.lng_sw
data['north'] = l.lat_ne
data['east'] = l.lng_ne
data['lat'] = l.lat_center
data['lng'] = l.lng_center
data['area'] = l.area
data['created'] = int(l.created.strftime('%s'))
data['modified'] = int(l.modified.strftime('%s'))
data['alternativeNames'] = [l.name for l in l.alt_names]
try:
if l.creator:
data['user'] = l.creator.user_name
except SQLObjectNotFound:
pass
data['annotations'] = []
for a in l.layers:
data['annotations'].append(a.hid)
locations.append(data)
2011-12-04 17:05:48 +00:00
2011-12-05 13:49:34 +00:00
with open(os.path.join(prefix, 'locations.json'), 'w') as f:
2011-12-04 17:05:48 +00:00
json.dump(locations, f, indent=2)
notes = []
for n in Notes.select(Notes.q.notes!=''):
notes.append({
'user': n.user.user_name,
'item': n.video.hid,
'note': n.notes
})
2011-12-05 13:49:34 +00:00
with open(os.path.join(prefix, 'notes.json'), 'w') as f:
2011-12-04 17:05:48 +00:00
json.dump(notes, f, indent=2)