11import requests
22from BeautifulSoup import BeautifulSoup
33import getpass
4+ import time
5+ import random
6+ import re
7+ import urllib
48
59class CsdnHandler ():
610 """Csdn operator"""
@@ -14,9 +18,9 @@ def login(self):
1418 html = self .sess .get (url ).text
1519 soup = BeautifulSoup (html )
1620
17- lt = self .get_element_value (soup , 'name' , 'lt' )
18- execution = self .get_element_value (soup , 'name' , 'execution' )
19- _eventId = self .get_element_value (soup , 'name' , '_eventId' )
21+ lt = self .getElementValue (soup , 'name' , 'lt' )
22+ execution = self .getElementValue (soup , 'name' , 'execution' )
23+ _eventId = self .getElementValue (soup , 'name' , '_eventId' )
2024
2125 data = {
2226 'username' : username ,
@@ -28,17 +32,104 @@ def login(self):
2832
2933 response = self .sess .post (url , data )
3034
31- return self .is_login_success (response )
35+ return self .isLoginSuccess (response )
36+
37+ def autoComment (self ):
38+ if self .getSourceIds () is False :
39+ print 'Autocomment failed!'
40+ return
41+
42+ print 'Total %d source(s) wait for comment.' % len (self .sourceids )
43+
44+ nhandled = 0
45+ for sourceid in self .sourceids :
46+ left = len (self .sourceids ) - nhandled
47+
48+ sec = random .randrange (61 ,71 )
49+ print 'Wait %d seconds for start. %s source(s) left.' % (sec , left )
50+ time .sleep (sec )
51+
52+ self .comment (sourceid )
53+ nhandled += 1
54+
55+ print 'Finished!'
56+
57+ def getSourceIds (self ):
58+ self .sourceids = set ()
59+ pagecount = self .getPageCount ()
60+ if pagecount == 0 :
61+ return False
62+
63+ print 'Pagecount is %d.' % pagecount
64+
65+ pattern = re .compile (r'.+/(\d+)#comment' )
66+
67+ for n in range (1 , pagecount + 1 ):
68+ url = 'http://download.csdn.net/my/downloads/%d' % n
69+ html = self .sess .get (url ).text
70+ soup = BeautifulSoup (html )
71+ sourcelist = soup .findAll ('a' , attrs = {'class' : 'btn-comment' })
72+ if sourcelist is None :
73+ continue
74+ for source in sourcelist :
75+ href = source .get ('href' , None )
76+ if href is not None :
77+ rematch = pattern .match (href )
78+ if rematch is not None :
79+ self .sourceids .add (rematch .group (1 ))
80+
81+ return len (self .sourceids ) > 0
82+
83+ def getPageCount (self ):
84+ url = 'http://download.csdn.net/my/downloads'
85+ html = self .sess .get (url ).text
86+ soup = BeautifulSoup (html )
87+
88+ pagelist = soup .findAll ('a' , attrs = {'class' : 'pageliststy' })
89+ if pagelist is None :
90+ return 0
91+
92+ lasthref = pagelist [len (pagelist ) - 1 ].get ('href' , None )
93+ if lasthref is None :
94+ return 0
95+ return int (filter (str .isdigit , str (lasthref )))
96+
97+ def comment (self , sourceid ):
98+ print 'sourceid %s commenting...' % sourceid
99+ contents = [
100+ 'It just soso, but thank you all the same.' ,
101+ 'Neither good nor bad.' ,
102+ 'It is a nice resource, thanks for share.' ,
103+ 'It is useful for me, thanks.' ,
104+ 'I have looking this for long, thanks.'
105+ ]
106+ rating = random .randrange (1 ,6 )
107+ content = contents [rating - 1 ]
108+ t = '%d' % (time .time () * 1000 )
109+
110+ paramsmap = {
111+ 'sourceid' : sourceid ,
112+ 'content' : content ,
113+ 'rating' : rating ,
114+ 't' : t
115+ }
116+ params = urllib .urlencode (paramsmap )
117+ url = 'http://download.csdn.net/index.php/comment/post_comment?%s' % params
118+ html = self .sess .get (url ).text
119+ if html .find ('({"succ":1})' ) != - 1 :
120+ print 'sourceid %s comment succeed!' % sourceid
121+ else :
122+ print 'sourceid %s comment failed! response is %s.' % (sourceid , html )
32123
33124 @staticmethod
34- def get_element_value (soup , element_name , element_value ):
125+ def getElementValue (soup , element_name , element_value ):
35126 element = soup .find (attrs = {element_name : element_value })
36127 if element is None :
37128 return None
38129 return element .get ('value' , None )
39130
40131 @staticmethod
41- def is_login_success (response ):
132+ def isLoginSuccess (response ):
42133 if response .status_code != 200 :
43134 return False
44135 return - 1 != response .content .find ('lastLoginIP' )
0 commit comments