Ejemplo n.º 1
0
def main():
    args = parse_args()
    timeout = args.timeout

    adb_bin = get_adb_tool()
    if use_monitor:
        os.system("{0} connect 127.0.0.1:62001".format(adb_bin))

    check_screenshot(filename="screenshot.png", directory=data_directory)

    std_pipe = ProcessStdout()
    ## start to sync qa to cloud
    sync_job = threading.Thread(target=sync_data_daemon,
                                args=(std_pipe.queue, ))
    sync_job.daemon = True
    sync_job.start()

    ## spaw baidu count
    baidu_queue = Queue(5)
    baidu_search_job = multiprocessing.Process(target=baidu_count_daemon,
                                               args=(baidu_queue,
                                                     std_pipe.queue, timeout))
    baidu_search_job.daemon = True
    baidu_search_job.start()

    ## spaw crawler
    knowledge_queue = Queue(5)
    knowledge_craw_job = multiprocessing.Process(target=crawler_daemon,
                                                 args=(knowledge_queue,
                                                       std_pipe.queue))
    knowledge_craw_job.daemon = True
    knowledge_craw_job.start()

    ## output threading
    output_job = threading.Thread(target=std_pipe.run_forever)
    output_job.daemon = True
    output_job.start()

    if enable_chrome:
        closer = Event()
        noticer = Event()
        noticer.clear()
        reader, writer = Pipe()
        browser_daemon = multiprocessing.Process(target=run_browser,
                                                 args=(
                                                     closer,
                                                     noticer,
                                                     reader,
                                                 ))
        browser_daemon.daemon = True
        browser_daemon.start()

    def __inner_job():
        start = time.time()
        image_binary = analyze_current_screen_text(
            directory=data_directory,
            compress_level=image_compress_level[0],
            crop_area=crop_areas[game_type])
        if not image_binary:
            print("do not detect question and answers")
            return

        keywords = get_text_from_image(image_data=image_binary,
                                       timeout=timeout)
        if not keywords:
            print("text not recognize")
            return

        true_flag, real_question, question, answers = parse_question_and_answer(
            keywords)

        ### parse for answer
        answers = map(lambda a: a.rsplit(":")[-1], answers)
        answers = list(map(lambda a: a.rsplit(".")[-1], answers))

        std_pipe.write(
            stdout_template.QUESTION_TPL.format(real_question,
                                                "\n".join(answers)))

        # notice baidu and craw
        baidu_queue.put((question, answers, true_flag))
        knowledge_queue.put(question)
        if enable_chrome:
            writer.send(question)
            noticer.set()

        end = time.time()
        std_pipe.write(stdout_template.TIME_CONSUME_TPL.format(end - start))
        save_screen(directory=data_directory)
        save_question_answers_to_file(real_question,
                                      answers,
                                      directory=data_directory)

    prompt_message()
    while True:
        enter = input("按Enter键开始,切换游戏请输入s,按ESC键退出...\n")
        if enter == chr(27):
            break
        if enter == 's':
            prompt_message()
        try:
            __inner_job()
        except Exception as e:
            logger.error(str(e), exc_info=True)

    print("欢迎下次使用")
    if enable_chrome:
        reader.close()
        writer.close()
        closer.set()
        time.sleep(3)
Ejemplo n.º 2
0
def main():
    args = parse_args()
    timeout = args.timeout

    adb_bin = get_adb_tool()
    if use_monitor:
        os.system("{0} connect 127.0.0.1:62001".format(adb_bin))

    check_screenshot(filename="screenshot.png", directory=data_directory)

    # stdout_queue = Queue(10)
    # ## spaw baidu count
    # baidu_queue = Queue(5)
    # baidu_search_job = multiprocessing.Process(target=baidu_count_daemon,
    #                                            args=(baidu_queue, stdout_queue, timeout))
    # baidu_search_job.daemon = True
    # baidu_search_job.start()
    #
    # ## spaw crawler
    # knowledge_queue = Queue(5)
    # knowledge_craw_job = multiprocessing.Process(target=crawler_daemon,
    #                                              args=(knowledge_queue, stdout_queue))
    # knowledge_craw_job.daemon = True
    # knowledge_craw_job.start()
    #
    # ## output threading
    # output_job = threading.Thread(target=print_terminal, args=(stdout_queue,))
    # output_job.daemon = True
    # output_job.start()

    if enable_chrome:
        closer = Event()
        noticer = Event()
        noticer.clear()
        reader, writer = Pipe()
        browser_daemon = multiprocessing.Process(target=run_browser,
                                                 args=(
                                                     closer,
                                                     noticer,
                                                     reader,
                                                 ))
        browser_daemon.daemon = True
        browser_daemon.start()

    def __inner_job():
        start = time.time()
        text_binary = analyze_current_screen_text(
            directory=data_directory,
            compress_level=image_compress_level[0],
            crop_area=crop_areas[game_type],
            use_monitor=use_monitor)
        keywords = get_text_from_image(image_data=text_binary, timeout=timeout)
        if not keywords:
            print("text not recognize")
            return

        true_flag, real_question, question, answers = parse_question_and_answer(
            keywords)

        if game_type == "UC答题":
            answers = map(lambda a: a.rsplit(":")[-1], answers)

        print("~" * 60)
        print("{0}\n{1}".format(real_question, "\n".join(answers)))
        print("~" * 60)

        # ### refresh question
        # stdout_queue.put({
        #     "type": 0,
        #     "data": "{0}\n{1}".format(question, "\n".join(answers))
        # })
        #
        # # notice baidu and craw
        # baidu_queue.put((
        #     question, answers, true_flag
        # ))
        # knowledge_queue.put(question)

        if enable_chrome:
            writer.send(question)
            noticer.set()

        summary = baidu_count(question, answers, timeout=timeout)
        summary_li = sorted(summary.items(),
                            key=operator.itemgetter(1),
                            reverse=True)
        if true_flag:
            recommend = "{0}\n{1}".format(
                "肯定回答(**): {0}".format(summary_li[0][0]),
                "否定回答(  ): {0}".format(summary_li[-1][0]))
        else:
            recommend = "{0}\n{1}".format(
                "肯定回答(  ): {0}".format(summary_li[0][0]),
                "否定回答(**): {0}".format(summary_li[-1][0]))
        print("*" * 60)
        print("\n".join(
            map(lambda item: "{0}: {1}".format(item[0], item[1]), summary_li)))
        print(recommend)
        print("*" * 60)

        ans = kwquery(real_question)
        print("-" * 60)
        print(wrap(" ".join(ans), 60))
        print("-" * 60)

        end = time.time()
        # stdout_queue.put({
        #     "type": 3,
        #     "data": "use {0} 秒".format(end - start)
        # })
        print("use {0} 秒".format(end - start))
        save_screen(directory=data_directory)
        time.sleep(1)

    print("""
            请选择答题节目:
              1. 百万英雄
              2. 冲顶大会
              3. 芝士超人
              4. UC答题
            """)
    game_type = input("输入节目序号: ")
    if game_type == "1":
        game_type = '百万英雄'
    elif game_type == "2":
        game_type = '冲顶大会'
    elif game_type == "3":
        game_type = "芝士超人"
    elif game_type == "4":
        game_type = "UC答题"
    else:
        game_type = '百万英雄'

    while True:
        enter = input("按Enter键开始,按ESC键退出...")
        if enter == chr(27):
            break
        try:
            clear_screen()
            __inner_job()
        except Exception as e:
            import traceback

            traceback.print_exc()
            print(str(e))

    print("欢迎下次使用")
    if enable_chrome:
        reader.close()
        writer.close()
        closer.set()
        time.sleep(3)
Ejemplo n.º 3
0
def main():
    args = parse_args()
    timeout = args.timeout

    ## start crawler
    # crawler_noticer = Event()
    # crawler_noticer.clear()
    # result_noticer = Event()
    # result_noticer.clear()
    # qreader, qwriter = Pipe()
    # stdreader, stdwriter = Pipe()
    # crawler = multiprocessing.Process(
    #     target=crawler_daemon,
    #     args=(crawler_noticer, qreader, result_noticer, stdwriter)
    # )
    # crawler.daemon = True
    # crawler.start()

    adb_bin = get_adb_tool()
    if use_monitor:
        os.system("{0} connect 127.0.0.1:62001".format(adb_bin))

    check_screenshot(filename="screenshot.png", directory=data_directory)

    if enable_chrome:
        closer = Event()
        noticer = Event()
        closer.clear()
        noticer.clear()
        reader, writer = Pipe()
        browser_daemon = multiprocessing.Process(target=run_browser,
                                                 args=(
                                                     closer,
                                                     noticer,
                                                     reader,
                                                 ))
        browser_daemon.daemon = True
        browser_daemon.start()

    def __inner_job():
        start = time.time()
        text_binary = analyze_current_screen_text(
            directory=data_directory,
            compress_level=image_compress_level[0],
            crop_area=crop_areas[game_type],
            use_monitor=use_monitor)
        keywords = get_text_from_image(image_data=text_binary, )
        if not keywords:
            print("text not recognize")
            return

        true_flag, real_question, question, answers = parse_question_and_answer(
            keywords)

        ## notice crawler to work
        # qwriter.send(real_question.strip("?"))
        # crawler_noticer.set()

        print('-' * 72)
        print(real_question)
        print('-' * 72)
        print("\n".join(answers))

        # notice browser
        if enable_chrome:
            writer.send(question)
            noticer.set()

        search_question = pre_process_question(question)
        summary = baidu_count(search_question, answers, timeout=timeout)
        summary_li = sorted(summary.items(),
                            key=operator.itemgetter(1),
                            reverse=True)
        data = [("选项", "同比")]
        for a, w in summary_li:
            data.append((a, w))
        table = AsciiTable(data)
        print(table.table)

        print("*" * 72)
        if true_flag:
            print("肯定回答(**): ", summary_li[0][0])
            print("否定回答(  ): ", summary_li[-1][0])
        else:
            print("肯定回答(  ): ", summary_li[0][0])
            print("否定回答(**): ", summary_li[-1][0])
        print("*" * 72)

        # try crawler
        # retry = 4
        # while retry:
        #     if result_noticer.is_set():
        #         print("~" * 60)
        #         print(stdreader.recv())
        #         print("~" * 60)
        #         break
        #     retry -= 1
        #     time.sleep(1)
        # result_noticer.clear()

        print("~" * 60)
        print(kwquery(real_question.strip("?")))
        print("~" * 60)

        end = time.time()
        print("use {0} 秒".format(end - start))
        save_screen(directory=data_directory)

    print("""
    请选择答题节目:
      1. 百万英雄
      2. 冲顶大会
      3. 芝士超人
    """)
    game_type = input("输入节目序号: ")
    if game_type == "1":
        game_type = '百万英雄'
    elif game_type == "2":
        game_type = '冲顶大会'
    elif game_type == "3":
        game_type = "芝士超人"
    else:
        game_type = '百万英雄'

    while True:
        print("""
    请在答题开始前就运行程序,
    答题开始的时候按Enter预测答案
                """)

        print("当前选择答题游戏: {}\n".format(game_type))

        enter = input("按Enter键开始,按ESC键退出...")
        if enter == chr(27):
            break
        try:
            __inner_job()
        except Exception as e:
            print(str(e))

        print("欢迎下次使用")

    if enable_chrome:
        reader.close()
        writer.close()
        closer.set()
        time.sleep(3)
Ejemplo n.º 4
0
def main():
    args = parse_args()
    timeout = args.timeout

    adb_bin = get_adb_tool()
    if use_monitor:
        os.system("{0} connect 127.0.0.1:62001".format(adb_bin))

    check_screenshot(filename="screenshot.png", directory=data_directory)

    std_pipe = ProcessStdout()
    sync_job = threading.Thread(target=sync_data_daemon, args=(std_pipe.queue,))
    sync_job.daemon = True
    sync_job.start()

    if enable_chrome:
        closer = Event()
        noticer = Event()
        noticer.clear()
        reader, writer = Pipe()
        browser_daemon = multiprocessing.Process(
            target=run_browser, args=(closer, noticer, reader,))
        browser_daemon.daemon = True
        browser_daemon.start()

    def __inner_job():
        start = time.time()
        image_binary = analyze_current_screen_text(
            directory=data_directory,
            compress_level=image_compress_level[0],
            crop_area=crop_areas[game_type]
        )
        if not image_binary:
            print("do not detect question and answers")
            return

        keywords = get_text_from_image(
            image_data=image_binary,
            timeout=timeout
        )
        if not keywords:
            print("text not recognize")
            return

        true_flag, real_question, question, answers = parse_question_and_answer(keywords)

        ### parse for answer
        answers = map(lambda a: a.rsplit(":")[-1], answers)
        answers = list(map(lambda a: a.rsplit(".")[-1], answers))

        print("~" * 60)
        print("{0}\n{1}".format(real_question, "\n".join(answers)))
        print("~" * 60)

        if enable_chrome:
            writer.send(question)
            noticer.set()

        summary = baidu_count(question, answers, timeout=timeout)
        summary_li = sorted(summary.items(), key=operator.itemgetter(1), reverse=True)

        if true_flag:
            recommend = "{0}\n{1}".format(
                "肯定回答(**): {0}".format(summary_li[0][0]),
                "否定回答(  ): {0}".format(summary_li[-1][0]))
        else:
            recommend = "{0}\n{1}".format(
                "肯定回答(  ): {0}".format(summary_li[0][0]),
                "否定回答(**): {0}".format(summary_li[-1][0]))
        print("*" * 60)
        print(recommend)
        print("*" * 60)

        ans = kwquery(real_question)
        print("-" * 60)
        print(wrap(" ".join(ans), 60))
        print("-" * 60)

        end = time.time()
        print("use {0} 秒".format(end - start))

        save_screen(directory=data_directory)
        save_question_answers_to_file(real_question, answers, directory=data_directory)

    prompt_message()
    while True:
        enter = input("按Enter键开始,切换游戏请输入s,按ESC键退出...\n")
        if enter == chr(27):
            break
        if enter == 's':
            prompt_message()
        try:
            __inner_job()
        except Exception as e:
            logger.error(str(e), exc_info=True)

    print("欢迎下次使用")
    if enable_chrome:
        reader.close()
        writer.close()
        closer.set()
        time.sleep(3)
Ejemplo n.º 5
0
def main():
    args = parse_args()
    timeout = args.timeout

    adb_bin = get_adb_tool()
    if use_monitor:
        os.system("{0} connect 127.0.0.1:62001".format(adb_bin))

    check_screenshot(filename="screenshot.png", directory=data_directory)

    std_pipe = ProcessStdout()
    ## start to sync qa to cloud
    sync_job = threading.Thread(target=sync_data_daemon, args=(std_pipe.queue,))
    sync_job.daemon = True
    sync_job.start()

    ## spaw baidu count
    baidu_queue = Queue(5)
    baidu_search_job = multiprocessing.Process(target=baidu_count_daemon,
                                               args=(baidu_queue, std_pipe.queue, timeout))
    baidu_search_job.daemon = True
    baidu_search_job.start()

    ## spaw crawler
    knowledge_queue = Queue(5)
    knowledge_craw_job = multiprocessing.Process(target=crawler_daemon,
                                                 args=(knowledge_queue, std_pipe.queue))
    knowledge_craw_job.daemon = True
    knowledge_craw_job.start()

    ## output threading
    output_job = threading.Thread(target=std_pipe.run_forever)
    output_job.daemon = True
    output_job.start()

    if enable_chrome:
        closer = Event()
        noticer = Event()
        noticer.clear()
        reader, writer = Pipe()
        browser_daemon = multiprocessing.Process(
            target=run_browser, args=(closer, noticer, reader,))
        browser_daemon.daemon = True
        browser_daemon.start()

    def __inner_job():
        start = time.time()
        image_binary = analyze_current_screen_text(
            directory=data_directory,
            compress_level=image_compress_level[0],
            crop_area=crop_areas[game_type]
        )
        if not image_binary:
            print("do not detect question and answers")
            return

        keywords = get_text_from_image(
            image_data=image_binary,
            timeout=timeout
        )
        if not keywords:
            print("text not recognize")
            return

        true_flag, real_question, question, answers = parse_question_and_answer(keywords)

        ### parse for answer
        answers = map(lambda a: a.rsplit(":")[-1], answers)
        answers = list(map(lambda a: a.rsplit(".")[-1], answers))

        std_pipe.write(stdout_template.QUESTION_TPL.format(real_question, "\n".join(answers)))

        # notice baidu and craw
        baidu_queue.put((
            question, answers, true_flag
        ))
        knowledge_queue.put(question)
        if enable_chrome:
            writer.send(question)
            noticer.set()

        end = time.time()
        std_pipe.write(stdout_template.TIME_CONSUME_TPL.format(end - start))
        save_screen(directory=data_directory)
        save_question_answers_to_file(real_question, answers, directory=data_directory)

    prompt_message()
    while True:
        enter = input("按Enter键开始,切换游戏请输入s,按ESC键退出...\n")
        if enter == chr(27):
            break
        if enter == 's':
            prompt_message()
        try:
            __inner_job()
        except Exception as e:
            logger.error(str(e), exc_info=True)

    print("欢迎下次使用")
    if enable_chrome:
        reader.close()
        writer.close()
        closer.set()
        time.sleep(3)