Merge pull request #38 from philippeitis/patch-2
Browse files- pytube/query.py +15 -25
pytube/query.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
# -*- coding: utf-8 -*-
|
2 |
"""This module provides a query interface for media streams and captions."""
|
3 |
-
from typing import List, Optional
|
4 |
|
5 |
from pytube import Stream, Caption
|
6 |
|
@@ -162,16 +162,15 @@ class StreamQuery:
|
|
162 |
filters.append(lambda s: s.is_adaptive)
|
163 |
|
164 |
if custom_filter_functions:
|
165 |
-
|
166 |
-
filters.append(fn)
|
167 |
|
168 |
if is_dash is not None:
|
169 |
filters.append(lambda s: s.is_dash == is_dash)
|
170 |
|
171 |
fmt_streams = self.fmt_streams
|
172 |
for fn in filters:
|
173 |
-
fmt_streams =
|
174 |
-
return StreamQuery(fmt_streams)
|
175 |
|
176 |
def order_by(self, attribute_name: str) -> "StreamQuery":
|
177 |
"""Apply a sort order. Filters out stream the do not have the attribute.
|
@@ -182,30 +181,21 @@ class StreamQuery:
|
|
182 |
has_attribute = [
|
183 |
s for s in self.fmt_streams if getattr(s, attribute_name) is not None
|
184 |
]
|
185 |
-
|
186 |
-
integer_attr_repr: Optional[Dict[str, int]] = None
|
187 |
-
|
188 |
-
# check that the attribute value is a string
|
189 |
if has_attribute and isinstance(getattr(has_attribute[0], attribute_name), str):
|
190 |
-
#
|
|
|
191 |
try:
|
192 |
-
|
193 |
-
|
194 |
-
|
|
|
|
|
|
|
195 |
)
|
196 |
-
for s in has_attribute
|
197 |
-
}
|
198 |
-
except ValueError:
|
199 |
-
integer_attr_repr = None
|
200 |
-
|
201 |
-
# lookup integer values if we have them
|
202 |
-
if integer_attr_repr is not None:
|
203 |
-
return StreamQuery(
|
204 |
-
sorted(
|
205 |
-
has_attribute,
|
206 |
-
key=lambda s: integer_attr_repr[getattr(s, attribute_name)], # type: ignore # noqa: E501
|
207 |
)
|
208 |
-
|
|
|
209 |
|
210 |
return StreamQuery(
|
211 |
sorted(has_attribute, key=lambda s: getattr(s, attribute_name))
|
|
|
1 |
# -*- coding: utf-8 -*-
|
2 |
"""This module provides a query interface for media streams and captions."""
|
3 |
+
from typing import List, Optional
|
4 |
|
5 |
from pytube import Stream, Caption
|
6 |
|
|
|
162 |
filters.append(lambda s: s.is_adaptive)
|
163 |
|
164 |
if custom_filter_functions:
|
165 |
+
filters.extend(custom_filter_functions)
|
|
|
166 |
|
167 |
if is_dash is not None:
|
168 |
filters.append(lambda s: s.is_dash == is_dash)
|
169 |
|
170 |
fmt_streams = self.fmt_streams
|
171 |
for fn in filters:
|
172 |
+
fmt_streams = filter(fn, fmt_streams)
|
173 |
+
return StreamQuery(list(fmt_streams))
|
174 |
|
175 |
def order_by(self, attribute_name: str) -> "StreamQuery":
|
176 |
"""Apply a sort order. Filters out stream the do not have the attribute.
|
|
|
181 |
has_attribute = [
|
182 |
s for s in self.fmt_streams if getattr(s, attribute_name) is not None
|
183 |
]
|
184 |
+
# Check that the attributes have string values.
|
|
|
|
|
|
|
185 |
if has_attribute and isinstance(getattr(has_attribute[0], attribute_name), str):
|
186 |
+
# Try to return a StreamQuery sorted by the integer representations
|
187 |
+
# of the values.
|
188 |
try:
|
189 |
+
return StreamQuery(
|
190 |
+
sorted(
|
191 |
+
has_attribute,
|
192 |
+
key=lambda s: int(
|
193 |
+
"".join(filter(str.isdigit, getattr(s, attribute_name)))
|
194 |
+
), # type: ignore # noqa: E501
|
195 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
)
|
197 |
+
except ValueError:
|
198 |
+
pass
|
199 |
|
200 |
return StreamQuery(
|
201 |
sorted(has_attribute, key=lambda s: getattr(s, attribute_name))
|