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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
#!/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_video 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_room = list(db.get_sj_room_table())
map_room = {}
for row in sj_room:
try:
room = SJRoom.objects.get(name=row['name'])
except SJRoom.DoesNotExist:
print "ERROR"
map_room[row['id']] = room.pk
sj_video = list(db.get_sj_video_table())
map_video = {}
for row in sj_video:
user = admin
pk = map_user.get(row['userid'], 0)
if pk:
user = User.objects.get(pk=pk)
room = SJRoom.objects.get(name='main')
pk = map_room.get(row['roomid'], 0)
if pk:
room = SJRoom.objects.get(pk=pk)
try:
video = SJContent.objects.get(
user=user,
datetime=datetime.fromtimestamp(row['date']),
room=room,
content_type='video')
except SJContent.DoesNotExist:
video = SJContent(
user=user,
datetime=datetime.fromtimestamp(row['date']),
room=room,
content_type='video')
video.settings = dict(
url=row['url'],
title=row['title'],
likes=row['likes'],
removed=row['removed'],
thumbnail=row['thumbnail'],
username=row['username']
)
video.save()
row['__sjcontent_video_pk'] = video.pk
map_video[row['id']] = video.pk
|