Mattwmaster58 commited on
Commit
01c7ca7
·
1 Parent(s): 63c3954

Cache filesize (#290)

Browse files

Cache the `filesize` so that it is only requested for once. Fixes #212

PS
those first four commits are just me not gitting it.

Files changed (1) hide show
  1. pytube/streams.py +5 -2
pytube/streams.py CHANGED
@@ -48,6 +48,7 @@ class Stream(object):
48
  self.res = None # resolution (e.g.: 480p, 720p, 1080p)
49
  self.url = None # signed download url
50
 
 
51
  self.mime_type = None # content identifier (e.g.: video/mp4)
52
  self.type = None # the part of the mime before the slash
53
  self.subtype = None # the part of the mime after the slash
@@ -159,8 +160,10 @@ class Stream(object):
159
  :returns:
160
  Filesize (in bytes) of the stream.
161
  """
162
- headers = request.get(self.url, headers=True)
163
- return int(headers['content-length'])
 
 
164
 
165
  @property
166
  def default_filename(self):
 
48
  self.res = None # resolution (e.g.: 480p, 720p, 1080p)
49
  self.url = None # signed download url
50
 
51
+ self._filesize = None # filesize in bytes
52
  self.mime_type = None # content identifier (e.g.: video/mp4)
53
  self.type = None # the part of the mime before the slash
54
  self.subtype = None # the part of the mime after the slash
 
160
  :returns:
161
  Filesize (in bytes) of the stream.
162
  """
163
+ if self._filesize is None:
164
+ headers = request.get(self.url, headers=True)
165
+ self._filesize = int(headers['content-length'])
166
+ return self._filesize
167
 
168
  @property
169
  def default_filename(self):