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