Esempio n. 1
0
async def fetch_medal(show=True, list_wanted_medal=[]):
    printlist = []
    list_medal = []
    if show:
        printlist.append('查询勋章信息')
        printlist.append(
            '{} {} {:^12} {:^10} {} {:^6} {}'.format(adjust_for_chinese('勋章'), adjust_for_chinese('主播昵称'), '亲密度', '今日的亲密度', adjust_for_chinese('排名'), '勋章状态', '房间号码'))
    dic_worn = {'1': '正在佩戴', '0': '待机状态'}
    json_response = await bilibili.request_fetchmedal()
    # print(json_response)
    if not json_response['code']:
        for i in json_response['data']['fansMedalList']:
            if 'roomid' in i:
                list_medal.append((i['roomid'], int(i['dayLimit']) - int(i['todayFeed']), i['medal_name'], i['level']))
                if show:
                    printlist.append('{} {} {:^14} {:^14} {} {:^6} {:^9}'.format(adjust_for_chinese(i['medal_name'] + '|' + str(i['level'])), adjust_for_chinese(i['anchorInfo']['uname']), str(i['intimacy']) + '/' + str(i['next_intimacy']), str(i['todayFeed']) + '/' + str(i['dayLimit']), adjust_for_chinese(str(i['rank'])), dic_worn[str(i['status'])], i['roomid']))
        if show:
            printer.info(printlist, True)
        if list_wanted_medal:
            list_return_medal = []
            for roomid in list_wanted_medal:
                for i in list_medal:
                    if i[0] == roomid:
                        list_return_medal.append(i[:3])
                        break
        else:
            list_return_medal = [i[:3] for i in sorted(list_medal, key=itemgetter(3), reverse=True)]
        return list_return_medal
Esempio n. 2
0
 def handle_danmu(self, body):
     dic = json.loads(body.decode('utf-8'))
     cmd = dic['cmd']
     # print(cmd)
     if cmd == 'DANMU_MSG':
         info = dic['info']
         ori = info[1]
         uid = info[2][0]
         print(ori)
         try:
             msg = self.__reverse(ori)
             result = self.__combine_piece(uid, msg)
             print('监控read dic', self.__read)
             if result is None:
                 return True
             print(result)
             type, raffle_id, room_id = result
             if type == '+':
                 printer.info([
                     f'{self._area_id}号弹幕监控检测到{room_id:^9}的大航海(id: {raffle_id})'
                 ], True)
                 RaffleHandler().push2queue((room_id, raffle_id),
                                            'handle_guard_raffle')
                 bili_stats.add2pushed_raffles('Yj协同大航海', 2)
         except Exception:
             printer.warn(f'Yj监控房间内可能有恶意干扰{uid}: {ori}')
     return True
Esempio n. 3
0
    async def read_datas(self):
        while True:
            datas = await self._read_bytes()
            # 本函数对bytes进行相关操作,不特别声明,均为bytes
            if datas is None:
                return
            data_l = 0
            len_datas = len(datas)
            while data_l != len_datas:
                # 每片data都分为header和body,data和data可能粘连
                # data_l == header_l && next_data_l = next_header_l
                # ||header_l...header_r|body_l...body_r||next_data_l...
                tuple_header = self.structer.unpack_from(datas[data_l:])
                len_data, len_header, ver, opt, seq = tuple_header
                body_l = data_l + len_header
                next_data_l = data_l + len_data
                body = datas[body_l:next_data_l]
                # 人气值(或者在线人数或者类似)以及心跳
                if opt == 3:
                    # UserCount, = struct.unpack('!I', remain_data)
                    # printer.debug(f'弹幕心跳检测{self._area_id}')
                    pass
                # cmd
                elif opt == 5:
                    if not self.handle_danmu(body):
                        return
                # 握手确认
                elif opt == 8:
                    printer.info(
                        [f'{self._area_id}号弹幕监控进入房间({self._room_id})'], True)
                else:
                    printer.warn(datas[data_l:next_data_l])

                data_l = next_data_l
Esempio n. 4
0
def tchain(spec, cacheSizeMB=None):
    chain = r.TChain(spec["treeName"])
    for fileName in spec["fileNames"]:
        chain.Add(fileName)

    if cacheSizeMB:
        chain.SetCacheSize(cacheSizeMB * 1024**2)

    if spec["treeName"] == "Events":  # CMS CDAQ
        chain.SetBranchStatus("*", 0)
        found = False
        for branch in spec["rawCollections"]:
            if sw.use_fwlite:
                for suffix in [".obj", ".present"]:
                    branch1 = branch + suffix
                    if chain.GetBranch(branch1):
                        chain.SetBranchStatus(branch1, 1)
                        found = True
                    else:
                        printer.info("Could not find branch %s" % branch1)
            else:
                if chain.GetBranch(branch):
                    chain.SetBranchStatus(branch, 1)
                    found = True
                else:
                    printer.info("Could not find branch %s" % branch)
            if found:
                spec["rawCollection"] = branch
                break

        if not found:
            sys.exit("Could not find any branches: see configuration/sw.py")

    return chain
Esempio n. 5
0
    async def check_result(self):
        while True:
            results = dyn_raffle_sql.select_rafflestatus(
                1, None,
                utils.curr_time() - 900)  # 延迟15min处理抽奖
            results += dyn_raffle_sql.select_rafflestatus(
                -1, None,
                utils.curr_time() - 900)
            printer.info(['正在查找已经结束的动态抽奖:', results], True)
            for dyn_raffle_status in results:

                dyn_raffle_results: Optional[
                    DynRaffleResults] = await notifier.exec_func(
                        -1, DynRaffleHandlerTask.fetch_dyn_raffle_results,
                        dyn_raffle_status)
                print(dyn_raffle_status, dyn_raffle_results)

                await notifier.exec_task_awaitable(-2,
                                                   DynRaffleHandlerTask,
                                                   2,
                                                   dyn_raffle_status,
                                                   dyn_raffle_results,
                                                   delay_range=(0, 30))
                if dyn_raffle_results is not None:
                    dyn_raffle_sql.insert_dynraffle_results_table(
                        dyn_raffle_results)
                dyn_raffle_sql.del_from_dynraffle_status_table(
                    dyn_raffle_status.dyn_id)

            await asyncio.sleep(120)
    def __add_final_state(self, dfa):
        """
        Returns a DFA with one more state than the given DFA, which will be the new final state.
        The acceptance of the new final state will be identical to the acceptance of the previous final state

        @param DFA: DFA with n states
        @return new_DFA: DFA with n+1 states
        """
        # finding final state of given DFA
        prev_final_state = "q" + str(len(dfa.states) - 2)
        # finding final state of new DFA
        new_final_state = "q" + str(len(dfa.states) - 1)
        # Building new DFA
        new_states = dfa.states | {new_final_state}  # Adding new final state
        new_transitions = deepcopy(dfa.transitions)
        if new_transitions['q0'].get('0') == 'q0':
            new_transitions[prev_final_state]['1'] = new_final_state
        else:
            new_transitions[prev_final_state]['0'] = new_final_state
        new_transitions[new_final_state] = {}
        new_transitions[new_final_state]['0'] = new_final_state
        new_transitions[new_final_state]['1'] = new_final_state
        # new final state will agree with acceptance of prev final state
        if dfa.reaches_qf(prev_final_state):
            new_transitions[new_final_state]['#'] = 'qF'
        new_accepting = deepcopy(dfa.accepting)
        new_dfa = DFA(new_states, new_transitions, 'q0', new_accepting)
        info(new_dfa)
        return new_dfa
Esempio n. 7
0
async def handle_1_TV_raffle(num, raffle_type, real_roomid, raffleid):
    while True:
        printer.info([f'参与房间{real_roomid:^9}的 {raffle_type} 抽奖:{raffleid}'],
                     True)
        json_response2 = await OnlineNet().req('get_gift_of_TV', raffle_type,
                                               real_roomid, raffleid)
        code = json_response2['code']
        if not code:
            break
        elif code == -403:
            return True
        elif code == -405:
            print('没抢到。。。。。')
            printer.warn(f'{raffleid}  {raffle_type} {num}')
            return False
        elif code == -400:
            print(json_response2)
            return
        elif code != -401 and code != -403:
            print(json_response2)
            return

    printer.info(
        [f'房间{real_roomid:^9}的抽奖:{raffleid} = {json_response2["msg"]}'], True)
    return True
    def __remove_transition_from_qn(self, dfa):
        new_transitions = deepcopy(dfa.transitions)
        last_state_index = self.__get_index_of_qn(dfa)
        if last_state_index is not None:
            last_state = 'q%s' % last_state_index
            if all(symbol not in dfa.transitions[last_state]
                   for symbol in ['0', '1']):
                return deepcopy(dfa)
            if last_state_index == 0 or any(
                    symbol not in dfa.transitions[last_state]
                    for symbol in ['0', '1']):
                transition_to_remove = self.randomizer.get_prng().choice([
                    symbol for symbol in dfa.transitions[last_state]
                    if symbol != '#'
                ])
            else:
                prev_state = 'q%s' % (last_state_index - 1)
                transition_to_remove = '0' if new_transitions[prev_state].get(
                    '0') == last_state else '1'
            new_transitions[last_state].pop(transition_to_remove)

        new_dfa = DFA(deepcopy(dfa.states), new_transitions, 'q0',
                      deepcopy(dfa.accepting))
        info(new_dfa)
        return new_dfa
 def get_random_neighbor(self, dfa, positive_examples):
     """
     Chooses randomly a potential random neighbor and calls function try_option in order to check the validity of the chosen neighbor.
     If the random neighbor is not valid for some reason (as will be described in the try_option documantion),
     the function will re-choose a potential neighbor.
     Number of retries is limited to 20. Once a valid neighbor is found, it will be returned by the function.
     If by the end of 20 tries it doesn't find a valid neighbor, the current DFA is returned.
     """
     options = [
         self.__flip_acceptance_of_random_state, self.__remove_final_state,
         self.__switching_transitions, self.__add_final_state,
         self.__increase_accepting_from_left,
         self.__increase_accepting_from_right,
         self.__decrease_accepting_from_left,
         self.__decrease_accepting_from_right,
         self.__remove_transition_from_qn, self.__remove_self_transitions
     ]
     for i in range(20):  #Limited to 20 tries
         if i >= 1:
             info("Raffles another hypothesis.")
         chosen_option = self.randomizer.get_prng().choice(options)
         result_dfa = self.__try_option(dfa, positive_examples,
                                        chosen_option)
         if result_dfa is not None:
             result_dfa.positive_examples = dfa.positive_examples
             return result_dfa
     return dfa
Esempio n. 10
0
    async def _verify_tcp_req(self, reader: asyncio.StreamReader,
                              writer: asyncio.StreamWriter) -> Receiver:
        conn = TcpReceiverConn(writer=writer, reader=reader)
        try:
            json_data = await conn.recv_json()
            if json_data is not None:
                orig_key = json_data['data']['key']
                key = sql.is_key_verified(orig_key)
                if key is not None:
                    if self._broadcaster.can_pass_max_users_test(
                            key.key_index, key.key_max_users):
                        user = Receiver(user_conn=conn,
                                        user_key_index=key.key_index)
                        return user
                    else:
                        info(f'KEY({key.key_index[:5]}***...)用户过多')
                        raise tcp_req_exception.MaxError(conn)
                else:
                    info('有人恶意尝试KEY')
                    raise tcp_req_exception.VerificationError(conn)
            else:
                raise tcp_req_exception.DataError(conn)

        except tcp_req_exception.TcpReqError:
            raise
        except:
            raise tcp_req_exception.OtherError(conn)
    def __change_accepting_states(self, dfa, refer_state, size_change):
        """
        @param DFA: DFA with n states and x accpeting states
        @param refer_state: 'first_acc' for left-most accepting state or 'last_acc' for right-most accepting state.
        @param size_change: 'increase' or 'decrease'.
        @return new_DFA: DFA with change in accepting states per the other parameters.
        """
        accepting_ints = [
            int(state[1:]) for state in dfa.states if dfa.reaches_qf(state)
        ]
        new_transitions = deepcopy(dfa.transitions)
        if refer_state == 'first_acc':
            min_acc_index = min(accepting_ints)
            if size_change == 'decrease' or min_acc_index == 0:
                new_transitions['q' + str(min_acc_index)].pop('#')
            else:
                new_transitions['q' + str(min_acc_index - 1)]['#'] = 'qF'
        else:
            max_acc_index = max(accepting_ints)
            if size_change == 'decrease' or max_acc_index == len(
                    dfa.states) - 2:
                new_transitions['q' + str(max_acc_index)].pop('#')
            else:
                new_transitions['q' + str(max_acc_index + 1)]['#'] = 'qF'

        new_dfa = DFA(deepcopy(dfa.states), new_transitions, 'q0',
                      deepcopy(dfa.accepting))
        info(new_dfa)
        return new_dfa
Esempio n. 12
0
    def download_lfs_sources(self):
        """
        Download sources for 'lfs'.
        """
        urls_list = []

        # .- get 'urls' from 'extra_download_urls' for
        # components on 'lfs' book's builders
        urls_list = self.get_download_urls()

        # .- move into 'sources' directory
        os.chdir(self.downloader_data["lfsbuilder_sources_directory"])

        # .- download the 'wget-list' file
        printer.info("Downloading 'wget-list' for '{n}'".format(n=self.downloader_data["name"]))
        wget_list_filename = self.downloader_data["lfs_wget_link"].split("/")[-1]
        self.download_file_from_url(wget_list_filename, self.downloader_data["lfs_wget_link"])

        # .- add download urls from 'wget-list'
        for url in tools.list_from_file(wget_list_filename):
            if (url is not None) and (url not in urls_list):
                urls_list.append(url)

        printer.info("Downloading sources for '{n}'".format(n=self.downloader_data["name"]))

        # .- ensure there is no any empty string on 'urls_list'
        tools.remove_element(urls_list, "")

        # .- download
        for url in urls_list:
            if url is not None:
                filename = url.split("/")[-1]
                self.download_file_from_url(filename, url)
Esempio n. 13
0
 def handle_danmu(self, body):
     dic = json.loads(body.decode('utf-8'))
     cmd = dic['cmd']
     # print(cmd)
     if cmd == 'DANMU_MSG':
         info = dic['info']
         ori = info[1]
         uid = info[2][0]
         print(ori)
         try:
             msg = self.__reverse(ori)
             '''
             if msg is not None:
                 msg_id, type, id = msg
                 if type == '~' and not msg_id % 2:
                     storm_id = id
                     print('节奏风暴', storm_id)
             '''
             result = self.__combine_piece(uid, msg)
             print('监控read dic', self.__read)
             if result is None:
                 return True
             print(result)
             type, raffle_id, room_id = result
             if type == '+':
                 printer.info([f'{self._area_id}号弹幕监控检测到{room_id:^9}的大航海(id: {raffle_id})'], True)
                 rafflehandler.Rafflehandler.Put2Queue((room_id, raffle_id), rafflehandler.handle_1_room_guard)
                 Statistics.add2pushed_raffle('Yj协同大航海', 2)
         except Exception:
             printer.warn(f'Yj监控房间内可能有恶意干扰{uid}: {ori}')
     return True
Esempio n. 14
0
 async def close(self):
     try:
         await self.ws.close()
     except:
         print('请联系开发者', sys.exc_info()[0], sys.exc_info()[1])
     if not self.ws.closed:
         printer.info([f'请联系开发者  {self._area_id}号弹幕收尾模块状态{self.ws.closed}'], True)
Esempio n. 15
0
    async def run(self):
        self.roomid = ConfigLoader().dic_user['other_control']['raffle_minitor_roomid']
        if not self.roomid:
            return
        self.danmuji = bilibiliCilent.YjMonitorHandler(self.roomid, self.areaid)
        while True:
            print('# 正在启动直播监控弹幕姬')
            time_start = int(utils.CurrentTime())
            connect_results = await self.danmuji.connectServer()
            # print(connect_results)
            if not connect_results:
                continue
            task_main = asyncio.ensure_future(self.danmuji.ReceiveMessageLoop())
            task_heartbeat = asyncio.ensure_future(self.danmuji.HeartbeatLoop())
            finished, pending = await asyncio.wait([task_main, task_heartbeat], return_when=asyncio.FIRST_COMPLETED)
            print('主弹幕姬异常或主动断开,正在处理剩余信息')
            time_end = int(utils.CurrentTime())
            if not task_heartbeat.done():
                task_heartbeat.cancel()
            task_terminate = asyncio.ensure_future(self.danmuji.close_connection())
            await asyncio.wait(pending)
            await asyncio.wait([task_terminate])
            printer.info(['主弹幕姬退出,剩余任务处理完毕'], True)
            if time_end - time_start < 5:
                print('# 当前网络不稳定,为避免频繁不必要尝试,将自动在5秒后重试')
                await asyncio.sleep(5)
            
            

        
                    
Esempio n. 16
0
 async def dig_and_filter(self, doc_id: int, uid: int, post_time: int,
                          describe: str):
     dyn_raffle_status: DynRaffleStatus = await notifier.exec_func(
         -1, DynRaffleHandlerTask.fetch_dyn_raffle_status, doc_id, uid,
         post_time, describe)
     if dyn_raffle_status.lottery_time <= utils.curr_time() + 60:
         printer.info([f'{doc_id}的动态抽奖已经开奖或马上开奖,不再参与'], True)
         return
     for key_word in self.dyn_raffle_description_filter:
         if key_word in dyn_raffle_status.describe:
             printer.info([f'{doc_id}的动态抽奖正文触发关键词过滤({key_word})'], True)
             return
     for key_word in self.dyn_prize_cmt_filter:
         if key_word in dyn_raffle_status.prize_cmt_1st or \
                 key_word in dyn_raffle_status.prize_cmt_2nd or \
                 key_word in dyn_raffle_status.prize_cmt_3rd:
             printer.info([f'{doc_id}的动态抽奖正文触发关键词过滤({key_word})'], True)
             return
     # 如果是刚刚出来的抽奖,就延迟150秒,
     if dyn_raffle_status.post_time >= utils.curr_time() - 150:
         printer.info([f'{doc_id}的动态抽奖触发时间约束,休眠150秒后再正式参与'], True)
         await asyncio.sleep(150)
     printer.info([f'{doc_id}的动态抽奖通过时间和关键词过滤'], True)
     notifier.exec_task(-1,
                        DynRaffleHandlerTask,
                        0,
                        dyn_raffle_status,
                        delay_range=(0, 0))
 async def HeartbeatLoop(self):
     printer.info([f'{self.area_id}号弹幕监控开始心跳(心跳间隔30s,后续不再提示)'], True)
     while self.connected:
         if not (await self.SendSocketData(opt=2, body='')):
             self.connected = False
             return
         await asyncio.sleep(30)
Esempio n. 18
0
def run_batch(create_plots, base_seed, quantifier_type, initial_temperature,
              threshold, alpha, num_simulations, **kwargs):
    tasks = [((create_plots, seed, quantifier_type, initial_temperature,
               threshold, alpha), kwargs)
             for seed in range(base_seed, base_seed + num_simulations)]
    with Pool(maxtasksperchild=100) as pool:
        results = pool.map(run_single_simulation_for_multiprocessing, tasks)
        result_hypotheses, is_successes, output_dirs = zip(*results)
        total_success = sum(is_successes)
        info('########### Total success for quantifier %s is %d of %d' %
             (quantifier_type, total_success, num_simulations))
        with open(
                os.path.join(
                    run_dir(quantifier_type, initial_temperature, alpha,
                            threshold), 'num_success.csv'),
                'w') as f_num_success:
            f_num_success.write(
                'percent_success,total_success,num_simulations,quantifier,base_seed\n%f,%d,%d,%s,%s\n'
                % (total_success / float(num_simulations), total_success,
                   num_simulations, quantifier_type, base_seed))
        with open("result_hypotheses.pickle", "wb") as f:
            pickle.dump(result_hypotheses, f)

        mdl_scores = [x.get_mdl() for x in result_hypotheses]
        print(f"Mean MDL: {np.mean(mdl_scores)}, std {np.std(mdl_scores)}")
        best_hypothesis_idx = np.argmin(mdl_scores)
        best_hypothesis = result_hypotheses[best_hypothesis_idx]
        best_hypothesis_dir = str(
            pathlib.Path(output_dirs[best_hypothesis_idx]).parent)
        print(f"Best hypothesis dir: {best_hypothesis_dir}")
        best_hypothesis.plot_transitions('H_best', best_hypothesis_dir)

        return total_success
    async def ReceiveMessageLoop(self):
        while True:
            bytes_datas = await self.ReadSocketData()
            if bytes_datas is None:
                break
            len_read = 0
            len_bytes_datas = len(bytes_datas)
            while len_read != len_bytes_datas:
                state = None
                split_header = self.structer.unpack(bytes_datas[len_read:16 +
                                                                len_read])
                len_data, len_header, ver, opt, seq = split_header
                remain_data = bytes_datas[len_read + 16:len_read + len_data]
                # 人气值/心跳 3s间隔
                if opt == 3:
                    # self._UserCount, = struct.unpack('!I', remain_data)
                    printer.debug(f'弹幕心跳检测{self.area_id}')
                # cmd
                elif opt == 5:
                    messages = remain_data.decode('utf-8')
                    dic = json.loads(messages)
                    state = self.loop_func(dic)
                # 握手确认
                elif opt == 8:
                    printer.info([f'{self.area_id}号弹幕监控进入房间({self.roomid})'],
                                 True)
                else:
                    printer.warn(bytes_datas[len_read:len_read + len_data])

                if state is not None and not state:
                    return
                len_read += len_data
Esempio n. 20
0
def run_single_simulation_for_multiprocessing(args_and_kwargs):
    args, kwargs = args_and_kwargs
    create_plots, seed, args_for_simulation = args[0], args[1], args[2:]
    set_up_logging('out.log')
    info('##### Task is:', args_and_kwargs)
    return SingleSimulationRunner(create_plots, seed).run_single_simulation(
        *args_for_simulation, **kwargs)
Esempio n. 21
0
    async def handle_msg(self):
        while True:
            bytes_datas = await self.__read_msg()
            if bytes_datas is None:
                return
            len_read = 0
            len_bytes_datas = len(bytes_datas)
            while len_read != len_bytes_datas:
                split_header = self.structer.unpack(bytes_datas[len_read:16 +
                                                                len_read])
                len_data, len_header, ver, opt, seq = split_header
                remain_data = bytes_datas[len_read + 16:len_read + len_data]
                # 人气值/心跳 3s间隔
                if opt == 3:
                    # self._UserCount, = struct.unpack('!I', remain_data)
                    printer.debug(f'弹幕心跳检测{self.area_id}')
                # cmd
                elif opt == 5:
                    if not self.handle_danmu(remain_data):
                        return
                # 握手确认
                elif opt == 8:
                    printer.info([f'{self.area_id}号弹幕监控进入房间({self.roomid})'],
                                 True)
                else:
                    printer.warn(bytes_datas[len_read:len_read + len_data])

                len_read += len_data
Esempio n. 22
0
async def run():
    while 1:
        printer.info(["心跳"], True)
        login.HandleExpire()
        await heartbeat()
        await draw_lottery()
        await asyncio.sleep(300)
Esempio n. 23
0
async def handle_1_TV_raffle_black(num, real_roomid, raffleid, raffle_type):
    # print('ffffffffffggggdgdfddf')
    for i in range(50):
        json_response2 = await bilibili.get_gift_of_TV_app(
            real_roomid, raffleid, raffle_type)
        code = json_response2['code']
        if not code:
            break
        elif code == -403:
            return True
        elif code == -405:
            print('没抢到。。。。。')
            printer.warn(raffleid)
            return False
        elif code != -401 and code != -403:
            # print('00', json_response2)
            pass
        # await asyncio.sleep()
    code = json_response2['code']
    if code:
        await asyncio.sleep(5)
        return
    data = json_response2['data']
    Statistics.append_to_TVlist(raffleid, real_roomid)
    Statistics.add_to_result(data['gift_name'], int(data['gift_num']))
    printer.info([f'参与了房间{real_roomid:^9}的道具抽奖'], True)
    # printer.info([f'# 道具抽奖状态: {json_response2["msg"]}'])
    printer.info([
        f'# 房间{real_roomid:^9}网页端活动抽奖结果: {data["gift_name"]}X{data["gift_num"]}'
    ])
    return True
Esempio n. 24
0
async def handle_1_TV_raffle(num, real_roomid, raffleid, raffle_type):
    while True:
        json_response2 = await bilibili.get_gift_of_TV_app(
            real_roomid, raffleid, raffle_type)
        code = json_response2['code']
        if not code:
            break
        elif code == -403:
            return True
        elif code == -405:
            print('没抢到。。。。。')
            printer.warn(raffleid)
            return False
        elif code == 400:
            print(json_response2)
            tasklist = []
            for i in range(60):
                task = asyncio.ensure_future(
                    handle_1_TV_raffle_black(num, real_roomid, raffleid,
                                             raffle_type))
                tasklist.append(task)
            await asyncio.wait(tasklist, return_when=asyncio.FIRST_COMPLETED)
            return
        elif code != -401 and code != -403:
            pass

    data = json_response2['data']
    Statistics.append_to_TVlist(raffleid, real_roomid)
    Statistics.add_to_result(data['gift_name'], int(data['gift_num']))
    printer.info([f'参与了房间{real_roomid:^9}的道具抽奖'], True)
    # printer.info([f'# 道具抽奖状态: {json_response2["msg"]}'])
    printer.info([
        f'# 房间{real_roomid:^9}网页端活动抽奖结果: {data["gift_name"]}X{data["gift_num"]}'
    ])
    return True
Esempio n. 25
0
 async def run(self):
     self.danmuji = bilibiliClient()
     while True:
         print('# 正在启动直播监控弹幕姬')
         time_start = int(utils.CurrentTime())
         connect_results = await self.danmuji.connectServer()
         # print(connect_results)
         if not connect_results:
             continue
         task_main = asyncio.ensure_future(
             self.danmuji.ReceiveMessageLoop())
         task_heartbeat = asyncio.ensure_future(
             self.danmuji.HeartbeatLoop())
         finished, pending = await asyncio.wait(
             [task_main, task_heartbeat],
             return_when=asyncio.FIRST_COMPLETED)
         print('主弹幕姬异常或主动断开,正在处理剩余信息')
         self.danmuji.connected = False
         time_end = int(utils.CurrentTime())
         if not task_heartbeat.done():
             task_heartbeat.cancel()
         task_terminate = asyncio.ensure_future(
             self.danmuji.close_connection())
         await asyncio.wait(pending)
         await asyncio.wait([task_terminate])
         printer.info(['主弹幕姬退出,剩余任务处理完毕'], True)
         if time_end - time_start < 5:
             print('# 当前网络不稳定,为避免频繁不必要尝试,将自动在5秒后重试')
             await asyncio.sleep(5)
Esempio n. 26
0
 def info(self, *objects, with_userid=True, **kwargs):
     if with_userid:
         printer.info(*objects,
                      **kwargs,
                      extra_info=f'用户id:{self.id} 名字:{self.alias}')
     else:
         printer.info(*objects, **kwargs)
Esempio n. 27
0
 async def close_connection(self):
     try:
         await self.ws.close()
     except:
         print('请联系开发者', sys.exc_info()[0], sys.exc_info()[1])
     printer.info([f'{self.area_id}号弹幕收尾模块状态{self.ws.closed}'], True)
     self.connected = False
Esempio n. 28
0
 def handle_danmu(self, body):
     dic = json.loads(body.decode('utf-8'))
     cmd = dic['cmd']
     # print(cmd)
     if cmd == 'DANMU_MSG':
         info = dic['info']
         ori = info[1]
         uid = info[2][0]
         # print('测试', self.__read, ori)
         try:
             msg = self.__reverse(ori)
             if msg is not None:
                 msg_id, type, id = msg
                 if type == '~' and not msg_id % 2:
                     raffle_id = id
                     printer.info([
                         f'{self._area_id}号弹幕监控检测到{"0":^9}的节奏风暴(id: {raffle_id})'
                     ], True)
                     # raffle_handler.exec_at_once(StormRaffleHandlerTask, 0, raffle_id)
                     bili_statistics.add2pushed_raffles('Yj协同节奏风暴', 2)
             result = self.__combine_piece(uid, msg)
             if result is None:
                 return True
             type, raffle_id, real_roomid = result
             if type == '+':
                 printer.info([
                     f'{self._area_id}号弹幕监控检测到{real_roomid:^9}的大航海(id: {raffle_id})'
                 ], True)
                 raffle_handler.push2queue(GuardRaffleHandlerTask,
                                           real_roomid, raffle_id)
                 bili_statistics.add2pushed_raffles('Yj协同大航海', 2)
         except Exception:
             printer.warn(f'Yj监控房间内可能有恶意干扰{uid}: {ori}')
     return True
def optimize_inittemp_and_alpha(opt_run_id, quantifier_type, alpha_domain,
                                initial_temperature_domain, threshold,
                                num_simulations_in_each_batch,
                                run_batch_kwargs):
    info('Starting grid optimization')
    with open(opt_output_path(opt_run_id, quantifier_type, 'opt_args.tsv'),
              'w') as f_opt_args:
        f_opt_args.write(pprint.pformat(locals(), indent=4))
    eval_path = opt_output_path(opt_run_id, quantifier_type, 'grid_eval.csv')
    with open(eval_path, 'w') as f_results:
        f_results.write(
            'evaluation,qunatifier,alpha,initial_temperature,threshold\n')
        for alpha, init_temp in itertools.product(alpha_domain,
                                                  initial_temperature_domain):
            f_value = f_inittemp_and_alpha(
                alpha=alpha,
                initial_temperature=init_temp,
                quantifier_type=quantifier_type,
                threshold=threshold,
                num_simulations=num_simulations_in_each_batch,
                run_batch_kwargs=run_batch_kwargs)
            f_results.write(','.join(
                map(str,
                    [f_value, quantifier_type, alpha, init_temp, threshold])) +
                            '\n')
    info('Finished grid optimization')
    return eval_path
Esempio n. 30
0
def tchain(spec, cacheSizeMB=None):
    chain = r.TChain(spec["treeName"])
    for fileName in spec["fileNames"]:
        chain.Add(fileName)

    if cacheSizeMB:
        chain.SetCacheSize(cacheSizeMB * 1024**2)

    if spec["treeName"] == "Events":  # CMS CDAQ
        chain.SetBranchStatus("*", 0)
        found = False
        for branch in spec["rawCollections"]:
            if sw.use_fwlite:
                for suffix in [".obj", ".present"]:
                    branch1 = branch + suffix
                    if chain.GetBranch(branch1):
                        chain.SetBranchStatus(branch1, 1)
                        found = True
                    else:
                        printer.info("Could not find branch %s" % branch1)
            else:
                if chain.GetBranch(branch):
                    chain.SetBranchStatus(branch, 1)
                    found = True
                else:
                    printer.info("Could not find branch %s" % branch)
            if found:
                spec["rawCollection"] = branch
                break

        if not found:
            sys.exit("Could not find any branches: see configuration/sw.py")

    return chain
Esempio n. 31
0
    async def _read_datas(self):
        while True:
            datas = await self._read_bytes()
            # 本函数对bytes进行相关操作,不特别声明,均为bytes
            if datas is None:
                return
            data_l = 0
            len_datas = len(datas)
            while data_l != len_datas:
                # 每片data都分为header和body,data和data可能粘连
                # data_l == header_l && next_data_l == next_header_l
                # ||header_l...header_r|body_l...body_r||next_data_l...
                tuple_header = self.structer.unpack_from(datas[data_l:])
                len_data, len_header, _, opt, _ = tuple_header
                body_l = data_l + len_header
                next_data_l = data_l + len_data
                body = datas[body_l:next_data_l]
                # 人气值(或者在线人数或者类似)以及心跳
                if opt == 3:
                    # num_watching, = struct.unpack('!I', body)
                    # print(f'弹幕心跳检测{self._area_id}')
                    pass
                # cmd
                elif opt == 5:
                    if not self.handle_danmu(json.loads(body.decode('utf-8'))):
                        return
                # 握手确认
                elif opt == 8:
                    printer.info(
                        [f'{self._area_id}号弹幕监控进入房间({self._room_id})'], True)
                else:
                    printer.warn(f'弹幕数据错误:{datas}')
                    return

                data_l = next_data_l
Esempio n. 32
0
def printChannelSummary(outputFile):
    f = r.TFile(outputFile)

    hs = []
    for name in ["MatchedFibersCh%d" % i for i in range(3)] + ["MatchedTriggerTowers"]:
        h = f.Get(name)
        if h:
            hs.append(h)

    lines = []
    if hs:
        words = ["nMatched"]
        words += ["FibCh%d" % i for i in range(3)]
        words.append("  TPs")
        words = "   ".join(words)
        lines.append(words)
        lines.append("-" * len(words))
    else:
        return

    for iBin in range(0, 2 + hs[0].GetNbinsX()):
        xs = [h.GetBinCenter(iBin) for h in hs]
        if len(set(xs)) != 1:
            printer.error("binnings for nMatched do not match.")
            return

        for h in hs:
            w = h.GetBinWidth(iBin)
            if 1.0e-6 < abs(w - 1.0):
                printer.warning("Histogram %s bin %d has width %g" % (h.GetName(), iBin, w))

        x = xs[0]
        cs = [h.GetBinContent(iBin) for h in hs]

        if any(cs):
            s = ["   %4d" % x]
            s += ["%4d" % c for c in cs]
            lines.append("     ".join(s))

    if len(lines) > 12:
        printer.info("suppressed printing of match histogram (more than 10 different occupancies)")
    else:
        for line in lines:
            print(line)

    f.Close()
Esempio n. 33
0
def check_and_adjust(options):
    if not all([options.file1, options.feds1]):
        sys.exit("--file1 and --feds1 are required (see './oneRun.py --help').")
    if not options.outputFile.endswith(".root"):
        sys.exit("--output-file must end with .root (%s)" % options.outputFile)
    if 0 <= options.sparseLoop:
        if options.file2:
            sys.exit("--sparse-loop does not work with --file2")
        if options.nEventsSkip:
            sys.exit("--sparse-loop does not work with --nevents-skip")
    if options.feds2 and (not options.file2) and (not options.noLoop):
        # print "INFO: using --file1 also for --file2; also using identity map"
        options.file2 = options.file1
        options.identityMap = True

    if options.patterns:
        printer.info("setting nEvents=1 (--patterns was passed)")
        options.nEvents = 1
Esempio n. 34
0
def pruneFeds(chain, s):
    wargs = {}

    remove = {}
    for fedId in s["fedIds"]:
        wargs[fedId] = {"tree": chain}
        if s["treeName"] == "Events":  # CMS CDAQ
            wfunc = wordsOneFed
            wargs[fedId].update({"fedId": fedId,
                                 "collection": s["rawCollection"],
                                 "product": sw.use_fwlite})
        elif s["treeName"] == "CMSRAW":  # HCAL local
            wfunc = wordsOneChunk
            wargs[fedId]["branch"] = s["branch"](fedId)
        else:
            wfunc = wordsOneBranch
            wargs[fedId]["branch"] = s["branch"](fedId)

        raw = wfunc(**wargs[fedId])
        if not raw:
            remove[fedId] = "no branch %s" % wargs[fedId].get("branch")
        elif not raw.size():
            remove[fedId] = "read zero bytes"

    for fedId, msg in sorted(remove.items()):
        del wargs[fedId]
        # printer.warning("removing FED %4d from spec (%s)." % (fedId, msg))
    if remove:
        printer.info("No data from FED%s %s" % ("s" if 2 <= len(remove) else "", utils.shortList(remove.keys())))

    if wargs:
        del s["fedIds"]
        s["fedId0"] = sorted(wargs.keys())[0]
        for v in ["wfunc", "wargs"]:
            s[v] = eval(v)
    elif s["treeName"] == "Events":
        sys.exit("No listed FEDs had any data.")
    else:
        sys.exit(branches(chain))
Esempio n. 35
0
def check_and_adjust(options):
    if not all([options.file1, options.feds1]):
        sys.exit("--file1 and --feds1 are required (see './oneRun.py --help').")
    if not options.outputFile.endswith(".root"):
        sys.exit("--output-file must end with .root (%s)" % options.outputFile)
    if 0 <= options.sparseLoop:
        if options.file2:
            sys.exit("--sparse-loop does not work with --file2")
        if options.nEventsSkip:
            sys.exit("--sparse-loop does not work with --nevents-skip")
    if options.feds2 and (not options.file2) and (not options.noLoop):
        # print "INFO: using --file1 also for --file2; also using identity map"
        options.file2 = options.file1
        options.identityMap = True

    options.plugins = options.plugins.split(",")
    if "patterns" in options.plugins:
        printer.info("setting nEvents=1 ('patterns' was in list of plugins)")
        options.nEvents = 1

    if 1 <= options.dump and "printraw" not in options.plugins:
        printer.info("adding printraw to list of plugins ('--dump' was at least 1)")
        options.plugins.append("printraw")