Guest User

torrentplayer.py

a guest
Apr 9th, 2015
433
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. import re
  3. import sys
  4. import os
  5.  
  6. videoextensions = (".avi",".mp4",".mkv")
  7. downloadlocation='/home/user'
  8.  
  9. def tokenize(text, match=re.compile("([idel])|(\d+):|(-?\d+)").match):
  10.     i = 0
  11.     while i < len(text):
  12.         m = match(text, i)
  13.         s = m.group(m.lastindex)
  14.         i = m.end()
  15.         if m.lastindex == 2:
  16.             yield "s"
  17.             yield text[i:i+int(s)]
  18.             i = i + int(s)
  19.         else:
  20.             yield s
  21.  
  22. def decode_item(next, token):
  23.     if token == "i":
  24.         # integer: "i" value "e"
  25.         data = int(next())
  26.         if next() != "e":
  27.             raise ValueError
  28.     elif token == "s":
  29.         # string: "s" value (virtual tokens)
  30.         data = next()
  31.     elif token == "l" or token == "d":
  32.         # container: "l" (or "d") values "e"
  33.         data = []
  34.         tok = next()
  35.         while tok != "e":
  36.             data.append(decode_item(next, tok))
  37.             tok = next()
  38.         if token == "d":
  39.             data = dict(zip(data[0::2], data[1::2]))
  40.     else:
  41.         raise ValueError
  42.     return data
  43.  
  44. def decode(text):
  45.     try:
  46.         src = tokenize(text)
  47.         data = decode_item(src.next, src.next())
  48.         for token in src: # look for more tokens
  49.             raise SyntaxError("trailing junk")
  50.     except (AttributeError, ValueError, StopIteration):
  51.         raise SyntaxError("syntax error")
  52.     return data
  53.  
  54.  
  55. if __name__ == '__main__':
  56.     if not sys.argv[1].endswith(".torrent"):
  57.         print "Not a .torrent file!\nExiting..."
  58.         exit()
  59.     with open(sys.argv[1],"rb") as data:
  60.         torrent = decode(data.read())
  61.         if torrent['info']['name'] and torrent['info']['name'].endswith(videoextensions):
  62.             # cleanup previous peerflix instance
  63.             os.system('killall -15 peerflix')
  64.             # start peerflix and play torrent with SMPlayer
  65.             cmd = 'peerflix -g -r -f ' + downloadlocation + ' ' + sys.argv[1]
  66.             os.system(cmd)
  67.         else:
  68.             print 'Not a single video file, transferring to default torrent app'
  69.             cmd = 'xdg-open ' + sys.argv[1]
  70.             os.system(cmd)
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×