Skip to content

Commit

Permalink
add more features & refactor the config part
Browse files Browse the repository at this point in the history
  • Loading branch information
kliner committed Apr 24, 2017
1 parent fa07d02 commit 6324caf
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 153 deletions.
48 changes: 34 additions & 14 deletions DanMuJi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#coding=utf-8
import bili
import bili_sender
import bili_config
import thread
import struct
import time
Expand All @@ -13,11 +14,13 @@
DEBUG = 0

class DanmakuHandler(bili.DanmakuHandler):
def __init__(self, startGiftResponse = False, showTime = False, showNotification = False, showSysGift = False):
bili_sender.init()
tts_s = None

def __init__(self, cookies, startGiftResponse = False, showTime = False, showNotification = False, showSysGift = False, tts = False, color = 'white'):
self.sender = bili_sender.Sender(cookies)
self.cnt = 9
self.notificationTimers = {}
self.showNotification, self.showTime, self.showSysGift = showNotification, showTime, showSysGift
self.showNotification, self.showTime, self.showSysGift, self.tts, self.color = showNotification, showTime, showSysGift, tts, color
self.date_format = '%H:%M:%S'
self.gifts = []
self.LOCK = threading.Lock()
Expand Down Expand Up @@ -46,6 +49,9 @@ def handleDanmaku(self, danmaku):
print '\033[91m%s\033[0m : \033[94m%s\033[0m' % (user, content)
if self.showNotification:
self.showMacNotification(user, content)
if self.tts:
self.say(content)

elif raw['cmd'] == 'SEND_GIFT':
data = raw['data']
uname, num, giftName = data['uname'].encode('utf-8'), data['num'], data['giftName'].encode('utf-8')
Expand All @@ -61,10 +67,9 @@ def handleDanmaku(self, danmaku):
roomid = str(raw['roomid'])
tm = time.strftime(self.date_format, time.localtime(time.time() + 180 + 18000))
#print '%s房间小电视啦,有效期至%s(。・ω・。)ノ' % (roomid, tm)
if self.showSysGift: bili_sender.sendDanmaku(self.roomid, '%s房间小电视啦,有效期至%s' % (roomid, tm))
if self.showSysGift: self.sender.sendDanmaku(self.roomid, '%s房间小电视啦,有效期至%s' % (roomid, tm), self.color)
tvid = raw['tv_id']
bili_sender.joinSmallTV(roomid, tvid)

self.sender.joinSmallTV(roomid, tvid)

else:
if DEBUG: print raw
Expand All @@ -81,7 +86,7 @@ def giftResponseThread(self):
if t_uname != uname: uname = '大家'
if t_giftName != giftName: giftName = '礼物'
#print '谢谢%s送的%s个%s' % (uname, str(num), giftName)
bili_sender.sendDanmaku(self.roomid, '谢谢%s送的%s个%s' % (uname, str(num), giftName))
self.sender.sendDanmaku(self.roomid, '谢谢%s送的%s个%s' % (uname, str(num), giftName), self.color)
while self.gifts: self.gifts.pop()
self.LOCK.release()
time.sleep(2)
Expand All @@ -106,19 +111,34 @@ def closeMacNotification(self, group):
cmd = "terminal-notifier -remove '%s' > /dev/null" % group
subprocess.Popen(cmd, shell=True)


def say(self, s):
cmd = 'say \'%s\'' % (s)
if DEBUG: print cmd
if not self.tts_s:
self.tts_s = subprocess.Popen(cmd, shell=True)
else:
self.tts_s.kill()
self.tts_s = subprocess.Popen(cmd, shell=True)

if __name__ == '__main__':
argv = sys.argv
roomid = 90012
startGiftResponse, showTime, showNotification, showSysGift = False, False, False, False
if len(argv) == 2:
roomid = int(argv[1])
if len(argv) == 3:
roomid = int(argv[1])
startGiftResponse, showTime, showNotification, showSysGift = int(argv[2][0]), int(argv[2][1]), int(argv[2][2]), int(argv[2][3])
config = bili_config.Config('config.txt')
cookies = config.cookies
data = config.data[str(roomid)]

startGiftResponse = data.get("GiftResponse", False)
showTime = data.get("ShowTime", False)
showNotification = data.get("MacNotification", False)
showSysGift = data.get("SmallTVHint", False)
tts = data.get("MacTTS", False)
color = data.get("DanmakuColor", "white")

handler = DanmakuHandler(cookies, startGiftResponse = startGiftResponse, showTime = showTime, showNotification = showNotification, showSysGift = showSysGift, tts = tts, color = color)

py = bili.BiliHelper(roomid, DanmakuHandler(startGiftResponse = startGiftResponse, showTime = showTime, showNotification = showNotification, showSysGift = showSysGift))
py = bili.BiliHelper(roomid, handler)
while 1:
cmd = raw_input()
bili_sender.sendDanmaku(roomid, cmd)
handler.sender.sendDanmaku(roomid, cmd, color)
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ This is a python live.bilibili.com helper library.
# Function

* Danmaku receiver / sender.
* Auto join SmallTV.
* Mac Notification / TTS.
* A basic music VOD.
* Gift auto response.
* Easy to add new functions.
Expand Down
102 changes: 0 additions & 102 deletions ShuoHuaDeDanMuJi.py

This file was deleted.

1 change: 0 additions & 1 deletion bili.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def setRoomId(self, roomid):
def handleDanmaku(self, danmaku):
pass


class BiliHelper(object):

def __init__(self, roomid, packetHandler):
Expand Down
70 changes: 34 additions & 36 deletions bili_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,44 @@
SEND_URL = 'http://live.bilibili.com/msg/send'
TV_URL = 'http://api.live.bilibili.com/SmallTV/join'

cookies = {}
class Sender(object):

def init():
with open(r'cookie.txt','r') as f:
try:
for line in f.read().split(';'):
name, value = line.strip().split('=', 1)
cookies[name] = value
except Exception, e:
print 'warning! please set correct cookies into \'cookie.txt\''
def __init__(self, cookies):
self.cookies = cookies

def sendDanmaku(roomid, content):
content = content.strip()
if not content: return
params = {
"color":6737151, #16777215-white, #6737151-blue
"fontsize":25,
"mode":1,
"msg":content,
"rnd":int(time.time()),
"roomid":roomid
}
r = requests.post(SEND_URL, data=params, cookies=cookies)
result = r.content
raw = json.loads(result)
if raw['code'] != 0: print raw['msg']

def joinSmallTV(roomid, tv_id):
params = {
'roomid':roomid,
'id':tv_id,
'_':int(time.time() * 100)
def sendDanmaku(self, roomid, content, color='white'):
content = content.strip()
if not content: return
if color == 'white': color = 16777215;
elif color == 'blue': color = 6737151;
params = {
"color":color, #16777215-white, #6737151-blue
"fontsize":25,
"mode":1,
"msg":content,
"rnd":int(time.time()),
"roomid":roomid
}
r = requests.get(TV_URL, params=params, cookies=cookies)
result = r.content
raw = json.loads(result)
if raw['code'] != 0: print raw['msg']
r = requests.post(SEND_URL, data=params, cookies=self.cookies)
result = r.content
raw = json.loads(result)
if raw['code'] != 0: print raw['msg']

def joinSmallTV(self, roomid, tv_id):
params = {
'roomid':roomid,
'id':tv_id,
'_':int(time.time() * 100)
}
r = requests.get(TV_URL, params=params, cookies=cookies)
result = r.content
raw = json.loads(result)
if raw['code'] != 0: print raw['msg']

if __name__ == '__main__':
init()
import bili_config
config = bili_config.Config('config.txt')
sender = Sender(config.cookies)
while 1:
content = raw_input()
sendDanmaku(90012, content)
sender.sendDanmaku(90012, content)
31 changes: 31 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/python
#coding=utf-8
import json

class Config(object):

data = {}
cookies = {}

def __init__(self, path):
self.read(path)

def read(self, path):

with open(r'config.txt','r') as f:
s = f.read()
self.data = json.loads(s)

with open(r'cookie.txt','r') as f:
s = f.read()
if 'cookie:' == s[:7]: s = s[7:] # do strip
try:
for line in s.split(';'):
name, value = line.strip().split('=', 1)
self.cookies[name] = value
except Exception, e:
print 'warning! please set correct cookies into \'cookie.txt\'', e

if __name__ == '__main__':
config = Config("config.txt")
print config.data["90012"]
24 changes: 24 additions & 0 deletions config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"90012": {
"GiftResponse": true,
"ShowTime": false,
"SmallTVHint": true,
"MacNotification": true,
"MacTTS": true,
"DanmakuColor": "blue"
},
"30040": {
"GiftResponse": false,
"ShowTime": false,
"SmallTVHint": false,
"MacNotification": false,
"MacTTS": true
},
"207919": {
"GiftResponse": true,
"ShowTime": false,
"SmallTVHint": true,
"MacNotification": false,
"MacTTS": false
}
}

0 comments on commit 6324caf

Please sign in to comment.