Example #1
0
def renameTorrent(path, cate=None, type='first'):

    print("I'm renameTorrent()...")
    print("Renaming type is -> " + type)

    # 判断path参数的文件夹是否存在, 不存在就退出
    if os.path.exists(path):
        print(path + " exists...")
    else:
        print(path + " not exists!!! Terminating ...")
        exit()

    winerror = []
    for file in os.listdir(path):
        if os.path.isfile(path + file) & file.endswith('.torrent'):
            print(path + file)

            torrent_metadata = Bencode.read_file(path + file)
            torrent_tracker = torrent_metadata['announce'][0:50]
            torrent_name = torrent_metadata['info']['name']
            if ("private" in torrent_metadata['info'].keys()):
                torrent_is_private = torrent_metadata['info']['private']
            else:
                print("!!!!!")
                print("Torrent don't have PRIVATE attribute...")
                print("!!!!!")
                torrent_is_private = 0
            # print (torrent_metadata['info'].keys());
            # print (torrent_tracker);
            # print (torrent_name + '\n');

            for k, v in cateDict.items():
                # print (k);
                # if re.search(k, torrent_tracker.decode()):
                if re.search(k, torrent_tracker):
                    # print (v)

                    # 增加随机UUID,防止重命名文件时失败
                    uid = uuid.uuid1().hex[0:8]
                    uid = '_' + uid + '_'

                    file_site_first = '[' + v + ']_' + torrent_name + uid + '.torrent'
                    file_site_last = torrent_name + uid + '[' + v + ']' + '.torrent'

                    # print (file_site_first);
                    # print (file_site_last);

                    if (type == 'first'):
                        file_new = file_site_first
                    else:
                        file_new = file_site_last

                    # 切换命名方法
                    # file_new = file_site_first;
                    # file_new = file_site_last;

                    try:
                        if os.path.join(path,
                                        file_new) == os.path.join(path, file):
                            print(file)
                            print("No need to Rename...\n")
                            pass
                        else:
                            print(file + " Renaming -> \n" + file_new)
                            os.rename(os.path.join(path, file),
                                      os.path.join(path, file_new))
                            print("")
                    except WindowsError:
                        pass
                        # winerror.append(file)
    print("renameTorrent done...!\n")
Example #2
0
def test_read_file(torr_test_file, struct_torr_file):
    decoded = Bencode.read_file(torr_test_file)
    assert decoded == struct_torr_file
Example #3
0
def test_read_file():
    decoded = Bencode.read_file(FPATH_TORRENT_SIMPLE)
    assert decoded == STRUCT_TORRENT_SIMPLE
Example #4
0
def test_read_file_dir(torr_test_dir, struct_torr_dir):
    decoded = Bencode.read_file(torr_test_dir)
    assert decoded == struct_torr_dir
Example #5
0
def test_read_file_dir():
    decoded = Bencode.read_file(FPATH_TORRENT_WITH_DIR)
    assert decoded == STRUCT_TORRENT_WITH_DIR