Reputation: 1
I used code of programm that you can see below.The logic of class is to get some properties from html code from YouTube page.For long time it worked fine, but now not. The reason of problem is the next: jdk/jre uses Internet explorer as default browser and now YouTube not support ie (It returns the page with suggestion of updating browser). The question is : how to change default browser taht java uses?
I switched the default browser of the system to Chrome and default browser of Intellij IDE to Chrome too, but it didn't give any result to me.
@Component(immediate = true, service = LastActualVideoService.class)
public class LastActualVideoServiceServiceImpl implements LastActualVideoService {
private final Logger logger = LoggerFactory.getLogger(getClass());
private static final String LINK_TO_YOU_TUBE = "https://www.youtube.com/embed/";
private static final String TRIGGER_FOR_VIDEO = "/watch?v=";
private static final String VIDEO_SELECTOR = "/videos";
private static final String HTML_SEPARATOR = "\\A";
private static final String ERROR_MASSAGE = "Incorrect input URL";
private static final String OPEN_TITLE_TAG = "<title>";
private static final String CLOSE_TITLE_TAG = "</title>";
@Override
public YouTubeChannelInfo getVideoBlob(String channelURL) {
channelURL = channelURL.concat(VIDEO_SELECTOR);
try (InputStream response = new URL(channelURL).openStream()) {
Scanner scanner = new Scanner(response);
String responseBody = scanner.useDelimiter(HTML_SEPARATOR).next();
String uniqueVideo = responseBody.substring(responseBody.indexOf(TRIGGER_FOR_VIDEO), responseBody.indexOf(TRIGGER_FOR_VIDEO) + 20);
String title = responseBody.substring(responseBody.indexOf(OPEN_TITLE_TAG) + 7, responseBody.indexOf(CLOSE_TITLE_TAG));
String linkToVideo = LINK_TO_YOU_TUBE.concat(uniqueVideo.substring(uniqueVideo.lastIndexOf('=') + 1));
return new YouTubeChannelInfo(linkToVideo, title, channelURL);
} catch (IOException e) {
logger.error(ERROR_MASSAGE, e);
return null;
}
}
}
Upvotes: 0
Views: 505
Reputation: 8163
URL.openStream does not "use the browser", your Java program acts as HTTP client itself. The way the remote server can know what type of browser is connecting is the user agent that the client sends with the request. It's possible that Youtube does not recognize or like whatever the default is.
Like Joachim Rohde commented, the solution is to manually set the user agent to something Youtube will recognize as supported.
Upvotes: 1