Last active
April 8, 2023 05:38
-
-
Save Emory-M/4a3cb1787bc73c02285fd2fabf470858 to your computer and use it in GitHub Desktop.
Download m3u8 and merge ts file with python urllib2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf8 | |
import sys, urllib2 | |
import socks | |
from sockshandler import SocksiPyHandler | |
from urlparse import urlparse | |
if len(sys.argv) < 2: | |
print 'Usage: python %s [url]' % sys.argv[0] | |
exit() | |
def parseurl(): | |
url = sys.argv[1] | |
urlgroup = urlparse(url) | |
host = urlgroup.scheme + '://' + urlgroup.hostname | |
file = urlgroup.path.split('/')[-1].split('.')[0] + '.ts' | |
opener = urllib2.build_opener(SocksiPyHandler(socks.SOCKS5, '127.0.0.1')) | |
response = opener.open(url) | |
print 'm3u8 done!' | |
count = 0 | |
line = response.readline().strip() | |
while line: | |
if not line.startswith('#') and line != '': | |
ts = opener.open(host+line).read() | |
tsfile = open(file, 'ab') | |
tsfile.write(ts) | |
count += 1 | |
print 'part %s done!' % count | |
line = response.readline().strip() | |
print 'file: ' + file | |
if __name__ == "__main__": | |
try: | |
parseurl() | |
except KeyboardInterrupt: | |
print '' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment