Skip to content

Instantly share code, notes, and snippets.

@bradenbest
Created March 18, 2018 18:18
Show Gist options
  • Save bradenbest/a3c531307a2f76e690901522c628b6b0 to your computer and use it in GitHub Desktop.
Save bradenbest/a3c531307a2f76e690901522c628b6b0 to your computer and use it in GitHub Desktop.
python/test/mal-interop-example
#!/usr/bin/env python3
# ^^^ IMPORTANT! Always make sure to specify which python. Can't tell you how many times I've had a python program break on me cause it *assumed* python 3 was my default. In fact, mal does that, too. I should probably open an issue on that.
# Note: this is a set of three fake program meant to demonstrate program interoperability. This does not actually torrent anything.
# This is meant to simulate passing 'mal torrent' as requested here:
# https://github.com/ryukinix/mal/issues/64
# this doesn't have to be marked executable. `python3 mal-torrent.py` should work also.
import os
import subprocess
import sys
def strbetween(str, begin, end):
"""Returns the substring of str between the first occurrences of begin and end"""
s = str.find(begin)
if s != -1:
str = str[s + len(begin):]
e = str.find(end)
if e != -1:
str = str[:e]
return str
def main():
anime_to_torrent = "Boobtown" # Ironically, this is probably a real anime
torrent_browse_cmd = "./nyaa-browser.py"
torrent_browse_result = None
torrent_client_cmd = "./torrent-client.py"
# If using execvp, you'll need the PATH environment variable.
print("Calling %s with anime '%s'" % (torrent_browse_cmd, anime_to_torrent))
torrent_browse_result = subprocess.run([torrent_browse_cmd, anime_to_torrent], stdout=subprocess.PIPE)
if torrent_browse_result.returncode != 0:
print("mal: %s exited with an error" % torrent_browse_cmd)
return 1
print("result: %s" % (torrent_browse_result.stdout))
print("Now I will hand the rest to %s" % torrent_client_cmd)
os.system("%s \"%s\"" % (torrent_client_cmd, strbetween(str(torrent_browse_result.stdout), "b'", "\\n'")))
return 0
# at this point, the torrent command has successfully outsourced to the nyaa browser and torrent client. Remember that system() spawns a default shell in the background, so on Linux that's sh, on Windows, that's the shell used by cmd.exe. If it wouldn't work by typing it into the shell, it won't work here. So make sure that it can actually execute in a common user scenario, particularly on Windows if you're choosing to support it, as Windows software is notorious for not adding themselves to PATH. On Windows, if you can't find it, you'd probably have to ask the user for the location of the executable ala 'open with...' or something.
sys.exit(main())
#!/usr/bin/env python3
# see mal-torrent.py
# This must be marked executable, as it would be if it were in /usr/local/bin
import sys
if sys.argv[1] == "Boobtown":
print("<insert really long magnet url here>")
sys.exit(0)
else:
print("nyaa-browser: I was unable to find a torrent for '%s'" % sys.argv[1])
sys.exit(1)
#!/usr/bin/env python3
# see mal-torrent.py
# This must be marked executable, as it would be if it were in /usr/local/bin
import sys
if sys.argv[1] == "<insert really long magnet url here>":
print("Torrenting '%s'..." % sys.argv[1])
print("Success! Yar Har Fiddle dee-dee.\nBeing a pirate is all right with me!\nDo what you want cause a pirate is free.\nYou are a pirate!")
print("Bye.")
sys.exit(0)
else:
print("torrent-client: can't find a tracker for %s" % sys.argv[1])
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment