forked from huzhenjie/m3u8_downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
m3u8_downloader.py
58 lines (52 loc) · 1.55 KB
/
m3u8_downloader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from urlparse import urlparse
import requests
import urllib
import os
import sys
def get_url_list(host, body):
lines = body.split('\n')
ts_url_list = []
for line in lines:
if not line.startswith('#') and line != '':
if line.startswith('http'):
ts_url_list.append(line)
else:
ts_url_list.append('%s/%s' % (host, line))
return ts_url_list
def get_host(url):
urlgroup = urlparse(url)
return urlgroup.scheme + '://' + urlgroup.hostname
def get_m3u8_body(url):
print 'read m3u8 file:', url
session = requests.Session()
adapter = requests.adapters.HTTPAdapter(pool_connections=10, pool_maxsize=10, max_retries=10)
session.mount('http://', adapter)
session.mount('https://', adapter)
r = session.get(url, timeout=10)
return r.content
def download_ts_file(ts_url_list, download_dir):
i = 0
for ts_url in reversed(ts_url_list):
i += 1
file_name = ts_url[ts_url.rfind('/'):]
curr_path = '%s%s' % (download_dir, file_name)
print '\n[process]: %s/%s' % (i, len(ts_url_list))
print '[download]:', ts_url
print '[target]:', curr_path
if os.path.isfile(curr_path):
print '[warn]: file already exist'
continue
urllib.urlretrieve(ts_url, curr_path)
def main(url, download_dir):
host = get_host(url)
body = get_m3u8_body(url)
ts_url_list = get_url_list(host, body)
print 'total file count:', len(ts_url_list)
download_ts_file(ts_url_list, download_dir)
if __name__ == '__main__':
args = sys.argv
if len(args) > 2:
main(args[1], args[2])
else:
print 'Fail, params error, try:'
print 'python', args[0], 'your_m3u8_url', 'your_local_dir\n'