Last active 1454713092

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

Revision 58d9f45fff217466de6df3c5a8c2e610f1d77902

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
19
20urls = ["http://api1.torrenttags.com/v1/", "http://api2.torrenttags.com/v1/", "http://api3.torrenttags.com/v1/"]
21
22def get_api_url(method=None):
23 return choice(urls) + method
24
25def get_ticket():
26 data = get(get_api_url("get-ticket")).json()
27 return data["status"], data["ticket"]
28
29def get_result(hash, ticket=None, rethtml=False):
30 if not ticket:
31 ticket = get_ticket()
32 if len(hash) != 40:
33 return False
34 data = Soup(get(get_api_url("get-tags-html"), params={'ticket': ticket, 'torrent': hash}).json()["html"], "html.parser")
35 if "no_claims" in data.find('img')["src"]:
36 return True, data
37 return False, data
38
39def main():
40 parser = ArgumentParser()
41 parser.add_argument("hash", help="Torrent hash to look up")
42 parser.add_argument("--html", help="Prints html response to stdout, includes Chilling Effects reports if available", action="store_true")
43 args = parser.parse_args()
44 result, data = get_result(args.hash, rethtml=args.html)
45 if args.html:
46 print(data)
47 if result:
48 return "{}: No claims (yet!)".format(args.hash), 0
49 else:
50 return "{}: Claims found".format(args.hash), 2
51
52if __name__ == "__main__":
53 outp, ret = main()
54 print(outp, file=stderr)
55 exit(ret)
56