-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExtractGoogleGroup.py
executable file
·479 lines (399 loc) · 19.8 KB
/
ExtractGoogleGroup.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
This script extracts entries from a google group to a data structure
Require 'BeautifulSoup' module
Released under the GPLv3. Report bugs to [email protected]
Hosted on https://github.com/tangramor/extract-google-group
(c) Wang Jun Hua, homepage: http://blog.bbsers.org/tattoo
General Public License: http://www.gnu.org/licenses/gpl-3.0.html
"""
__VERSION__="0.1"
import string
import re
import sys
import os
import codecs
import xmlrpclib
import urllib
import urllib2
from BeautifulSoup import BeautifulSoup,Tag,CData
import logging
class Extract:
def __init__(self, name):
self.groupname = name
#基本URL,第一个命令行参数应该是Google Group的名称
self.rootUrl = "https://groups.google.com"
self.baseUrl = self.rootUrl + "/group/" + self.groupname + "/"
#每页主题列表数目Google缺省设置为30条
self.topicNumPerPage = 30
#取得页面源码并返回soup对象
def _fetchPage(self, url):
logging.info("Begin to fetch page %s", url)
req = urllib2.Request(url)
req.add_header('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.5) Gecko/20070713 Firefox/2.0.0.5')
page = urllib2.build_opener().open(req).read()
logging.info("Fetch page successfully")
return BeautifulSoup(page)
def testFetchPage(self):
print self._fetchPage(self.baseUrl)
#取得主题总数
def _extractTotalTopicNumberFromPage(self):
url = self.baseUrl + "topics?tsc=1"
soup = self._fetchPage(url)
b = soup.find('div', {'class' : 'maincontbox'})
b = b.find('span')
b = b.findAll('b')[2]
return b.string
def getTotalTopicNumber(self):
cacheFile = None
cacheName = "_totalTopicNumber.cache"
if os.path.exists(cacheName):
logging.info('Found cache file')
cacheFile = open(cacheName,'r')
strNumber = cacheFile.readline()
cacheFile.close()
if strNumber:
logging.info("The total topics number of this group is: %s", strNumber)
return int(strNumber)
else:
cacheFile = open(cacheName,'w')
strNumber = self._extractTotalTopicNumberFromPage()
cacheFile.write(strNumber)
cacheFile.close()
logging.info("The total topics number of this group is: %s", strNumber)
return int(strNumber)
else:
cacheFile = open(cacheName,'w')
strNumber = self._extractTotalTopicNumberFromPage()
cacheFile.write(strNumber)
cacheFile.close()
logging.info("The total topics number of this group is: %s", strNumber)
return int(strNumber)
return int(b.string)
#取得当前google group的主题总数
def _totalTopicNumber(self):
self.totalTopicNumber = self.getTotalTopicNumber()
def testGetTotalTopicNumber(self):
self._totalTopicNumber()
print self.totalTopicNumber
#取得主题列表的页数,Google Group缺省是30个主题一页
def getTotalTopicListPageNumber(self, topicNumber):
iNumber = int(topicNumber)
if (iNumber % self.topicNumPerPage) > 0:
x = iNumber / self.topicNumPerPage + 1
logging.info("The total number of topic list page is: %d", x)
return x
else:
x = iNumber / self.topicNumPerPage
logging.info("The total number of topic list page is: %d", x)
return x
#取得当前google group的主题列表页数
def _totalTopicListPageNumber(self):
self._totalTopicNumber()
self.totalTopicListPageNumber = self.getTotalTopicListPageNumber(self.totalTopicNumber)
def testGetTotalTopicListPageNumber(self):
self._totalTopicListPageNumber()
print self.totalTopicListPageNumber
def _setup(self):
self._totalTopicNumber()
self._totalTopicListPageNumber()
#去到第x页主题列表并返回页面soup对象
def goToTopicListPage(self, page):
self._setup()
if page < self.totalTopicListPageNumber:
url = self.baseUrl + "topics?gvc=2&start=" + str(page * self.topicNumPerPage)
logging.info("Going to URL: %s", url)
return self._fetchPage(url)
elif page == self.totalTopicListPageNumber:
url = self.baseUrl + "topics?start=" + str(self.totalTopicNumber) + "&sa=N"
logging.info("Going to URL: %s", url)
return self._fetchPage(url)
else:
logging.info("No page to go...")
return ""
def testGoToTopicListPage(self):
self._totalTopicListPageNumber()
print self.goToTopicListPage(self.totalTopicListPageNumber)
#取得一个主题列表内所有主题的标题和链接
def getTopicAndUrlInTopicListPage(self, page):
list = []
logging.info("The page number to go is: %d", page)
b = self.goToTopicListPage(page)
b = b.find('div', {'class' : 'maincontoutboxatt'})
b = b.findAll('table')[0]
b = b.findAll('tr')
if b:
i = 0
for tr in b:
if i > 1: #前两个tr内容为表头信息,抛弃
tds = tr.findAll('td')
entry = {'subject' : '', 'link' : ''}
tmp = tds[1].find('a')
entry['link'] = self.rootUrl + tmp['href']
entry['subject'] = u''.join(map(CData, tmp.contents))
logging.info("Get topic: %s", entry['subject'])
list.append(entry)
i += 1
return list
def testGetTopicAndUrlInTopicListPage(self):
self._totalTopicListPageNumber()
print self.getTopicAndUrlInTopicListPage(self.totalTopicListPageNumber)
#给内容字符串中的链接添加前缀"https://groups.google.com"
def _addPrefixToUrl(self, s):
return s.replace("href=\"/", "href=\"" + self.rootUrl + "/")
def testAddPrefixToUrl(self):
logging.info("Test add <a href=\"/group/bbser/about\"> here!\n" + self._addPrefixToUrl("Test add <a href=\"/group/bbser/about\"> here!"))
print self._addPrefixToUrl("Test add <a href=\"/group/bbser/about\"> here!")
#从GroupName_group_members.csv中根据email地址前后缀取得完整email地址和用户昵称
def _getMailAddrFromMemberListCSV(self, prefix, surfix):
from UTF8CSV import UnicodeReader
csvreader = UnicodeReader(open(self.groupname + "_group_members.csv", 'rb'))
logging.debug("The prefix: %s; the surfix: %s", prefix, surfix)
for row in csvreader:
if (prefix in row[0]) & (surfix in row[0]):
return row
def testGetMailAddrFromMemberListCSV(self):
print self._getMailAddrFromMemberListCSV("tkh8", "@163.com")
#取得一个主题列表页内所有主题的内容
def getTopicContentInTopicListPage(self, list):
"""
Structure of a topic
topic
|-from (author)
|-email
|-date
|-subject
|-content
|-topiclink
|-individual_link (the link only to this paste)
|-replies
|-id
|-from (replier)
|-email
|-date
|-subject (often is "Re: $subject")
|-content
|-link (the link only to this paste)
"""
topics = []
for entry in list:
threads = {'from':'', 'email':'', 'date':'', 'subject':'','content':'', 'topiclink':'', 'individual_link':'', 'replies':[]}
threads['topiclink'] = entry['link']
threads['subject'] = entry['subject']
topicPage = self._fetchPage(entry['link'])
heads = topicPage.findAll('div', {'id' : 'oh'})
bodies = topicPage.findAll('div', {'id' : 'inbdy'})
#google group帖子里常常会有引用链接,需要使用javascript展开和收拢
togScript = u'''
<script language="javascript1.3"><!--
function tog() {
// tog: toggle the visibility of html elements (arguments[1..]) from none to
// arguments[0]. Return what should be returned in a javascript onevent().
display = arguments[0];
for( var i=1; i<arguments.length; i++ ) {
var x = document.getElementById(arguments[i]);
if (!x) continue;
if (x.style.display == "none" || x.style.display == "") {
x.style.display = display;
} else {
x.style.display = "none";
}
}
var e = is_ie ? window.event : this;
if (e) {
if (is_ie) {
e.cancelBubble = true;
e.returnValue = false;
return false;
} else {
return false;
}
}
}
function tog_quote( idnum ) {
return tog( "block", "qheader_shown_" + idnum, "qheader_hidden_" + idnum,
"qhide_" + idnum );
}
//--></script>
'''
for i in range(len(heads)):
head = heads[i]
body = bodies[i]
#1楼
if i == 0:
tmp = head.findAll('div')
fromtext = tmp[2].find('b').contents
if (fromtext[0].find('"') != -1) & (len(fromtext) == 3):
nameAndMail = fromtext[0].split('"')
author = nameAndMail[1]
email = self._addPrefixToUrl((str(nameAndMail[2]).replace("<", "")) + (str(fromtext[1])) + (str(fromtext[2]).replace(">", ""))).lstrip()
x = self._getMailAddrFromMemberListCSV(str(nameAndMail[2]).replace("<", "").lstrip(), str(fromtext[2]).replace(">", ""))
if x:
email = x[0]
if x[1]:
author = x[1]
elif (fromtext[0].find('"') != -1) & (len(fromtext) == 5):
nameAndMail = fromtext[0].split('"')
domainAndName = fromtext[2].split('<')
author = nameAndMail[1] + str(fromtext[1]) + str(domainAndName[0]).replace(""", "")
logging.debug("author: %s", author)
email = self._addPrefixToUrl((str(domainAndName[1])) + (str(fromtext[3])) + (str(fromtext[4]).replace(">", ""))).lstrip()
x = self._getMailAddrFromMemberListCSV(str(domainAndName[1]).lstrip(), str(fromtext[4]).replace(">", ""))
if x:
email = x[0]
if x[1]:
author = x[1]
elif fromtext[0].find('<') != -1:
nameAndMail = fromtext[0].split('<')
author = nameAndMail[0]
prefix = str(nameAndMail[1])
surfix = str(fromtext[2]).replace(">", "")
email = self._addPrefixToUrl(prefix + (str(fromtext[1])) + surfix).lstrip()
x = self._getMailAddrFromMemberListCSV(prefix, surfix)
if x:
email = x[0]
if x[1]:
author = x[1]
else:
email = self._addPrefixToUrl(str(fromtext[0]) + (str(fromtext[1])) + str(fromtext[2])).lstrip()
author = email
prefix = str(fromtext[0]).lstrip()
if prefix != None:
x = self._getMailAddrFromMemberListCSV(prefix, str(fromtext[2]))
if x:
email = x[0]
if x[1]:
author = x[1]
else:
author = email
#有时候帖子没有包含当地时间这一行,导致标题行上升了一位
if len(head.find(attrs={'class' : 'fontsize2'}).findAll('div')) == 3:
logging.debug("The link div: \n%s", tmp[5])
link = self._addPrefixToUrl(tmp[5].findAll('a')[2]['href'])
else:
logging.debug("The link div: \n%s", tmp[6])
link = self._addPrefixToUrl(tmp[6].findAll('a')[2]['href'])
threads['from'] = author
threads['email'] = email
#date = tmp[3].find('b').string.replace(" ", "").replace("\n", "").rpartition(':')
#threads['date'] = date[0] + date[1] + date[2].partition(' ')[0]
#use split() instead to support python 2.4
date = tmp[3].find('b').string.replace(" ", "").replace("\n", "").split(':')
threads['date'] = date[0] + ":" + date[1] + ":" + date[2].split(' ')[0]
#print threads['date']
content = u''.join(map(CData, body.contents))
content = re.sub(r'a class="qt" href="\?hide_quotes=[^"]+"', 'a class="qt" style="cursor:hand"', content)
#content = content.replace("'", "\\\'")
content = togScript + self._addPrefixToUrl(content)
threads['content'] = content
threads['individual_link'] = link
else:
#对1楼的回复帖子们
reply = {'id':'', 'from':'', 'email':'', 'date':'', 'subject':'','content':'', 'link':''}
tmp = head.findAll('div')
fromtext = tmp[2].find('b').contents
if (fromtext[0].find('"') != -1) & (len(fromtext) == 3):
nameAndMail = fromtext[0].split('"')
author = nameAndMail[1]
logging.debug("nameAndEmail variable: %s", nameAndMail)
email = self._addPrefixToUrl((str(nameAndMail[2]).replace("<", "")) + (str(fromtext[1])) + (str(fromtext[2]).replace(">", ""))).lstrip()
x = self._getMailAddrFromMemberListCSV(str(nameAndMail[2]).replace("<", "").lstrip(), str(fromtext[2]).replace(">", ""))
if x:
email = x[0]
if x[1]:
author = x[1]
elif (fromtext[0].find('"') != -1) & (len(fromtext) == 5):
nameAndMail = fromtext[0].split('"')
domainAndName = fromtext[2].split('<')
author = nameAndMail[1] + str(fromtext[1]) + str(domainAndName[0]).replace(""", "")
logging.debug("author: %s", author)
email = self._addPrefixToUrl((str(domainAndName[1])) + (str(fromtext[3])) + (str(fromtext[4]).replace(">", ""))).lstrip()
x = self._getMailAddrFromMemberListCSV(str(domainAndName[1]).lstrip(), str(fromtext[4]).replace(">", ""))
if x:
email = x[0]
if x[1]:
author = x[1]
elif fromtext[0].find('<') != -1:
nameAndMail = fromtext[0].split('<')
author = nameAndMail[0]
prefix = str(nameAndMail[1])
surfix = str(fromtext[2]).replace(">", "")
email = self._addPrefixToUrl(prefix + (str(fromtext[1])) + surfix).lstrip()
x = self._getMailAddrFromMemberListCSV(prefix, surfix)
if x:
email = x[0]
if x[1]:
author = x[1]
else:
email = self._addPrefixToUrl(str(fromtext[0]) + (str(fromtext[1])) + str(fromtext[2])).lstrip()
author = email
x = self._getMailAddrFromMemberListCSV(str(fromtext[0]).lstrip(), str(fromtext[2]))
if x:
email = x[0]
if x[1]:
author = x[1]
else:
author = email
#有时候帖子没有包含当地时间这一行,导致标题行上升了一位
if len(head.find(attrs={'class' : 'fontsize2'}).findAll('div')) == 3:
logging.debug(tmp[4])
subject = u''.join(map(CData, tmp[4].find('b').contents))
link = self._addPrefixToUrl(tmp[5].findAll('a')[2]['href'])
else:
logging.debug(tmp[5])
subject = u''.join(map(CData, tmp[5].find('b').contents))
link = self._addPrefixToUrl(tmp[6].findAll('a')[2]['href'])
reply['link'] = link
reply['id'] = i
reply['from'] = author
reply['email'] = email
#date = tmp[3].find('b').string.replace(" ", "").replace("\n", "").rpartition(':')
#reply['date'] = date[0] + date[1] + date[2].partition(' ')[0]
#use split() instead to support python 2.4
date = tmp[3].find('b').string.replace(" ", "").replace("\n", "").split(':')
reply['date'] = date[0] + ":" + date[1] + ":" + date[2].split(' ')[0]
logging.debug(reply['date'])
reply['subject'] = subject
content = u''.join(map(CData, body.contents))
content = re.sub(r'a class="qt" href="\?hide_quotes=[^"]+"', 'a class="qt" style="cursor:hand"', content)
#content = content.replace("'", "\\\'") #this should be done by user
content = togScript + self._addPrefixToUrl(content)
reply['content'] = content
threads['replies'].append(reply)
i += 1
#threads['replies'].reverse()
topics.append(threads)
return topics
def testGetTopicContentInTopicListPage(self):
self._totalTopicListPageNumber()
print self.getTopicContentInTopicListPage(self.getTopicAndUrlInTopicListPage(self.totalTopicListPageNumber))
#日期格式转换到Unix timestamp
def dateToTimestamp(self, date):
import time
return time.mktime(time.strptime(date, "%a, %d %b %Y %H:%M:%S"))
def testDateToTimestamp(self):
print self.dateToTimestamp("Fri, 26 May 2006 4:54:30")
#日期转换为中国常用格式
def chineseDate(self, date):
import time
week = [u'一', u'二', u'三', u'四', u'五', u'六', u'日']
logging.debug("Date to convert: %s", date)
x = time.strptime(date, "%a, %d %b %Y %H:%M:%S")
converted = time.strftime("%Y年%m月%d日 %H:%M:%S 星期", x).decode('utf8') + week[x[6]]
logging.debug("Date converted: %s", converted)
return converted
def testChineseDate(self):
print self.chineseDate("Fri, 26 May 2006 4:54:30") + u' hello'
#---------------------------------方法测试---------------------------------
if __name__ == '__main__':
test = Extract("bbser")
#test.testGetTotalTopicNumber()
#test.testGetTotalTopicListPageNumber()
#test.testGoToTopicListPage()
#test.testGetTopicAndUrlInTopicListPage()
#test.testAddPrefixToUrl()
#test.testGetMailAddrFromMemberListCSV()
#test.testGetTopicContentInTopicListPage()
#test.testDateToTimestamp()
#test.testChineseDate()