Skip to content

Commit 3f5c896

Browse files
authored
Merge pull request TeamNewPipe#841 from TiA4f8R/yt-respect-order-of-clients-for-streams
[YouTube] Use correct order of clients to get better streams first
2 parents 5f8f392 + 9f9af35 commit 3f5c896

File tree

2 files changed

+136
-8
lines changed

2 files changed

+136
-8
lines changed

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.schabi.newpipe.extractor.stream.SubtitlesStream;
6161
import org.schabi.newpipe.extractor.stream.VideoStream;
6262
import org.schabi.newpipe.extractor.utils.JsonUtils;
63+
import org.schabi.newpipe.extractor.utils.Pair;
6364
import org.schabi.newpipe.extractor.utils.Parser;
6465
import org.schabi.newpipe.extractor.utils.Utils;
6566

@@ -71,7 +72,6 @@
7172
import java.util.ArrayList;
7273
import java.util.Arrays;
7374
import java.util.Collections;
74-
import java.util.HashMap;
7575
import java.util.LinkedHashMap;
7676
import java.util.List;
7777
import java.util.Locale;
@@ -1170,19 +1170,19 @@ private Map<String, ItagItem> getItags(@Nonnull final String streamingDataKey,
11701170
return urlAndItags;
11711171
}
11721172

1173-
final Map<String, JsonObject> streamingDataAndCpnLoopMap = new HashMap<>();
1173+
final List<Pair<JsonObject, String>> streamingDataAndCpnLoopList = new ArrayList<>();
11741174
// Use the androidStreamingData object first because there is no n param and no
11751175
// signatureCiphers in streaming URLs of the Android client
1176-
streamingDataAndCpnLoopMap.put(androidCpn, androidStreamingData);
1177-
streamingDataAndCpnLoopMap.put(html5Cpn, html5StreamingData);
1176+
streamingDataAndCpnLoopList.add(new Pair<>(androidStreamingData, androidCpn));
1177+
streamingDataAndCpnLoopList.add(new Pair<>(html5StreamingData, html5Cpn));
11781178
// Use the iosStreamingData object in the last position because most of the available
11791179
// streams can be extracted with the Android and web clients and also because the iOS
11801180
// client is only enabled by default on livestreams
1181-
streamingDataAndCpnLoopMap.put(iosCpn, iosStreamingData);
1181+
streamingDataAndCpnLoopList.add(new Pair<>(iosStreamingData, iosCpn));
11821182

1183-
for (final Map.Entry<String, JsonObject> entry : streamingDataAndCpnLoopMap.entrySet()) {
1184-
urlAndItags.putAll(getStreamsFromStreamingDataKey(entry.getValue(), streamingDataKey,
1185-
itagTypeWanted, entry.getKey()));
1183+
for (final Pair<JsonObject, String> pair : streamingDataAndCpnLoopList) {
1184+
urlAndItags.putAll(getStreamsFromStreamingDataKey(pair.getFirst(), streamingDataKey,
1185+
itagTypeWanted, pair.getSecond()));
11861186
}
11871187

11881188
return urlAndItags;
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package org.schabi.newpipe.extractor.utils;
2+
3+
import java.io.Serializable;
4+
import java.util.Objects;
5+
6+
/**
7+
* Serializable class to create a pair of objects.
8+
*
9+
* <p>
10+
* The two objects of the pair must be {@link Serializable serializable} and can be of the same
11+
* type.
12+
* </p>
13+
*
14+
* <p>
15+
* Note that this class is not intended to be used as a general-purpose pair and should only be
16+
* used when interfacing with the extractor.
17+
* </p>
18+
*
19+
* @param <F> the type of the first object, which must be {@link Serializable}
20+
* @param <S> the type of the second object, which must be {@link Serializable}
21+
*/
22+
public class Pair<F extends Serializable, S extends Serializable> implements Serializable {
23+
24+
/**
25+
* The first object of the pair.
26+
*/
27+
private F firstObject;
28+
29+
/**
30+
* The second object of the pair.
31+
*/
32+
private S secondObject;
33+
34+
/**
35+
* Creates a new {@link Pair} object.
36+
*
37+
* @param first the first object of the pair
38+
* @param second the second object of the pair
39+
*/
40+
public Pair(final F first, final S second) {
41+
firstObject = first;
42+
secondObject = second;
43+
}
44+
45+
/**
46+
* Sets the first object, which must be of the {@link F} type.
47+
*
48+
* @param first the new first object of the pair
49+
*/
50+
public void setFirst(final F first) {
51+
firstObject = first;
52+
}
53+
54+
/**
55+
* Sets the first object, which must be of the {@link S} type.
56+
*
57+
* @param second the new first object of the pair
58+
*/
59+
public void setSecond(final S second) {
60+
secondObject = second;
61+
}
62+
63+
/**
64+
* Gets the first object of the pair.
65+
*
66+
* @return the first object of the pair
67+
*/
68+
public F getFirst() {
69+
return firstObject;
70+
}
71+
72+
/**
73+
* Gets the second object of the pair.
74+
*
75+
* @return the second object of the pair
76+
*/
77+
public S getSecond() {
78+
return this.secondObject;
79+
}
80+
81+
/**
82+
* Returns a string representation of the current {@code Pair}.
83+
*
84+
* <p>
85+
* The string representation will look like this:
86+
* <code>
87+
* {<i>firstObject.toString()</i>, <i>secondObject.toString()</i>}
88+
* </code>
89+
* </p>
90+
*
91+
* @return a string representation of the current {@code Pair}
92+
*/
93+
@Override
94+
public String toString() {
95+
return "{" + firstObject + ", " + secondObject + "}";
96+
}
97+
98+
/**
99+
* Reveals whether an object is equal to this {@code Pair} instance.
100+
*
101+
* @param obj the object to compare with this {@code Pair} instance
102+
* @return whether an object is equal to this {@code Pair} instance
103+
*/
104+
@Override
105+
public boolean equals(final Object obj) {
106+
if (this == obj) {
107+
return true;
108+
}
109+
110+
if (obj == null || getClass() != obj.getClass()) {
111+
return false;
112+
}
113+
114+
final Pair<?, ?> pair = (Pair<?, ?>) obj;
115+
return Objects.equals(firstObject, pair.firstObject) && Objects.equals(secondObject,
116+
pair.secondObject);
117+
}
118+
119+
/**
120+
* Returns a hash code of the current {@code Pair} by using the first and second object.
121+
*
122+
* @return a hash code of the current {@code Pair}
123+
*/
124+
@Override
125+
public int hashCode() {
126+
return Objects.hash(firstObject, secondObject);
127+
}
128+
}

0 commit comments

Comments
 (0)