Skip to content

Edit

edit #

Edit torrent module.

Provides a facility by which certain properties of a torrent meta file can be edited by the user. The various command line arguments indicate which fields should be edited, and what the new value should be. Depending on what fields are chosen to edit, this command can trigger a new info hash which means the torrent will no longer be able to participate in the same swarm as the original unedited torrent.

edit_torrent(metafile: str, args: dict) -> dict #

Edit the properties and values in a torrent meta file.

PARAMETER DESCRIPTION
metafile

path to the torrent meta file.

TYPE: str

args

key value pairs of the properties to be edited.

TYPE: dict

RETURNS DESCRIPTION
dict

The edited and nested Meta and info dictionaries.

Source code in torrentfile\edit.py
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
def edit_torrent(metafile: str, args: dict) -> dict:
    """
    Edit the properties and values in a torrent meta file.

    Parameters
    ----------
    metafile : str
        path to the torrent meta file.
    args : dict
        key value pairs of the properties to be edited.

    Returns
    -------
    dict
        The edited and nested Meta and info dictionaries.
    """
    logger.debug("editing torrent file %s", metafile)
    meta = pyben.load(metafile)
    info = meta["info"]
    filter_empty(args, meta, info)

    if "comment" in args:
        info["comment"] = args["comment"]

    if "source" in args:
        info["source"] = args["source"]

    if "private" in args:
        info["private"] = 1

    if "announce" in args:
        val = args.get("announce", None)
        if isinstance(val, str):
            vallist = val.split()
            meta["announce"] = vallist[0]
            meta["announce-list"] = [vallist]
        elif isinstance(val, list):
            meta["announce"] = val[0]
            meta["announce-list"] = [val]

    if "url-list" in args:
        val = args.get("url-list")
        if isinstance(val, str):
            meta["url-list"] = val.split()
        elif isinstance(val, list):
            meta["url-list"] = val

    if "httpseeds" in args:
        val = args.get("httpseeds")
        if isinstance(val, str):
            meta["httpseeds"] = val.split()
        elif isinstance(val, list):
            meta["httpseeds"] = val

    meta["info"] = info
    os.remove(metafile)
    pyben.dump(meta, metafile)
    return meta

filter_empty(args: dict, meta: dict, info: dict) #

Remove the fields that were not used by the original file creator.

PARAMETER DESCRIPTION
args

Editable metafile properties from user.

TYPE: dict

meta

Metafile data dictionary.

TYPE: dict

info

Metafile info dictionary.

TYPE: dict

Source code in torrentfile\edit.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def filter_empty(args: dict, meta: dict, info: dict):
    """
    Remove the fields that were not used by the original file creator.

    Parameters
    ----------
    args : dict
        Editable metafile properties from user.
    meta : dict
        Metafile data dictionary.
    info : dict
        Metafile info dictionary.
    """
    for key, val in list(args.items()):
        if val is None:
            del args[key]
            continue

        if val == "":
            if key in meta:
                del meta[key]
            elif key in info:
                del info[key]
            del args[key]
            logger.debug("removeing empty fields %s", val)