python 实现 自动定时发送人人状态

2013.9.20  因为人人改版,可能已无法使用


#-*- coding:UTF-8 -*-
'''
======================================
此程序根据 http://www.oschina.net/code/snippet_946076_17870 内容改编
Adapted BY: jxy
Mail:czjxy8898@gmail.com
======================================
'''
from sgmllib import SGMLParser
import sys,urllib2,urllib,cookielib
import datetime
import time
class spider(SGMLParser):
    def __init__(self,email,password):
        SGMLParser.__init__(self)
        self.h3=False
        self.h3_is_ready=False
        self.div=False
        self.h3_and_div=False
        self.a=False
        self.depth=0
        self.names=""
        self.dic={}   
         
        self.email=email
        self.password=password
        self.domain='renren.com'
        try:
            cookie=cookielib.CookieJar()
            cookieProc=urllib2.HTTPCookieProcessor(cookie)
        except:
            raise
        else:
            opener=urllib2.build_opener(cookieProc)
            urllib2.install_opener(opener)       

    def login(self):
        print 'start login'
        url='http://www.renren.com/PLogin.do'
        postdata={
                  'email':self.email,
                  'password':self.password,
                  'domain':self.domain  
                  }
        try:
          req=urllib2.Request(
                            url,
                            urllib.urlencode(postdata)
                            )
          self.file=urllib2.urlopen(req).read()
          idPos = self.file.index("'id':'")
          self.id=self.file[idPos+6:idPos+15]
          tokPos=self.file.index("get_check:'")
          self.tok=self.file[tokPos+11:tokPos+21]
          rtkPos=self.file.index("get_check_x:'")
          self.rtk=self.file[rtkPos+13:rtkPos+21]
          print 'success'
          return 1
        except:
          print 'error'
          return 0


    def publish(self,content):
        url1='http://shell.renren.com/'+self.id+'/status'
        postdata={
                  'content':content,
                  'hostid':self.id,
                  'requestToken':self.tok,
                  '_rtk':self.rtk,
                  'channel':'renren',
                  }
        req1=urllib2.Request(
                            url1,
                            urllib.urlencode(postdata)            
                            )
        self.file1=urllib2.urlopen(req1).read()
        print '%s:\n  %s have sended\n:(%s)'% (datetime.datetime.now(),self.email,postdata.get('content',''))

def tt(T=5):
  while 1:
    t = time.localtime()
    content='24小时测试:现在时间'+str(t.tm_hour)+'时'+str(t.tm_min)+'分'+str(t.tm_sec)+'秒'
    print content
    renrenspider.publish(content)
    time.sleep(60)
    T=T-1
    if T==0:
      break
user=raw_input('email or phone number:')
password=raw_input('password:')
renrenspider=spider(user,password)
if renrenspider.login() :
  #content=raw_input('please input content:')
 # renrenspider.publish(content.decode('gb2312').encode('utf-8'))

  while 1:
   if time.localtime().tm_sec==0 and time.localtime().tm_min==0:
      tt(24)
      break
   time.sleep(1)


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值