4 from odict
import OrderedDict
6 def load_mappings(fname
="trackers"):
8 Initialises tracker to ratio multiple mappings from the specified
12 logger
= logging
.getLogger("cheatbt")
13 logger
.info('loading mappings from "' + fname
+ '"')
19 tracker
, multiple
= l
.split(":")
20 mappings
[tracker
.strip()] = int(multiple
.strip())
24 if "default" not in mappings
:
25 mappings
["default"] = 1
29 def cheat(url
, mappings
):
31 Modifies BitTorrent tracker URLs, faking the amount of data
32 uploaded. All other URLs should pass through unimpeded.
35 parsed
= urlparse
.urlparse(url
)
37 if "=" not in parsed
.query
:
40 query
= OrderedDict([i
.split("=") for i
in parsed
.query
.split("&")])
41 if "uploaded" not in query
or query
["uploaded"] == "0":
44 if parsed
.hostname
in mappings
:
45 multiple
= mappings
[parsed
.hostname
]
47 if "default" in mappings
:
48 multiple
= mappings
["default"]
52 # Don't bother munging the URL if the upload amount isn't going
57 fakeupload
= int(query
["uploaded"])
59 logger
= logging
.getLogger("cheatbt")
60 logger
.debug("%s: %d -> %d" % (parsed
.hostname
, fakeupload
,
61 fakeupload
* multiple
))
63 fakeupload
= fakeupload
* multiple
64 query
["uploaded"] = str(fakeupload
)
67 for k
in query
.keys():
68 new_query
+= k
+ "=" + query
[k
] + "&"
69 new_query
= new_query
[:-1] # Remove trailing "&"
71 # <scheme>://<netloc>/<path>;<params>?<query>#<fragment>
72 new_url
= urlparse
.urlunparse((parsed
.scheme
, parsed
.netloc
, parsed
.path
,
73 parsed
.params
, new_query
, parsed
.fragment
))