torrenttags.py
                        
                             · 1.4 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
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):
    if not ticket:
        ticket = get_ticket()
    if len(hash) != 40:
        return False
    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
    return False
def main():
    parser = ArgumentParser()
    parser.add_argument("hash", help="Torrent hash to look up")
    args = parser.parse_args()
    if get_result(args.hash):
        return "No claims (yet!)", 0
    else:
        return "Claims found", 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 | |
| 20 | urls = ["http://api1.torrenttags.com/v1/", "http://api2.torrenttags.com/v1/", "http://api3.torrenttags.com/v1/"] | 
| 21 | |
| 22 | def get_api_url(method=None): | 
| 23 | return choice(urls) + method | 
| 24 | |
| 25 | def get_ticket(): | 
| 26 | data = get(get_api_url("get-ticket")).json() | 
| 27 | return data["status"], data["ticket"] | 
| 28 | |
| 29 | def 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 | |
| 39 | def 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 | |
| 48 | if __name__ == "__main__": | 
| 49 | outp, ret = main() | 
| 50 | print(outp, file=stderr) | 
| 51 | exit(ret) | 
| 52 |