-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration.py
76 lines (57 loc) · 2.07 KB
/
migration.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
#!/usr/bin/python3
# ubuntu abhängigkeit:
# apt install python3-mysql.connector
import sys
import mysql.connector as mc
import xml.etree.ElementTree as ET
try:
connection = mc.connect(
host="localhost",
db="prosody",
user="root", passwd="root"
)
connection2 = mc.connect(
host="localhost",
db="ejabberd",
user="root", passwd="root"
)
except mc.Error as e:
print("Error %d: %s" % (e.args[0], e.args[1]))
sys.exit(1)
cursor = connection.cursor()
cursor2 = connection2.cursor()
print("Nachrichten werden von Prosody geladen...")
cursor.execute("SELECT * FROM prosodyarchive")
result = cursor.fetchall()
format_str = """INSERT INTO archive (`username`, `timestamp`, `peer`, `bare_peer`, `xml`, `txt`, `kind`)
VALUES ('{username}', {timestamp}, '{peer}', '{bare_peer}', '{xml}', '{txt}', 'chat');"""
anzahl = len(result)
print("Fertig geladen. Fange an umzubauen und in ejabberd zu laden.")
for r in result:
print("Verarbeite Nachricht " + str(r[0]) + " von " + str(anzahl))
dom = ET.fromstring(r[8])
timestamp = str(r[5]) + "000000" # microsekunden
if r[6].decode("utf-8") in dom.get('to'):
peerstr = dom.get('to')
elif r[6].decode("utf-8") in dom.get('from'):
peerstr = dom.get('from')
else:
peerstr = r[6].decode("utf-8")
text = "" # text aus xml Feld 'body'
for bodytext in dom.findall('body'):
if bodytext.text != None:
text += bodytext.text
text = text.replace("'", "'") # escapen
text = text.replace("\\", "\\\\") # noch mehr escapen
userName = r[2].decode("utf-8")
barepeer = r[6].decode("utf-8")
xmlstr = r[8].decode("utf-8")
xmlstr = xmlstr.replace("'", "\\'") # noch mal escapen
sql_command = format_str.format(username=userName, timestamp=timestamp, peer=peerstr, bare_peer=barepeer, xml=xmlstr, txt=text)
# print(sql_command) # test ausgabe des sql Befehls
cursor2.execute(sql_command)
connection2.commit()
cursor2.close()
connection2.close()
cursor.close()
connection.close()