Last active 1454713092

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

Revision 3d3db5a4f352fa6142024ab090e4e14e99b6682c

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):
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
37 return False
38
39def main():
40 parser = ArgumentParser()
41 parser.add_argument("hash", help="Torrent hash to look up")
42 args = parser.parse_args()
43 if get_result(args.hash):
44 return "No claims (yet!)", 0
45 else:
46 return "Claims found", 2
47
48if __name__ == "__main__":
49 outp, ret = main()
50 print(outp, file=stderr)
51 exit(ret)
52