Merge pull request #287 from Mattwmaster58/numerical
Browse files- pytube/query.py +15 -1
- tests/test_query.py +24 -1
pytube/query.py
CHANGED
@@ -162,9 +162,23 @@ class StreamQuery:
|
|
162 |
:param str attribute_name:
|
163 |
The name of the attribute to sort by.
|
164 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
fmt_streams = sorted(
|
166 |
self.fmt_streams,
|
167 |
-
key=
|
168 |
)
|
169 |
return StreamQuery(fmt_streams)
|
170 |
|
|
|
162 |
:param str attribute_name:
|
163 |
The name of the attribute to sort by.
|
164 |
"""
|
165 |
+
integer_attr_repr = {}
|
166 |
+
for stream in self.fmt_streams:
|
167 |
+
attr = getattr(stream, attribute_name)
|
168 |
+
if attr is None:
|
169 |
+
break
|
170 |
+
num = [char for char in attr if char.isdigit()]
|
171 |
+
integer_attr_repr[attr] = int(''.join(num)) if num else None
|
172 |
+
|
173 |
+
# if every attribute has an integer representation
|
174 |
+
if integer_attr_repr and all(integer_attr_repr.values()):
|
175 |
+
def key(s): return integer_attr_repr[getattr(s, attribute_name)]
|
176 |
+
else:
|
177 |
+
def key(s): return getattr(s, attribute_name)
|
178 |
+
|
179 |
fmt_streams = sorted(
|
180 |
self.fmt_streams,
|
181 |
+
key=key
|
182 |
)
|
183 |
return StreamQuery(fmt_streams)
|
184 |
|
tests/test_query.py
CHANGED
@@ -80,6 +80,7 @@ def test_order_by_descending(cipher_signature):
|
|
80 |
"""Ensure :meth:`~pytube.StreamQuery.desc` sorts the list of
|
81 |
:class:`Stream <Stream>` instances in the reverse order.
|
82 |
"""
|
|
|
83 |
itags = [
|
84 |
s.itag for s in cipher_signature.streams
|
85 |
.filter(progressive=True)
|
@@ -87,14 +88,25 @@ def test_order_by_descending(cipher_signature):
|
|
87 |
.desc()
|
88 |
.all()
|
89 |
]
|
90 |
-
|
91 |
assert itags == ['43', '36', '22', '18', '17']
|
92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
def test_order_by_ascending(cipher_signature):
|
95 |
"""Ensure :meth:`~pytube.StreamQuery.desc` sorts the list of
|
96 |
:class:`Stream <Stream>` instances in ascending order.
|
97 |
"""
|
|
|
98 |
itags = [
|
99 |
s.itag for s in cipher_signature.streams
|
100 |
.filter(progressive=True)
|
@@ -105,6 +117,17 @@ def test_order_by_ascending(cipher_signature):
|
|
105 |
|
106 |
assert itags == ['17', '18', '22', '36', '43']
|
107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
def test_get_by_itag(cipher_signature):
|
110 |
"""Ensure :meth:`~pytube.StreamQuery.get_by_itag` returns the expected
|
|
|
80 |
"""Ensure :meth:`~pytube.StreamQuery.desc` sorts the list of
|
81 |
:class:`Stream <Stream>` instances in the reverse order.
|
82 |
"""
|
83 |
+
# numerical values
|
84 |
itags = [
|
85 |
s.itag for s in cipher_signature.streams
|
86 |
.filter(progressive=True)
|
|
|
88 |
.desc()
|
89 |
.all()
|
90 |
]
|
|
|
91 |
assert itags == ['43', '36', '22', '18', '17']
|
92 |
|
93 |
+
# non numerical values
|
94 |
+
mime_types = [
|
95 |
+
s.mime_type for s in cipher_signature.streams
|
96 |
+
.filter(progressive=True)
|
97 |
+
.order_by('mime_type')
|
98 |
+
.desc()
|
99 |
+
.all()
|
100 |
+
]
|
101 |
+
assert mime_types == ['video/webm', 'video/mp4',
|
102 |
+
'video/mp4', 'video/3gpp', 'video/3gpp']
|
103 |
+
|
104 |
|
105 |
def test_order_by_ascending(cipher_signature):
|
106 |
"""Ensure :meth:`~pytube.StreamQuery.desc` sorts the list of
|
107 |
:class:`Stream <Stream>` instances in ascending order.
|
108 |
"""
|
109 |
+
# numerical values
|
110 |
itags = [
|
111 |
s.itag for s in cipher_signature.streams
|
112 |
.filter(progressive=True)
|
|
|
117 |
|
118 |
assert itags == ['17', '18', '22', '36', '43']
|
119 |
|
120 |
+
# non numerical values
|
121 |
+
mime_types = [
|
122 |
+
s.mime_type for s in cipher_signature.streams
|
123 |
+
.filter(progressive=True)
|
124 |
+
.order_by('mime_type')
|
125 |
+
.asc()
|
126 |
+
.all()
|
127 |
+
]
|
128 |
+
assert mime_types == ['video/3gpp', 'video/3gpp',
|
129 |
+
'video/mp4', 'video/mp4', 'video/webm']
|
130 |
+
|
131 |
|
132 |
def test_get_by_itag(cipher_signature):
|
133 |
"""Ensure :meth:`~pytube.StreamQuery.get_by_itag` returns the expected
|