[downloader/fragment] remove unused code
[youtube-dl] / youtube_dl / downloader / fragment.py
index 44a3c10403bedb2380178320d42729f94d63eee5..0a3b1ece066a8784a621235118145c01b6154850 100644 (file)
@@ -3,6 +3,7 @@ from __future__ import division, unicode_literals
 import os
 import time
 import io
+import json
 
 from .common import FileDownloader
 from .http import HttpFD
@@ -11,7 +12,6 @@ from ..utils import (
     encodeFilename,
     sanitize_open,
     sanitized_Request,
-    compat_str,
 )
 
 
@@ -63,8 +63,12 @@ class FragmentFD(FileDownloader):
     def _append_fragment(self, ctx, frag_content):
         ctx['dest_stream'].write(frag_content)
         if not (ctx.get('live') or ctx['tmpfilename'] == '-'):
-            frag_index_stream, _ = sanitize_open(ctx['tmpfilename'] + '.fragindex', 'w')
-            frag_index_stream.write(compat_str(ctx['frag_index']))
+            frag_index_stream, _ = sanitize_open(self.ytdl_filename(ctx['filename']), 'w')
+            frag_index_stream.write(json.dumps({
+                'download': {
+                    'last_fragment_index': ctx['fragment_index']
+                },
+            }))
             frag_index_stream.close()
 
     def _prepare_frag_download(self, ctx):
@@ -94,9 +98,10 @@ class FragmentFD(FileDownloader):
         if os.path.isfile(encodeFilename(tmpfilename)):
             open_mode = 'ab'
             resume_len = os.path.getsize(encodeFilename(tmpfilename))
-            if os.path.isfile(encodeFilename(tmpfilename + '.fragindex')):
-                frag_index_stream, _ = sanitize_open(tmpfilename + '.fragindex', 'r')
-                frag_index = int(frag_index_stream.read())
+            ytdl_filename = encodeFilename(self.ytdl_filename(ctx['filename']))
+            if os.path.isfile(ytdl_filename):
+                frag_index_stream, _ = sanitize_open(ytdl_filename, 'r')
+                frag_index = json.loads(frag_index_stream.read())['download']['last_fragment_index']
                 frag_index_stream.close()
         dest_stream, tmpfilename = sanitize_open(tmpfilename, open_mode)
 
@@ -104,7 +109,7 @@ class FragmentFD(FileDownloader):
             'dl': dl,
             'dest_stream': dest_stream,
             'tmpfilename': tmpfilename,
-            'frag_index': frag_index,
+            'fragment_index': frag_index,
             # Total complete fragments downloaded so far in bytes
             'complete_frags_downloaded_bytes': resume_len,
         })
@@ -116,8 +121,8 @@ class FragmentFD(FileDownloader):
         state = {
             'status': 'downloading',
             'downloaded_bytes': ctx['complete_frags_downloaded_bytes'],
-            'frag_index': ctx['frag_index'],
-            'frag_count': total_frags,
+            'fragment_index': ctx['fragment_index'],
+            'fragment_count': total_frags,
             'filename': ctx['filename'],
             'tmpfilename': ctx['tmpfilename'],
         }
@@ -140,12 +145,12 @@ class FragmentFD(FileDownloader):
             if not ctx['live']:
                 estimated_size = (
                     (ctx['complete_frags_downloaded_bytes'] + frag_total_bytes) /
-                    (state['frag_index'] + 1) * total_frags)
+                    (state['fragment_index'] + 1) * total_frags)
                 state['total_bytes_estimate'] = estimated_size
 
             if s['status'] == 'finished':
-                state['frag_index'] += 1
-                ctx['frag_index'] = state['frag_index']
+                state['fragment_index'] += 1
+                ctx['fragment_index'] = state['fragment_index']
                 state['downloaded_bytes'] += frag_total_bytes - ctx['prev_frag_downloaded_bytes']
                 ctx['complete_frags_downloaded_bytes'] = state['downloaded_bytes']
                 ctx['prev_frag_downloaded_bytes'] = 0
@@ -167,8 +172,9 @@ class FragmentFD(FileDownloader):
 
     def _finish_frag_download(self, ctx):
         ctx['dest_stream'].close()
-        if os.path.isfile(encodeFilename(ctx['tmpfilename'] + '.fragindex')):
-            os.remove(encodeFilename(ctx['tmpfilename'] + '.fragindex'))
+        ytdl_filename = encodeFilename(self.ytdl_filename(ctx['filename']))
+        if os.path.isfile(ytdl_filename):
+            os.remove(ytdl_filename)
         elapsed = time.time() - ctx['started']
         self.try_rename(ctx['tmpfilename'], ctx['filename'])
         fsize = os.path.getsize(encodeFilename(ctx['filename']))