Coverage for torrentfile\edit.py: 100%
53 statements
« prev ^ index » next coverage.py v7.3.0, created at 2023-08-27 21:50 -0700
« prev ^ index » next coverage.py v7.3.0, created at 2023-08-27 21:50 -0700
1#! /usr/bin/python3
2# -*- coding: utf-8 -*-
4##############################################################################
5# Copyright (C) 2021-current alexpdev
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18##############################################################################
19"""
20Edit torrent module.
22Provides a facility by which certain properties of a torrent meta file can be
23edited by the user. The various command line arguments indicate which fields
24should be edited, and what the new value should be. Depending on what fields
25are chosen to edit, this command can trigger a new info hash which means the
26torrent will no longer be able to participate in the same swarm as the original
27unedited torrent.
28"""
30import os
31import logging
33import pyben
35logger = logging.getLogger(__name__)
38def filter_empty(args: dict, meta: dict, info: dict):
39 """
40 Remove the fields that were not used by the original file creator.
42 Parameters
43 ----------
44 args : dict
45 Editable metafile properties from user.
46 meta : dict
47 Metafile data dictionary.
48 info : dict
49 Metafile info dictionary.
50 """
51 for key, val in list(args.items()):
52 if val is None:
53 del args[key]
54 continue
56 if val == "":
57 if key in meta:
58 del meta[key]
59 elif key in info:
60 del info[key]
61 del args[key]
62 logger.debug("removeing empty fields %s", val)
65def edit_torrent(metafile: str, args: dict) -> dict:
66 """
67 Edit the properties and values in a torrent meta file.
69 Parameters
70 ----------
71 metafile : str
72 path to the torrent meta file.
73 args : dict
74 key value pairs of the properties to be edited.
76 Returns
77 -------
78 dict
79 The edited and nested Meta and info dictionaries.
80 """
81 logger.debug("editing torrent file %s", metafile)
82 meta = pyben.load(metafile)
83 info = meta["info"]
84 filter_empty(args, meta, info)
86 if "comment" in args:
87 info["comment"] = args["comment"]
89 if "source" in args:
90 info["source"] = args["source"]
92 if "private" in args:
93 info["private"] = 1
95 if "announce" in args:
96 val = args.get("announce", None)
97 if isinstance(val, str):
98 vallist = val.split()
99 meta["announce"] = vallist[0]
100 meta["announce-list"] = [vallist]
101 elif isinstance(val, list):
102 meta["announce"] = val[0]
103 meta["announce-list"] = [val]
105 if "url-list" in args:
106 val = args.get("url-list")
107 if isinstance(val, str):
108 meta["url-list"] = val.split()
109 elif isinstance(val, list):
110 meta["url-list"] = val
112 if "httpseeds" in args:
113 val = args.get("httpseeds")
114 if isinstance(val, str):
115 meta["httpseeds"] = val.split()
116 elif isinstance(val, list):
117 meta["httpseeds"] = val
119 meta["info"] = info
120 os.remove(metafile)
121 pyben.dump(meta, metafile)
122 return meta