diff options
| author | root <root@lalalizard.com> | 2012-12-19 03:39:27 -0500 |
|---|---|---|
| committer | root <root@lalalizard.com> | 2012-12-19 03:39:27 -0500 |
| commit | 7320fc1c7e56235b1c4cf5d09e0cea7fbbca654a (patch) | |
| tree | 552d20094c3b8188930352ec42b4e8f30c5aa805 /tools/migrate_search.py | |
| parent | a79d045cc366670d87a4782acc79ec2955c71dd1 (diff) | |
migrate tools
Diffstat (limited to 'tools/migrate_search.py')
| -rwxr-xr-x | tools/migrate_search.py | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/tools/migrate_search.py b/tools/migrate_search.py new file mode 100755 index 0000000..93c4070 --- /dev/null +++ b/tools/migrate_search.py @@ -0,0 +1,130 @@ +#!/usr/bin/env python + +import os +import sys +import chardet +import json + +sys.path.append(os.path.join(os.path.dirname(__file__), '..')) +os.environ['DJANGO_SETTINGS_MODULE'] = 'scannerjammer.settings' + +from db import db as DB +from pprint import pprint + +import django +from datetime import datetime + +from django.conf import settings +from django.contrib.auth.models import User + +from backend.models import SJLike +from backend.models import SJRoom +from backend.models import SJContent +from backend.models import SJSearch +from backend.models import SJUserProfile + +from backend.views import thraw +from backend.views import freeze + +class MigrateDB(DB): + + def __init__(self, *args, **kwargs): + super(MigrateDB, self).__init__(*args, **kwargs) + + def get_table(self, name): + self.execute('SELECT * FROM %s' % name) + fields = [d[0] for d in self.cursor.description] + rows = self.cursor.fetchall() + for row in rows: + row = list(row) + for i, r in enumerate(row[:]): + if isinstance(r, str): + detect = chardet.detect(r) + try: + row[i] = unicode(r, detect['encoding'] or 'utf8', 'replace') + except LookupError: + row[i] = unicode(r, 'utf8', 'replace') + yield dict(zip(fields, row)) + + def get_radio_chat_table(self): + return self.get_table('radio_chat') + + def get_search_log_table(self): + return self.get_table('search_log') + + def get_sj_bg_table(self): + return self.get_table('sj_bg') + + def get_sj_chat_table(self): + return self.get_table('sj_chat') + + def get_sj_likes_table(self): + return self.get_table('sj_likes') + + def get_sj_likes_tmp_table(self): + return self.get_table('sj_likes_tmp') + + def get_sj_room_table(self): + return self.get_table('sj_room') + + def get_sj_search_log_table(self): + return self.get_table('sj_search_log') + + def get_sj_session_table(self): + return self.get_table('sj_sesson') + + def get_sj_url_table(self): + return self.get_table('sj_url') + + def get_sj_url_tmp_table(self): + return self.get_table('sj_url_tmp') + + def get_sj_user_table(self): + return self.get_table('sj_user') + + def get_sj_video_table(self): + return self.get_table('sj_video') + +if __name__ == '__main__': + + admin = User.objects.get(username='admin') + + db = MigrateDB() + db.connect() + + # Migrate sj_search_log table + sj_user = list(db.get_sj_user_table()) + map_user = {} + for row in sj_user: + try: + user = User.objects.get(username=row['username']) + except User.DoesNotExist: + print "ERROR in users" + + map_user[row['id']] = user.pk + + sj_search_log = list(db.get_sj_search_log_table()) + for row in sj_search_log: + user = admin + pk = map_user.get(row['userid'], 0) + if pk: + user = User.objects.get(pk=pk) + try: + sjsearch = SJSearch.objects.get( + user=user, + datetime=datetime.fromtimestamp(row['date']) + ) + print 'SJ Search Log Found' + except SJSearch.DoesNotExist: + sjsearch = SJSearch( + user=user, + datetime=datetime.fromtimestamp(row['date']) + ) + sjsearch.settings = dict( + url=row['url'], + count=row['count'], + terms=row['terms'], + title=row['title'] + ) + sjsearch.save() + |
