-
Notifications
You must be signed in to change notification settings - Fork 0
/
separate.py
320 lines (269 loc) · 8.62 KB
/
separate.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
from grab import Grab
import psycopg2
import os
import urllib.request
import time
import gc
def downloadpage(url):
for j in range(1, 6):
try:
html = urllib.request.urlopen(url).read()
except urllib.request.HTTPError:
try:
html = urllib.request.urlopen(url).read()
except Exception as e:
# html = 'Error 404'
f = open(str(i) + '.html', 'wb')
f.close()
if e.code:
print("Error " + str(e.code) + ": " + url)
break
else:
f = open(str(i) + '.html', 'wb')
f.write(html)
f.close()
print("Downloaded: " + url)
break
except Exception:
print ("Waiting " + str(5 * j) + " seconds...")
time.sleep(5 * j)
else:
f = open(str(i) + '.html', 'wb')
f.write(html)
f.close()
print("Downloaded: " + url)
break
def debugoutput():
global header
global topic
global author
global raiting
global date
global numberoftags
global tags
print(header)
print(topic)
print(author)
print(raiting)
print(date)
print(numberofcomments)
print(tags)
def authoradd(a):
SQL = "SELECT author_table.author_id FROM author_table WHERE author_table.author_nick='{0}'"
cur.execute(SQL.format(a))
ans = cur.fetchone()
if ans is None:
SQL = "INSERT INTO author_table(author_nick) VALUES ('{0}')"
cur.execute(SQL.format(a))
SQL = "SELECT author_table.author_id FROM author_table WHERE author_table.author_nick='{0}'"
cur.execute(SQL.format(a))
ans = cur.fetchone()
authorid = int(ans[0])
return authorid
def tagadd(t):
t = t.replace("'", '')
SQL = "SELECT tag_table.tag_id FROM tag_table WHERE tag_table.tag_name='{0}'"
cur.execute(SQL.format(t))
ans = cur.fetchone()
if ans is None:
SQL = '''INSERT INTO tag_table(tag_name) VALUES ('{0}')'''
cur.execute(SQL.format(t))
SQL = "SELECT tag_table.tag_id FROM tag_table WHERE tag_table.tag_name='{0}'"
cur.execute(SQL.format(t))
ans = cur.fetchone()
tagid = int(ans[0])
return tagid
def formatdate(d):
# print(d)
dd, mm, yyyy = d.split(' ', 2)
yyyy = yyyy[:4:]
try:
int(yyyy)
except:
yyyy = '1970'
if i > 247197:
yyyy = '2015'
mounth = {'января': '01',
'февраля': '02',
'марта': '03',
'апреля': '04',
'мая': '05',
'июня': '06',
'июля': '07',
'августа': '08',
'сентября': '09',
'октября': '10',
'ноября': '11',
'декабря': '12'}
mm = mounth[mm]
newdate = yyyy + '-' + mm + '-' + dd
return newdate
def getheader():
try:
header = g.doc.select('//h1[@class="title"]').text()
except:
try:
header = g.doc.select('//span[@class="post_title"]').text()
except:
header = g.doc.select('//h1[@class="megapost-head__title"]').text()
finally:
return header
finally:
return header
def gettopic():
try:
topic = g.doc.select('//div[@class="content html_format"]').text()
except:
topic = g.doc.select('//div[@class="content-text"]').text()
return topic
def getauthor():
try:
author = g.doc.select('//*[@class="author-info__nickname"]').text()
except:
try:
author = g.doc.select('//*[@class="author-info__name"]').text()
except:
author = g.doc.select('//*[@class="megapost-cover__blog-link"]').text()
if author[0] == '@':
author = author[1::]
return author
def getraiting():
try:
raitingstr = g.doc.select('//span[@class="voting-wjt__counter-score js-score"]').text()
except:
raiting = 0
else:
if raitingstr[0] == '–':
raitingstr = '-' + raitingstr[1::]
raiting = int(raitingstr)
return raiting
def getdate():
try:
date = g.doc.select('//div[@class="published"]').text()
except:
date = g.doc.select('//ul[@class="list list_inline"]/li[@class="list__item"][1]').text()
return formatdate(date)
def fillarticletable():
SQL = """INSERT INTO article_table(\
article_id, article_head, article_topic, article_rating, author_id, article_date)\
VALUES (%s, %s, %s, %s, %s, %s)"""
cur.execute(SQL, (i, header, topic, raiting, author_id, date))
def gettags():
tags = g.doc.select('//ul[@class="tags"]').text_list()
if not tags:
tags = g.doc.select('//ul[@class="tags icon_tag"]').text_list()
return tags
def filltagstable():
if tags:
for j in tags[0].split(', '):
j.lower()
# print(j)
tag_id = tagadd(j)
SQL = '''INSERT INTO tag_to_article_table(tag_id, article_id) VALUES (%s, %s)'''
cur.execute(SQL, (tag_id, i))
def getnumberofcomments():
try:
numberofcomments = g.doc.select('//span[@id="comments_count"]').text()
return numberofcomments
except:
return 0
def fillnumberofcomments():
SQL = '''UPDATE article_table SET article_comments_number = {0} WHERE article_id={1}'''
cur.execute(SQL.format(numberofcomments, i))
def fillcommentstable():
j = 1
while True:
# Comment id
# print(j)
try:
commentid = g.doc.select('//div[@class="comment_item"][{0}]/@id'.format(j)).text()[8::]
# print(commentid)
except:
# print("No comments")
break
# Comment text
try:
commenttext = g.doc.select('//div[@id="comment_{0}"]/div[@class="comment_body "]\
/div[@class="message html_format "]'.format(commentid)).text()
# print(commenttext)
except:
# Only in case of UFO (author banned)
j += 1
continue
# Comment parent
try:
commentparent = g.doc.select('//div[@id="comment_{0}"]/span[@class="parent_id"]\
/@data_parent_id'.format(commentid)).text()
except:
# In case of root comment
commentparent = 0
# finally:
# print(commentparent)
# Comment author
try:
commentauthor = g.doc.select('//div[@rel="{0}"]/a[@class="comment-item__username"]'.format(commentid)).text()
except:
commentauthor = g.doc.select('//div[@rel="{0}"]/a[@class="username"]'.format(commentid)).text()
finally:
commentauthorid = authoradd(commentauthor)
# print(commentauthor)
# DB filling
SQL = '''INSERT INTO comments_table\
(comment_id, comment_text, comment_parent_id, comment_article_id, comment_author_id)\
VALUES (%s, %s, %s, %s, %s);'''
cur.execute(SQL, (commentid, commenttext, commentparent, i, commentauthorid))
j += 1
# MAIN
gc.enable()
db = psycopg2.connect(database='', user='', host='', port=)
cur = db.cursor()
# os.chdir('')
g = Grab
for i in range(258593, 268278): # 268278
print(i)
try:
if os.path.getsize(str(i) + '.html') < 5500: # Draft or 404 Error
continue
except FileNotFoundError:
print('File {0}.html not found.\nTrying to download.'.format(str(i)))
url = 'http://habrahabr.ru/post/' + str(i)
downloadpage(url)
if os.path.getsize(str(i) + '.html') < 5500: # Draft or 404 Error
continue
f = open(str(i) + '.html', 'rb')
g = Grab(f.read())
f.close()
try:
g.doc.select('//title').text()
except:
url = 'http://habrahabr.ru/post/' + str(i)
downloadpage(url)
if os.path.getsize(str(i) + '.html') < 5500: # Draft or 404 Error
continue
f = open(str(i) + '.html', 'rb')
g = Grab(f.read())
f.close()
# Header
header = getheader()
# Topic
topic = gettopic()
# Author
author = getauthor()
author_id = authoradd(author)
# Raiting
raiting = getraiting()
# Publishing date
date = getdate()
# Filling article table
fillarticletable()
# Tags
tags = gettags()
filltagstable()
# Comments
numberofcomments = getnumberofcomments()
fillnumberofcomments()
if int(numberofcomments) > 0:
fillcommentstable()
db.commit()
# gc.collect()
# debugoutput()