Advertisement
Guest User

Untitled

a guest
Nov 25th, 2018
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. #
  3. # Output video url of an archived stream.me video
  4. #
  5. # This does not use the official API and is likely to break in the future
  6. #
  7. # last modified: 11/25/18
  8.  
  9. HELP = """\
  10. Usage:    {script} [-o] https://[www.]stream.me/archive/USER/ANYTHING/ID
  11.  -o output "TITLE.mp4" in the first line
  12.    
  13. Examples: {script} -o URL | xargs -d "\\n" curl -o
  14.          {script} URL | xargs mpv"""
  15.  
  16.  
  17. import json, sys
  18. from urllib import urlopen
  19. from HTMLParser import HTMLParser
  20.  
  21.  
  22. if len(sys.argv) < 2 or sys.argv[1] in ('-h', '--help'):
  23.     print HELP.format(script=sys.argv[0])
  24.     sys.exit(1)
  25. elif sys.argv[1] == '-o':
  26.     Outfile = True
  27.     Url = sys.argv[2]
  28. else:
  29.     Outfile = False
  30.     Url = sys.argv[1]
  31.  
  32.  
  33. class ContextExtractor(HTMLParser):
  34.     """Extract "__context" variable from archived video page"""
  35.     capture = False
  36.  
  37.     def handle_starttag(self, tag, attrs):
  38.         if tag == 'script' and not attrs:
  39.             self.capture = True
  40.     def handle_endtag(self, tag):
  41.         self.capture = False
  42.     def handle_data(self, data):
  43.         if self.capture:
  44.             self.content = data
  45.  
  46.     def toJSON(self, js):
  47.         """sanitize JS variable declaration to valid JSON"""
  48.         return js[js.find('{'):].strip().rstrip(';')
  49.  
  50.     def getContext(self, page):
  51.         self.feed(page)
  52.         return self.toJSON(self.content)
  53.  
  54. context = ContextExtractor().getContext(urlopen(Url).read())
  55.  
  56. vod = json.loads(context)['vod']
  57. manifest_url = vod['_links']['manifest']['href']
  58.  
  59. if Outfile:
  60.     print '{}.mp4'.format(vod['title'])
  61.  
  62. # load manifest and extract mp4 file location
  63. print json.load(urlopen(manifest_url)
  64.         )['formats']['mp4-http']['origin']['location']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement