tq.dcgi - gopherbay - A Gopher interface to The Pirate Bay
 (HTM) git clone https://git.parazyd.org/gopherbay
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
       tq.dcgi (8544B)
       ---
            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(r'''
           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((quote("udp://tracker.coppersurfer.tk:6969/announce"),
          107                        quote("udp://tracker.openbittorrent.com:6969/announce"),
          108                        quote("udp://9.rarbg.to:2710/announce"),
          109                        quote("udp://9.rarbg.me:2780/announce"),
          110                        quote("udp://9.rarbg.to:2730/announce"),
          111                        quote("udp://tracker.opentrackr.org:1337"),
          112                        quote("http://p4p.arenabg.com:1337/announce"),
          113                        quote("udp://tracker.torrent.eu.org:451/announce"),
          114                        quote("udp://tracker.tiny-vps.com:6969/announce"),
          115                        quote("udp://open.stealth.si:80/announce")))
          116     return "&tr=%s" % trs
          117 
          118 
          119 def httpget(url):
          120     r = get(url)
          121     return r.text
          122 
          123 
          124 def q(_argv):
          125     query = _argv[1] if _argv[1] else _argv[2]
          126 
          127     if query[0:10] == "top100:all":
          128         data = httpget(server + "/precompiled/data_top100_all.json")
          129     elif query[0:14] == "top100:recent_":
          130         data = httpget(server + "/precompiled/data_top100_48h_" + query[14:] + ".json")
          131     elif query[0:13] == "top100:recent":
          132         data = httpget(server + "/precompiled/data_top100_recent.json")
          133     elif query[0:7] == "top100:":
          134         data = httpget(server + "/precompiled/data_top100_" + query[7:] + ".json")
          135     else:
          136         data = httpget(server + "/q.php?q=" + quote(query))
          137 
          138     if not data:
          139         print("Internal error")
          140         return
          141 
          142     jsondata = loads(data)
          143 
          144     print("\n[7|Search again.|/q.dcgi?|server|port]")
          145 
          146     print("\n-=-[ Search Results ]-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n")
          147 
          148     for i in jsondata:
          149         name = i.get("name")
          150         seeders = int(i.get("seeders"))
          151         leechers = int(i.get("leechers"))
          152         files = int(i.get("num_files"))
          153         size = print_size(int(i.get("size")))
          154         time = strftime("%Y-%m-%d %H:%M:%S", gmtime(int(i.get("added"))))
          155         category_name = print_category(int(i.get("category")))
          156         category = int(i.get("category"))
          157         username = i.get("username")
          158         status = i.get("status")
          159         ih = i.get("info_hash")
          160         magnet = print_magnet(i["info_hash"], quote(i["name"]))
          161 
          162         print("\nName: %s" % name)
          163         print("Seeders: %d | Leechers: %d | Files: %d | Size: %s" % (seeders, leechers, files, size))
          164         print("Uploaded: %s" % time)
          165         print("[1|Category: %s|/q.dcgi?category:%d|server|port]" % (category_name, category))
          166         print("[1|Uploader: %s (%s)|/q.dcgi?user:%s|server|port]" % (username, status, username))
          167         print("[h|Magnet Link: %s|URL:%s|server|port]" % (ih, magnet))
          168 
          169 
          170 def print_index():
          171     print("-=-[ Pirate Search ]-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=")
          172     print("[7|Search torrents|/q.dcgi?|server|port]")
          173     print("[1|Browse torrents|/browse.dcgi|server|port]")
          174     print("[1|Recent torrents|/q.dcgi?top100:recent|server|port]")
          175     print("[1|Top 100|/top.dcgi|server|port]")
          176     print("[1|Top 100 (48h)|/top48.dcgi|server|port]")
          177     print("\n-=-[ About ]-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
          178     print("[h|Donate BTC: %s|URL:bitcoin:%s|server|port]" % (btc, btc))
          179     print("[h|Donate XMR: %s|URL:monero:%s|server|port]" % (xmr, xmr))
          180     print("[h|Donate ZEC: %s|URL:zcash:%s|server|port]\n" % (zec, zec))
          181 
          182 
          183 if __name__ == "__main__":
          184     if basename(argv[0]) == "index.dcgi":
          185         print_boat()
          186         print_index()
          187     elif basename(argv[0]) == "q.dcgi":
          188         print_boat()
          189         q(argv)
          190     elif basename(argv[0]) == "top.dcgi":
          191         print_boat()
          192         print_all_categories(top100=True)
          193     elif basename(argv[0]) == "top48.dcgi":
          194         print_boat()
          195         print_all_categories(top100=True, recent=True)
          196     elif basename(argv[0]) == "browse.dcgi":
          197         print_boat()
          198         print_all_categories()
          199     else:
          200         print("Internal error.")