]>
Commit | Line | Data |
---|---|---|
6800d337 | 1 | from __future__ import unicode_literals |
6800d337 YCH |
2 | |
3 | import re | |
4 | ||
453a1617 YCH |
5 | from .common import FileDownloader |
6 | from ..compat import compat_urllib_request | |
7 | ||
6800d337 YCH |
8 | |
9 | class DashSegmentsFD(FileDownloader): | |
10 | """ | |
11 | Download segments in a DASH manifest | |
12 | """ | |
13 | def real_download(self, filename, info_dict): | |
14 | self.report_destination(filename) | |
15 | tmpfilename = self.temp_name(filename) | |
16 | base_url = info_dict['url'] | |
17 | segment_urls = info_dict['segment_urls'] | |
18 | ||
5bf3276e YCH |
19 | is_test = self.params.get('test', False) |
20 | remaining_bytes = self._TEST_FILE_SIZE if is_test else None | |
93dfcb93 | 21 | byte_counter = 0 |
6800d337 | 22 | |
5bf3276e | 23 | def append_url_to_file(outf, target_url, target_name, remaining_bytes=None): |
6800d337 YCH |
24 | self.to_screen('[DashSegments] %s: Downloading %s' % (info_dict['id'], target_name)) |
25 | req = compat_urllib_request.Request(target_url) | |
5bf3276e YCH |
26 | if remaining_bytes is not None: |
27 | req.add_header('Range', 'bytes=0-%d' % (remaining_bytes - 1)) | |
28 | ||
6800d337 | 29 | data = self.ydl.urlopen(req).read() |
5bf3276e YCH |
30 | |
31 | if remaining_bytes is not None: | |
32 | data = data[:remaining_bytes] | |
33 | ||
6800d337 | 34 | outf.write(data) |
93dfcb93 | 35 | return len(data) |
6800d337 YCH |
36 | |
37 | def combine_url(base_url, target_url): | |
38 | if re.match(r'^https?://', target_url): | |
39 | return target_url | |
59db9f80 | 40 | return '%s%s%s' % (base_url, '' if base_url.endswith('/') else '/', target_url) |
6800d337 YCH |
41 | |
42 | with open(tmpfilename, 'wb') as outf: | |
43 | append_url_to_file( | |
44 | outf, combine_url(base_url, info_dict['initialization_url']), | |
45 | 'initialization segment') | |
46 | for i, segment_url in enumerate(segment_urls): | |
93dfcb93 | 47 | segment_len = append_url_to_file( |
6800d337 | 48 | outf, combine_url(base_url, segment_url), |
5bf3276e YCH |
49 | 'segment %d / %d' % (i + 1, len(segment_urls)), |
50 | remaining_bytes) | |
93dfcb93 | 51 | byte_counter += segment_len |
5bf3276e YCH |
52 | if remaining_bytes is not None: |
53 | remaining_bytes -= segment_len | |
54 | if remaining_bytes <= 0: | |
55 | break | |
6800d337 YCH |
56 | |
57 | self.try_rename(tmpfilename, filename) | |
58 | ||
59 | self._hook_progress({ | |
93dfcb93 YCH |
60 | 'downloaded_bytes': byte_counter, |
61 | 'total_bytes': byte_counter, | |
6800d337 YCH |
62 | 'filename': filename, |
63 | 'status': 'finished', | |
64 | }) | |
65 | ||
66 | return True |