# Buggy code
def append_url_to_file(target_url, tmp_filename, segment_name):
    target_filename = "%s-%s" % (tmp_filename, segment_name)
    count = 0
    while count <= fragment_retries:
        try:
            success = ctx["dl"].download(
                target_filename, {"url": combine_url(base_url, target_url)}
            )
            if not success:
                return False
            down, target_sanitized = sanitize_open(target_filename, "rb")
            ctx["dest_stream"].write(down.read())
            down.close()
            segments_filenames.append(target_sanitized)
            break
        except (compat_urllib_error.HTTPError,) as err:
            # YouTube may often return 404 HTTP error for a fragment causing the
            # whole download to fail. However if the same fragment is immediately
            # retried with the same request data this usually succeeds (1-2 attemps
            # is usually enough) thus allowing to download the whole file successfully.
            # So, we will retry all fragments that fail with 404 HTTP error for now.
            if err.code != 404:
                raise
            # Retry fragment
            count += 1
            if count <= fragment_retries:
                self.report_retry_fragment(segment_name, count, fragment_retries)
    if count > fragment_retries:
        self.report_error("giving up after %s fragment retries" % fragment_retries)
        return False


# Traceback (urllib.error.HTTPError)
PS C:\dev\youtube-dl\master> youtube-dl.exe https://www.twitch.tv/naysayer88/v/85713845 -
v
[debug] System config: []
[debug] User config: []
[debug] Command-line args: ['https://www.twitch.tv/naysayer88/v/85713845', '-v']
[debug] Encodings: locale cp1251, fs mbcs, out cp866, pref cp1251
[debug] youtube-dl version 2016.08.24.1
[debug] Python version 3.4.4 - Windows-10-10.0.10240
[debug] exe versions: ffmpeg N-81192-g04da20e, ffprobe N-81192-g04da20e, rtmpdump 2.4
[debug] Proxy map: {}
[twitch:vod] 85713845: Downloading vod info JSON
[twitch:vod] 85713845: Downloading vod access token
[twitch:vod] 85713845: Downloading m3u8 information
[debug] Invoking downloader on 'http://vod.edgecast.hls.ttvnw.net/v1/AUTH_system/vods_60f4/naysayer88_23003365072_506378255/chunked/index-dvr.m3u8'
[hlsnative] Downloading m3u8 manifest
[hlsnative] Total fragments: 43
[download] Destination: Gammin' - Obduction Spoilerama-v85713845.mp4
[download]  34.9% of ~13.53MiB at Unknown speed ETA Unknown ETAERROR: unable to download video data: HTTP Error 400: Bad Request
Traceback (most recent call last):
File "C:\Users\dst\AppData\Roaming\Build archive\youtube-dl\rg3\tmpvarrvgfb\build\youtube_dl\YoutubeDL.py", line 1694, in process_info
File "C:\Users\dst\AppData\Roaming\Build archive\youtube-dl\rg3\tmpvarrvgfb\build\youtube_dl\YoutubeDL.py", line 1636, in dl
File "C:\Users\dst\AppData\Roaming\Build archive\youtube-dl\rg3\tmpvarrvgfb\build\youtube_dl\downloader\common.py", line 354, in download
File "C:\Users\dst\AppData\Roaming\Build archive\youtube-dl\rg3\tmpvarrvgfb\build\youtube_dl\downloader\hls.py", line 102, in real_download
File "C:\Users\dst\AppData\Roaming\Build archive\youtube-dl\rg3\tmpvarrvgfb\build\youtube_dl\downloader\common.py", line 354, in download
File "C:\Users\dst\AppData\Roaming\Build archive\youtube-dl\rg3\tmpvarrvgfb\build\youtube_dl\downloader\http.py", line 58, in real_download
File "C:\Users\dst\AppData\Roaming\Build archive\youtube-dl\rg3\tmpvarrvgfb\build\youtube_dl\YoutubeDL.py", line 1996, in urlopen
File "C:\Python\Python34\lib\urllib\request.py", line 470, in open
response = meth(req, response)
File "C:\Python\Python34\lib\urllib\request.py", line 580, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python\Python34\lib\urllib\request.py", line 508, in error
return self._call_chain(*args)
File "C:\Python\Python34\lib\urllib\request.py", line 442, in _call_chain
result = func(*args)
File "C:\Python\Python34\lib\urllib\request.py", line 588, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request

# Fixed code
def append_url_to_file(target_url, tmp_filename, segment_name):
    target_filename = "%s-%s" % (tmp_filename, segment_name)
    count = 0
    while count <= fragment_retries:
        try:
            success = ctx["dl"].download(
                target_filename, {"url": combine_url(base_url, target_url)}
            )
            if not success:
                return False
            down, target_sanitized = sanitize_open(target_filename, "rb")
            ctx["dest_stream"].write(down.read())
            down.close()
            segments_filenames.append(target_sanitized)
            break
        except compat_urllib_error.HTTPError:
            # YouTube may often return 404 HTTP error for a fragment causing the
            # whole download to fail. However if the same fragment is immediately
            # retried with the same request data this usually succeeds (1-2 attemps
            # is usually enough) thus allowing to download the whole file successfully.
            # To be future-proof we will retry all fragments that fail with any
            # HTTP error.
            count += 1
            if count <= fragment_retries:
                self.report_retry_fragment(segment_name, count, fragment_retries)
    if count > fragment_retries:
        if skip_unavailable_fragments:
            self.report_skip_fragment(segment_name)
            return
        self.report_error("giving up after %s fragment retries" % fragment_retries)
        return False