Exemplo n.º 1
0
def fast_post(
        api: object,
        board: str,
        title: str,
        content: str,
        post_type: int,
        sign_file) -> None:
    api._goto_board(board)

    cmd_list = list()
    cmd_list.append(command.Ctrl_P)
    cmd_list.append(str(post_type))
    cmd_list.append(command.Enter)
    cmd_list.append(str(title))
    cmd_list.append(command.Enter)
    cmd_list.append(str(content))
    cmd_list.append(command.Ctrl_X)
    cmd = ''.join(cmd_list)

    target_list = [
        connect_core.TargetUnit(
            i18n.HasPostPermission,
            '發表文章於【',
            break_detect=True,
        ),
        connect_core.TargetUnit(
            i18n.NoPermission,
            '使用者不可發言',
            break_detect=True,
        ),
        connect_core.TargetUnit(
            i18n.NoPermission,
            '無法發文: 未達看板要求權限',
            break_detect=True
        ),
        connect_core.TargetUnit(
            i18n.AnyKeyContinue,
            '任意鍵繼續',
            break_detect=True,
        ),
        connect_core.TargetUnit(
            i18n.SaveFile,
            '確定要儲存檔案嗎',
            response='s' + command.Enter,
        ),
        connect_core.TargetUnit(
            i18n.SelectSignature,
            'x=隨機',
            response=str(sign_file) + command.Enter,
        ),
    ]
    index = api.connect_core.fast_send(cmd, target_list)
    if index < 0:
        screens.show(api.config, api.connect_core.get_screen_queue())
        raise exceptions.UnknownError(i18n.UnknownError)
    if index == 1 or index == 2:
        raise exceptions.NoPermission(i18n.NoPermission)
Exemplo n.º 2
0
def push(api, board: str, push_type: int, push_content: str, post_aid: str,
         post_index: int) -> None:
    api._goto_board(board)

    cmd_list = list()

    if post_aid is not None:
        cmd_list.append('#' + post_aid)
    elif post_index != 0:
        cmd_list.append(str(post_index))
    cmd_list.append(command.Enter)
    cmd_list.append(command.Push)

    cmd = ''.join(cmd_list)

    target_list = [
        connect_core.TargetUnit(i18n.HasPushPermission,
                                '您覺得這篇',
                                log_level=log.level.DEBUG,
                                break_detect=True),
        connect_core.TargetUnit(i18n.OnlyArrow,
                                '加註方式',
                                log_level=log.level.DEBUG,
                                break_detect=True),
        connect_core.TargetUnit(i18n.NoFastPush,
                                '禁止快速連續推文',
                                log_level=log.level.INFO,
                                break_detect=True,
                                exceptions_=exceptions.NoFastPush()),
        connect_core.TargetUnit(i18n.NoFastPush,
                                '禁止短時間內大量推文',
                                log_level=log.level.INFO,
                                break_detect=True,
                                exceptions_=exceptions.NoFastPush()),
        connect_core.TargetUnit(i18n.NoPermission,
                                '使用者不可發言',
                                log_level=log.level.INFO,
                                break_detect=True,
                                exceptions_=exceptions.NoPermission(
                                    i18n.NoPermission)),
        connect_core.TargetUnit(i18n.NoPush,
                                '◆ 抱歉, 禁止推薦',
                                log_level=log.level.INFO,
                                break_detect=True,
                                exceptions_=exceptions.NoPush()),
    ]

    index = api.connect_core.send(cmd, target_list)

    if index == -1:
        if post_aid is not None:
            raise exceptions.NoSuchPost(board, post_aid)
        else:
            raise exceptions.NoSuchPost(board, post_index)

    cmd_list = list()

    if index == 0:
        push_option_line = api.connect_core.get_screen_queue()[-1]
        push_option_line = push_option_line.split('\n')[-1]
        log.show_value(api.config, log.level.DEBUG, 'Push option line',
                       push_option_line)

        enable_push = '值得推薦' in push_option_line
        enable_boo = '給它噓聲' in push_option_line
        enable_arrow = '只加→註解' in push_option_line

        log.show_value(api.config, log.level.DEBUG, 'Push', enable_push)
        log.show_value(api.config, log.level.DEBUG, 'Boo', enable_boo)
        log.show_value(api.config, log.level.DEBUG, 'Arrow', enable_arrow)

        if push_type == data_type.push_type.PUSH and not enable_push:
            push_type = data_type.push_type.ARROW
        elif push_type == data_type.push_type.BOO and not enable_boo:
            push_type = data_type.push_type.ARROW
        elif push_type == data_type.push_type.ARROW and not enable_arrow:
            push_type = data_type.push_type.PUSH

        cmd_list.append(str(push_type))
    # elif index == 1:
    #     push_type = data_type.push_type.ARROW

    cmd_list.append(push_content)
    cmd_list.append(command.Enter)
    cmd_list.append('y')
    cmd_list.append(command.Enter)

    cmd = ''.join(cmd_list)

    target_list = [
        connect_core.TargetUnit([
            i18n.Push,
            i18n.Success,
        ],
                                screens.Target.InBoard,
                                break_detect=True,
                                log_level=log.level.DEBUG),
    ]

    api.connect_core.send(cmd, target_list)
Exemplo n.º 3
0
def get_board_info(
        api,
        board: str,
        get_post_kind: bool,
        get_board_limit: bool,
        call_by_others: bool) -> None:
    if call_by_others:
        log_level = log.level.DEBUG
    else:
        log_level = log.level.INFO

    api._goto_board(board, refresh=True)

    ori_screen = api.connect_core.get_screen_queue()[-1]

    nuser = None
    for line in ori_screen.split('\n'):
        if '編號' not in line:
            continue
        if '日 期' not in line:
            continue
        if '人氣' not in line:
            continue

        nuser = line
        break

    if nuser is None:
        raise exceptions.NoSuchBoard(api.config, board)

    # print('------------------------')
    # print('nuser', nuser)
    # print('------------------------')
    if '[靜]' in nuser:
        online_user = 0
    else:
        if '編號' not in nuser or '人氣' not in nuser:
            raise exceptions.NoSuchBoard(api.config, board)
        pattern = re.compile('[\d]+')
        r = pattern.search(nuser)
        if r is None:
            raise exceptions.NoSuchBoard(api.config, board)
        # 減一是把自己本身拿掉
        online_user = int(r.group(0)) - 1

    log.show_value(
        api.config,
        log.level.DEBUG,
        '人氣',
        online_user)

    target_list = [
        connect_core.TargetUnit(
            i18n.ReadingBoardInfo,
            '任意鍵繼續',
            break_detect=True,
            log_level=log_level
        ),
    ]

    api.connect_core.send(
        'i',
        target_list)

    ori_screen = api.connect_core.get_screen_queue()[-1]
    # print(ori_screen)

    p = re.compile('《(.+)》看板設定')
    r = p.search(ori_screen)
    if r is not None:
        boardname = r.group(0)[1:-5].strip()
    log.show_value(
        api.config,
        log.level.DEBUG,
        '看板名稱',
        boardname)

    if boardname.lower() != board.lower():
        raise exceptions.NoSuchBoard(api.config, board)

    p = re.compile('中文敘述: (.+)')
    r = p.search(ori_screen)
    if r is not None:
        chinese_des = r.group(0)[5:].strip()
    log.show_value(
        api.config,
        log.level.DEBUG,
        '中文敘述',
        chinese_des)

    p = re.compile('板主名單: (.+)')
    r = p.search(ori_screen)
    if r is not None:
        moderator_line = r.group(0)[5:].strip()
        if '(無)' in moderator_line:
            moderators = list()
        else:
            moderators = moderator_line.split('/')
            for moderator in moderators.copy():
                check = True
                for c in moderator:
                    if len(c.encode('big5')) > 1:
                        check = False
                        break
                if not check:
                    moderators.remove(moderator)

    log.show_value(
        api.config,
        log.level.DEBUG,
        '板主名單',
        moderators)

    open_status = ('公開狀態(是否隱形): 公開' in ori_screen)
    log.show_value(
        api.config,
        log.level.DEBUG,
        '公開狀態',
        open_status)

    into_top_ten_when_hide = (
            '隱板時 可以 進入十大排行榜' in ori_screen)
    log.show_value(
        api.config,
        log.level.DEBUG,
        '隱板時可以進入十大排行榜',
        into_top_ten_when_hide)

    non_board_members_post = ('開放 非看板會員發文' in ori_screen)
    log.show_value(
        api.config,
        log.level.DEBUG,
        '非看板會員發文',
        non_board_members_post)

    reply_post = ('開放 回應文章' in ori_screen)
    log.show_value(
        api.config,
        log.level.DEBUG,
        '回應文章',
        reply_post)

    self_del_post = ('開放 自刪文章' in ori_screen)
    log.show_value(
        api.config,
        log.level.DEBUG,
        '自刪文章',
        self_del_post)

    push_post = ('開放 推薦文章' in ori_screen)
    log.show_value(
        api.config,
        log.level.DEBUG,
        '推薦文章',
        push_post)

    boo_post = ('開放 噓文' in ori_screen)
    log.show_value(
        api.config,
        log.level.DEBUG,
        '噓文',
        boo_post)

    # 限制 快速連推文章, 最低間隔時間: 5 秒
    # 開放 快速連推文章

    fast_push = ('開放 快速連推文章' in ori_screen)
    log.show_value(
        api.config,
        log.level.DEBUG,
        '快速連推文章',
        fast_push)

    if not fast_push:
        p = re.compile('最低間隔時間: [\d]+')
        r = p.search(ori_screen)
        if r is not None:
            min_interval = r.group(0)[7:].strip()
            min_interval = int(min_interval)
        else:
            min_interval = 0
        log.show_value(
            api.config,
            log.level.DEBUG,
            '最低間隔時間',
            min_interval)
    else:
        min_interval = 0

    # 推文時 自動 記錄來源 IP
    # 推文時 不會 記錄來源 IP
    push_record_ip = ('推文時 自動 記錄來源 IP' in ori_screen)
    log.show_value(
        api.config,
        log.level.DEBUG,
        '記錄來源 IP',
        push_record_ip)

    # 推文時 對齊 開頭
    # 推文時 不用對齊 開頭
    push_aligned = ('推文時 對齊 開頭' in ori_screen)
    log.show_value(
        api.config,
        log.level.DEBUG,
        '對齊開頭',
        push_aligned)

    # 板主 可 刪除部份違規文字
    moderator_can_del_illegal_content = (
            '板主 可 刪除部份違規文字' in ori_screen)
    log.show_value(
        api.config,
        log.level.DEBUG,
        '板主可刪除部份違規文字',
        moderator_can_del_illegal_content)

    # 轉錄文章 會 自動記錄,且 需要 發文權限
    tran_post_auto_recorded_and_require_post_permissions = (
            '轉錄文章 會 自動記錄,且 需要 發文權限' in ori_screen)
    log.show_value(
        api.config,
        log.level.DEBUG,
        '轉錄文章 會 自動記錄,且 需要 發文權限',
        tran_post_auto_recorded_and_require_post_permissions)

    cool_mode = (
            '未 設為冷靜模式' not in ori_screen)
    log.show_value(
        api.config,
        log.level.DEBUG,
        '冷靜模式',
        cool_mode)

    require18 = (
            '禁止 未滿十八歲進入' in ori_screen)

    log.show_value(
        api.config,
        log.level.DEBUG,
        '禁止未滿十八歲進入',
        require18)

    p = re.compile('登入次數 [\d]+ 次以上')
    r = p.search(ori_screen)
    if r is not None:
        require_login_time = r.group(0).split(' ')[1]
        require_login_time = int(require_login_time)
    else:
        require_login_time = 0
    log.show_value(
        api.config,
        log.level.DEBUG,
        '發文限制登入次數',
        require_login_time)

    p = re.compile('退文篇數 [\d]+ 篇以下')
    r = p.search(ori_screen)
    if r is not None:
        require_illegal_post = r.group(0).split(' ')[1]
        require_illegal_post = int(require_illegal_post)
    else:
        require_illegal_post = 0
    log.show_value(
        api.config,
        log.level.DEBUG,
        '發文限制退文篇數',
        require_illegal_post)

    kind_list = None
    login_limit = None
    retirement_limit = None

    if get_board_limit:

        api._goto_board(board)

        # Go certain board, then get board info
        cmd_list = list()
        cmd_list.append('I')
        cmd = ''.join(cmd_list)

        # If no certain limit return both zero
        target_list = [
            connect_core.TargetUnit(
                i18n.Done,
                '無特別限制',
                break_detect=True
            )
        ]

        index = api.connect_core.send(
            cmd,
            target_list)

        if not index == 0:
            # Regex to get limit
            ori_screen = api.connect_core.get_screen_queue()[-1]
            screen_lines = ori_screen.split('\n')

            for i in screen_lines:
                # regex login limit
                if '登入次數' in i and '次以上' in i:
                    type_pattern = re.compile('登入次數 (\d*) 次以上')
                    login_limit = type_pattern.findall(i)
                    
                    if not len(login_limit) == 0:
                        login_limit = int(login_limit[0])
                    else:
                        login_limit = 0

                # regex retirement limit
                if '退文篇數' in i and '篇以下' in i:
                    type_pattern = re.compile('退文篇數 (\d*) 篇以下')
                    retirement_limit = type_pattern.findall(i)

                    if not len(retirement_limit) == 0:
                        retirement_limit = int(retirement_limit[0])
                    else:
                        retirement_limit = 0

                if not (login_limit == None or retirement_limit == None):
                    break

            # Clear post status
            cmd_list = list()
            cmd_list.append(command.Ctrl_C)
            cmd_list.append(command.Ctrl_C)
            cmd = ''.join(cmd_list)

            target_list = [
                connect_core.TargetUnit(
                    i18n.Done,
                    screens.Target.InBoard,
                    break_detect=True
                )
            ]
            api.connect_core.send(
                cmd,
                target_list)
        else:
            login_limit = 0
            retirement_limit = 0

    if get_post_kind:

        api._goto_board(board)

        # Go certain board, then post to get post type info
        cmd_list = list()
        cmd_list.append(command.Ctrl_P)
        cmd = ''.join(cmd_list)

        target_list = [
            connect_core.TargetUnit(
                i18n.NoPermission,
                '無法發文: 未達看板要求權限',
                break_detect=True
            ),
            connect_core.TargetUnit(
                i18n.Done,
                '或不選)',
                break_detect=True
            )
        ]

        index = api.connect_core.send(
            cmd,
            target_list)

        if index == 0:
            raise exceptions.NoPermission(i18n.NoPermission)
            # no post permission

        ori_screen = api.connect_core.get_screen_queue()[-1]
        screen_lines = ori_screen.split('\n')

        for i in screen_lines:
            if '種類:' in i:
                type_pattern = re.compile('\d\.([^\ ]*)')
                # 0 is not present any type that the key hold None object
                kind_list = type_pattern.findall(i)
                break

        # Clear post status
        cmd_list = list()
        cmd_list.append(command.Ctrl_C)
        cmd_list.append(command.Ctrl_C)
        cmd = ''.join(cmd_list)

        target_list = [
            connect_core.TargetUnit(
                i18n.Done,
                screens.Target.InBoard,
                break_detect=True
            )
        ]
        api.connect_core.send(
            cmd,
            target_list)

    board_info = data_type.BoardInfo(
        boardname,
        online_user,
        chinese_des,
        moderators,
        open_status,
        into_top_ten_when_hide,
        non_board_members_post,
        reply_post,
        self_del_post,
        push_post,
        boo_post,
        fast_push,
        min_interval,
        push_record_ip,
        push_aligned,
        moderator_can_del_illegal_content,
        tran_post_auto_recorded_and_require_post_permissions,
        cool_mode,
        require18,
        require_login_time,
        require_illegal_post,
        kind_list,
        login_limit,
        retirement_limit)
    return board_info
Exemplo n.º 4
0
def post(
        api: object,
        board: str,
        title: str,
        content: str,
        post_type: int,
        sign_file) -> None:
    cmd_list = []
    cmd_list.append(command.GoMainMenu)
    cmd_list.append('qs')
    cmd_list.append(board)
    cmd_list.append(command.Enter)
    cmd_list.append(command.Ctrl_C * 2)
    cmd_list.append(command.Space)
    cmd_list.append(command.Ctrl_P)

    cmd = ''.join(cmd_list)

    target_list = [
        connect_core.TargetUnit(
            i18n.HasPostPermission,
            '發表文章於【',
            break_detect=True,
        ),
        connect_core.TargetUnit(
            i18n.NoPermission,
            '使用者不可發言',
            break_detect=True,
        )
    ]
    index = api.connect_core.send(cmd, target_list)
    if index < 0:
        screens.show(api.config, api.connect_core.get_screen_queue())
        raise exceptions.UnknownError(i18n.UnknownError)
    if index == 1:
        raise exceptions.NoPermission(i18n.NoPermission)

    screens.show(api.config, api.connect_core.get_screen_queue())

    cmd_list = []
    cmd_list.append(str(post_type))
    cmd_list.append(command.Enter)
    cmd_list.append(str(title))
    cmd_list.append(command.Enter)
    cmd_list.append(command.Ctrl_Y * 40)
    cmd_list.append(str(content))
    cmd_list.append(command.Ctrl_X)
    cmd = ''.join(cmd_list)

    target_list = [
        connect_core.TargetUnit(
            i18n.AnyKeyContinue,
            '任意鍵繼續',
            break_detect=True,
        ),
        connect_core.TargetUnit(
            i18n.SaveFile,
            '確定要儲存檔案嗎',
            response='s' + command.Enter,
        ),
        connect_core.TargetUnit(
            i18n.SelectSignature,
            'x=隨機',
            response=str(sign_file) + command.Enter,
        ),
    ]
    index = api.connect_core.send(
        cmd,
        target_list,
        screen_timeout=api.config.screen_post_timeout
    )
Exemplo n.º 5
0
def del_post(
        api,
        board_info,
        board,
        post_aid: str = None,
        post_index: int = 0) -> None:

    check_author = True
    for moderator in board_info.moderators:
        if api._ID.lower() == moderator.lower():
            check_author = False
            break

    post_info = api.get_post(board, post_aid=post_aid, post_index=post_index, query=True)
    if post_info.delete_status != data_type.post_delete_status.NOT_DELETED:
        if post_aid is not None:
            raise exceptions.DeletedPost(board, post_aid)
        else:
            raise exceptions.DeletedPost(board, post_index)

    if check_author:
        if api._ID.lower() != post_info.author.lower():
            raise exceptions.NoPermission(i18n.NoPermission)

    api._goto_board(board)

    cmd_list = list()

    if post_aid is not None:
        cmd_list.append('#' + post_aid)
    elif post_index != 0:
        cmd_list.append(str(post_index))
    cmd_list.append(command.Enter)
    cmd_list.append('d')

    cmd = ''.join(cmd_list)

    api.confirm = False

    def confirm_delete_handler(screen):
        api.confirm = True

    target_list = [
        connect_core.TargetUnit(
            i18n.AnyKeyContinue,
            '請按任意鍵繼續',
            response=' '),
        connect_core.TargetUnit(
            i18n.ConfirmDelete,
            '請確定刪除(Y/N)?[N]',
            response='y' + command.Enter,
            max_match=1,
            handler=confirm_delete_handler),
        connect_core.TargetUnit(
            i18n.DeleteSuccess,
            screens.Target.InBoard,
            break_detect=True),
    ]

    index = api.connect_core.send(
        cmd,
        target_list)

    # last_screen = api.connect_core.get_screen_queue()[-1]
    # print(api.confirm)
    # print(last_screen)
    # print(index)

    if index == 1:
        if not api.confirm:
            raise exceptions.NoPermission(i18n.NoPermission)

    if index == -1:
        if post_aid is not None:
            raise exceptions.NoSuchPost(board, post_aid)
        else:
            raise exceptions.NoSuchPost(board, post_index)