-
Notifications
You must be signed in to change notification settings - Fork 0
/
makedb.py
161 lines (153 loc) · 5.13 KB
/
makedb.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
import sqlite3
import os
import xml.etree.cElementTree as etree
import logging
ANATHOMY = {
'badges': {
'Id': 'INTEGER',
'UserId': 'INTEGER',
'Name': 'TEXT',
'Date': 'DATETIME',
'Class': 'INTEGER', #1: Gold, 2: Silver, 3:Bronze.
'TagBased': 'INTEGER'#1(True) or 0(False). Based on tags like Java,Python etc.
},
'comments': {
'Id': 'INTEGER',
'PostId': 'INTEGER',
'Score': 'INTEGER',
'Text': 'TEXT',
'CreationDate': 'DATETIME',
'UserId': 'INTEGER',
'UserDisplayName': 'TEXT'
},
'posts': {
'Id': 'INTEGER',
'PostTypeId': 'INTEGER', # 1: Question, 2: Answer
'ParentID': 'INTEGER', # (only present if PostTypeId is 2)
'AcceptedAnswerId': 'INTEGER', # (only present if PostTypeId is 1)
'CreationDate': 'DATETIME',
'Score': 'INTEGER',
'ViewCount': 'INTEGER',
'Body': 'TEXT',
'OwnerUserId': 'INTEGER', # (present only if user has not been deleted)
'OwnerDisplayName': 'TEXT',
'LastEditorUserId': 'INTEGER',
'LastEditorDisplayName': 'TEXT', # ="Rich B"
'LastEditDate': 'DATETIME', #="2009-03-05T22:28:34.823"
'LastActivityDate': 'DATETIME', #="2009-03-11T12:51:01.480"
'CommunityOwnedDate': 'DATETIME', #(present only if post is community wikied)
'Title': 'TEXT',
'Tags': 'TEXT',
'AnswerCount': 'INTEGER',
'CommentCount': 'INTEGER',
'FavoriteCount': 'INTEGER',
'ClosedDate': 'DATETIME'
},
'votes': {
'Id': 'INTEGER',
'PostId': 'INTEGER',
'UserId': 'INTEGER',
'VoteTypeId': 'INTEGER',
# - 1: AcceptedByOriginator
# - 2: UpMod
# - 3: DownMod
# - 4: Offensive
# - 5: Favorite
# - 6: Close
# - 7: Reopen
# - 8: BountyStart
# - 9: BountyClose
# - 10: Deletion
# - 11: Undeletion
# - 12: Spam
# - 13: InformModerator
'CreationDate': 'DATETIME',
'BountyAmount': 'INTEGER'
},
'posthistory': {
'Id': 'INTEGER',
'PostHistoryTypeId': 'INTEGER',
'PostId': 'INTEGER',
'RevisionGUID': 'INTEGER',
'CreationDate': 'DATETIME',
'UserId': 'INTEGER',
'UserDisplayName': 'TEXT',
'Comment': 'TEXT',
'Text': 'TEXT'
},
'postlinks': {
'Id': 'INTEGER',
'CreationDate': 'DATETIME',
'PostId': 'INTEGER',
'RelatedPostId': 'INTEGER',
'PostLinkTypeId': 'INTEGER',
'LinkTypeId': 'INTEGER'
},
'users': {
'Id': 'INTEGER',
'Reputation': 'INTEGER',
'CreationDate': 'DATETIME',
'DisplayName': 'TEXT',
'LastAccessDate': 'DATETIME',
'WebsiteUrl': 'TEXT',
'Location': 'TEXT',
'Age': 'INTEGER',
'AboutMe': 'TEXT',
'Views': 'INTEGER',
'UpVotes': 'INTEGER',
'DownVotes': 'INTEGER',
'EmailHash': 'TEXT',
'AccountId': 'INTEGER',
'ProfileImageUrl': 'TEXT'
},
'tags': {
'Id': 'INTEGER',
'TagName': 'TEXT',
'Count': 'INTEGER',
'ExcerptPostId': 'INTEGER',
'WikiPostId': 'INTEGER'
}
}
def dump_files(file_names, anathomy,
dump_path='.',
dump_database_name='so-dump.db',
create_query='CREATE TABLE IF NOT EXISTS {table} ({fields})',
insert_query='INSERT INTO {table} ({columns}) VALUES ({values})',
log_filename='so-parser.log'):
logging.basicConfig(filename=os.path.join(dump_path, log_filename), level=logging.INFO)
db = sqlite3.connect(os.path.join(dump_path, dump_database_name))
for file in file_names:
print
"Opening {0}.xml".format(file)
with open(os.path.join(dump_path, file + '.xml')) as xml_file:
tree = etree.iterparse(xml_file)
table_name = file
sql_create = create_query.format(
table=table_name,
fields=", ".join(['{0} {1}'.format(name, type) for name, type in anathomy[table_name].items()]))
print('Creating table {0}'.format(table_name))
try:
logging.info(sql_create)
db.execute(sql_create)
except Exception, e:
logging.warning(e)
for events, row in tree:
try:
if row.attrib.values():
logging.debug(row.attrib.keys())
query = insert_query.format(
table=table_name,
columns=', '.join(row.attrib.keys()),
values=('?, ' * len(row.attrib.keys()))[:-2])
db.execute(query, row.attrib.values())
print ".",
except Exception, e:
logging.warning(e)
print "x",
finally:
row.clear()
print "\n"
db.commit()
del (tree)
if __name__ == '__main__':
dump_files(ANATHOMY.keys(), ANATHOMY)