Exemplo n.º 1
0
class QqWeibo(object):
    """docstring for QqWeibo"""
    def __init__(self, app_key = "", app_secret = "", redirect_uri = "" ):
        self.app_key = app_key
        self.app_secret = app_secret
        self.redirect_uri = redirect_uri

    def CreateClient( self, app_key, app_secret, redirect_uri ):
        self.client = APIClient( app_key, app_secret, redirect_uri = redirect_uri )
        return self.client

    def LoginToQq( self ):
        client = self.CreateClient( self.app_key, self.app_secret, self.redirect_uri )
        return client.get_authorize_url()

    def GetQqAccessToken( self, code, open_id ):
        client = self.CreateClient( self.app_key, self.app_secret, self.redirect_uri )
        recived = self.client.request_access_token( code )
        self.client.set_access_token( recived.access_token, open_id, recived.expires_in )
        return recived.access_token

    def GetClient( self ):
        return self.client
        
        
        
Exemplo n.º 2
0
class Tenc(object):
    def __init__(self, app_key, app_secret, callback_url, access_token,
                 openid):
        self.api = APIClient(app_key, app_secret, redirect_uri=callback_url)
        self.api.set_access_token(access_token, openid, "1521094035")

    def getTimeline(self, count):
        """ 获取用户当前发布的微博 """
        timeline = self.api.get.statuses__broadcast_timeline(format="json",
                                                             pageflag=0,
                                                             pagetime=0,
                                                             lastid=0,
                                                             contenttype=0,
                                                             reqnum=count,
                                                             type=1)

        return timeline.data

    def getFavorites(self, count):
        """ 获取用户当前收藏的微博 """
        favs = self.api.get.fav__list_t(format="json",
                                        pageflag=0,
                                        pagetime=0,
                                        lastid=0,
                                        reqnum=count)

        return favs.data

    def getUid(self):
        """ 获取当前用户UID """
        uid = self.api.get.user__info(format="json")

        return uid.data.name

    def getUser(self, name):
        """ 获取用户信息 """
        user = self.api.get.user__other_info(format="json", name=name)

        return user.data

    def postPic(self, content, pic_url, longitude=None, latitude=None):
        if longitude == None or latitude == None:
            t = self.api.post.t__add_pic(content=content, pic=pic_url)
        else:
            t = self.api.post.t__add_pic(content=content,
                                         longitude=longitude,
                                         latitude=latitude,
                                         pic=pic_url)

        return t.id
Exemplo n.º 3
0
class QQWeiboFetcher():
  '''init'''
  def __init__(self, app_key, app_secret, callback_url, open_id=None):
        self.app_key = str(app_key)
        self.app_secret = str(app_secret)
        self.open_id = open_id
        #self.qq_login = str(qq_login)
        #self.qq_password = str(qq_password)
        self.callback_url = str(callback_url)
        self.api_reset_time = 0
        self.remaining_api_hits = 0

  def authorize(self):
    #self.open_id = open_id
    self.client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
    url = self.client.get_authorize_url()
    print "authorize url=%s", url
    access_code = raw_input("paste access code:")
    if not self.open_id:
      self.open_id = raw_input("paste open id:")
    #open_key = raw_input("paste open key:")

    r = self.client.request_access_token(access_code)
    access_token = r.access_token  # access token. e.g. abc123xyz456
    expires_in = r.expires_in      # token expires in
    self.client.set_access_token(r.access_token, self.open_id, r.expires_in)
    print "expires_in=" , time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(expires_in)) ,
    print "current_time=", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime( time.time() ))
  
  #renew access_token
  def renewToken(self):
    r = self.client.refresh_token(self.client.access_token)
    self.client.set_access_token(r.access_token, fetcher.client.open_id, r.expires_in)
    print "renew access_token expires_in=" , time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(expires_in)) ,
    print "current_time=", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime( time.time() ))
  
  #send API request after authentication
  def run(self, api_name, params_dict) :
    #parse parameter string into dictionary obj.
    ret = self.client.__getattr__(api_name)(**params_dict)
    return json.dumps(ret,ensure_ascii=False)
Exemplo n.º 4
0
class Tenc(object):
    def __init__(self, app_key, app_secret, callback_url, access_token, openid):
        self.api = APIClient(app_key, app_secret, redirect_uri=callback_url)
        self.api.set_access_token(access_token, openid, "1521094035")

    def postTweet(self, content, longitude=None, latitude=None):
        if longitude==None or latitude==None:
            self.api.post.t__add(content=content)
        else:
            self.api.post.t__add(content=content, longitude=longitude, latitude=latitude)

        return

    def postPic(self, content, pic_url, longitude=None, latitude=None):
        if longitude==None or latitude==None:
            self.api.post.t__add_pic_url(content=content, pic_url=pic_url)
        else:
            self.api.post.t__add_pic_url(content=content, 
                longitude=longitude, latitude=latitude, pic_url=pic_url)

        return
Exemplo n.º 5
0
class Tenc(object):
    def __init__(self, app_key, app_secret, callback_url, access_token, openid):
        self.api = APIClient(app_key, app_secret, redirect_uri=callback_url)
        self.api.set_access_token(access_token, openid, "1521094035")
    
    def getTimeline(self, count):
        """ 获取用户当前发布的微博 """
        timeline = self.api.get.statuses__broadcast_timeline(format="json",pageflag=0,pagetime=0,lastid=0,contenttype=0,reqnum=count, type=1)
        
        return timeline.data
    
    def getFavorites(self, count):
        """ 获取用户当前收藏的微博 """
        favs = self.api.get.fav__list_t(format="json",pageflag=0,pagetime=0,lastid=0,reqnum=count)
        
        return favs.data
    
    def getUid(self):
        """ 获取当前用户UID """
        uid = self.api.get.user__info(format="json")
        
        return uid.data.name
    
    def getUser(self, name):
        """ 获取用户信息 """
        user = self.api.get.user__other_info(format="json",name=name)
        
        return user.data

    def postPic(self, content, pic_url, longitude=None, latitude=None):
        if longitude==None or latitude==None:
            t = self.api.post.t__add_pic(content= content, pic=pic_url)
        else:
            t = self.api.post.t__add_pic(content= content, longitude=longitude, latitude=latitude, pic=pic_url)

        return t.id
Exemplo n.º 6
0
class Weibo:
    
    def __init__(self, api_info):
        self.client = APIClient(app_key = api_info['app_key'], app_secret = api_info['app_secret'])
        self.client.set_access_token(api_info['access_token'], api_info['open_id'],api_info['expires_in'])

    def search_weibo(self, text, page = 1, page_size = 30):
        return self.client.get.search__t(keyword = text, page = page, pagesize = page_size)

    def show_my_weibo(self):
        return self.client.post.statuses__broadcast_timeline()
    
    def get_users_weibo(self, username_list, page_flag = 1, page_time = 0, last_id = 0 ):
        return self.client.get.statuses__users_timeline_ids(names = username_list, type = 1, contenttype = 0, pageflag = page_flag, pagetime = page_time, lastid = last_id, reqnum = 70)

    def get_user_weibo(self, username, page_flag = 1, page_time = 0, last_id = 0 ):
        return self.client.get.statuses__user_timeline(name = username, type = 1, contenttype = 0, pageflag = page_flag, pagetime = page_time, lastid = last_id, reqnum = 70)

    def create_weibo(self, text):
        return self.client.post.t__add(content = "hello world!", clientip = CLIENT_IP)
        #return self.client.post.statuses__update(status = text)

    def upload_pic(self, pic):
        return self.client.upload.t__upload_pic(pic = pic, pic_url = "")
        #if pic_type == 1:
        #    return self.client.upload.t__upload_pic(pic_url = pic_url, pic_type = pic_type)
        #elif pic_type == 2:
        #    return self.client.upload.t__upload_pic(pic = pic, pic_type = pic_type)

    def create_weibo_with_pic(self, text, pic):
        return self.client.upload.t__add_pic(content = text, pic = pic )

    def create_weibo_with_pic_url(self, text, pic_url):
        return self.client.upload.t__add_pic_url(content = text, pic_url = pic_url )

    def repost_weibo(self, id, text):
        return self.client.post.t__re_add(reid = id, content = text)
        #return self.client.post.statuses__repost(id = id, status = text, is_comment = is_comment)
    
    def comments_show(self, id):
        ''' list all the reply of one weibo '''
        return self.client.get.comments__show(id = id)
        
    def comments_create(self, id, text):
        return self.client.post.t__comment(reid = id, content = text)

    def reply_create(self, id, text):
        return self.client.post.t__reply(reid = id, content = text)

    def comments_reply(self, id, cid, text):
        return self.client.post.comments__reply(id = id , cid = cid, comment = text)

    def get_reply_list(self, id, flg = 2, page_flag = 0, twitter_id = 0, req_num = 10, page_time = 0):
        return self.client.get.t__re_list(rootid = id, reqnum = req_num, flag = flg, pageflag = page_flag, pagetime = page_time, twitterid = twitter_id)
    def get_all_reply_list(self, id, max_num = 100):
        all_reply = []
        page_flag = 0
        twitter_id = 0
        page_time = 0
        get = 0
        res = None
        while True:
            try:
                res = self.get_reply_list(id, page_flag = page_flag, twitter_id = twitter_id, page_time = page_time)
                data = res.data
                last_weibo = data.info[-1]
            except:
                time.sleep(10)
                log.error("get reply failed " + str(sys.exc_info()))
                if res:log.error(str(res))
                break
                #continue
            page_time = last_weibo.timestamp
            twitter_id = last_weibo.id
            page_flag = 1
            all_reply.extend(data.info)
            get += len(data.info)
            print get, max_num, data.hasnext, twitter_id, page_time
            #if data.hasnext == 0: break
            if get >= max_num: break
        return all_reply
Exemplo n.º 7
0
    if type=="noon":
        return u"人是铁,饭是钢,同志们饭点到了!"
    if type=="evening":
        return u"同志们要知道南宁是个堵车的地方,不下班更待何时!"
    if type=="night":
        return u"别忘记晾衣服,关好门,大家晚安!"
    return msg
        
        



qq = APIClient("801058005", "31cc09205420a004f3575467387145a7", redirect_uri="callback_uri")
t = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
tsp = time.mktime(time.localtime(time.time()))+100
qq.set_access_token("83d36ac2caa900ab53374903183cba06", "5FD49B7E7A810115834085C835A0B42E", tsp)

msg = ""
if sys.argv[1]=="normal":
    #msg = u'智能家居机器人提醒:目前您室内温度为 %f ℃ %s' % (get_room_temperature(),get_day_weather())
    msg = u'智能家居机器人提醒:目前您室内温度为 %f ℃' % get_room_temperature()
if sys.argv[1]=="morning":
    #msg = u'智能家居机器人提醒:%s %s' % (say_hello(sys.argv[1]),get_room_temperature())
    msg = u'智能家居机器人提醒:%s' % say_hello(sys.argv[1])
if sys.argv[1]=="noon":
    msg = u'智能家居机器人提醒:%s' % say_hello(sys.argv[1])
if sys.argv[1]=="evening":
    msg = u'智能家居机器人提醒:%s' % say_hello(sys.argv[1])
if sys.argv[1]=="night":
    msg = u'智能家居机器人提醒:%s' % say_hello(sys.argv[1])
    
Exemplo n.º 8
0
# coding: utf-8
from qqweibo import APIClient
qq = APIClient("app_key", "app_secret", redirect_uri="callback_uri")
qq.set_access_token("access_token", "openid", "expire")
#print qq.post.t__add(content="test", clientip="202.22.251.18")
print qq.post.t__add_pic_url(content="test", clientip="real ip", pic_url="pic url")