-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathextract.py
295 lines (257 loc) · 10.5 KB
/
extract.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import os
import json
from bs4 import BeautifulSoup
with open('entelechy2014.xml', 'r',encoding="utf8") as f:
data = f.read()
# print(data)
data = BeautifulSoup(data,'lxml')
users = data.find_all(attrs={'name':'yqpg_users'})
# Users
userDict = []
for user in users[1:]:
uDict = {
'ID':user.find(attrs={'name':'ID'}).text,
'user_login':user.find(attrs={'name':'user_login'}).text,
'user_email':user.find(attrs={'name':'user_email'}).text,
'display_name':user.find(attrs={'name':'display_name'}).text
}
userDict.append(uDict)
# Posts and Pages
articles = data.find_all(attrs={'name':'yqpg_posts'})
articlesDict = []
for article in articles[1:]:
artDict = {
'ID': article.find(attrs={'name':'ID'}).text,
'post_author':article.find(attrs={'name':'post_author'}).text,
'post_date_gmt':article.find(attrs={'name':'post_date_gmt'}).text,
'post_content':article.find(attrs={'name':'post_content'}).text,
'post_title':article.find(attrs={'name':'post_title'}).text,
'post_excerpt':article.find(attrs={'name':'post_excerpt'}).text,
'post_status':article.find(attrs={'name':'post_status'}).text,
'comment_status':article.find(attrs={'name':'comment_status'}).text,
'post_name':article.find(attrs={'name':'post_name'}).text,
'post_modified_gmt':article.find(attrs={'name':'post_modified_gmt'}).text,
'post_parent':article.find(attrs={'name':'post_parent'}).text,
'guid':article.find(attrs={'name':'guid'}).text,
'post_type':article.find(attrs={'name':'post_type'}).text,
'post_mime_type':article.find(attrs={'name':'post_mime_type'}).text,
'comment_count':article.find(attrs={'name':'comment_count'}).text,
'like':"",
'dislike':""
}
if artDict['post_parent']=='0':
artDict['revision'] = []
artDict['attachment'] = []
artDict['other']=[]
articlesDict.append(artDict)
else:
for i in range(len(articlesDict)):
if articlesDict[i]['ID']==artDict['post_parent']:
if artDict['post_type']=='attachment':
articlesDict[i]['attachment'].append(artDict)
elif artDict['post_type']=='revision':
articlesDict[i]['revision'].append(artDict)
else:
articlesDict[i]['other'].append(artDict)
likeDislikes = data.find_all(attrs={'name':'yqpg_like_dislike_counters'})
for likeDislike in likeDislikes[1:]:
ldlDict = {
'id':likeDislike.find(attrs={'name':'id'}).text,
'post_id':likeDislike.find(attrs={'name':'post_id'}).text,
'ul_key':likeDislike.find(attrs={'name':'ul_key'}).text,
'ul_value':likeDislike.find(attrs={'name':'ul_value'}).text
}
i=0
for ar in articlesDict:
if ar['ID'] == ldlDict['post_id']:
if ldlDict['ul_key']=='like' or ldlDict['ul_key']=='c_like':
articlesDict[i]['like']=ldlDict['ul_value']
elif ldlDict['ul_key']=='dislike' or ldlDict['ul_key']=='c_dislike':
articlesDict[i]['dislike']=ldlDict['ul_value']
break
parentDirArt = "ArticlesFolder"
try:
os.mkdir(parentDirArt)
except:
print("Folder creation error")
parentDirPage = "PagesFolder"
try:
os.mkdir(parentDirPage)
except:
print("Pages Folder creation error")
for article in articlesDict:
if article['post_type']=='post':
path = os.path.join(parentDirArt,article['ID'])
try:
os.mkdir(path)
with open(os.path.join(path,f"{article['ID']}.json"),"w",encoding='utf-8') as f:
json.dump(article,f, indent = 5, sort_keys=True,ensure_ascii=False)
except:
print(f"Error in folder creation {article['ID']}")
elif article['post_type']=='page':
path = os.path.join(parentDirPage,article['ID'])
try:
os.mkdir(path)
with open(os.path.join(path,f"{article['ID']}.json"),"w",encoding='utf-8') as f:
json.dump(article,f, indent = 5, sort_keys=True,ensure_ascii=False)
except:
print(f"Error in folder creation {article['ID']}")
# Comments
comments = data.find_all(attrs={'name':'yqpg_comments'})
commentsDict = []
# approved comments
for comment in comments[1:]:
comDict = {
'comment_ID':comment.find(attrs={'name':'comment_ID'}).text,
'comment_post_ID':comment.find(attrs={'name':'comment_post_ID'}).text,
'comment_date_gmt':comment.find(attrs={'name':'comment_date_gmt'}).text,
'comment_content':comment.find(attrs={'name':'comment_content'}).text,
'comment_karma':comment.find(attrs={'name':'commnent_karma'}),
'comment_parent':comment.find(attrs={'name':'comment_parent'}).text,
'user_id':comment.find(attrs={'name':'user_id'}).text
}
commentsDict.append(comDict)
# nested comments
def nestedComment(comment,path):
try:
direc = os.listdir(path)
except:
return False
for list in direc:
if list == "comment.json":
continue
elif list == comment['comment_parent']:
# print(f"{comment['comment_ID']} + {list}")
path1 = os.path.join(path,list)
path1 = os.path.join(path1,f"{comment['comment_ID']}")
# print(path1)
try:
os.mkdir(path1)
with open(os.path.join(path1,"comment.json"),"w",encoding='utf-8') as f:
json.dump(comment,f, indent = 5, sort_keys=True,ensure_ascii=False)
return True
except:
print(f"Error in folder creation {comment['comment_ID']}")
else:
path2 = os.path.join(path,list)
flag = nestedComment(comment,path2)
if flag==True:
return True
return False
# Filling up comments
for comment in commentsDict:
if comment['comment_parent']=="0":
path = os.path.join(parentDirArt,comment['comment_post_ID'])
path = os.path.join(path,f"{comment['comment_ID']}")
try:
os.mkdir(path)
with open(os.path.join(path,"comment.json"),"w",encoding='utf-8') as f:
json.dump(comment,f, indent = 5, sort_keys=True,ensure_ascii=False)
except:
try:
path = os.path.join(parentDirPage,comment['comment_post_ID'])
path = os.path.join(path,f"{comment['comment_ID']}")
os.mkdir(path)
with open(os.path.join(path,"comment.json"),"w",encoding='utf-8') as f:
json.dump(comment,f, indent = 5, sort_keys=True,ensure_ascii=False)
except:
print(f"{comment['comment_ID']} has trouble")
else:
path = os.path.join(parentDirArt,comment['comment_post_ID'])
flag = nestedComment(comment,path)
if flag==False:
path = os.path.join(parentDirPage,comment['comment_post_ID'])
flag = nestedComment(comment,path)
# tags and their relations
terms = data.find_all(attrs={'name':'yqpg_terms'})
termsDict = []
for term in terms[1:]:
tDict = {
'term_id': term.find(attrs={'name':'term_id'}).text,
'name': term.find(attrs={'name':'term_id'}).text,
'slug':term.find(attrs={'name':'slug'}).text
}
termsDict.append(tDict)
termsTaxonomy = data.find_all(attrs={'name':'yqpg_term_taxonomy'})
termsTaxonomyDict = []
categories = []
posttags = []
authors = []
for termTax in termsTaxonomy[1:]:
ttDict = {
'term_taxonomy_id' : termTax.find(attrs={'name':'term_taxonomy_id'}).text,
'term_id' : termTax.find(attrs={'name':'term_id'}).text,
'taxonomy': termTax.find(attrs={'name':'taxonomy'}).text,
'description': termTax.find(attrs={'name':'description'}).text,
'parent': termTax.find(attrs={'name':'parent'}).text,
'count': termTax.find(attrs={'name':'count'}).text,
'posts': []
}
for tr in termsDict:
if tr['term_id']==ttDict['term_id']:
ttDict['name'] = tr['name']
ttDict['slug'] = tr['slug']
break
if ttDict['taxonomy']=='author':
authors.append(ttDict)
elif ttDict['taxonomy']=='post_tag':
posttags.append(ttDict)
elif ttDict['taxonomy']=='category':
categories.append(ttDict)
else:
termsTaxonomyDict.append(ttDict)
termsRelation = data.find_all(attrs={'name':'yqpg_term_relationships'})
termsRelationDict = []
for termRel in termsRelation[1:]:
terRel = {
'object_id':termRel.find(attrs={'name':'object_id'}).text,
'term_taxonomy_id':termRel.find(attrs={'name':'term_taxonomy_id'}).text
}
termsRelationDict.append(terRel)
flag = False
i=0
for ca in categories:
if ca['term_taxonomy_id']==terRel['term_taxonomy_id']:
flag = True
categories[i]['posts'].append(terRel['object_id'])
break
i+=1
if flag==True:
continue
i=0
for ca in posttags:
if ca['term_taxonomy_id']==terRel['term_taxonomy_id']:
flag = True
posttags[i]['posts'].append(terRel['object_id'])
break
i+=1
if flag==True:
continue
i=0
for ca in authors:
if ca['term_taxonomy_id']==terRel['term_taxonomy_id']:
flag = True
authors[i]['posts'].append(terRel['object_id'])
break
i+=1
os.mkdir('Config')
with open(os.path.join("Config","categories.json"),"w") as f:
json.dump(categories,f, indent = 5, sort_keys=True,ensure_ascii=False)
with open(os.path.join("Config","postTags.json"),"w") as f:
json.dump(posttags,f, indent = 5, sort_keys=True,ensure_ascii=False)
os.mkdir('Edition')
for pt in posttags:
if pt['slug'][:7]=='edition':
with open(os.path.join("Edition",f"{pt['name']}.json"),"w") as f:
json.dump(pt,f, indent = 5, sort_keys=True,ensure_ascii=False)
for au in authors:
s = au['description'].split(' ')
for us in userDict:
if us['ID']==s[len(s)-2]:
us['description']=au['description']
us['count']=au['count']
us['name']=au['name']
us['slug']=au['slug']
break
with open(os.path.join("Config","users.json"),"w") as f:
json.dump(userDict,f, indent = 5, sort_keys=True)