summaryrefslogtreecommitdiff
path: root/tools/migrate_search.py
blob: 93c40701d32718b078799231d9c8efe8e0daa468 (plain)
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
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()