forked from bravecollective/forums
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer.py
More file actions
87 lines (63 loc) · 2.92 KB
/
Copy pathtransfer.py
File metadata and controls
87 lines (63 loc) · 2.92 KB
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
# encoding: utf-8
from __future__ import unicode_literals, print_function
import MySQLdb
import re
import HTMLParser
from bson import ObjectId
from datetime import datetime
from itertools import groupby
from brave.forums.component.forum.model import Forum
from brave.forums.component.thread.model import Thread
from brave.forums.component.comment.model import Comment
from brave.forums.auth.model import Character, Entity
from brave.core.util.eve import APICall, populate_calls
from marrow.util.bunch import Bunch
log = __import__('logging').getLogger(__name__)
def import_evebb(old_fid, new_fid, host='mysql.bravenewbies.net', user='amcgregor', password='', database='bravenew_evebb', prefix='evebb_'):
forum = Forum.objects(short=new_fid).first()
if not forum:
log.error("Forum not found: %s", new_fid)
return
_lookup = APICall.objects.get(name='eve.CharacterID')
_lookup_cache = dict()
lookup = lambda i: _lookup_cache[i] if i in _lookup_cache else _lookup_cache.setdefault(i, _lookup(names=i).row[0].characterID)
db = MySQLdb.connect(host=host, user=user, passwd=password, db=database, use_unicode=True)
query = """SELECT
topic.id, topic.subject, topic.last_post, topic.num_views, topic.num_replies, topic.closed, topic.sticky,
post.poster, post.message, post.posted, post.edited
FROM {prefix}posts AS post
LEFT JOIN {prefix}topics as topic ON post.topic_id = topic.id
WHERE topic.poll_type = 0
AND topic.poster NOT LIKE 'CCP %'
AND topic.forum_id = {forum_id}
ORDER BY topic.posted ASC, post.posted ASC
"""
cursor = db.cursor()
cursor.execute(query.format(prefix=prefix, forum_id=old_fid))
current = None
columns = tuple([d[0].decode('utf8') for d in cursor.description])
for row in cursor:
row = Bunch(zip(columns, row))
if current != row.id:
current = row.id
print("Thread", row.id, row.subject, row.posted, row.poster)
thread = Thread(forum=forum, title=row.subject, modified=datetime.fromtimestamp(row.last_post))
thread.stat.views = row.num_views
thread.stat.comments = row.num_replies + 1
thread.flag.sticky = bool(row.sticky)
thread.flag.locked = bool(row.closed)
thread.save()
print("Reply", row.posted, row.poster)
try:
poster = Character.objects.get_or_create(character=Entity(
id = lookup(row.poster),
name = row.poster
))[0]
except Character.MultipleObjectsReturned:
poster = Character.objects(character__name=row.poster).first()
comment = Comment(
id = ObjectId.from_datetime(datetime.fromtimestamp(row.posted)),
message = row.message,
creator = poster
)
Thread.objects(id=thread.id).update_one(push__comments=comment)