gopherbay

A Gopher interface to The Pirate Bay
git clone https://git.parazyd.org/gopherbay
Log | Files | Refs | README

q.dcgi (8145B)


      1 #!/usr/bin/env python3
      2 # Beerware License, parazyd <parazyd@dyne.org>
      3 
      4 from json import loads
      5 from sys import argv
      6 from time import strftime, gmtime
      7 from urllib.parse import quote
      8 from os.path import basename
      9 from requests import get
     10 
     11 from config import server, categories, btc, xmr, zec
     12 
     13 
     14 def print_boat():
     15     print('''
     16                                  .                   .
     17                               _..-''"""\          _.--'"""\\
     18                               |         L         |        \\
     19                   _           / _.-.---.\.        / .-.----.\\
     20                 _/-|---     _/<"---'"""""\\\\`.    /-'--''"""""\\
     21                |       \     |            L`.`-. |            L
     22                /_.-.---.L    |            \  \  `|            J`.
     23              _/--'""""  \    F            \L  \  |             L
     24                L      `. L  J  _.---.-"""-.\`. L_/ _.--|"""""--.\ `.
     25                |        \+. /-"__.--'""""   \ `./'"---'""""""   \`. `.
     26                F   _____ \ `F"        `.     \  \                L `.
     27               /.-'"_|---'"\ |           `    JL |                 L  `.`.
     28              <-'""         \|    _.-.------._ A J    _.-.-----`.--|   ``.`.
     29               L         `. |/.-'"_.-`---'""\."| /-'"---'"""""   \`.\.  \ `.`.
     30               |  _.------\.<'"""            L\ L\                `.`\`. \  `.
     31          _.-'//'"--'"""   L\|       ________\ `.F     ___.-------._L \ `-\   \`.
     32         /___| F             F _.--'"_|-------L  /_.-'"_.-|-'"""""""\  L   L   `.`.
     33             | F  _.-'|"""""/'"-'"""          J <'"""                L J   |     `.`.
     34             |/-'-''/|""\ )-|\                 F \                   |  L .'"""`\\""-\\\\_
     35              F`-'-'-(`-')  | \                F  \                  |___`"""`.""`.-'"
     36 ------------/        `-'---|  F               L   L             __     |"""""`-'"__________
     37           .'_         |    |__L          __  J__  |    _.--'""""   `".----'".'
     38          '""""""""""""|--._+--F _.-'""||"   """___/.-'"   ||-'"/""""" \_. .'
     39          J------------(___\__/'_____.--------'-------'""""""""           /
     40          `-.                                        _.__.__.__.____     J_.-._
     41     .'`-._ (-`--`---.'--._`---._.-'`-._.-'_.-'``-._'               `-''-'
     42 
     43 
     44                            === The Gopher Bay ===
     45 
     46 ''')
     47 
     48 
     49 def print_size(size):
     50     if size >= 2**50: return "%.2f PiB" % (size/(2**50))
     51     if size >= 2**40: return "%.2f TiB" % (size/(2**40))
     52     if size >= 2**30: return "%.2f GiB" % (size/(2**30))
     53     if size >= 2**20: return "%.2f MiB" % (size/(2**20))
     54     if size >= 2**10: return "%.2f KiB" % (size/(2**10))
     55     return "%s B"
     56 
     57 
     58 def print_category(cat):
     59     c = categories.get(cat)
     60     if not c: return "n/a"
     61     mc = str(cat)[0] + "00"
     62     mc = categories.get(int(mc))
     63     if not mc: return c
     64     return "%s > %s" % (mc, c)
     65 
     66 
     67 def print_all_categories(top100=False, recent=False):
     68     if top100:
     69         print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=[ Browse Top100 ]-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-")
     70     else:
     71         print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=[ Browse Categories ]-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=")
     72 
     73     if top100 and not recent:
     74         print("[1|Total Top100|/q.dcgi?top100:all|server|port]")
     75     elif top100 and recent:
     76         print("[1|Total Top100 (48h)|/q.dcgi?top100:recent|server|port]")
     77 
     78     for i in categories:
     79         if i % 100 == 0:
     80             print()
     81             if i == 100:
     82                 print("-=-[ Audio ]-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
     83             elif i == 200:
     84                 print("-=-[ Video ]-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
     85             elif i == 300:
     86                 print("-=-[ Applications ]-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=")
     87             elif i == 400:
     88                 print("-=-[ Games ]-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
     89             elif i == 500:
     90                 print("-=-[ Porn ]-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=")
     91             elif i == 600:
     92                 print("-=-[ Other ]-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
     93         if top100 and not recent:
     94             print("[1|%s|/q.dcgi?%s:%d|server|port]" % (categories[i], "top100", i))
     95         elif top100 and recent:
     96             print("[1|%s (48h)|/q.dcgi?%s_%d|server|port]" % (categories[i], "top100:recent", i))
     97         else:
     98             print("[1|%s|/q.dcgi?%s:%d|server|port]" % (categories[i], "category", i))
     99 
    100 
    101 def print_magnet(ihash, name):
    102     return "magnet:?xt=urn:btih:%s&dn%s%s" % (ihash, quote(name), print_trackers())
    103 
    104 
    105 def print_trackers():
    106     trs = "&tr=".join((
    107         quote("udp://tracker.coppersurfer.tk:6969/announce"),
    108         quote("udp://9.rarbg.me:2850/announce"),
    109         quote("udp://9.rarbg.to:2920/announce"),
    110         quote("udp://tracker.opentrackr.org:1337"),
    111         quote("udp://tracker.leechers-paradise.org:6969/announce")))
    112     return "&tr=%s" % trs
    113 
    114 
    115 def httpget(url):
    116     r = get(url)
    117     return r.text
    118 
    119 
    120 def q(_argv):
    121     query = _argv[1] if _argv[1] else _argv[2]
    122 
    123     if query[0:10] == "top100:all":
    124         data = httpget(server + "/precompiled/data_top100_all.json")
    125     elif query[0:14] == "top100:recent_":
    126         data = httpget(server + "/precompiled/data_top100_48h_" + query[14:] + ".json")
    127     elif query[0:13] == "top100:recent":
    128         data = httpget(server + "/precompiled/data_top100_recent.json")
    129     elif query[0:7] == "top100:":
    130         data = httpget(server + "/precompiled/data_top100_" + query[7:] + ".json")
    131     else:
    132         data = httpget(server + "/q.php?q=" + quote(query))
    133 
    134     if not data:
    135         print("Internal error")
    136         return
    137 
    138     jsondata = loads(data)
    139 
    140     print("\n[7|Search again.|/q.dcgi?|server|port]")
    141 
    142     print("\n-=-[ Search Results ]-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n")
    143 
    144     for i in jsondata:
    145         name = i.get("name")
    146         seeders = int(i.get("seeders"))
    147         leechers = int(i.get("leechers"))
    148         files = int(i.get("num_files"))
    149         size = print_size(int(i.get("size")))
    150         time = strftime("%Y-%m-%d %H:%M:%S", gmtime(int(i.get("added"))))
    151         category_name = print_category(int(i.get("category")))
    152         category = int(i.get("category"))
    153         username = i.get("username")
    154         status = i.get("status")
    155         ih = i.get("info_hash")
    156         magnet = print_magnet(i["info_hash"], quote(i["name"]))
    157 
    158         print("\nName: %s" % name)
    159         print("Seeders: %d | Leechers: %d | Files: %d | Size: %s" % (seeders, leechers, files, size))
    160         print("Uploaded: %s" % time)
    161         print("[1|Category: %s|/q.dcgi?category:%d|server|port]" % (category_name, category))
    162         print("[1|Uploader: %s (%s)|/q.dcgi?user:%s|server|port]" % (username, status, username))
    163         print("[h|Magnet Link: %s|URL:%s|server|port]" % (ih, magnet))
    164 
    165 
    166 def print_index():
    167     print("-=-[ Pirate Search ]-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=")
    168     print("[7|Search torrents|/q.dcgi?|server|port]")
    169     print("[1|Browse torrents|/browse.dcgi|server|port]")
    170     print("[1|Recent torrents|/q.dcgi?top100:recent|server|port]")
    171     print("[1|Top 100|/top.dcgi|server|port]")
    172     print("[1|Top 100 (48h)|/top48.dcgi|server|port]")
    173     print("\n-=-[ About ]-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
    174     print("[h|Donate BTC: %s|URL:bitcoin:%s|server|port]" % (btc, btc))
    175     print("[h|Donate XMR: %s|URL:monero:%s|server|port]" % (xmr, xmr))
    176     print("[h|Donate ZEC: %s|URL:zcash:%s|server|port]\n" % (zec, zec))
    177 
    178 
    179 if __name__ == "__main__":
    180     if basename(argv[0]) == "index.dcgi":
    181         print_boat()
    182         print_index()
    183     elif basename(argv[0]) == "q.dcgi":
    184         print_boat()
    185         q(argv)
    186     elif basename(argv[0]) == "top.dcgi":
    187         print_boat()
    188         print_all_categories(top100=True)
    189     elif basename(argv[0]) == "top48.dcgi":
    190         print_boat()
    191         print_all_categories(top100=True, recent=True)
    192     elif basename(argv[0]) == "browse.dcgi":
    193         print_boat()
    194         print_all_categories()
    195     else:
    196         print("Internal error.")