def get_user_info_by_uid(username, password, uid): launcher = SinaLauncher(username, password) launcher.login() session = launcher.session url = 'https://m.weibo.cn/api/container/getIndex?uid={uid}&luicode=10000011&lfid=231018{uid}_-_longbloglist_original&type=uid&value={uid}&containerid=100505{uid}'.format( uid=str(uid)) userInfo = json.loads(session.get(url=url).text)['data']['userInfo'] uid = userInfo['id'] screen_name = userInfo.get('screen_name', '') follow_count = userInfo.get('follow_count', 0) followers_count = userInfo.get('followers_count', 0) statuses_count = userInfo.get('statuses_count', 0) description = userInfo.get('description', '') profile_image_url = userInfo.get('profile_image_url', '') gender = userInfo.get('gender', '') profile_url = userInfo.get('profile_url', '') verified_reason = userInfo.get('verified_reason', '') item = { 'uid': uid, 'screen_name': screen_name, 'follow_count': follow_count, 'followers_count': followers_count, 'statuses_count': statuses_count, 'description': description, 'profile_image_url': profile_image_url, 'gender': gender, 'profile_url': profile_url, 'verified_reason': verified_reason } return item
def execute(uname, upasswd): xnr = SinaLauncher(uname, upasswd) print xnr.login() print 'uname::',uname uid = xnr.uid current_ts = int(time.time()) timestamp_retweet, timestamp_like, timestamp_at, timestamp_private, \ timestamp_comment_receive, timestamp_comment_make = newest_time_func(xnr.uid) print timestamp_retweet, timestamp_like, timestamp_at, \ timestamp_private, timestamp_comment_receive, timestamp_comment_make #try: print 'start run weibo_feedback_follow.py ...' fans, follow, groups = FeedbackFollow(xnr.uid, current_ts).execute() print 'run weibo_feedback_follow.py done!' # except: # print 'Except Abort' #try: print 'start run weibo_feedback_at.py ...' FeedbackAt(xnr.uid, current_ts, fans, follow, groups, timestamp_at).execute() print 'run weibo_feedback_at.py done!' # except: # print 'Except Abort' #try: print 'start run weibo_feedback_comment.py ...' FeedbackComment(xnr.uid, current_ts, fans, follow, groups, timestamp_comment_make, timestamp_comment_receive).execute() print 'run weibo_feedback_comment.py done!' # except: # print 'Except Abort' # try: print 'start run weibo_feedback_like.py ...' FeedbackLike(xnr.uid, current_ts, fans, follow, groups, timestamp_like).execute() print 'run weibo_feedback_like.py done!' # except: # print 'Except Abort' # try: print 'start run weibo_feedback_private.py ...' # print 'timestamp_private:::',timestamp_private # print 'current_ts::::::',current_ts FeedbackPrivate(xnr.uid, current_ts, fans, follow, groups, timestamp_private).execute() print 'run weibo_feedback_private.py done!' # except: # print 'Except Abort' #try: print 'start run weibo_feedback_retweet.py ...' FeedbackRetweet(xnr.uid, current_ts, fans, follow, groups, timestamp_retweet).execute() print 'run weibo_feedback_retweet.py done!'
def execute(): """ 输入用户名、密码登录 输入发布内容(和rank) :return: """ xnr = SinaLauncher('*****@*****.**', 'Bh123456') xnr.login() #user = SinaOperateAPI() #user = SinaOperateAPI().getUserShow(screen_name='巨星大大') print xnr.uid
def execute(uname, upasswd, account_type): xnr = SinaLauncher(uname, upasswd, account_type) print xnr.login() print 'uname::', uname uid = xnr.uid current_ts = int(time.time()) timestamp_retweet, timestamp_like, timestamp_at, timestamp_private, \ timestamp_comment_receive, timestamp_comment_make = newest_time_func(xnr.uid) print '-----------------------------------------------------------------------' print timestamp_retweet, timestamp_like, timestamp_at, \ timestamp_private, timestamp_comment_receive, timestamp_comment_make print '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++' try: print 'start run weibo_feedback_follow.py ...' FeedbackFollow(uname, upasswd).execute() print 'run weibo_feedback_follow.py done!' except: print uname, upasswd print 'Except Abort'
def execute(uname, upasswd): # next weibo user_info start receiving xnr = SinaLauncher(uname, upasswd) login_info = xnr.login() if login_info: print 'login successful------------================-------------------===================' uid = xnr.uid print 'uname::next weibo user_info start receiving', uname, xnr.uid current_ts = int(time.time()) timestamp_retweet, timestamp_like, timestamp_at, timestamp_private, \ timestamp_comment_receive, timestamp_comment_make = newest_time_func(xnr.uid) print timestamp_retweet, timestamp_like, timestamp_at, \ timestamp_private, timestamp_comment_receive, timestamp_comment_make # start get follows and fans print 'start get follows and fans function weibo_feedback_follow.py...' FeedbackFollow(uname, upasswd).execute() else: print 'login error haaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' print 'user_name or user_password error about user is ', uname, upasswd
def execute(uname, upasswd): # next weibo user_info start receiving xnr = SinaLauncher(uname, upasswd) login_info = xnr.login() if login_info: print 'login successful------------================-------------------===================' uid = xnr.uid print 'uname::next weibo user_info start receiving',uname,xnr.uid current_ts = int(time.time()) timestamp_retweet, timestamp_like, timestamp_at, timestamp_private, \ timestamp_comment_receive, timestamp_comment_make = newest_time_func(xnr.uid) print timestamp_retweet, timestamp_like, timestamp_at, \ timestamp_private, timestamp_comment_receive, timestamp_comment_make try: print 'start run weibo_feedback_comment.py ...' FeedbackComment(uname, upasswd).execute() print 'run weibo_feedback_comment.py done!' except Exception as e: print e else: print 'login error haaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' print 'user_name or user_password error about user is ', uname,upasswd
class SinaOperateAPI: def __init__(self, username, password): self.launcher = SinaLauncher(username, password, '') self.launcher.login() self.uid = self.launcher.uid # 当前用户id self.session = self.launcher.session self._headers = { "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1]", "Referer": "https://m.weibo.cn", 'Host': 'm.weibo.cn' } def publish(self, content): if not content: return False st = re.search(r"st: '(.*?)',", self.session.get('https://m.weibo.cn').text).group(1) url = 'https://m.weibo.cn/api/statuses/update' post_data = {'content': content.decode('utf-8'), 'st': st} resp = self.session.post(url, data=post_data, headers=self._headers) # 输出了状态 print resp.status_code if str(resp.status_code) == '200': return True, '成功' else: return False, '失败', resp.status_code def retweet(self, mid, content=''): st = re.search(r"st: '(.*?)',", self.session.get('https://m.weibo.cn').text).group(1) url = 'https://m.weibo.cn/api/statuses/repost' post_data = { 'id': mid, 'content': content.decode('utf-8'), 'mid': mid, 'st': st } resp = self.session.post(url, data=post_data, headers=self._headers) if str(resp.status_code) == '200': print "retweet----------------------------" return True, '成功' else: return False, '失败', resp.status_code def comment(self, mid, content=''): st = re.search(r"st: '(.*?)',", self.session.get('https://m.weibo.cn').text).group(1) url = 'https://m.weibo.cn/api/comments/create' post_data = { 'id': mid, 'content': content.decode('utf-8'), 'mid': mid, 'st': st } resp = self.session.post(url, data=post_data, headers=self._headers) if str(resp.status_code) == '200': return True, '成功' else: return False, '失败', resp.status_code #def receive(self, uid, content=''): # st = re.search(r"st: '(.*?)',", self.session.get('https://m.weibo.cn').text).group(1) # url = 'https://m.weibo.cn/api/chat/send' # post_data = {'uid': uid, 'content': content, 'st': st} # resp = self.session.post(url, data=post_data, headers=self._headers) # if str(resp.status_code) == '200': # return True, '成功' # else: # return False, '失败', resp.status_code def receive(self, r_mid, mid, content=''): st = re.search(r"st: '(.*?)',", self.session.get('https://m.weibo.cn').text).group(1) url = 'https://m.weibo.cn/api/comments/reply' post_data = { 'id': str(r_mid), 'reply': str(mid), 'content': content, 'withReply': '1', 'mid': str(r_mid), 'cid': str(mid), 'st': st } resp = self.session.post(url, data=post_data, headers=self._headers) if str(resp.status_code) == '200': return True, '成功' else: return False, '失败', resp.status_code def privmessage(self, uid, content=''): st = re.search(r"st: '(.*?)',", self.session.get('https://m.weibo.cn').text).group(1) url = 'https://m.weibo.cn/api/chat/send' post_data = {'uid': uid, 'content': content, 'st': st} resp = self.session.post(url, data=post_data, headers=self._headers) if str(resp.status_code) == '200': return True, '成功' else: return False, '失败', resp.status_code def like(self, mid): st = re.search(r"st: '(.*?)',", self.session.get('https://m.weibo.cn').text).group(1) url = 'https://m.weibo.cn/api/attitudes/create' post_data = {'id': mid, 'attitude': 'heart', 'st': st} resp = self.session.post(url, data=post_data, headers=self._headers) if str(resp.status_code) == '200': return True, '成功' else: return False, '失败', resp.status_code def like_to_comment(self, mid): st = re.search(r"st: '(.*?)',", self.session.get('https://m.weibo.cn').text).group(1) url = 'https://m.weibo.cn/api/likes/update' post_data = {'id': mid, 'type': 'comment', 'st': st} resp = self.session.post(url, data=post_data, headers=self._headers) if str(resp.status_code) == '200': return True, '成功' else: return False, '失败', resp.status_code def unlike(self, mid): st = re.search(r"st: '(.*?)',", self.session.get('https://m.weibo.cn').text).group(1) url = 'https://m.weibo.cn/api/attitudes/destroy' post_data = {'id': mid, 'attitude': 'heart', 'st': st} resp = self.session.post(url, data=post_data, headers=self._headers) if str(resp.status_code) == '200': return True, '成功' else: return False, '失败', resp.status_code def followed(self, uid): st = re.search(r"st: '(.*?)',", self.session.get('https://m.weibo.cn').text).group(1) url = 'https://m.weibo.cn/api/friendships/create' post_data = {'uid': uid, 'st': st} resp = self.session.post(url, data=post_data, headers=self._headers) if str(resp.status_code) == '200': return True, '成功' else: return False, '失败', resp.status_code def unfollowed(self, uid): st = re.search(r"st: '(.*?)',", self.session.get('https://m.weibo.cn').text).group(1) url = 'https://m.weibo.cn/api/friendships/destory' post_data = {'uid': uid, 'st': st} resp = self.session.post(url, data=post_data, headers=self._headers) if str(resp.status_code) == '200': return True, '成功' else: return False, '失败', resp.status_code
} wb_json = json.dumps(wb_item) # print wb_json json_list.append(wb_json) # 分页 next_pageUrl = getUrlToPattern(html, comment_url, pattern='page', text_pattern='下一页') # print next_pageUrl if next_pageUrl: comment_url = next_pageUrl[0] elif not next_pageUrl or tags: break return json_list def execute(self): inbox = self.commentInbox() executeES('weibo_feedback_comment', 'text', inbox) outbox = self.commentOutbox() executeES('weibo_feedback_comment', 'text', outbox) if __name__ == '__main__': xnr = SinaLauncher('*****@*****.**', 'xnr1234567') xnr.login() FeedbackComment(xnr.uid).execute()
class FeedbackLike: def __init__(self, username, password): self.launcher = SinaLauncher(username, password) self.launcher.login() self.uid = self.launcher.uid self.session = self.launcher.session @staticmethod def read_datetime(raw_datatime): datetime_splited_list = raw_datatime.split(' ') month = str(datetime_splited_list[1]) month = [ i.values()[0] for i in [{ 'Jan': '01' }, { 'Feb': '02' }, { 'Mar': '03' }, { 'Apr': '04' }, { 'May': '05' }, { 'Jun': '06' }, { 'Jul': '07' }, { 'Aug': '08' }, { 'Sep': '09' }, { 'Oct': '10' }, { 'Nov': '11' }, { 'Dec': '12' }] if month == i.keys()[0] ][0] day = datetime_splited_list[2] time = datetime_splited_list[3] year = datetime_splited_list[5] datetime = year + '-' + month + '-' + day + ' ' + time return datetime def likeInbox(self): json_list = [] page = 1 while True: like_inbox_url = 'https://m.weibo.cn/message/attitude?page={}'.format( page) resp = self.session.get(like_inbox_url) try: resp.json()['data'] except: break data_list = resp.json()['data'] for data in data_list: photo_url = data['user']['profile_image_url'] uid = data['user']['id'] nickname = data['user']['screen_name'] mid = data['status']['mid'] timestamp = int( time.mktime( time.strptime(self.read_datetime(data['created_at']), '%Y-%m-%d %H:%M:%S'))) text = re.sub(r'<.*?>', r'', data['status']['text']) root_mid = data['status']['mid'] root_uid = data['status']['user']['id'] _type = 'stranger' type1 = 'followed' if data['user']['following'] is True else '' type2 = 'follow' if data['user']['follow_me'] is True else '' weibo_type = 'friend' if type1 and type2 else _type update_time = int(time.time()) item = { 'photo_url': photo_url, 'uid': str(uid), 'nick_name': nickname, 'mid': str(mid), 'timestamp': timestamp, 'text': text, 'root_mid': str(root_mid), 'root_uid': str(root_uid), 'weibo_type': weibo_type, 'update_time': update_time } json_list.append(json.dumps(item, ensure_ascii=False)) page += 1 return json_list def excute(self): likes = self.likeInbox() executeES('weibo_feedback_like', 'text', likes)
class FeedbackFollow: def __init__(self, username, password): print '-----------------------' self.launcher = SinaLauncher(username, password, 'phone') self.launcher.login() print '---------------' self.uid = self.launcher.uid self.session = self.launcher.session @staticmethod def read_datetime(raw_datatime): datetime_splited_list = raw_datatime.split(' ') month = str(datetime_splited_list[1]) month = [ i.values()[0] for i in [{ 'Jan': '01' }, { 'Feb': '02' }, { 'Mar': '03' }, { 'Apr': '04' }, { 'May': '05' }, { 'Jun': '06' }, { 'Jul': '07' }, { 'Aug': '08' }, { 'Sep': '09' }, { 'Oct': '10' }, { 'Nov': '11' }, { 'Dec': '12' }] if month == i.keys()[0] ][0] day = datetime_splited_list[2] time = datetime_splited_list[3] year = datetime_splited_list[5] datetime = year + '-' + month + '-' + day + ' ' + time return datetime def follow(self): json_list = [] page = 1 while True: follow_url = 'https://m.weibo.cn/api/container/getIndex?containerid=231093_-_selffollowed&page={}'.format( page) resp = self.session.get(follow_url) #print resp try: resp.json() except: #print resp continue if resp.json().has_key('msg'): break if page == 1: cards = resp.json()['data']['cards'] for card in cards: try: if card['title'].decode('utf-8') == '全部关注': data_list = card['card_group'] except: continue else: cards = resp.json()['data']['cards'][0] data_list = cards['card_group'] for data in data_list: photo_url = data['user']['profile_image_url'] uid = data['user']['id'] nickname = data['user']['screen_name'] if not nickname: nickname = '' #print nickname mid = uid sex = data['user']['gender'] if not sex: sex = 'unknown' elif sex == 0: sex = 'female' elif sex == 1: sex = 'male' description = data['user']['description'] if not description: description = '' root_uid = uid update_time = int(time.time()) item = { 'photo_url': photo_url, 'uid': str(uid), 'mid': str(uid), 'nick_name': nickname, 'timestamp': 0, 'sex': sex, 'description': description, 'follow_source': '', 'gid': '0', 'gname': '', 'root_uid': self.uid, 'weibo_type': 'follow', 'update_time': update_time } json_list.append(json.dumps(item, ensure_ascii=False)) page += 1 time.sleep(random.randint(2, 4)) return json_list def fans(self): json_list = [] page = 1 while True: fans_url = 'https://m.weibo.cn/api/container/getIndex?containerid=231016_-_selffans&page={}'.format( page) resp = self.session.get(fans_url) print resp try: resp.json() except: resp.json() continue if resp.json().has_key('msg'): break if page == 1: cards = resp.json()['data']['cards'] for card in cards: try: if card['title'].decode('utf-8') == '全部粉丝': data_list = card['card_group'] except: continue else: cards = resp.json()['data']['cards'][1] data_list = cards['card_group'] for data in data_list: photo_url = data['user']['profile_image_url'] uid = data['user']['id'] nickname = data['user']['screen_name'] if not nickname: nickname = '' #print nickname mid = uid sex = data['user']['gender'] if not sex: sex = 'unknown' elif sex == 0: sex = 'female' elif sex == 1: sex = 'male' else: sex = 'unknown' follower = data['user']['follow_count'] if not follower: follower = 0 fans = data['user']['followers_count'] if not fans: fans = 0 description = data['user']['description'] if not description: description = '' root_uid = uid update_time = int(time.time()) item = { 'photo_url': photo_url, 'uid': str(uid), 'mid': str(uid), 'nick_name': nickname, 'timestamp': 0, 'sex': sex, 'follower': str(follower), 'fan_source': '', 'fans': str(fans), 'geo': '', 'description': description, 'weibos': '', 'root_uid': str(self.uid), 'weibo_type': 'followed', 'update_time': update_time } json_list.append(json.dumps(item, ensure_ascii=False)) page += 1 time.sleep(random.randint(2, 4)) return json_list def execute(self): follow = self.follow() fans = self.fans() groups = '' print 'follow', follow, len(follow) print 'fans', fans, len(fans) print '+++++++++++++++++++++++++++++++++++++' executeES('weibo_feedback_follow', 'text', follow) print '-------------------------------------' executeES('weibo_feedback_fans', 'text', fans) print '+++++++++++++++++++++++++++++++++++++'
class FeedbackPrivate: def __init__(self, username, password): self.launcher = SinaLauncher(username, password) self.launcher.login() self.uid = self.launcher.uid self.session = self.launcher.session @staticmethod def switch_datetime_to_timestamp(raw_datatime): raw_datatime = raw_datatime.decode('utf-8') print raw_datatime timestamp = '0' if '秒前'.decode('utf-8') in raw_datatime: second = re.search('\d+', raw_datatime).group(1) timestamp = str(int(time.time()) + int(second)) elif '分钟前'.decode('utf-8') in raw_datatime: minute = re.search('(\d+)', raw_datatime).group(1) timestamp = str(int(time.time()) + int(minute) * 60) elif '小时前'.decode('utf-8') in raw_datatime: hour = re.search('(\d+)', raw_datatime).group(1) timestamp = str(int(time.time()) + int(hour) * 60 * 60) elif raw_datatime.count('-') == 1: year = time.strftime('%Y', time.localtime(time.time())) raw_datatime = year+'-'+raw_datatime timestamp = str(int(time.mktime(time.strptime(raw_datatime, '%Y-%m-%d')))) elif raw_datatime.count('-') == 2: timestamp = str(int(time.mktime(time.strptime(raw_datatime, '%Y-%m-%d')))) return timestamp def messages(self): json_list = [] page = 1 while True: messages_url = 'https://m.weibo.cn/message/msglist?page={}'.format(page) resp = self.session.get(messages_url) if not resp.json()['data']: break else: pass data_list = resp.json()['data'] for data in data_list: photo_url = data['user']['avatar_large'] uid = data['user']['id'] nick_name = data['user']['screen_name'] timestamp = self.switch_datetime_to_timestamp(data['created_at']) text = data['text'] root_uid = uid #_type = 'stranger' #type1 = 'followed' if data['user']['following'] is True else '' #type2 = 'follow' if data['user']['follow_me'] is True else '' #weibo_type = 'friend' if type1 and type2 else _type if uid == self.uid: private_type = 'make' else: private_type = 'receive' update_time = int(time.time()) item = { 'photo_url': photo_url, 'uid': str(uid), 'nick_name': nick_name, 'mid': '', 'timestamp': timestamp, 'text': text, 'root_uid': str(self.uid), 'weibo_type': '', 'root_uid': str(root_uid), 'private_type': private_type, 'w_new_count': '0', 'update_time': update_time } json_list.append(json.dumps(item, ensure_ascii=False)) page += 1 return json_list def execute(self): mess = self.messages() executeES('weibo_feedback_private', 'text', mess)