def select_files_add_menu(
        torrent_id: int) -> Tuple[str, telegram.InlineKeyboardMarkup]:
    SIZE_OF_LINE = 100
    KEYBORD_WIDTH = 5
    torrent = transClient.get_torrent(torrent_id)
    if len(torrent.name) >= SIZE_OF_LINE:
        name = f"{torrent.name[:SIZE_OF_LINE]}.."
    else:
        name = torrent.name
    text = f"*{escape_markdown(name, 2)}*\n"
    text += "Files:\n"
    column = 0
    row = 0
    file_keyboard: list[list[telegram.InlineKeyboardButton]] = [[]]
    for file_id, file in enumerate(torrent.files()):
        raw_name = file.name.split("/")
        filename = raw_name[1] if len(raw_name) == 2 else file.name
        if len(filename) >= SIZE_OF_LINE:
            filename = f"{filename[:SIZE_OF_LINE]}.."
        id = escape_markdown(f"{file_id+1}. ", 2)
        filename = escape_markdown(filename, 2, "PRE")
        file_size_raw = trans_utils.format_size(file.size)
        file_size = escape_markdown(
            f"{round(file_size_raw[0], 2)} {file_size_raw[1]}", 2)
        if column >= KEYBORD_WIDTH:
            file_keyboard.append([])
            column = 0
            row += 1
        if file.selected:
            text += f"*{id}*`{filename}`  {file_size}\n"
            button = telegram.InlineKeyboardButton(
                f"{file_id+1}. ✅",
                callback_data=f"fileselect_{torrent_id}_{file_id}_0",
            )
        else:
            text += f"*{id}*~{filename}~  {file_size}\n"
            button = telegram.InlineKeyboardButton(
                f"{file_id+1}. ❌",
                callback_data=f"fileselect_{torrent_id}_{file_id}_1",
            )
        column += 1
        file_keyboard[row].append(button)
    total_size = trans_utils.format_size(torrent.totalSize)
    size_when_done = trans_utils.format_size(torrent.sizeWhenDone)
    text += escape_markdown(
        f"Size to download: {round(size_when_done[0], 2)} {size_when_done[1]}"
        f" / {round(total_size[0], 2)} {total_size[1]}",
        2,
    )
    control_buttons = [
        [
            telegram.InlineKeyboardButton(
                "⏪Back",
                callback_data=f"addmenu_{torrent_id}",
            )
        ],
    ]
    reply_markup = telegram.InlineKeyboardMarkup(file_keyboard +
                                                 control_buttons)
    return text, reply_markup
def add_menu(torrent_id: int) -> Tuple[str, telegram.InlineKeyboardMarkup]:
    torrent = transClient.get_torrent(torrent_id)
    text = "🆕__Adding torrent__🆕\n"
    text += f"*{escape_markdown(torrent.name, 2)}*\n"
    size_in_bytes = transClient.free_space(DISK)
    total_size = trans_utils.format_size(torrent.totalSize)
    size_when_done = trans_utils.format_size(torrent.sizeWhenDone)
    raw_text = (
        f"Size to download: {round(size_when_done[0], 2)} {size_when_done[1]}"
        f" / {round(total_size[0], 2)} {total_size[1]}\n")
    if size_in_bytes is not None:
        free_memory = trans_utils.format_size(size_in_bytes)
        raw_text += f"Free disk space: {round(free_memory[0], 2)} {free_memory[1]}\n"
    else:
        raw_text += "Could not get free disk space\n"
    text += escape_markdown(raw_text, 2)
    reply_markup = telegram.InlineKeyboardMarkup([
        [
            telegram.InlineKeyboardButton(
                "📂Files",
                callback_data=f"selectfiles_{torrent_id}",
            )
        ],
        [
            telegram.InlineKeyboardButton(
                "▶️Start",
                callback_data=f"torrentadd_{torrent_id}_start",
            ),
            telegram.InlineKeyboardButton(
                "❌Cancel",
                callback_data=f"torrentadd_{torrent_id}_cancel",
            ),
        ],
    ])
    return text, reply_markup
Beispiel #3
0
 def testFormatSize(self):
     table = {
         512                : (512, 'B'),
         1024               : (1.0, 'KiB'),
         1048575            : (1023.999, 'KiB'),
         1048576            : (1.0, 'MiB'),
         1073741824         : (1.0, 'GiB'),
         1099511627776      : (1.0, 'TiB'),
         1125899906842624   : (1.0, 'PiB'),
         1152921504606846976: (1.0, 'EiB'),
     }
     for size, expected in table.items():
         result = tu.format_size(size)
         self.assertAlmostEqual(result[0], expected[0], 4)
         self.assertEqual(result[1], expected[1])
Beispiel #4
0
def test_format_size(size, expected):
    result = utils.format_size(size)
    assert_almost_eq(result[0], expected[0])
    assert result[1] == expected[1]
def get_memory() -> str:
    size_in_bytes = transClient.free_space(DISK)
    if size_in_bytes is None:
        return "Something went wrong"
    free_memory = trans_utils.format_size(size_in_bytes)
    return f"Free {round(free_memory[0], 2)} {free_memory[1]}"
     )
 else:
     text += escape_markdown(
         f"{utils.progress_bar(torrent.recheckProgress * 100)}  "
         f"{(round(torrent.recheckProgress * 100, 1))}% ",
         2,
     )
 text += f"{STATUS_LIST[torrent.status]}\n"
 if download := torrent.rateDownload:
     speed = trans_utils.format_speed(download)
     raw_text = (f"Time remaining: {utils.formated_eta(torrent)}\n"
                 f"Download rate: {round(speed[0], 1)} {speed[1]}\n")
     text += escape_markdown(raw_text, 2)
 if torrent.status != "seeding":
     downloaded_bytes: int = torrent.sizeWhenDone - torrent.leftUntilDone
     downloaded = trans_utils.format_size(downloaded_bytes)
     raw_text = f"Downloaded: {round(downloaded[0],2)} {downloaded[1]}\n"
     text += escape_markdown(raw_text, 2)
 if upload := torrent.rateUpload:
     speed = trans_utils.format_speed(upload)
     raw_text = f"Upload rate: {round(speed[0], 1)} {speed[1]}\n"
     text += escape_markdown(raw_text, 2)
 size_when_done = trans_utils.format_size(torrent.sizeWhenDone)
 total_size = trans_utils.format_size(torrent.totalSize)
 total_uploaded = trans_utils.format_size(torrent.uploadedEver)
 raw_text = (
     f"Size to download: {round(size_when_done[0], 2)} {size_when_done[1]}"
     f" / {round(total_size[0], 2)} {total_size[1]}\n")
 raw_text += (
     f"Total ever uploaded: {round(total_uploaded[0], 2)} {total_uploaded[1]}\n"
 )