This repository has been archived by the owner on Feb 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathimport_lsp.py
executable file
·221 lines (159 loc) · 6.08 KB
/
import_lsp.py
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python
"""
This is a WIP script for importing data from the old LSP.
It expects a MySQL server without password and with the db being named 'lsp'
running on localhost.
"""
# TODO: How to properly import time data?
# TODO: Import subcategories (tags)
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'lsp2.settings'
import django
django.setup()
from django.db import transaction
transaction.set_autocommit(False)
from lsp2.submissions.models import *
import pymysql
import ftfy
###############################################################################
print("Connecting to database… ", end="")
con = pymysql.connect(host='localhost', user='root', passwd=None, db='lsp')
cur = con.cursor()
print("Done")
###############################################################################
def fix_encoding(text):
return ftfy.fix_text(text)
def print_status(cursor, name):
print(
"\rImported %d of %d %s" % (cursor.rownumber, cursor.rowcount, name),
end=''
)
###############################################################################
cur.execute(
'SELECT id, login, password, realname, is_admin FROM users GROUP BY login')
for row in cur:
id, login, password, realname, is_admin = row
realname = fix_encoding(realname)
u = User(
pk=id,
username=login,
first_name=realname.split()[0] if realname else "",
last_name=realname[realname.rfind(' '):] if realname else "",
password='sha1$$' + password,
is_staff=is_admin
)
u.save()
print_status(cur, "users")
print()
User(username="admin", password="pbkdf2_sha256$12000$FAyIyGH6tztt$ttgkL2UapbnKsZMJiB06dfUt7r19oELU2jCmIMMydXk=",is_superuser=1, is_staff=1).save()
###############################################################################
licenses = (
(1, 'Artistic License 2.0', 'http://opensource.org/licenses/Artistic-2.0'),
(2, 'BSD', 'http://opensource.org/licenses/bsd-license.php'),
(3, 'Common Public License', 'http://opensource.org/licenses/cpl1.0.php'),
(4, 'GNU Free Documentation License', 'http://www.gnu.org/copyleft/fdl.html'),
(5, 'Green openmusic', 'http://openmusic.linuxtag.org/green.html'),
(6, 'Yellow openmusic', 'http://openmusic.linuxtag.org/yellow.html'),
(7, 'Red openmusic', 'http://openmusic.linuxtag.org/red.html'),
(8, 'Creative Commons (by)', 'http://creativecommons.org/licenses/by/4.0/'),
(9, 'Creative Commons (by-nc)', 'http://creativecommons.org/licenses/by-nc/4.0/'),
(10, 'Creative Commons (by-nd)', 'http://creativecommons.org/licenses/by-nd/4.0/'),
(11, 'Creative Commons (by-sa)', 'http://creativecommons.org/licenses/by-sa/4.0/'),
(13, 'Creative Commons (by-nc-sa)', 'http://creativecommons.org/licenses/by-nc-sa/4.0/'),
(12, 'Creative Commons (by-nc-nd)', 'http://creativecommons.org/licenses/by-nc-nd/4.0/'),
)
for l in licenses:
License(
id=l[0],
name=l[1],
url=l[2]
).save()
print("Imported licenses")
###############################################################################
categories = {}
cur.execute('SELECT id, name FROM categories')
for row in cur:
categories[row[0]] = row[1]
print_status(cur, "categories")
print()
###############################################################################
cur.execute('SELECT id, user_id, filename, license_id, category, subcategory, \
description, insert_date, update_date, rate, size, hash, downloads FROM files')
for row in cur:
id, user_id, filename, license_id, category, subcategory, description, \
insert_date, update_date, rate, size, hash, downloads = row
if category < 1 or category > 6:
print("Fatal: Unknown category %d" % category)
continue
cat = categories[category]
if cat == "Projects":
# TODO: Get length of project in seconds
s = ProjectSubmission(
seconds=None
)
elif cat == "Samples":
# TODO: Get length of sample in milliseconds
s = SampleSubmission(
milliseconds=None
)
elif cat == "UI themes":
s = ThemeSubmission()
filename = filename[0:filename.rfind('.')]
elif cat == "Presets":
pass
elif cat == "Tutorials":
pass
elif cat == "Screenshots":
pass
s.id = id
s.uploader = User.objects.get(pk=user_id)
s.name = filename[0:filename.rfind('.')]
s.license = None if license_id==0 else License.objects.get(pk=license_id)
s.description = description
s.submitDate = insert_date
s.updateDate = update_date
s.save()
f = File(submission=s, filename=filename, version=1, size=size, downloadCount=downloads, sha1hash=hash, date=insert_date)
f.save()
print_status(cur, "files")
print()
###############################################################################
cur.execute('SELECT id, user_id, file_id, `date`, `text` FROM comments')
skipped = 0
for row in cur:
id, user_id, file_id, date, text = row
if Submission.objects.filter(pk=file_id).count() == 0:
skipped += 1
continue
comment = Comment(
pk=id,
user=User.objects.get(pk=user_id),
submission=Submission.objects.get(pk=file_id),
date=date,
text=text
)
comment.save()
print_status(cur, "comments")
print(" (%d skipped because of missing submission)" % skipped)
###############################################################################
cur.execute('SELECT id, file_id, user_id, stars FROM ratings')
skipped = 0
for row in cur:
id, file_id, user_id, stars = row
if Submission.objects.filter(pk=file_id).count() == 0:
skipped += 1
continue
# 3 stars ratings will not be counted
if stars == 3:
continue
vote = Vote(
user=User.objects.get(pk=user_id),
submission=Submission.objects.get(pk=file_id),
date=None,
upvote=1 if stars > 3 else 0
)
vote.save()
print_status(cur, "ratings")
print(" (%d skipped because of missing submission)" % skipped)
###############################################################################
transaction.commit()