Bilibili Live 闷声发大财

用途

自动向B站服务器发送心跳包,让服务器以为你一直在看直播从而实现24h挂机涨经验

食用方法

  1. 保存源代码为bilive.py并上传至服务器(当然你愿意在自己电脑上挂我也管不着)
  2. ssh输入crontab -e ,加入*/5 * * * * python 存放路径/bilive.py  > 存放路径/blive.log
  3. Enjoy!(Log保留最后一次的输出,如有问题请及时将log拷出回复我~)
  4. 服务器挂机用户建议将userid = raw_input()和pwd = raw_input()直接修改成你的用户名密码,防止cookie过期

源代码

#coding=UTF-8
import urllib
import urllib2
import cookielib
import json
import sys
import os
import re
import datetime
import time
reload(sys)
sys.setdefaultencoding('utf-8')
COOKIE_FILE = 'BILIVE_COOKIES_DATA'
def login(COOKIE_FILE):
	cookie = cookielib.MozillaCookieJar(COOKIE_FILE)
	opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))  
	response = opener.open('https://passport.bilibili.com/ajax/miniLogin/minilogin')
	print'Please input your bilibili Username:'
	userid = raw_input()
	print'Please input your bilibili Password:'
	pwd = raw_input()
	postdata = urllib.urlencode({
            	'keep':'0',  
            	'captcha':'',
            	'userid':userid,
            	'pwd':pwd
        	})    
	loginUrl = 'https://passport.bilibili.com/ajax/miniLogin/login' 
	response = opener.open(loginUrl,postdata)
	liveUrl = 'http://live.bilibili.com/User/getUserInfo' 
	result = opener.open(liveUrl)
	cookie.save(ignore_discard=True, ignore_expires=True)
	return opener
def check_login(opener):
	liveUrl = 'http://live.bilibili.com/User/getUserInfo' 
	result = opener.open(liveUrl)
	result = result.read()
	s = json.loads(result)
	if s["code"] == "REPONSE_OK":
		return s
	else:
		print'Login Failed: ' + result.decode('unicode_escape')
		os.remove(COOKIE_FILE)
		time.sleep(3)
		exit()
def heart(opener):
	postdata = ''
	heartUrl = 'http://live.bilibili.com/User/userOnlineHeart'
	roomId = get_room_id(opener)
	refererHeader = 'http://live.bilibili.com/' + roomId
	opener.addheaders = [('Referer', refererHeader)]
	result = opener.open(heartUrl,postdata)
	result = result.read()
	return result

def get_room_id(opener):
	liveUrl = 'http://live.bilibili.com/'
	result = opener.open(liveUrl)
	result = result.read()
	res = re.search(r'data-roomid="(\d+)"', result)
	if res:
		return res.group(1)
	else:
		print'Get RoomId Failed'
		exit()
			
cookie = cookielib.MozillaCookieJar(COOKIE_FILE)   
if not os.path.exists(COOKIE_FILE):
	opener = login(COOKIE_FILE)
else:
	cookie.load(COOKIE_FILE, ignore_discard=True, ignore_expires=True)
	opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))

s = check_login(opener)
print'Hello, '+s["data"]["uname"]+'!'
upr = str(s["data"]['user_next_intimacy'] - s["data"]['user_intimacy'])
result = heart(opener)
h = json.loads(result)
if h["code"] != 0:
	for x in xrange(1,6):
		print "Heart Status: Error, Retrying......("+ str(x) +")"
		if x != 5:
			time.sleep(2)
		else:
			time.sleep(10)
		result = heart(opener)
		h = json.loads(result)
		if h["code"] != 0:
			continue
		else:
			print 'Live Level: '+ str(s["data"]['user_level']) +'\nUpgrade requires: '+ upr +'\nLevel Rank: '+ str(s["data"]['user_level_rank']) +'\nHeart Status: Successful\nHeart Time: ' + time.strftime("%Y-%m-%d %H:%M:%S") + '\nDebug:'+ result.decode('unicode_escape')
			time.sleep(3)
			exit()
	print 'Live Level: '+ str(s["data"]['user_level']) +'\nUpgrade requires: '+ upr +'\nLevel Rank: '+ str(s["data"]['user_level_rank']) +'\nHeart Status: Error\nHeart Time: ' + time.strftime("%Y-%m-%d %H:%M:%S") + '\nDebug:'+ result.decode('unicode_escape')
else:
	print 'Live Level: '+ str(s["data"]['user_level']) +'\nUpgrade requires: '+ upr +'\nLevel Rank: '+ str(s["data"]['user_level_rank']) +'\nHeart Status: Successful\nHeart Time: ' + time.strftime("%Y-%m-%d %H:%M:%S") + '\nDebug:'+ result.decode('unicode_escape')

感谢 @latelan 为代码添加伪造 Referer 功能

经验计算方式

每5分钟可以获得3000点经验(B站活动期间可能更高)

Python 3.5+ 用户

请使用 @Septs 提供的 bilibili-live-kit

关于特色图像

前往Y2B查看