TorrentFile API Documentation

Edit Module

module
torrentfile.edit

Edit torrent meta file.

Functions
  • edit_torrent(metafile, args) Edit the properties and values in a torrent meta file.
  • filter_empty(args, meta, info) Remove dictionary keys with empty values.

torrentfile.edit

Edit torrent meta file.

edit_torrent(metafile, args)

Edit the properties and values in a torrent meta file.

Parameters:

Name Type Description Default
metafile `str`

path to the torrent meta file.

required
args `dict`

key value pairs of the properties to be edited.

required
Source code in torrentfile\edit.py
def edit_torrent(metafile, args):
    """
    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.
    """
    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

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

filter_empty(args, meta, info)

Remove dictionary keys with empty values.

Parameters:

Name Type Description Default
args `dict`

Editable metafile properties from user.

required
meta `dict`

Metafile data dictionary.

required
info `dict`

Metafile info dictionary.

required
Source code in torrentfile\edit.py
def filter_empty(args, meta, info):
    """
    Remove dictionary keys with empty values.

    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]