def PlayGame(Model): global Time_re global retime global Log global dmc dmc = DanMuClient(URL) if not dmc.isValid(): print('Url not valid') Model = Model.lower() retime = time.time() Log = Stat(MATCH) if Model == 'freedom': Time_re = Thread(target=Time_Change, args=(Model, )) Time_re.start() if Model == 'democracy': Time_re = Thread(target=Time_load, args=(Model, )) Time_re.start() @dmc.danmu def danmu_fn(msg): button = msg['Content'].lower() print(msg) if button in MATCH: Log[button] += 1 if Model == 'freedom': shell.AppActivate("VisualBoyAdvance") time.sleep(.02) press(button) DataJ = DanmuRecord(msg['NickName'], button).json() requests.post("http://127.0.0.1:5000/record", data=DataJ) if Model == 'democracy': Log[button] += 1 dmc.start(blockThread=True)
def begin(roomID): # for roomID in anchor_roomId: # roomID = str(roomID) # print roomID roomID = str(roomID) dmc = DanMuClient('http://www.douyu.com/'+roomID) # dmc = DanMuClient(roomUrl) if not dmc.isValid(): print('Url not valid') @dmc.danmu def danmu_fn(msg): pp('[%s]: [%s] %s' % (roomID,msg['NickName'], msg['Content'])) # msg=",".join([str(time.time()),msg['NickName'], msg['Content']]) msg = str(msg['Content']) addMsg(msg,roomID); # @dmc.gift # def gift_fn(msg): # pp('[%s]:[%s] sent a gift!' % (roomId,msg['NickName'])) # msg=",".join([str(time.time()),msg['NickName'],"sent a gift!"]) # addMsg(msg); # @dmc.other # def other_fn(msg): # pp('[%s]:Other message received'% msg['NickName']) dmc.start(blockThread = True)
def run(self): def pp(msg): self.item_changed_signal.emit( msg.encode(sys.stdin.encoding, 'ignore').decode(sys.stdin.encoding)) # self.sleep(1) # self.item_changed_signal.emit(msg.encode(sys.stdin.encoding, 'ignore').decode(sys.stdin.encoding)) # print(msg.encode(sys.stdin.encoding, 'ignore').decode(sys.stdin.encoding)) dmc = DanMuClient(self.url) if not dmc.isValid(): print('Url not valid') @dmc.danmu def danmu_fn(msg): pp(msg['Content']) # pp('[%s] %s' % (msg['NickName'], msg['Content'])) @dmc.gift def gift_fn(msg): pass # pp('[%s] sent a gift!' % msg['NickName']) @dmc.other def other_fn(msg): pass # pp('Other message received') dmc.start(blockThread=True)
def get_danmu(roomId): roomId = roomId dmc = DanMuClient('https://www.douyu.com/' + str(roomId)) if not dmc.isValid(): print('Url not valid') @dmc.danmu def danmu_fn(msg): db = dbOperation.DbOperation() db.insert_danmu(roomId, msg['NickName'], msg['Content']) # print(roomId, msg['NickName'], msg['Content']) dmc.start(blockThread=True)
def _run(self, room, game): """Run for multiprocessing.""" dmc = DanMuClient('http://www.douyu.com/{}'.format(room)) if not dmc.isValid(): self.log('http://www.douyu.com/{}'.format(room), "Url not valid") @dmc.default def danmu_fn(msg): """Dan mu function.""" res = "\t".join( (str(room), str(game), str(time.time()), msg["MsgType"], msg["NickName"], msg["Content"], "\n")) self.msg_queue.put(res) dmc.start(blockThread=False) time.sleep(3600) dmc.stop()
def begin(roomID): try: roomID = str(roomID) print "I was at the %s! %s" % (roomID, ctime()) dmc = DanMuClient('http://www.douyu.com/' + roomID) # dmc = DanMuClient(roomUrl) if not dmc.isValid(): print('Url not valid') @dmc.danmu def danmu_fn(msg): pp('[%s]: [%s] %s' % (roomID, msg['NickName'], msg['Content'])) # msg=",".join([str(time.time()),msg['NickName'], msg['Content']]) msg = str(msg['Content']) addMsg(msg, roomID) dmc.start(blockThread=True) # dmc.start() except Exception as e: print(e)
def begin(): dmc = DanMuClient('http://www.douyu.com/' + roomID) if not dmc.isValid(): print('Url not valid') @dmc.danmu def danmu_fn(msg): pp('[%s] %s' % (msg['NickName'], msg['Content'])) msg = ",".join([str(time.time()), msg['NickName'], msg['Content']]) addMsg(msg) @dmc.gift def gift_fn(msg): pp('[%s] sent a gift!' % msg['NickName']) msg = ",".join([str(time.time()), msg['NickName'], "sent a gift!"]) addMsg(msg) @dmc.other def other_fn(msg): pp('Other message received') dmc.start(blockThread=True)
def start_danmu(live_url, relation): ''' 开始运行弹幕监听 :param live_url: :return: ''' client = MongoClient(host=danmu_config.MONGO_HOST, port=danmu_config.MONGO_PORT) db = client[danmu_config.MONGO_DB] dmc = DanMuClient(live_url) if not dmc.isValid(): client.close() pass @dmc.danmu def danmu_fn(msg): # info = msg.encode(sys.stdin.encoding, 'ignore').decode(sys.stdin.encoding) data, coll_name = save_danmu(live_url, msg, relation) if data is not None and coll_name is not None: coll = db[coll_name] try: coll.insert(data) except Exception: pass @dmc.gift def gift_fn(msg): # info = msg.encode(sys.stdin.encoding, 'ignore').decode(sys.stdin.encoding) data, coll_name = save_gift(live_url, msg, relation) if data is not None and coll_name is not None: coll = db[coll_name] try: coll.insert(data) except Exception: pass dmc.start(blockThread=True) client.close()
def main(url, log): dmc = DanMuClient(url) if not dmc.isValid(): print('Url not valid') else: logger = DanmuLogger(url, log) logger.print("Start to crawler %s ..." % url) @dmc.danmu def danmu_fn(msg): message = make_message('[%s] %s' % (msg['NickName'], msg['Content'])) logger.print(message) @dmc.gift def gift_fn(msg): message = make_message('[%s] sent a gift!' % msg['NickName']) logger.print(message) @dmc.other def other_fn(msg): message = make_message('Other message received') logger.print(message) dmc.start(blockThread=True)
import time, sys from danmu import DanMuClient def pp(msg): print(msg.encode(sys.stdin.encoding, 'ignore'). decode(sys.stdin.encoding)) dmc = DanMuClient('http://www.douyu.com/lslalala') if not dmc.isValid(): print('Url not valid') @dmc.danmu def danmu_fn(msg): pp('[%s] %s' % (msg['NickName'], msg['Content'])) @dmc.gift def gift_fn(msg): pp('[%s] sent a gift!' % msg['NickName']) @dmc.other def other_fn(msg): pp('Other message received') dmc.start(blockThread = True)
import time, sys, json from danmu import DanMuClient def out(p): sys.stdout.write(json.dumps(p) + "\n") sys.stdout.flush() dmc = DanMuClient(sys.argv[1]) if not dmc.isValid(): print('Url invalid') @dmc.danmu def danmu_fn(msg): out({'type': 'danmu', 'data': msg}) @dmc.gift def gift_fn(msg): out({'type': 'gift', 'data': msg}) @dmc.other def other_fn(msg): out({'type': 'other', 'data': msg}) dmc.start(blockThread = True)
import time, sys from danmu import DanMuClient def pp(msg): print(msg.encode(sys.stdin.encoding, 'ignore').decode(sys.stdin.encoding)) # dmc = DanMuClient('http://www.douyu.com/lslalala') #pass # dmc = DanMuClient('http://www.huya.com/baozha') #fail # dmc = DanMuClient('https://live.bilibili.com/392') #pass # dmc = DanMuClient('https://www.quanmin.tv/333') #pass dmc = DanMuClient('https://www.panda.tv/1258651') #pass if not dmc.isValid(): print('Url not valid') @dmc.danmu def danmu_fn(msg): pp('[%s] %s' % (msg['NickName'], msg['Content'])) @dmc.gift def gift_fn(msg): pp('[%s] sent a gift!' % msg['NickName']) @dmc.other def other_fn(msg): pp('Other message received')
if "col" not in msg: msg["col"] = "0" width, height = os.get_terminal_size() len_msg = len(msg['nn'] + msg['txt']) + 28 if width < len_msg: msg['txt'] = msg['txt'][:width - len(msg['nn']) - 28] txt = "[%s] :: %s > %s" % (t_col(msg["level"]), c.cyan(msg["nn"], style="underline"), color[msg["col"]](msg["txt"])) return n + b + txt try: dmc = DanMuClient(sys.argv[1]) if not dmc.isValid(): raise Exception("Invalid URL") except IndexError as ie: print(c.red("No enough parameters.")) exit(1) except Exception as e: print(c.red(e.args[1])) exit(1) @dmc.danmu def danmu_fn(msg) -> None: try: print(col(msg), end="\n") except Exception as e: print(e) finally:
class OperatorQueue: REQUEST_PATTERN = '(?:进攻|防守) *(.+)' def __init__(self, url, keyword): OperatorQueue.REQUEST_PATTERN = keyword + OperatorQueue.REQUEST_PATTERN print(OperatorQueue.REQUEST_PATTERN) self.changed = False self._dmc = DanMuClient(url) if not self._dmc.isValid(): raise ValueError('Invalid url') self._attacker_nick_name = [] for o in Attacker.OP_NAME_DICT.values(): for nn in o: self._attacker_nick_name.append(nn) self._defender_nick_name = [] for o in Defender.OP_NAME_DICT.values(): for nn in o: self._defender_nick_name.append(nn) self._attacker_queue = [] self._defender_queue = [] @self._dmc.danmu def danmu_fn(msg): self.process(msg) self._dmc.start(blockThread=False) def find_operator_queue(self, nickname): if nickname.lower() in self._attacker_nick_name: return self._attacker_queue elif nickname.lower() in self._defender_nick_name: return self._defender_queue else: return None def process(self, msg): print('Processing: ') print('[{0}] \"{1}\"'.format(msg['NickName'], msg['Content'])) m = re.match(OperatorQueue.REQUEST_PATTERN, msg['Content'].lower()) if m: self.changed = True q = self.find_operator_queue(m.group(1)) if q is None: print('Illegal operator name: ' + m.group(1)) return find = False for operator in q: if operator.match_name(m.group(1)): operator.update(msg['NickName']) find = True break if not find: try: new_operator = Attacker(m.group(1), msg['NickName']) if q == self._attacker_queue \ else Defender(m.group(1), msg['NickName']) q.append(new_operator) except ValueError as e: print(e) return heapq.heapify(q) print(' ' + repr(self._attacker_queue)) print(' ' + repr(self._defender_queue)) def peek_3(self, side): q = self._attacker_queue if side == 'attacker' \ else self._defender_queue if side == 'defender' else None if len(q) < 3: return q lst = q[0:3] if q[1] > q[2]: lst[1], lst[2] = q[2], q[1] return lst def next_attacker(self): if len(self._attacker_queue) > 0: self._attacker_queue.pop(0) self.changed = True else: print('Attacker queue is empty!') def next_defender(self): if len(self._defender_queue) > 0: self._defender_queue.pop(0) self.changed = True else: print('Defender queue is empty!') def get_text(self): if not self.changed: return None self.changed = False text = 'ATTACKERS:\n' for op in self.peek_3('attacker'): text += ' ' + op.name.upper() + '\n' for _ in range(3 - len(self.peek_3('attacker'))): text += ' --\n' text += 'DEFENDERS:\n' for op in self.peek_3('defender'): text += ' ' + op.name.upper() + '\n' for _ in range(3 - len(self.peek_3('defender'))): text += ' --\n' return text.rstrip()