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 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 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次/小时
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 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 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 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 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)
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)
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 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) weibo_pic_mid_thumb = weibo_status.get('bmiddle_pic', None) weibo_pic_addition = '' if weibo_pic: weibo_pic_addition = u'([点击查看原始配图](%s))\n<img src="%s" style="max-height:300px;"/>' % (weibo_pic, weibo_pic_mid_thumb) return '[**%s**](%s)\n%s\n%s' % ( user_name, user_profile_url, weibo_content, weibo_pic_addition ) return '' md_file = codecs.open('1.md', 'w+', 'utf-8') for i in range(1, 100): faved_weibos = c.get('favorites', count=20, page=i) if not faved_weibos['favorites']: break for weibo in faved_weibos['favorites']: md_string = '' md_string += '- ' + status2md(weibo['status']) if weibo['status'].get('retweeted_status', None): md_string += '\n> ' + status2md(weibo['status']['retweeted_status']) + '\n' md_string += '\n---\n' md_file.write(md_string) if md_file: md_file.close()
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"])
#!/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'])
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)
import sys from weibo import Client ## 微连接 移动应用 申请 API_KEY = "xxxxx" API_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxxx" REDIRECT_URI = "https://api.weibo.com/oauth2/default.html" c = Client(API_KEY, API_SECRET, REDIRECT_URI) # 通过手动操作,访问 authorize_url 得到 access或者authorize code url = c.authorize_url print(url) code = input() c.set_code(code) out = open("res.txt", 'w', encoding="utf8") ## 获取公共微博 d = c.get('statuses/public_timeline', count=200) for i in range(len(d['statuses'])): cc = d['statuses'][i]['text'].strip() cc = cc.replace('\u200b', '').replace('\n', ' ').strip() out.write(cc + '\n')
CALLBACK_URL = 'http://heisaman.xyz/callback' # callback url n = 0 try: token = { u'access_token': u'2.001psIGCSorweBd556e626159iQzBB', u'remind_in': u'157679999', u'uid': u'1923041062', u'expires_at': 1629809571 } c = Client(APP_KEY, APP_SECRET, CALLBACK_URL, token=token) # print token # {u'access_token': u'2.001psIGCSorweBd556e626159iQzBB', u'remind_in': u'157679999', u'uid': u'1923041062', u'expires_at': 1629809571} print(c.alive) favorites_total_number = c.get('favorites/ids', count=1)['total_number'] print("共收藏了微博{0}条".format(favorites_total_number)) for i in range(favorites_total_number // 50 + 1): print("第{0}页".format(i + 51)) res = c.get('favorites', count=50, page=i + 51) print("{0}条收藏".format(len(res['favorites']))) for favorite in res['favorites']: n = n + 1 if collection.find({"_id": favorite['status']['id']}).count() > 0: pass else: favorite['_id'] = favorite['status']['id'] if favorite['status']['created_at'] == '': created_at = datetime.strptime( 'Wed Nov 28 15:00:00 +0800 2012',
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))
from weibo import Client APP_KEY = '1521216146' # app key APP_SECRET = '212c28b62505183820922d0f4f45e327' # app secret CALLBACK_URL = 'http://heisaman.xyz/callback' # callback url token = { u'access_token': u'2.001psIGCSorweBd556e626159iQzBB', u'remind_in': u'157679999', u'uid': u'1923041062', u'expires_at': 1629809571 } c = Client(APP_KEY, APP_SECRET, CALLBACK_URL, token=token) res = c.get('statuses/bilateral_timeline') print("{0}".format(res))
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)
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)
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) weibo_pic_mid_thumb = weibo_status.get('bmiddle_pic', None) weibo_pic_addition = '' if weibo_pic: weibo_pic_addition = u'([点击查看原始配图](%s))\n<img src="%s" style="max-height:300px;"/>' % ( weibo_pic, weibo_pic_mid_thumb) return '[**%s**](%s)\n%s\n%s' % (user_name, user_profile_url, weibo_content, weibo_pic_addition) return '' md_file = codecs.open('1.md', 'w+', 'utf-8') for i in range(1, 100): faved_weibos = c.get('favorites', count=20, page=i) if not faved_weibos['favorites']: break for weibo in faved_weibos['favorites']: md_string = '' md_string += '- ' + status2md(weibo['status']) if weibo['status'].get('retweeted_status', None): md_string += '\n> ' + status2md( weibo['status']['retweeted_status']) + '\n' md_string += '\n---\n' md_file.write(md_string) if md_file: md_file.close()