class SlideRelationArranger(SlideBase.SlideBaseArranger):

	def __init__(self,text):
		self.dealer = SqlDealer('SlideWeibo')
		self.fail_num = 0

	def runTask(self,suid):
		baseUrl = "http://weibo.com/uid/follow"
		uid = suid
		weibo = WebWeiboSlider()
		#login
		weibo.login('13580473454','19921023')
		opener = weibo.getOpener()
		header = weibo.getHeader()
		
		print "start slide"
		# 逻辑,遍历十页取关注。
		while True:
			sqlstr = "Select uid from userTable where isSlideRelation = '0'"
			rs = self.dealer.select(sqlstr,(),1)
			if rs != None:
				uid = str(rs[0]);	
				print uid,
				print type(uid)
			else :
				print "Slide END"
				break
			slideRelation = SlideRelation("",self.dealer)
			s = baseUrl.replace("uid",uid)
			for i in range(10):
				page = i + 1
				ss = s + "?page="+str(page)
				req = urllib2.Request(ss,headers = header)
				res = -1
				while True:
					try:
						res = self.load(opener,req ,timeout = 10)
						print res
						if res == -1:
							continue
						break
					except Exception,e: 	
						print e
						time.sleep(5)
						continue
				text = res.read()
				slideRelation.text = text
				rs = slideRelation.run(uid)
				if rs != 1:
					break
			sqlstr = "UPDATE userTable SET isSlideRelation = '1' where uid= %s "
			data = [uid]
			self.dealer.executenow(sqlstr,list(data))
    def __init__(self,text):
	self.dealer = SqlDealer('SlideWeibo')
	self.fail_num = 0
class SlidePersonPageArranger(SlideBase.SlideBaseArranger):

    def __init__(self,text):
	self.dealer = SqlDealer('SlideWeibo')
	self.fail_num = 0
	
    def runTask(self):
	baseUrl = "http://weibo.com/aj/mblog/mbloglist?count=15&"
	weibo = WebWeiboSlider()
	#login
        weibo.login('*****@*****.**','weibo19921023')
	#weibo.login('13580473454','19921023')
       # weibo.login('*****@*****.**','19921023')
        opener = weibo.getOpener()
	header = weibo.getHeader()

	print 'start slide'
	while True:
            print "new uid start"
            sqlstr = "select count(*) from userTable where isSlide = '0'"
            rs = self.dealer.select(sqlstr,(),1)
            cc = int(rs[0])
            r = random.randint(0,cc-1)

	    sqlstr = "Select uid,username from userTable where isSlide = '0' LIMIT " + str(r) + " , 1 "
	    rs = self.dealer.select(sqlstr,(),1)
	    if rs != None:
		uid = str(rs[0])
                print uid
		username = unicode(rs[1])
	    else:
		print "Slide End"
		break
	    slidePersonPage = SlidePersonPage("",self.dealer)
	    s = str(baseUrl)
	    s += "uid=" + uid;
            num = 0
	    for i in range(10):
		page = i + 1
		pre_page = page - 1
		if page == 1:
		    pre_page = 1
		for j in range(3):
                    try:
                        pass
                        #time.sleep(5)
                    except Exception,e:
                        pass
		    ss = str(s)
		    #print "j " + str(j)
		    ss += "&pagebar=" + str(j)
		    if page > 1 or j != 0:
			ss += "&pre_page=" + str(pre_page) + "&page=" + str(page)	
		    else:
			ss += "&page=" + str(page)
		    print "url: " + ss
		    req = urllib2.Request(ss,headers = header)	
		    res = -1
		    while True:
                        print "try to load"
			try :
			    res = self.load(opener,req,timeout = 10)
			    if res == -1:
				continue
			    break
		        except Exception,e:
			    print e
			    #time.sleep(5)
			    continue

		    text = res.read()
                    print "load finish"
                    #print text
		    slidePersonPage.text = text
		    rs = slidePersonPage.execute(username,uid)
		    if rs != 1:
		        break
                    else:
                        num += 1
            if num <= 10:
                continue
	    timedate = time.time()
	    sqlstr = "UPDATE userTable SET isSlide = '1',last_slide="+ str(timedate)
	    sqlstr += " where uid = %s" 
	    data = [uid]
	    self.dealer.executenow(sqlstr,list(data))