Last active 1454713092

A script to look up claims on a torrent via TorrentTags.

torrenttags.py Raw
1#!/usr/bin/env python3
2__usage__ = """
3usage: torrenttags [-h] [--html] hash
4
5positional arguments:
6 hash Torrent hash to look up
7
8optional arguments:
9 -h, --help show this help message and exit
10 --html Prints html response to stdout, includes Chilling Effects
11 reports if available
12"""
13
14from requests import get
15from random import choice
16from bs4 import BeautifulSoup as Soup
17from argparse import ArgumentParser
18from sys import exit, stderr
19from base64 import b16encode, b32decode
20
21urls = ["http://api1.torrenttags.com/v1/", "http://api2.torrenttags.com/v1/", "http://api3.torrenttags.com/v1/"]
22
23def get_api_url(method=None):
24 return choice(urls) + method
25
26def get_ticket():
27 data = get(get_api_url("get-ticket")).json()
28 return data["status"], data["ticket"]
29
30def get_result(hash, ticket=None, rethtml=False):
31 if not ticket:
32 ticket = get_ticket()
33 if len(hash) != 40:
34 hash = convert_hash(hash)
35 data = Soup(get(get_api_url("get-tags-html"), params={'ticket': ticket, 'torrent': hash}).json()["html"], "html.parser")
36 if "no_claims" in data.find('img')["src"]:
37 return True, data
38 return False, data
39
40def convert_hash(torrent_hash):
41 """ Turns out hashes in magnet links can be base32 encoded, which shortens them to 32 characters """
42 b16 = b16encode(b32decode(torrent_hash))
43 return b16.decode('utf-8').lower()
44
45def main():
46 parser = ArgumentParser()
47 parser.add_argument("hash", help="Torrent hash to look up")
48 parser.add_argument("--html", help="Prints html response to stdout, includes Chilling Effects reports if available", action="store_true")
49 args = parser.parse_args()
50 result, data = get_result(args.hash, rethtml=args.html)
51 if args.html:
52 print(data)
53 if result:
54 return "{}: No claims (yet!)".format(args.hash), 0
55 else:
56 return "{}: Claims found".format(args.hash), 2
57
58if __name__ == "__main__":
59 outp, ret = main()
60 print(outp, file=stderr)
61 exit(ret)
62