Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2
- #
- # Output video url of an archived stream.me video
- #
- # This does not use the official API and is likely to break in the future
- #
- # last modified: 11/25/18
- HELP = """\
- Usage: {script} [-o] https://[www.]stream.me/archive/USER/ANYTHING/ID
- -o output "TITLE.mp4" in the first line
- Examples: {script} -o URL | xargs -d "\\n" curl -o
- {script} URL | xargs mpv"""
- import json, sys
- from urllib import urlopen
- from HTMLParser import HTMLParser
- if len(sys.argv) < 2 or sys.argv[1] in ('-h', '--help'):
- print HELP.format(script=sys.argv[0])
- sys.exit(1)
- elif sys.argv[1] == '-o':
- Outfile = True
- Url = sys.argv[2]
- else:
- Outfile = False
- Url = sys.argv[1]
- class ContextExtractor(HTMLParser):
- """Extract "__context" variable from archived video page"""
- capture = False
- def handle_starttag(self, tag, attrs):
- if tag == 'script' and not attrs:
- self.capture = True
- def handle_endtag(self, tag):
- self.capture = False
- def handle_data(self, data):
- if self.capture:
- self.content = data
- def toJSON(self, js):
- """sanitize JS variable declaration to valid JSON"""
- return js[js.find('{'):].strip().rstrip(';')
- def getContext(self, page):
- self.feed(page)
- return self.toJSON(self.content)
- context = ContextExtractor().getContext(urlopen(Url).read())
- vod = json.loads(context)['vod']
- manifest_url = vod['_links']['manifest']['href']
- if Outfile:
- print '{}.mp4'.format(vod['title'])
- # load manifest and extract mp4 file location
- print json.load(urlopen(manifest_url)
- )['formats']['mp4-http']['origin']['location']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement