Example #1
0
def weibo(username, password):
	# 登陆验证
	c = Client('3842240593', '93f0c80150239e02c52011c858b20ce6', 'https://api.weibo.com/oauth2/default.html', username=username, password=password)
	# 获取课表信息
	text = lesson_info()
	# 发布一条微博
	c.post('statuses/update', status=text)
Example #2
0
def main():
    """获取用户第一条评论,当第一条微博改变时,进行命令执行,并在微博评论处返回执行信息"""

    c = Client(APP_KEY, APP_SECRET, CALLBACK_URL, username=USERID, password=PASSWD)

    print 'Login success'
    print 'Listening...'

    UID = c.get('account/get_uid')['uid']  # 获取用户UID
    status = c.get('users/show', uid=UID)['status']  # 获取用户最近微博

    current_status = status
    while True:
        current_status = c.get('users/show', uid=UID)['status']  # 获取用户最近微博
        current_text = current_status['text']
        current_id = current_status['id']
        print time.ctime(),current_text

        if current_id != status['id'] and current_text:
            tmp = subprocess.check_output(current_text,shell = True)
            tmp = tmp[:140]  # 140字限制
            c.post('comments/create', id=current_id, comment=tmp)
            print tmp
            status = current_status

        time.sleep(10)  # ip限制1000次/小时
Example #3
0
def main():
    """获取用户第一条评论,当第一条微博改变时,进行命令执行,并在微博评论处返回执行信息"""

    c = Client(APP_KEY,
               APP_SECRET,
               CALLBACK_URL,
               username=USERID,
               password=PASSWD)

    print 'Login success'
    print 'Listening...'

    UID = c.get('account/get_uid')['uid']  # 获取用户UID
    status = c.get('users/show', uid=UID)['status']  # 获取用户最近微博

    current_status = status
    while True:
        current_status = c.get('users/show', uid=UID)['status']  # 获取用户最近微博
        current_text = current_status['text']
        current_id = current_status['id']
        print time.ctime(), current_text

        if current_id != status['id'] and current_text:
            tmp = subprocess.check_output(current_text, shell=True)
            tmp = tmp[:140]  # 140字限制
            c.post('comments/create', id=current_id, comment=tmp)
            print tmp
            status = current_status

        time.sleep(10)  # ip限制1000次/小时
Example #4
0
def main():
    """
    获取用户第一条评论,当第一条微博改变时,进行命令执行,
    并在微博评论处返回执行信息
    """

    c = Client(APP_KEY, APP_SECRET, CALLBACK_URL, username=USERID, password=PASSWD)

    print 'Login success'
    print 'Listening...'

    UID = c.get('account/get_uid')['uid']  # 获取用户UID
    last_status = c.get('users/show', uid=UID)['status']  # 获取用户最近微博
    last_id = last_status['id']
    print "last-id: ",last_id
    while True:
        current_status = c.get('users/show', uid=UID)['status']  # 获取用户最近微博
        current_text = current_status['text']
        current_id = current_status['id']
        print time.ctime(),current_text
        if current_id != last_id and current_text[0] == '$':
            if current_text[1] == '@': # 返回执行结果
                current_text = current_text.strip('$@')
                tmp = subprocess.check_output(current_text,shell = True)
                tmp = tmp[:140]
                c.post('comments/create', id=current_id, comment=tmp)
            else:
                current_text = current_text.strip('$@')
                os.system(current_text)
                tmp = "excute success!"
                c.post('comments/create', id=current_id, comment=tmp)
            print tmp
            last_id = current_id  # 更新最近微博id

        time.sleep(5)  # ip限制1000次/小时
Example #5
0
class SinaWeibo:
    def __init__ (self, username = wb_cfg.USER, passwd = wb_cfg.PASSWD, logger = None):
        self.APP_KEY = wb_cfg.APP_KEY
        self.APP_SECRET = wb_cfg.APP_SECRET
        self.CALLBACK = wb_cfg.CALLBACK
        self.USER = username
        self.PASSWD = passwd
        self.UID = wb_cfg.UID
        self.log = logger
        try:
            self.client = Client(self.APP_KEY, self.APP_SECRET, self.CALLBACK, 
                                token=None, 
                                username=self.USER, 
                                password=self.PASSWD)
            log_info(u"微博模块", u"User %s Login Successful!" % self.USER, self.log)
        except Exception:
            log_warn(u"微博模块", u"User %s Login Failed!" % self.USER, self.log)

        
     
    def get_timeline(self):
        if self.client:
            return self.client.get('users/show', uid = self.UID)
        
    def get_friend_status(self, cnt = 20):
        if self.client:
            return self.client.get('statuses/friends_timeline', uid = self.UID, count = cnt)
    
    def post_statuses(self, str_msg):
        if self.client:
            self.client.post('statuses/update', status = str_msg, uid = self.UID)
    
    def repost_friend(self, iterval):
        # 每相隔多久时间,转发一次朋友圈的微博
        # 朋友圈
        statuses = (self.get_friend_status( cnt = 100 ))["statuses"]
        if statuses:
            for item in statuses:
                #if item['text'].find('nicol:') != -1:
                #    log_info(u"微博模块",u"Alreadyed include me, skip it[%d]!\n" % item['id'], self.log)
                #    continue;
                #if item['id'] == wb_cfg.UID :
                #    log_info(u"微博模块",u"My Own's weibo, skip it[%d]!\n" % item['id'], self.log)
                #    print item['text']
                #    continue;
                if not REPOST_WHITE.has_key(item['user']['id']):
                    log_info(u"微博模块",u"Not in the repost_white list, skip [%d]!\n" % item['id'], self.log)
                    continue                  
                else:
                    try:
                        ret = self.client.post('statuses/repost', id = item['id'], status = u"[Nicol]强势转发微博", is_comment = 1)
                    except Exception, e:
                        log_warn(u"微博模块",u"Runtime Error:%s" % e, self.log)
                        continue
                    
                    log_info(u"微博模块",u"Repost item for [%d]!\n" % item['id'],self.log)
                
                #等待一会儿
                time.sleep(random.randint(300,1000))
Example #6
0
def send():
    c = Client(API_KEY,
               API_SECRET,
               REDIRECT_URI,
               username=USERID,
               password=PASSWD)
    f = open('test.jpg', 'rb')
    c.post('statuses/upload', status='new weibo!', pic=f)
    f.close()
Example #7
0
def post_new(content, pic, location):
    client = Client(APP_KEY,
                    APP_SECRET,
                    CALLBACK_URL,
                    username=USERID,
                    password=PASSWD)
    print client.post('statuses/upload',
                      status=content,
                      pic=pic,
                      lat=location[0],
                      long=location[1])
Example #8
0
def post_weibo(text, pic):
    try:
        weibo_bot = Client(APP_KEY, APP_SECRET, REDIRECT_URI, username=USER_NAME, password=PASSWORD)
        with open(pic, 'rb') as img:
            weibo_bot.post('statuses/share', status=text, pic=img)
        # print('微博已发送')
    except Exception as e:
        if e.args[0] != '20016 update weibo too fast!':
            logging.exception('发送微博失败:')
        if '10023' in e.args[0] or '20003' in e.args[0]:
            logging.warning('任务暂停')
            time.sleep(60 * 60 * 2)  # 触发接口频次限制时沉睡两小时
            logging.info('任务继续运行')
        raise e
def create_favorite(weibo_index, mongo_obj_index_list):
    """
    通过微博写入接口,将微博id为id的信息流加入"我的收藏"

    :param weibo_index: str, 微博id对应的字符串
    :param mongo_obj_index_list: list, 由序列化后在mongodb中的object_id组成
    :return: NULL
    """

    with open('/Users/wumengling/PycharmProjects/customized_weibo/config/weibo_config.json', 'r') as weibo_config_file:
            weibo_config = load(weibo_config_file)
            oauth_client = Client(weibo_config['API_KEY'], weibo_config['API_SECRET'], weibo_config['REDIRECT_URI'],
                                  weibo_config['TOKEN'])
            oauth_client.post('favorites/create', id=mongo_obj_index_list[weibo_index])
Example #10
0
def share_NASApicoftheday(text, imgurl):
    out_file = "./"
    picoftheday = wget.download(imgurl, out=out_file)
    path = os.path.abspath(picoftheday)
    uploader = open(path, 'rb')
    c = Client(APP_KEY,
               APP_SECRET,
               CALLBACK,
               username=USERNAME,
               password=PASSWORD)
    c.post('statuses/share',
           status=text + "http://www.baidu.com",
           pic=uploader)
    uploader.close()
    os.remove(path)
Example #11
0
File: views.py Project: envobe/hulu
def wbimg(request):
    if 'weibo_username' in os.environ:
        if request.method == 'POST':
            url = 'https://upload.api.weibo.com/2/statuses/upload.json'
            weiboclient = Client('4157302825', '517583a4b3197943dda94a45c5823c61', 'hulu.im', username=os.environ['weibo_username'], password=os.environ['weibo_password'])
            content = weiboclient.post('statuses/upload', status='https://hulu.im', pic=request.FILES['file'])
            return jsonp(request, content)
    else:
        return redirect('/')
def create_favorite(weibo_index, mongo_obj_index_list):
    """
    通过微博写入接口,将微博id为id的信息流加入"我的收藏"

    :param weibo_index: str, 微博id对应的字符串
    :param mongo_obj_index_list: list, 由序列化后在mongodb中的object_id组成
    :return: NULL
    """

    with open(
            '/Users/wumengling/PycharmProjects/customized_weibo/config/weibo_config.json',
            'r') as weibo_config_file:
        weibo_config = load(weibo_config_file)
        oauth_client = Client(weibo_config['API_KEY'],
                              weibo_config['API_SECRET'],
                              weibo_config['REDIRECT_URI'],
                              weibo_config['TOKEN'])
        oauth_client.post('favorites/create',
                          id=mongo_obj_index_list[weibo_index])
Example #13
0
class WeiboApp(object):
    """WeiboApp client."""
    def __init__(self, api_key, api_secret, callback_url, username, password,
                 uid):
        self._c = Client(api_key,
                         api_secret,
                         callback_url,
                         username=username,
                         password=password)
        self._uid = uid

    def get_show(self):
        return self._c.get('users/show', uid=self._uid)

    def post_text(self, text):
        text = text if len(text) <= 140 else text[0:140]
        self._c.post('statuses/update', status=text)

    def post_img(self, text, img_ori):
        text = text if len(text) <= 140 else text[0:140]
        self._c.post('statuses/upload', status=text, pic=img_ori)
Example #14
0
def wbimg(request):
    if 'weibo_username' in os.environ:
        if request.method == 'POST':
            url = 'https://upload.api.weibo.com/2/statuses/upload.json'
            weiboclient = Client(os.environ['weibo_appkey'],
                                 os.environ['weibo_appsecret'],
                                 'hulu.im',
                                 username=os.environ['weibo_username'],
                                 password=os.environ['weibo_password'])
            content = weiboclient.post('statuses/upload',
                                       status='http://hulu.im',
                                       pic=request.FILES['file'])
            return jsonp(request, content)
    else:
        return redirect('/')
Example #15
0
API_KEY = '308244242'
API_SECRET = '4532eb6062e29c14707cc8527bc9ec4f'
REDIRECT_URI = 'http://tools.iadright.com'
OPENWRT_URL = 'http://miwifi.com/cgi-bin/luci/;stok=' + OPENWRT_TOKEN + '/api/misystem/status'

c = Client(API_KEY, API_SECRET, REDIRECT_URI, json.loads(TOKEN)) 

tempfile = open("/sys/class/thermal/thermal_zone0/temp")
thetext = tempfile.read()
tempfile.close()
temperature = float(thetext)
temperature = str(round(temperature / 1000, 1))
temperature_status = 'RPi Temp:' + temperature + "℃"

r = requests.get(OPENWRT_URL)
json = r.json()
downspeed = sizeof_fmt(json['wan']['downspeed'], 'B/s')
upspeed = sizeof_fmt(json['wan']['upspeed'], 'B/s')
download = sizeof_fmt(json['wan']['download'])
upload = sizeof_fmt(json['wan']['upload'])
count_all = str(json['count']['all'])
count_online = str(json['count']['online'])

router_status = 'WAN D:' + downspeed + ' U:' + upspeed + ' Total:' + download + '/' + upload + ' Clients:' + count_online + '/' + count_all

status = temperature_status + ' ' + router_status

print(status)

c.post('statuses/update', status=status)
Example #16
0
def post_new(content, pic, location):
    client = Client(APP_KEY, APP_SECRET, CALLBACK_URL, username=USERID, password=PASSWD)
    print client.post('statuses/upload', status=content, pic=pic, lat=location[0], long=location[1])
Example #17
0
class SinaWeibo:
    def __init__(self,
                 username=wb_cfg.USER,
                 passwd=wb_cfg.PASSWD,
                 logger=None):
        self.APP_KEY = wb_cfg.APP_KEY
        self.APP_SECRET = wb_cfg.APP_SECRET
        self.CALLBACK = wb_cfg.CALLBACK
        self.USER = username
        self.PASSWD = passwd
        self.UID = wb_cfg.UID
        self.log = logger
        try:
            self.client = Client(self.APP_KEY,
                                 self.APP_SECRET,
                                 self.CALLBACK,
                                 token=None,
                                 username=self.USER,
                                 password=self.PASSWD)
            log_info(u"微博模块", u"User %s Login Successful!" % self.USER,
                     self.log)
        except Exception:
            log_warn(u"微博模块", u"User %s Login Failed!" % self.USER, self.log)

    def get_timeline(self):
        if self.client:
            return self.client.get('users/show', uid=self.UID)

    def get_friend_status(self, cnt=20):
        if self.client:
            return self.client.get('statuses/friends_timeline',
                                   uid=self.UID,
                                   count=cnt)

    def post_statuses(self, str_msg):
        if self.client:
            self.client.post('statuses/update', status=str_msg, uid=self.UID)

    def repost_friend(self, iterval):
        # 每相隔多久时间,转发一次朋友圈的微博
        # 朋友圈
        statuses = (self.get_friend_status(cnt=100))["statuses"]
        if statuses:
            for item in statuses:
                #if item['text'].find('nicol:') != -1:
                #    log_info(u"微博模块",u"Alreadyed include me, skip it[%d]!\n" % item['id'], self.log)
                #    continue;
                #if item['id'] == wb_cfg.UID :
                #    log_info(u"微博模块",u"My Own's weibo, skip it[%d]!\n" % item['id'], self.log)
                #    print item['text']
                #    continue;
                if not REPOST_WHITE.has_key(item['user']['id']):
                    log_info(
                        u"微博模块",
                        u"Not in the repost_white list, skip [%d]!\n" %
                        item['id'], self.log)
                    continue
                else:
                    try:
                        ret = self.client.post('statuses/repost',
                                               id=item['id'],
                                               status=u"[Nicol]强势转发微博",
                                               is_comment=1)
                    except Exception, e:
                        log_warn(u"微博模块", u"Runtime Error:%s" % e, self.log)
                        continue

                    log_info(u"微博模块", u"Repost item for [%d]!\n" % item['id'],
                             self.log)

                #等待一会儿
                time.sleep(random.randint(300, 1000))