Skip to content

Commit

Permalink
Merge branch 'master' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
kliner committed Jun 23, 2018
2 parents bd2ff2b + c7d5782 commit 4469ebd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pybili/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging

__author__ = 'kliner'
__version__ = '0.3.3'
__version__ = '0.3.5'

# init config & temp dir
home = os.path.expanduser("~")
Expand Down
7 changes: 7 additions & 0 deletions pybili/bili_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ def inputBoolean(self, msg):
a = raw_input(msg)
return a in 'Yy1'

def setCookies(self, s):
cf = ConfigParser.RawConfigParser()
cf.read(self.path)
if not cf.has_section('cookies'): cf.add_section('cookies')
cf.set('cookies', 'cookies', s)
cf.write(open(self.path, 'w'))

def setup(self):
cf = ConfigParser.RawConfigParser()
cf.read(self.path)
Expand Down
27 changes: 22 additions & 5 deletions pybili/bili_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
TV_URL = 'http://api.live.bilibili.com/gift/v2/smalltv/join'
QUERY_RAFFLE_URL = 'http://api.live.bilibili.com/activity/v1/Raffle/check'
RAFFLE_URL = 'http://api.live.bilibili.com/activity/v1/Raffle/join'
QUERY_FREE_SILVER = 'http://api.live.bilibili.com/FreeSilver/getCurrentTask'
GET_FREE_SILVER = 'http://api.live.bilibili.com/FreeSilver/getAward'
CAPTCHA_URL = 'http://api.live.bilibili.com/freeSilver/getCaptcha?ts=%i'
QUERY_FREE_SILVER = 'http://api.live.bilibili.com/lottery/v1/SilverBox/getCurrentTask'
GET_FREE_SILVER = 'http://api.live.bilibili.com/lottery/v1/SilverBox/getAward'
CAPTCHA_URL = 'http://api.live.bilibili.com/lottery/v1/SilverBox/getCaptcha?ts=%i'
SIGN_IN_URL = ''
GET_SIGN_INFO_URL = 'http://api.live.bilibili.com/sign/GetSignInfo'
GET_USER_INFO_URL = 'http://live.bilibili.com/user/getuserinfo'

class Sender(object):

Expand Down Expand Up @@ -66,10 +69,21 @@ def _parseHttpResult(self, url, r):
self.logger.debug(raw)
if raw['code'] == 65531:
self.logger.warn("API %s fail! WRONG HEADER!" % (url))
elif raw['code'] != 0:
elif raw['code'] != 0 and raw['code'] != 'REPONSE_OK':
self.logger.warn("API %s fail! MSG: %s" % (url, raw['msg']))
return raw

def isCookieValid(self):
raw = self._get(GET_USER_INFO_URL)
if raw['code'] != 0 and raw['code'] != 'REPONSE_OK':
return raw['msg']
else:
uname = raw['data']['uname']
return 'OK, welcome %s' % uname

def signIn(self):
return self._get(SIGN_IN_URL)

def sendDanmaku(self, roomid, content, color='white'):
content = content.strip()
if not content: return
Expand Down Expand Up @@ -148,8 +162,11 @@ def checkFreeSilver(self):
def downloadCaptcha(self, path):
t = int(time.time()*1000)
r = requests.get(CAPTCHA_URL % t, cookies=self.cookies)
raw = json.loads(r.content)
with open(path, 'w') as f:
for chunk in r: f.write(chunk)
s = raw['data']['img']
s = s.split(',')[1]
f.write(s.decode('base64'))
return 'ok'

def getFreeSilver(self, data):
Expand Down
24 changes: 24 additions & 0 deletions pybili/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import DanMuJi
import bili_sender
import bili_config
import thread
from flask import Flask
from flask import request

app = Flask(__name__)
config = bili_config.Config()
sender = bili_sender.Sender(config.cookies)

@app.route("/")
def hello():
return "Hello World!"

@app.route("/check")
def check():
return sender.isCookieValid()

@app.route('/setcookie.do', methods=['POST', 'GET'])
def setCookie():
c = request.form['c']
config.setCookies(c)
return 'OK'

0 comments on commit 4469ebd

Please sign in to comment.