Forked from richardkiss/App Engine Direct Export
Last active
August 29, 2015 14:25
-
-
Save idreamsi/144f3cd8878a16f38a7b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python | |
import sys | |
import time | |
import csv | |
import sqlite3 | |
from google.appengine.datastore import entity_pb | |
from google.appengine.api import datastore | |
from django.db import transaction | |
from home.models import Alias, Person | |
from operations.models import Statistic | |
def export_Person(obj, key_name): | |
# create a Django model instance based on Google Entity obj | |
PROPS = "create_time nickname website_link photo_url last_access time_to_post time_zone_offset_minutes fb_access_token location".split() | |
p = Person(id=key_name) | |
for prop in PROPS: | |
setattr(p, prop, obj.get(prop)) | |
p.save() | |
print p | |
def export_Statistic(obj, key_name): | |
# remove for clarity | |
# very similar to export_Person | |
pass | |
EXPORT_LOOKUP_1 = { | |
'Person' : export_Person, | |
'Statistic' : export_Statistic, | |
} | |
def export_Alias(obj, key_name): | |
# remove for clarity | |
# very similar to export_Person | |
pass | |
EXPORT_LOOKUP_2 = { | |
'Alias' : export_Alias, | |
# other entities removed for clarity | |
} | |
def main(): | |
ct = 0 | |
conn = sqlite3.connect("./backups/laffq.db", isolation_level=None) | |
for phase in (EXPORT_LOOKUP_1, EXPORT_LOOKUP_2): | |
cursor = conn.cursor() | |
cursor.execute('select id, value from result') | |
with transaction.commit_on_success(): | |
for entity_id, entity in cursor: | |
entity_proto = entity_pb.EntityProto(contents=entity) | |
obj = datastore.Entity._FromPb(entity_proto) | |
kind = obj.key().kind() | |
key_name = obj.key().id_or_name() | |
f = phase.get(kind) | |
if f: | |
f(obj, key_name) | |
print "done" | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment