Skip to content

Commit e8e535b

Browse files
committed
mediaccc: update linkhandlers & refactor tests
1 parent e7be952 commit e8e535b

5 files changed

Lines changed: 293 additions & 122 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/MediaCCCStreamExtractor.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,13 @@ public List<VideoStream> getVideoOnlyStreams() throws IOException, ExtractionExc
170170
return null;
171171
}
172172

173-
@Nonnull
174173
@Override
175174
public List<SubtitlesStream> getSubtitlesDefault() throws IOException, ExtractionException {
176175
return null;
177176
}
178177

179-
@Nonnull
180178
@Override
181-
public List<SubtitlesStream> getSubtitles(MediaFormat format) throws IOException, ExtractionException {
179+
public List<SubtitlesStream> getSubtitles(final MediaFormat format) throws IOException, ExtractionException {
182180
return null;
183181
}
184182

@@ -212,7 +210,6 @@ public void onFetchPage(@Nonnull Downloader downloader) throws IOException, Extr
212210
} catch (JsonParserException jpe) {
213211
throw new ExtractionException("Could not parse json returned by url: " + getLinkHandler().getUrl(), jpe);
214212
}
215-
216213
}
217214

218215
@Nonnull
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package org.schabi.newpipe.extractor.services.media_ccc.linkHandler;
22

3+
import org.schabi.newpipe.extractor.exceptions.FoundAdException;
34
import org.schabi.newpipe.extractor.exceptions.ParsingException;
45
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
56
import org.schabi.newpipe.extractor.utils.Parser;
7+
import org.schabi.newpipe.extractor.utils.Utils;
68

9+
import java.net.MalformedURLException;
10+
import java.net.URL;
711
import java.util.List;
812

913
public class MediaCCCConferenceLinkHandlerFactory extends ListLinkHandlerFactory {
@@ -14,19 +18,43 @@ public String getUrl(String id, List<String> contentFilter, String sortFilter) t
1418
}
1519

1620
@Override
17-
public String getId(String url) throws ParsingException {
18-
if (url.startsWith("https://api.media.ccc.de/public/conferences/")) {
19-
return url.replace("https://api.media.ccc.de/public/conferences/", "");
20-
} else if (url.startsWith("https://media.ccc.de/c/")) {
21-
return Parser.matchGroup1("https://media.ccc.de/c/([^?#]*)", url);
22-
} else {
23-
throw new ParsingException("Could not get id from url: " + url);
21+
public String getId(String urlString) throws ParsingException {
22+
if (urlString.startsWith("https://api.media.ccc.de/public/conferences/")) {
23+
return urlString.replace("https://api.media.ccc.de/public/conferences/", "");
24+
} else if (urlString.startsWith("https://media.ccc.de/c/")) {
25+
return Parser.matchGroup1("https://media.ccc.de/c/([^?#]*)", urlString);
2426
}
27+
28+
URL url;
29+
try {
30+
url = Utils.stringToURL(urlString);
31+
} catch (MalformedURLException e) {
32+
throw new IllegalArgumentException("The given URL is not valid");
33+
}
34+
35+
String path = url.getPath();
36+
// remove leading "/" of URL-path if URL-path is given
37+
if (!path.isEmpty()) {
38+
path = path.substring(1);
39+
}
40+
41+
if (path.contains("b/")) {
42+
return path.substring(2);
43+
}
44+
45+
throw new ParsingException("Could not get id from url: " + url);
46+
2547
}
2648

2749
@Override
2850
public boolean onAcceptUrl(String url) throws ParsingException {
29-
return url.startsWith("https://api.media.ccc.de/public/conferences/")
30-
|| url.startsWith("https://media.ccc.de/c/");
51+
try {
52+
getId(url);
53+
return true;
54+
} catch (FoundAdException fe) {
55+
throw fe;
56+
} catch (ParsingException e) {
57+
return false;
58+
}
3159
}
3260
}
Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
11
package org.schabi.newpipe.extractor.services.media_ccc.linkHandler;
22

3+
import org.schabi.newpipe.extractor.exceptions.FoundAdException;
34
import org.schabi.newpipe.extractor.exceptions.ParsingException;
45
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
6+
import org.schabi.newpipe.extractor.utils.Utils;
7+
8+
import java.net.MalformedURLException;
9+
import java.net.URL;
510

611
public class MediaCCCStreamLinkHandlerFactory extends LinkHandlerFactory {
712

813
@Override
9-
public String getId(String url) throws ParsingException {
10-
if (url.startsWith("https://api.media.ccc.de/public/events/") &&
11-
!url.contains("?q=")) {
12-
return url.replace("https://api.media.ccc.de/public/events/", "");
14+
public String getId(String urlString) throws ParsingException {
15+
if (urlString.startsWith("https://api.media.ccc.de/public/events/") &&
16+
!urlString.contains("?q=")) {
17+
return urlString.replace("https://api.media.ccc.de/public/events/", "");
18+
}
19+
20+
URL url;
21+
try {
22+
url = Utils.stringToURL(urlString);
23+
} catch (MalformedURLException e) {
24+
throw new IllegalArgumentException("The given URL is not valid");
25+
}
26+
27+
String path = url.getPath();
28+
// remove leading "/" of URL-path if URL-path is given
29+
if (!path.isEmpty()) {
30+
path = path.substring(1);
1331
}
32+
33+
if (path.contains("v/")) {
34+
return path.substring(2);
35+
}
36+
1437
throw new ParsingException("Could not get id from url: " + url);
1538
}
1639

@@ -21,7 +44,13 @@ public String getUrl(String id) throws ParsingException {
2144

2245
@Override
2346
public boolean onAcceptUrl(String url) throws ParsingException {
24-
return url.startsWith("https://api.media.ccc.de/public/events/") &&
25-
!url.contains("?q=");
47+
try {
48+
getId(url);
49+
return true;
50+
} catch (FoundAdException fe) {
51+
throw fe;
52+
} catch (ParsingException e) {
53+
return false;
54+
}
2655
}
2756
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/media_ccc/MediaCCCConferenceExtractorTest.java

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,86 @@
44
import org.junit.Test;
55
import org.schabi.newpipe.DownloaderTestImpl;
66
import org.schabi.newpipe.extractor.NewPipe;
7-
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
87
import org.schabi.newpipe.extractor.services.media_ccc.extractors.MediaCCCConferenceExtractor;
98

109
import static junit.framework.TestCase.assertEquals;
10+
import static junit.framework.TestCase.assertTrue;
1111
import static org.schabi.newpipe.extractor.ServiceList.MediaCCC;
1212

1313
/**
1414
* Test {@link MediaCCCConferenceExtractor}
1515
*/
1616
public class MediaCCCConferenceExtractorTest {
17-
private static ChannelExtractor extractor;
1817

19-
@BeforeClass
20-
public static void setUpClass() throws Exception {
21-
NewPipe.init(DownloaderTestImpl.getInstance());
22-
extractor = MediaCCC.getChannelExtractor("https://api.media.ccc.de/public/conferences/froscon2017");
23-
extractor.fetchPage();
24-
}
18+
public static class FrOSCon2017 {
19+
private static MediaCCCConferenceExtractor extractor;
2520

26-
@Test
27-
public void testName() throws Exception {
28-
assertEquals("FrOSCon 2017", extractor.getName());
29-
}
21+
@BeforeClass
22+
public static void setUpClass() throws Exception {
23+
NewPipe.init(DownloaderTestImpl.getInstance());
24+
extractor = (MediaCCCConferenceExtractor) MediaCCC.getChannelExtractor("https://media.ccc.de/c/froscon2017");
25+
extractor.fetchPage();
26+
}
3027

31-
@Test
32-
public void testGetUrl() throws Exception {
33-
assertEquals("https://api.media.ccc.de/public/conferences/froscon2017", extractor.getUrl());
34-
}
28+
@Test
29+
public void testName() throws Exception {
30+
assertEquals("FrOSCon 2017", extractor.getName());
31+
}
3532

36-
@Test
37-
public void testGetOriginalUrl() throws Exception {
38-
assertEquals("https://media.ccc.de/c/froscon2017", extractor.getOriginalUrl());
39-
}
33+
@Test
34+
public void testGetUrl() throws Exception {
35+
assertEquals("https://api.media.ccc.de/public/conferences/froscon2017", extractor.getUrl());
36+
}
37+
38+
@Test
39+
public void testGetOriginalUrl() throws Exception {
40+
assertEquals("https://media.ccc.de/c/froscon2017", extractor.getOriginalUrl());
41+
}
42+
43+
@Test
44+
public void testGetThumbnailUrl() throws Exception {
45+
assertEquals("https://static.media.ccc.de/media/events/froscon/2017/logo.png", extractor.getAvatarUrl());
46+
}
4047

41-
@Test
42-
public void testGetThumbnailUrl() throws Exception {
43-
assertEquals("https://static.media.ccc.de/media/events/froscon/2017/logo.png", extractor.getAvatarUrl());
48+
@Test
49+
public void testGetInitalPage() throws Exception {
50+
assertEquals(97, extractor.getInitialPage().getItems().size());
51+
}
4452
}
4553

46-
@Test
47-
public void testGetInitalPage() throws Exception {
48-
assertEquals(97, extractor.getInitialPage().getItems().size());
54+
public static class Oscal2019 {
55+
private static MediaCCCConferenceExtractor extractor;
56+
57+
@BeforeClass
58+
public static void setUpClass() throws Exception {
59+
NewPipe.init(DownloaderTestImpl.getInstance());
60+
extractor = (MediaCCCConferenceExtractor) MediaCCC.getChannelExtractor("https://media.ccc.de/c/oscal19");
61+
extractor.fetchPage();
62+
}
63+
64+
@Test
65+
public void testName() throws Exception {
66+
assertEquals("Open Source Conference Albania 2019", extractor.getName());
67+
}
68+
69+
@Test
70+
public void testGetUrl() throws Exception {
71+
assertEquals("https://api.media.ccc.de/public/conferences/oscal19", extractor.getUrl());
72+
}
73+
74+
@Test
75+
public void testGetOriginalUrl() throws Exception {
76+
assertEquals("https://media.ccc.de/c/oscal19", extractor.getOriginalUrl());
77+
}
78+
79+
@Test
80+
public void testGetThumbnailUrl() throws Exception {
81+
assertEquals("https://static.media.ccc.de/media/events/oscal/2019/oscal-19.png", extractor.getAvatarUrl());
82+
}
83+
84+
@Test
85+
public void testGetInitalPage() throws Exception {
86+
assertTrue(extractor.getInitialPage().getItems().size() >= 21);
87+
}
4988
}
5089
}

0 commit comments

Comments
 (0)