Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. from urllib2 import urlopen # Python 2
  2. from urllib2 import Request
  3. from json import loads
  4. from json import dumps
  5. from .tstreamer import TStreamer
  6. from time import sleep
  7.  
  8. from logger import Logger
  9.  
  10.  
  11. class Peerflix(TStreamer):
  12. def __init__(self, host, port=9000, base_url='torrents'):
  13. super(Peerflix, self).__init__(host=host,
  14. port=port,
  15. base_url=base_url)
  16.  
  17. def add_torrent(self, infohash):
  18. """Add torrent.
  19.  
  20. After torrent added it's downloading started automaticaly.
  21.  
  22. Arguments:
  23. infohash {str} -- torrent infohash
  24. """
  25. json_data = dumps({'link': 'magnet:?xt=urn:btih:{}'.format(infohash)})
  26.  
  27. req = Request(self.get_url(),
  28. json_data,
  29. {'Content-Type': 'application/json'})
  30. res = urlopen(req).read()
  31.  
  32. Logger.debug('torrent added {}'.format(res))
  33.  
  34. def get_videos(self, infohash):
  35. """Return videos list.
  36.  
  37. Arguments:
  38. infohash {str} -- torrent infohash
  39.  
  40. Returns:
  41. list -- list of files URLs
  42.  
  43. """
  44.  
  45. self.add_torrent(infohash)
  46.  
  47. sleep(1) # wait before torrent downloading starts
  48.  
  49. self.stop_torrent(infohash)
  50.  
  51. # get files
  52. json_res = urlopen(
  53. '{}/{}'.format(self.get_url(), infohash)).read()
  54. Logger.debug('json_res: {}'.format(json_res))
  55.  
  56. res = loads(json_res)
  57.  
  58. video_files = []
  59. idx = 0
  60. for video_file in res.get('files', []):
  61. name = video_file.get('name', '')
  62. path = video_file.get('path', '')
  63. link = video_file.get('link', '')
  64. idx += 1
  65.  
  66. video_files.append(self._create_file_item(idx, name, path, link))
  67.  
  68. return video_files
  69.  
  70. def stop_torrent(self, infohash):
  71. """Stop torrent downloading.
  72.  
  73. Arguments:
  74. infohash {str} -- torrent infohash
  75. """
  76. req = Request('{}/{}/stop'.format(self.get_url(), infohash), '')
  77. res = urlopen(req).read()
  78. Logger.debug('torrent_stop: {}'.format(res))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement