def authorize_app(self, app_data = APP_DATA): ''' authorize the app return the client for invoding weibo api must be invoked after the login function ''' c = Client(*app_data) self.driver.get(c.authorize_url) try: WebDriverWait(self.driver, 10).until( lambda x: x.find_element_by_css_selector('div.oauth_login_submit') ) # logging.info driver.pagself.e_source submit_button = self.driver.find_element_by_css_selector('p.oauth_formbtn').find_element_by_tag_name('a') submit_button.click() except TimeoutException: # there is no submit button, so the user may have authorized the app logging.info('the user has authorized the app') # parse the code # logging.info driver.current_url query_str = urllib.parse.urlparse(self.driver.current_url).query code = urllib.parse.parse_qs(query_str)['code'] c.set_code(code) logging.info('authorize the app success! code, {}'.format(code)) return c
def authorize_app(self, app_data=APP_DATA): """ authorize the app return the client for invoding weibo api must be invoked after the login function """ c = Client(*app_data) self.driver.get(c.authorize_url) try: WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_css_selector("div.oauth_login_submit")) # logging.info driver.pagself.e_source submit_button = self.driver.find_element_by_css_selector("p.oauth_formbtn").find_element_by_tag_name("a") submit_button.click() except TimeoutException: # there is no submit button, so the user may have authorized the app logging.info("the user has authorized the app") # parse the code # logging.info driver.current_url query_str = urllib.parse.urlparse(self.driver.current_url).query code = urllib.parse.parse_qs(query_str)["code"] c.set_code(code) logging.info("authorize the app success! code, {}".format(code)) return c
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)
def main(uid): client = Client(APP_KEY, APP_SECRET, CALLBACK_URL, username=USERID, password=PASSWD) data = client.get('statuses/friends_timeline') statuses = [status for status in data['statuses'] if status['user']['id'] == uid] statuses.sort(key=lambda x: x['id'], reverse=True) if not statuses: return weibo = get_weibo(uid) newest = get_user(statuses[0]) if weibo.user is None: weibo.user = newest diff = weibo.user.diff(newest) if diff: weibo.user = newest send_noti(u'{} 的微博资料更新'.format(weibo.user.name), u'{} 的微博资料有如下更新:\n{}'.format(weibo.user.name, u'\n'.join(diff))) tweet = get_tweet(statuses[0]) has_new = weibo.last != tweet.id if has_new: weibo.last = tweet.id weibo.tweets.append(tweet) send_noti(u'{} 发新微博啦~'.format(weibo.user.name), u'{} 通过【{}】发送了一条微博,内容是:\n{}'.format( weibo.user.name, BeautifulSoup(tweet.source).getText(), tweet.text )) if has_new or diff: save_weibo(uid, weibo)
def callback(): code = request.args.get('code', '') client = Client(APP_KEY, APP_SECRET, CALLBACK_URL) try: client.set_code(code) r = client.token_info uid = r['uid'] access_token = r['access_token'] expires_in = r['expires_in'] except: flash(u'微博登录没有成功') return redirect(url_for('login')) try: userinfo = client.get('users/show', uid=uid) screen_name = userinfo["screen_name"] profile_image_url = userinfo["profile_image_url"] mongo.db.users.update({"uid": uid}, {"$set": {"uid": uid, "access_token": access_token, "expires_in": expires_in, "screen_name": screen_name, "profile_image_url": profile_image_url}}, upsert=True, safe=True) session['uid'] = uid session['screen_name'] = screen_name session["profile_image_url"] = profile_image_url return redirect(url_for('index')) except Exception: flash(u'获取用户微博信息没有成功') return redirect(url_for('login'))
def init(): """ Instantiates a client. Besides the developer's credentials, weibo always a requires live login before using weibo API to prevent abusing. You can login with any plain weibo account. """ # API_KEY = os.getenv('API_KEY') # API_SECRET = os.getenv('API_SECRET') # REDIRECT_URI = os.getenv('REDIRECT_URI') try: client = Client(API_KEY, API_SECRET, REDIRECT_URI) except: print("Invalid API Credentials...") while True: # check if authorization succeeds, if not, try again try: webbrowser.open_new(client.authorize_url) print("Please authorize... \nIf your browser does not open automatically,"\ "please paste this URL to your browser manually: {}".format(client.authorize_url)) client.set_code(input("Input your code:\n")) break except: try_again = input( "Authorization failed... Input Y to try again...\n") if try_again != 'y' and try_again != 'Y': break return Client(API_KEY, API_SECRET, REDIRECT_URI, client.token)
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))
class WeiboCralwer(object): """docstrinWeiboCralwerg for """ def __init__(self, myid, **client_arg): # client_arg should be {API_KEY, API_SECRET, REDIRECT_URL, AUTH_URL, USER_NAME, USER_PWD} self.myid = myid self.client_arg = client_arg self.remaining_user_access = 0 self.remaining_ip_access = 0 self.wait_time = 0 self.weibo_i = None def loginWeibo(self): try: self.weibo_i = Client(self.client_arg['API_KEY'], self.client_arg['API_SECRET'], \ self.client_arg['REDIRECT_URL'], self.client_arg['token']) except Exception as e: print("Login failed") def checkLimit(self): limit_result = self.weibo_i.get('account/rate_limit_status', uid=self.myid) self.wait_time = limit_result['reset_time_in_seconds'] + 300 # if meet limit, sleep 300+ seconds self.remaining_ip_access = limit_result['remaining_ip_hits'] self.remaining_user_access = limit_result['remaining_user_hits'] return self.remaining_ip_access, self.remaining_user_access def get_friends_ids(self, uid): next_cursor = 0 friends_ids_list = [] while(True): result = self.weibo_i.get('friendships/friends/ids', uid=uid, cursor=next_cursor) ids_list = result['ids'] next_cursor += len(ids_list) # print(ids_list) if len(ids_list) == 0: break friends_ids_list.extend(ids_list) return friends_ids_list def get_friends_info(self, uid): next_cursor = 0 friend_info_list = [] while(True): result = self.weibo_i.get('friendships/friends', uid=uid, cursor=next_cursor) user_list = result['users'] next_cursor += len(user_list) if len(user_list) == 0: break friend_info_list.extend(user_list) return friend_info_list def get_blogs_by_ids(self, uid, group_id): # This access need more advanced permission results = self.weibo_i.get('statuses/show_batch', ids=group_id) for res in results: print("Weibo id: %d --> Text: %s" % (res.get("statuses", {}).get("id"))) def delay(): print("Stopping, begin after %s seconds" % self.wait_time) time.sleep(self.wait_time)
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 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()
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 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])
def get(self): code = self.get_argument('code') key, secret = self.get_app() client = Client(api_key=key, api_secret=secret, redirect_uri=self.callback_url) client.set_code(code) result = client.token self.set_auth(result) return self.redirect('/utility/sina/exec/')
def __init__(self, max_page): self.session = None self.MAX_PAGE = max_page token = { u'access_token': u'2.00pE39sBn1UT7E61e7174d95TdYVED', u'remind_in': u'157679999', u'uid': u'1720813027', u'expires_at': 1575304674 } self.client = Client(app_key, app_secret, redirect_uri, token) self.f = open("data", "w")
def craw_raw_data(api_name): """ 通过微博的API抓取数据 :param api_name: str :return: unicode """ 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']) return oauth_client.get(api_name, uid='1860068802', count=100)
class Wayterm(object): def __init__(self): self.app_key = '1746312660' self.app_secret = 'a113b12f49266b12125f6df1f9808045' self.callback_url = 'http://wayterm.nerocrux.org/done' self.template = Template() self.url = Url() self.reader = Reader() self.token = {} if self._read_access_token(): self.client = Client(self.app_key, self.app_secret, self.callback_url, self.token) else: self.client = Client(self.app_key, self.app_secret, self.callback_url) self.auth_url = self.url.shorten(self.client.authorize_url) print '[1] Open this url in your browser: ' + self.auth_url self.auth_code = raw_input('[2] Enter authorization code: ') self.client.set_code(self.auth_code) token = { 'access_token':self.client.token['access_token'], 'expires_at':self.client.token['expires_at'], 'uid':self.client.token['uid'], } self._write_access_token(token) print 'Authorization done. Enjoy!' def _read_access_token(self): try: self.token = yaml.load(open(os.path.join(os.getenv('HOME'), '.wayterm.yaml')).read()) except: return False return True def _write_access_token(self, token): stream = file(os.path.join(os.getenv('HOME'), '.wayterm.yaml'), 'w') yaml.dump(token, stream) def _init_print(self): self.reader.printfile('logo') def call(self, command): if command[0].lower() == 'exit': exit() if command[0].lower() == 'help': self.reader.printfile('help') return api = Api(self.client) api.call(command)
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])
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 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次/小时
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)
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('/')
def suggest(): user = login_user(session) if user: query = request.args.get('query', '') client = Client(APP_KEY, APP_SECRET, CALLBACK_URL) client.set_token(user["access_token"]) try: results = [] for s in client.get('search/suggestions/at_users', q=query, type=0, count=10): results.append(s['nickname']) return json.dumps({'query': query, 'suggestions': results}) except: raise return json.dumps({'query': query, 'suggestions': []})
def craw_raw_data(api_name): """ 通过微博的API抓取数据 :param api_name: str :return: unicode """ 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']) return oauth_client.get(api_name, uid='1860068802', count=100)
def getClient(app_key, app_secret, call_back, username, password): client = Client(api_key=app_key, api_secret=app_secret, redirect_uri=call_back, username=username, password=password) return client
def get_client(): global client client = Client(APP_KEY, APP_SECRET, redirect_uri=CALLBACK_URL, username=USERNAME, password=PASSWORD)
def create_api_client(self): return Client( self.app_key, self.app_secret, self.redirect_uri, username = self.username, password = self.password )
def setWeiboCredential(self, credentials): self.token[u'access_token'] = credentials[u'access_token'] self.token[u'remind_in'] = credentials[u'remind_in'] self.token[u'uid'] = credentials[u'uid'] self.token[u'expires_at'] = credentials[u'expires_at'] self.client = Client(credentials[u'API_KEY'], credentials[u'API_SECRET'], credentials[u'REDIRECT_URI'], self.token)
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次/小时
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])
def client_init(settings): """ use settings init the clients. """ clients = [] for user in settings.users: if 'api_secret' in dir(user): c = Client(user.app_key, user.api_secret, user.redirect_uri, username=user.username, password=user.password) clients.append(c) else: c = Client(settings.app_key, settings.api_secret, settings.redirect_uri, username=user.username, password=user.password) clients.append(c) return clients
def __init__(self): self.tokens = credential.tokens self.api_keys = credential.api_keys self.api_secrest = credential.api_secrest self.authorazation_url = "http://www.rutgers.edu" self.clients = [] for i in range(len(self.tokens)): logging.debug("Connecting to api number %d"%(i)) c = Client(self.api_keys[i], self.api_secrest[i], self.authorazation_url, self.tokens[i]) self.clients.append(c)
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)
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次/小时
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)
async def _GetBandoriManga(): pagenum = 1 listout = [] max_manga = [0, 0] client = Client(API_KEY, API_SECRET, REDIRECT_URI, token) #client.set_access_token(token['access_token'], token['expires_in']) with open(config.rel("bandori_last_manga.txt")) as f: bandori_last_manga = [int(f.readline()), int(f.readline())] while 1: data = client.get('statuses/friends_timeline', page=pagenum)['statuses'] if data: pagenum += 1 else: break for weibo in data: if weibo['user'][ 'name'] == 'BanGDream每日推' and '#bangdream四格漫画#' in weibo[ 'text']: if_2 = 1 if "2nd" in weibo['text'] else 0 match = re.search('第(.*?)(话|話)', weibo['text']) if not match: continue if bandori_last_manga[if_2] < int(match.group(1)): listout.append( config.cq.text(("2nd season" if if_2 == 1 else "") + match.group(0) + u':\n')) listout.append(config.cq.img(weibo['original_pic'])) if max_manga[if_2] < int(match.group(1)): max_manga[if_2] = int(match.group(1)) for i in (0, 1): if bandori_last_manga[i] < max_manga[i] and max_manga[i] != 999: bandori_last_manga[i] = max_manga[i] with open(config.rel("bandori_last_manga.txt"), 'w') as f: f.write(str(bandori_last_manga[0]) + '\n') f.write(str(bandori_last_manga[1])) return listout
def __init__(self): self.app_key = '1746312660' self.app_secret = 'a113b12f49266b12125f6df1f9808045' self.callback_url = 'http://wayterm.nerocrux.org/done' self.template = Template() self.url = Url() self.reader = Reader() self.token = {} if self._read_access_token(): self.client = Client(self.app_key, self.app_secret, self.callback_url, self.token) else: self.client = Client(self.app_key, self.app_secret, self.callback_url) self.auth_url = self.url.shorten(self.client.authorize_url) print '[1] Open this url in your browser: ' + self.auth_url self.auth_code = raw_input('[2] Enter authorization code: ') self.client.set_code(self.auth_code) token = { 'access_token':self.client.token['access_token'], 'expires_at':self.client.token['expires_at'], 'uid':self.client.token['uid'], } self._write_access_token(token) print 'Authorization done. Enjoy!'
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 status(): user = login_user(session) if user is None: return "" client = Client(APP_KEY, APP_SECRET, CALLBACK_URL) client.set_token(user["access_token"]) id = request.args.get('id', '') since_id = request.args.get('since_id', 0) reposts, source_weibo, since_id = load_reposts(app, weibo2db, r, client, id, since_id) if len(reposts) == 0: return "" #root tree_nodes = [] node = source_weibo["user"]["name"] location = source_weibo["user"]["location"] datetime = source_weibo["created_at"] img_url = source_weibo["user"]["profile_image_url"] weibo_url = "http://weibo.com/" + \ str(source_weibo["user"]["id"]) + \ "/" + base62.mid_to_str(source_weibo["mid"]) tree_nodes.append(Tree(node, location, datetime, int(id), img_url, weibo_url)) for repost in reposts: try: node = repost["user"]["name"] wid = repost["id"] img_url = repost["user"]["profile_image_url"] location = repost["user"]["location"] datetime = repost['created_at'] weibo_url = "http://weibo.com/" + \ str(repost["user"]["id"]) + \ "/" + base62.mid_to_str(repost["mid"]) tree_nodes.append(Tree(node, location, datetime, wid, img_url, weibo_url)) except: app.logger.error(repost) continue repost_users = re.findall(r'//@(\S+?):', repost["text"]) if len(repost_users): flag = True for node in tree_nodes[::-1]: if node.node == repost_users[0]: node.append_child(tree_nodes[-1]) flag = False break if flag: tree_nodes[0].append_child(tree_nodes[-1]) else: tree_nodes[0].append_child(tree_nodes[-1]) dt, max_width = buchheim.buchheim(tree_nodes[0]) gexf = Gexf("MOON_CLJ", "haha") graph = gexf.addGraph("directed", "static", "weibo graph") graph.addNodeAttribute("img_url", type="URI", force_id="img_url") graph.addNodeAttribute("name", type="string", force_id="name") graph.addNodeAttribute("location", type="string", force_id="location") graph.addNodeAttribute("datetime", type="string", force_id="datetime") graph.addNodeAttribute("repost_num", type="integer", force_id="repost_num") graph.addNodeAttribute("weibo_url", type="URI", force_id="weibo_url") rank = node_rank(tree_nodes[0]) add_node_edge(dt, graph, rank, Count(), max_width=max_width) return etree.tostring(gexf.getXML(), pretty_print=True, encoding='utf-8', xml_declaration=True)
from weibo import Client from datetime import datetime API_KEY = '308244242' API_SECRET = '4532eb6062e29c14707cc8527bc9ec4f' REDIRECT_URI = 'http://tools.iadright.com' c = Client(API_KEY, API_SECRET, REDIRECT_URI) print(c.authorize_url) c.set_code(input('Please input token:')) print(c.token)
def get_client(): return Client(app.config['WEIBO_CONSUMER_KEY'], app.config['WEIBO_CONSUMER_SECRET'], app.config['REDIRECT_URI'])
from weibo import Client from pprint import pprint from time import clock import json APP_KEY = "3722673574" APP_SECTER = "3686fea0a65da883b6c2a7586f350425" CALLBACK_URL = 'http://siliang.org' code = "004ba6f4d40736d7aff25f4203d46f73" c = Client(APP_KEY, APP_SECTER , CALLBACK_URL) # url = c.authorize_url # webbrowser.open_new(url) c.set_code('code') token = c.token c = Client(APP_KEY, APP_SECTER , CALLBACK_URL,token) #pprint(raw_data['statuses'][1]['text']) a = [] raw_data = c.get('statuses/public_timeline', count=200) for x in range(200): a = a.append(str(raw_data['statuses'][x]['text'])) print (a)
# author: [email protected] # reference: [doc of third-party weibo sdk](http://weibo.lxyu.net/) from weibo import Client from config.Weibo_API_Config import * from idTranslation import mid2id, id2mid client = Client(API_KEY, API_SECRET, REDIRECT_URI) print("please paste the URL to your browser"+client.authorize_url+", and authorize") client.set_code(input("input your code:")) userMsg = client.get('users/show', uid=2146965345) print(type(userMsg), userMsg) print("************************************************") comments = client.get('comments/show', id=4545555559354630, count=5)["comments"] # print(type(comments), comments) for comment in comments: print(comment["text"])
class SinaCrawler: def __init__(self, max_page): self.session = None self.MAX_PAGE = max_page token = { u'access_token': u'2.00pE39sBn1UT7E61e7174d95TdYVED', u'remind_in': u'157679999', u'uid': u'1720813027', u'expires_at': 1575304674 } self.client = Client(app_key, app_secret, redirect_uri, token) self.f = open("data", "w") def __del__(self): self.f.close() def userlogin(self, username, password): session = requests.Session() url_prelogin = '******' url_login = '******' #get servertime,nonce, pubkey,rsakv resp = session.get(url_prelogin) json_data = re.search('\((.*)\)', resp.content).group(1) data = json.loads(json_data) servertime = data['servertime'] nonce = data['nonce'] pubkey = data['pubkey'] rsakv = data['rsakv'] # calculate su su = base64.b64encode(urllib.quote(username)) #calculate sp rsaPublickey = int(pubkey, 16) key = rsa.PublicKey(rsaPublickey, 65537) message = str(servertime) + '\t' + str(nonce) + '\n' + str(password) sp = binascii.b2a_hex(rsa.encrypt(message, key)) postdata = { 'entry': 'weibo', 'gateway': '1', 'from': '', 'savestate': '7', 'userticket': '1', 'ssosimplelogin': '******', 'vsnf': '1', 'vsnval': '', 'su': su, 'service': 'miniblog', 'servertime': servertime, 'nonce': nonce, 'pwencode': 'rsa2', 'sp': sp, 'encoding': 'UTF-8', 'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack', 'returntype': 'META', 'rsakv': rsakv, } resp = session.post(url_login, data=postdata) # print resp.headers login_url = re.findall('replace\(\'(.*)\'\)', resp.content) # respo = session.get(login_url[0]) self.session = session def get_search_result(self, html_content): """ get search result from the html content Args: html_content: str for storing html content of the search page Return: None """ #content = re.findall(r"\"pid\":\"pl_user_feedList\"(?P<tips>[\w\W]*?)", html_content) html_content = html_content.strip() content = re.findall( r"\"pid\":\"pl_wb_feedlist\"(?P<tips>[\w\W]*?)</script>", html_content)[0] clean_content = string.replace(content, "\\\\", "\\") search_result = re.findall( r"<div class=\\\"WB_cardwrap S_bg2 clearfix\\\" >(?P<tips>[\w\W]*?)<\\/div>\\n<\\/div>", clean_content) return search_result def get_person_info(self, person_info_html): """ get person information from the html content Args: person_info_html : str indicating the personanl information Return: None """ txt = re.findall( r"=\\\"feed_list_content\\\">(?P<tips>[\w\W]*?)<\\/p>", person_info_html) content = string.replace(txt[0], '\n', '') content = string.replace(content, '\t', '') tag = True strs = "" for w in content: if w == '<': tag = False continue if w == '>': tag = True continue if tag: strs += w msg = strs.decode('unicode_escape').encode('utf8') msg = string.replace(msg, '\n', '') msg = string.replace(msg, '\t', '') uid = re.findall("uid=(?P<tips>\d+?)&", person_info_html)[0] time.sleep(random.random()) while True: try: info_dict = self.client.get('users/show', uid=uid) break except: time.sleep(random.randint(1, 40)) self.f.write('%s\t%s\t%s\t%s\t%s\t' % info_dict['screen_name'].encode('utf-8'), '\t', \ info_dict['gender'].encode('utf-8'), '\t', \ msg, '\t',\ info_dict['created_at'].encode('utf-8'), '\t', \ info_dict['location'].encode('utf-8')) def do_search_page(self, page, query): """ get search result of the page in the search html page Args: page : int indicating the number of the page Return: None """ search_url = "http://s.weibo.com/wb/%s&page=%d" % (query, page) html_page = self.session.get(search_url) all_results = self.get_search_result(html_page.content) res_cnt = 1 for res in all_results: print 'page %d result %d done' % (page, res_cnt) res_cnt += 1 information = self.get_person_info(res) def do_search(self, query): """ do search Args: query : str indicating the query Return: None """ self.f.write( 'screen_name\tgender\trelated_msg\tregister_time\tlocation\n') for page in range(1, self.MAX_PAGE + 1): time.sleep(random.random()) self.do_search_page(page, query)
class WeiboCralwer(object): """docstrinWeiboCralwerg for """ def __init__(self, myid, **client_arg): # client_arg should be {API_KEY, API_SECRET, REDIRECT_URL, AUTH_URL, USER_NAME, USER_PWD} self.myid = myid self.client_arg = client_arg self.remaining_user_access = 0 self.remaining_ip_access = 0 self.wait_time = 0 self.weibo_i = None def loginWeibo(self): try: self.weibo_i = Client(self.client_arg['API_KEY'], self.client_arg['API_SECRET'], \ self.client_arg['REDIRECT_URL'], self.client_arg['token']) except Exception as e: print("Login failed") def checkLimit(self): limit_result = self.weibo_i.get('account/rate_limit_status', uid=self.myid) self.wait_time = limit_result[ 'reset_time_in_seconds'] + 300 # if meet limit, sleep 300+ seconds self.remaining_ip_access = limit_result['remaining_ip_hits'] self.remaining_user_access = limit_result['remaining_user_hits'] return self.remaining_ip_access, self.remaining_user_access def get_friends_ids(self, uid): next_cursor = 0 friends_ids_list = [] while (True): result = self.weibo_i.get('friendships/friends/ids', uid=uid, cursor=next_cursor) ids_list = result['ids'] next_cursor += len(ids_list) # print(ids_list) if len(ids_list) == 0: break friends_ids_list.extend(ids_list) return friends_ids_list def get_friends_info(self, uid): next_cursor = 0 friend_info_list = [] while (True): result = self.weibo_i.get('friendships/friends', uid=uid, cursor=next_cursor) user_list = result['users'] next_cursor += len(user_list) if len(user_list) == 0: break friend_info_list.extend(user_list) return friend_info_list def get_blogs_by_ids(self, uid, group_id): # This access need more advanced permission results = self.weibo_i.get('statuses/show_batch', ids=group_id) for res in results: print("Weibo id: %d --> Text: %s" % (res.get("statuses", {}).get("id"))) def delay(): print("Stopping, begin after %s seconds" % self.wait_time) time.sleep(self.wait_time)
if abs(num) < 1024.0: return "%3.1f%s%s" % (num, unit, suffix) num /= 1024.0 return "%.1f%s%s" % (num, 'Yi', suffix) TOKEN = '{"expires_at": 1432321199, "access_token": "2.00FQbohB0163r16466e4dde5sLRuVB", "remind_in": "646258", "uid": "1563574905"}' OPENWRT_TOKEN = 'edd6c6c96d057ff458770ae907d227e9' 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'])
except: print "Shenyang has problem" print "finish running" return None a6 = 547191276 b6 = 'd053826b2de8f4f36c687655570e0909' c6 = 'https://github.com/ssong0429' token6 = { u'access_token': u'2.00OCw7ID0k8xBb940305c3130oJ9rV', u'remind_in': u'157679999', u'uid': u'2873028802', u'expires_at': 1618817558 } d6 = Client(a6, b6, c6, token6) a7 = 566044226 b7 = 'd8708645e9f4e4d793c175003a98e38c' c7 = 'https://github.com/ssong0429' token7 = { u'access_token': u'2.00OCw7ID0axD_c23a29106baEn4s6C', u'remind_in': u'157679999', u'uid': u'2873028802', u'expires_at': 1618817713 } d7 = Client(a7, b7, c7, token7) a8 = 3230547592 b8 = '5400732031651646bfe382fd31152176' c8 = 'https://github.com/ssong0429'
def loginWeibo(self): try: self.weibo_i = Client(self.client_arg['API_KEY'], self.client_arg['API_SECRET'], \ self.client_arg['REDIRECT_URL'], self.client_arg['token']) except Exception as e: print("Login failed")
def search(): user = login_user(session) if user: client = Client(APP_KEY, APP_SECRET, CALLBACK_URL) client.set_token(user["access_token"]) t = request.args.get('t', '') q = request.args.get('q', '') p = request.args.get('p', 1) u = request.args.get('u', 0) q = q.strip("@ \r\n\t") t = t.strip("@ \r\n\t") p = int(p) if t != '': retry = 0 page = p n_page = p + 3 t_statuses = [] tar_screen_name = None tar_profile_image_url = None tar_location = None while 1: try: if retry > 5: break statuses = client.get('statuses/user_timeline', uid=u, count=100, page=page)["statuses"] weibo2db.statuses(statuses) if tar_screen_name is None and len(statuses) > 0: tar_profile_image_url = statuses[0]["user"]["profile_image_url"] tar_screen_name = statuses[0]["user"]["name"] tar_location = statuses[0]["user"]["location"] for status in statuses: if t in status["text"] or [t in status["retweeted_status"]["text"] if "retweeted_status" in status else False][0]: t_statuses.append(status) if page == n_page: break else: page += 1 except Exception, e: app.logger.error(e) retry += 1 if len(t_statuses) == 0: flash(u"没有搜索到相关微博,请尝试下一页或者采用其他关键词") statuses = t_statuses p = page else: try: target_user = client.get('users/show', screen_name=q) except: flash(u"您输入的昵称不存在,请重新输入") return redirect(url_for('index')) u = target_user["id"] page = p tar_screen_name = target_user["screen_name"] tar_profile_image_url = target_user["profile_image_url"] tar_location = target_user["location"] try: statuses = client.get('statuses/user_timeline', uid=u, count=50, page=page)["statuses"] weibo2db.statuses(statuses) except: flash(u"获取微博信息失败,请刷新") statuses = [] for i in xrange(len(statuses)): weibo_url = "http://weibo.com/" \ + str(statuses[i]["user"]["id"]) \ + "/" + base62.mid_to_str(statuses[i]["mid"]) statuses[i]["weibo_url"] = weibo_url screen_name = session["screen_name"] profile_image_url = session["profile_image_url"] return render_template('weibolist.html', btnuserpicvisible='inline', btnloginvisible='none', t=t, q=q, p=int(p), u=u, screen_name=screen_name, profile_image_url=profile_image_url, tar_screen_name=tar_screen_name, tar_profile_image_url=tar_profile_image_url, tar_location=tar_location, statuses=statuses)
class SinaCrawler: def __init__(self, max_page): self.session = None self.MAX_PAGE = max_page token = {u'access_token': u'2.00pE39sBn1UT7E61e7174d95TdYVED', u'remind_in': u'157679999', u'uid': u'1720813027', u'expires_at': 1575304674} self.client = Client(app_key, app_secret, redirect_uri, token) self.f = open("data", "w") def __del__(self): self.f.close() def userlogin(self,username,password): session = requests.Session() url_prelogin = '******' url_login = '******' #get servertime,nonce, pubkey,rsakv resp = session.get(url_prelogin) json_data = re.search('\((.*)\)', resp.content).group(1) data = json.loads(json_data) servertime = data['servertime'] nonce = data['nonce'] pubkey = data['pubkey'] rsakv = data['rsakv'] # calculate su su = base64.b64encode(urllib.quote(username)) #calculate sp rsaPublickey= int(pubkey,16) key = rsa.PublicKey(rsaPublickey,65537) message = str(servertime) +'\t' + str(nonce) + '\n' + str(password) sp = binascii.b2a_hex(rsa.encrypt(message,key)) postdata = { 'entry': 'weibo', 'gateway': '1', 'from': '', 'savestate': '7', 'userticket': '1', 'ssosimplelogin': '******', 'vsnf': '1', 'vsnval': '', 'su': su, 'service': 'miniblog', 'servertime': servertime, 'nonce': nonce, 'pwencode': 'rsa2', 'sp': sp, 'encoding': 'UTF-8', 'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack', 'returntype': 'META', 'rsakv' : rsakv, } resp = session.post(url_login,data = postdata) # print resp.headers login_url = re.findall('replace\(\'(.*)\'\)',resp.content) # respo = session.get(login_url[0]) self.session = session def get_search_result(self, html_content): """ get search result from the html content Args: html_content: str for storing html content of the search page Return: None """ #content = re.findall(r"\"pid\":\"pl_user_feedList\"(?P<tips>[\w\W]*?)", html_content) html_content = html_content.strip() content = re.findall(r"\"pid\":\"pl_wb_feedlist\"(?P<tips>[\w\W]*?)</script>", html_content)[0] clean_content = string.replace(content, "\\\\", "\\") search_result = re.findall(r"<div class=\\\"WB_cardwrap S_bg2 clearfix\\\" >(?P<tips>[\w\W]*?)<\\/div>\\n<\\/div>", clean_content) return search_result def get_person_info(self, person_info_html): """ get person information from the html content Args: person_info_html : str indicating the personanl information Return: None """ txt = re.findall(r"=\\\"feed_list_content\\\">(?P<tips>[\w\W]*?)<\\/p>", person_info_html) content = string.replace(txt[0], '\n', '') content = string.replace(content, '\t', '') tag = True strs = "" for w in content: if w == '<': tag = False continue if w == '>': tag = True continue if tag: strs += w msg = strs.decode('unicode_escape').encode('utf8') msg = string.replace(msg, '\n', '') msg = string.replace(msg, '\t', '') uid = re.findall("uid=(?P<tips>\d+?)&", person_info_html)[0] time.sleep(random.random()) while True: try: info_dict = self.client.get('users/show', uid = uid) break except: time.sleep(random.randint(1,40)) self.f.write('%s\t%s\t%s\t%s\t%s\t' % info_dict['screen_name'].encode('utf-8'), '\t', \ info_dict['gender'].encode('utf-8'), '\t', \ msg, '\t',\ info_dict['created_at'].encode('utf-8'), '\t', \ info_dict['location'].encode('utf-8')) def do_search_page(self, page, query): """ get search result of the page in the search html page Args: page : int indicating the number of the page Return: None """ search_url = "http://s.weibo.com/wb/%s&page=%d" % (query, page) html_page = self.session.get(search_url) all_results = self.get_search_result(html_page.content) res_cnt = 1 for res in all_results: print 'page %d result %d done' % (page, res_cnt) res_cnt += 1 information = self.get_person_info(res) def do_search(self, query): """ do search Args: query : str indicating the query Return: None """ self.f.write('screen_name\tgender\trelated_msg\tregister_time\tlocation\n') for page in range(1, self.MAX_PAGE + 1): time.sleep(random.random()) self.do_search_page(page, query)
# coding=utf-8 __author__ = 'zhaoliang' __email__ = '*****@*****.**' __create__ = '2014/11/29' from weibo import Client import codecs # 前往新浪微博开放平台申请应用,补充上下面的参数 API_KEY = 'xxxx' API_SECRET = 'xxxx' REDIRECT_URI = 'xxxx' USER_NAME = 'xxxx' USER_PASSWORD = '******' c = Client(API_KEY, API_SECRET, REDIRECT_URI, username=USER_NAME, password=USER_PASSWORD) def md_escape(md): escape_char = ['\\', '`', '*', '_', '{', '}', '(', ')', '[', ']', '#', '+', '-', '!', '~'] for char in escape_char: md = md.replace(char, '\\'+char) return md def status2md(weibo_status): if weibo_status.get('user', None): user_name = weibo_status['user']['name'] user_profile_url = 'http://weibo.com/' + weibo_status['user']['profile_url'] weibo_content = md_escape(weibo_status['text']) weibo_pic = weibo_status.get('original_pic', None)
#!/usr/bin/env python # -*- coding: utf-8 -*- from weibo import Client import webbrowser, json import demjson APP_KEY = '415390189' APP_SECRET = '958ea2c93dcad4ab45a99098b44b016a' REDIRECT_URI = 'https://api.weibo.com/oauth2/authorize' client = Client(APP_KEY, APP_SECRET, REDIRECT_URI) url = client.authorize_url 'https://api.weibo.com/oauth2/authorize?client_id=415390189&response_type=code&redirect_uri=958ea2c93dcad4ab45a99098b44b016a' print (url) webbrowser.open_new(url) print ('输入url中code后面的内容后按回车键:') code = raw_input() client.set_code(code) token = client.token uuid = client.uid d = json.dumps(client.get('statuses/user_timeline', uid=uuid, separators=(',', ':'))) print (d) s = json.loads(d) length = len(s['statuses']) print (length) for i in range(0,length): print (s['statuses'][i]['text'])
def __init__(self, max_page): self.session = None self.MAX_PAGE = max_page token = {u'access_token': u'2.00pE39sBn1UT7E61e7174d95TdYVED', u'remind_in': u'157679999', u'uid': u'1720813027', u'expires_at': 1575304674} self.client = Client(app_key, app_secret, redirect_uri, token) self.f = open("data", "w")
import rsa import pymysql import urllib.parse from weibo import Client connection = pymysql.connect(host="119.23.46.71", user="******", passwd="helloroot", db="myblog", port=3306, charset="utf8") cur = connection.cursor() APP_KEY = '415390189' APP_SECRET = '958ea2c93dcad4ab45a99098b44b016a' REDIRECT_URI = 'https://api.weibo.com/oauth2/authorize' client = Client(APP_KEY, APP_SECRET, REDIRECT_URI) url = client.authorize_url 'https://api.weibo.com/oauth2/authorize?client_id=415390189&response_type=code&redirect_uri=958ea2c93dcad4ab45a99098b44b016a' class AppClient: def __init__(self): self._appKey = APP_KEY # your app key self._appSecret = APP_SECRET # your app secret self._callbackUrl = REDIRECT_URI # your callback url self._account = '*****@*****.**' # your weibo user name (eg.email) self._password = '******' # your weibo pwd self.AppCli = client self._author_url = self.AppCli.authorize_url def insert(self, user_id, name, location, url, text, created_at, type):