Skip to content

Instantly share code, notes, and snippets.

@mauriciocolli
Created May 14, 2017 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauriciocolli/8ac78739bc4b316f6a947f3cc3ce52ea to your computer and use it in GitHub Desktop.
Save mauriciocolli/8ac78739bc4b316f6a947f3cc3ce52ea to your computer and use it in GitHub Desktop.
import org.schabi.newpipe.extractor.Downloader;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
import org.schabi.newpipe.extractor.channel.ChannelInfo;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
public class GetStreamUrl {
public static void main(String argv[]) throws ExtractionException, InterruptedException {
NewPipe.init(initDownloader());
String[] channels = {
"https://www.youtube.com/user/comedycentral",
"https://www.youtube.com/channel/UC65afEgL62PGFWXY7n6CUbA",
"https://www.youtube.com/channel/UCa10nxShhzNrCE1o2ZOPztg",
"https://www.youtube.com/user/LinusTechTips",
"https://www.youtube.com/user/NationalGeographic",
"https://www.youtube.com/user/greatscottlab"
};
StreamingService youtube = NewPipe.getService("Youtube");
for (String channel : channels) {
new Thread(() -> {
try {
ChannelExtractor cExtractor = youtube.getChannelExtractorInstance(channel, 0);
ChannelInfo cInfo = ChannelInfo.getInfo(cExtractor);
System.out.printf("%-25s %-10s page=0 %s\n", cInfo.channel_name, cInfo.subscriberCount, cInfo.feed_url);
if (cInfo.hasNextPage) {
System.out.printf("%-25s has next page\n", cInfo.channel_name);
String origChannelName = cInfo.channel_name;
new Thread(() -> {
try {
ChannelInfo c = ChannelInfo.getInfo(youtube.getChannelExtractorInstance(channel, 1));
System.out.printf("%-25s %-10s page=1 origName = [%-25s] %s\n", c.channel_name, c.subscriberCount, origChannelName, c.feed_url);
} catch (ExtractionException | IOException e) {
e.printStackTrace();
}
}).start();
}
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
}
private static Downloader initDownloader() {
return new Downloader() {
@Override
public String download(String siteUrl, String language) throws IOException, ReCaptchaException {
Map<String, String> requestProperties = new HashMap<>();
requestProperties.put("Accept-Language", language);
return download(siteUrl, requestProperties);
}
@Override
public String download(String siteUrl, Map<String, String> customProperties) throws IOException, ReCaptchaException {
URL url = new URL(siteUrl);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
BufferedReader in = null;
StringBuilder response = new StringBuilder();
try {
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0");
in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
} catch (UnknownHostException uhe) {//thrown when there's no internet connection
throw new IOException("unknown host or no network", uhe);
//Toast.makeText(getActivity(), uhe.getMessage(), Toast.LENGTH_LONG).show();
} catch (Exception e) {
/*
* HTTP 429 == Too Many Request
* Receive from Youtube.com = ReCaptcha challenge request
* See : https://github.com/rg3/youtube-dl/issues/5138
*/
if (con.getResponseCode() == 429) {
throw new ReCaptchaException("reCaptcha Challenge requested");
}
throw new IOException(e);
} finally {
if (in != null) {
in.close();
}
}
return response.toString();
}
@Override
public String download(String siteUrl) throws IOException, ReCaptchaException {
Map<String, String> requestProperties = new HashMap<>();
return download(siteUrl, requestProperties);
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment