Skip to content

Instantly share code, notes, and snippets.

@maowug
Created June 3, 2013 13:59
Show Gist options
  • Save maowug/5698324 to your computer and use it in GitHub Desktop.
Save maowug/5698324 to your computer and use it in GitHub Desktop.
init django with tabelog data
#!/usr/bin/env python
#encoding: utf-8
from django.conf import settings
from django.db.models import signals
from django.db import transaction
#
# from tbhBase import models as tbhBaseModels
# from tbhBase.models import Rst,Rvw
import os
import cPickle
from bs4 import BeautifulSoup
def init(sender, created_models=[], **kwargs):
"""
:param sender:
:param created_models:
:param kwargs:
"""
if tbhBaseModels.Rst in created_models:
# init_data_path = os.path.join(settings.SITE_ROOT,'data','東京_154_1_銀座_67_2_イ_67_.data')
for root, dirs, files in os.walk(os.path.join(settings.SITE_ROOT,'data')):
for fl in files:
if fl=='.DS_Store':
continue
init_data_path=os.path.join(root, fl)
print "importing data from: "+init_data_path
try:
rstList=cPickle.load(open(init_data_path,'rw'))
except:
print "------------------- unPickle: "+init_data_path
continue
for rst in rstList:
newRst=Rst()
for k in rst.keys():
if k=='name':
newRst.name=rst[k].encode('utf-8')
elif k=='genre':
newRst.genre=rst[k].encode('utf-8')
elif k=='addr':
newRst.addr=rst[k].encode('utf-8')
elif k=='seat':
newRst.seat=rst[k].encode('utf-8')
elif k=='bgt':
newRst.bgt=rst[k].encode('utf-8')
elif k=='avg':
newRst.avg=rst[k].encode('utf-8')
elif k=='usage':
newRst.usage=rst[k].encode('utf-8')
elif k=='KWs':
newRst.KWs=','.join(rst[k]).encode('utf-8')
elif k=='rvwList':
newRst.save()
for rvw in rst[k]:
newRvw=Rvw()
newRvw.rst=newRst
newRvw.title=rvw['title'].encode('utf-8')
newRvw.profile=rvw['profile'].encode('utf-8')
newRvw.time=rvw['time'].encode('utf-8')
newRvw.prices=''.join(rvw['prices']).encode('utf-8')
newRvw.totalScore=str(rvw['totalScore']).encode('utf-8')
newRvw.scores=str(rvw['scores']).encode('utf-8')
newRvw.vote=str(rvw['vote']).encode('utf-8')
newRvw.situation=str(rvw['situation']).encode('utf-8')
# #comment todo:prettify
newRvw.comment=BeautifulSoup(rvw['comment'].encode('utf-8')).get_text()
newRvw.save()
pass
else:
pass
# print "sa39s"
#eof for-k in rst.keys()
newRst.save()
del rstList
signals.post_syncdb.connect(init, sender=tbhBaseModels)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment