I have managed a lot of youtube videos (not owned by me) by youtube video ID to show them on my web page.
But sometimes videos were unavailable suddenly (because of copyright issues or simply deleted etc.)
So I have created tiny program for check availablitiy of youtube videos by youtube video ID.
Here is the code.
// print out unavailable videos String[] ids = new String[]{"id1", "id2"}; for(String id : ids) { Thread.sleep(1000); if(!checkExistance(id)) { System.out.println(id); } } public static String checkExistance(String id) { try{ // any method is fine as long as you can get contents from url getStringContentsFromURL("https://gdata.youtube.com/feeds/api/videos/"+id, "utf-8"); } catch(Exception e) { return false; } return true; }Example implementation for getStringContentsFromURL method.
Sorry the implementation is a bit circumlocutory because I designed the code is reusable in my project.
public static String getStringContentsFromURL(String u, String charset) throws URISyntaxException, IOException { URL url = new URL(u); HttpURLConnection connection = null; try{ connection = (HttpURLConnection)url.openConnection(); return toString(connection.getInputStream(), charset); } finally{ if(connection != null){ connection.disconnect(); } } } public static String toString(InputStream is, String charsetName) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); copy(is, baos, new byte[_1K_BYTES]); return baos.toString(charsetName); } public final static void copy(InputStream is, OutputStream os, byte[] buffer) throws IOException { try{ for (int bytes = 0 ;(bytes = is.read(buffer)) != -1; ) { os.write(buffer, 0, bytes); } os.flush(); }finally{ if(is != null) { is.close(); } } }
Don't call api without "sleep" time in your loop!!
Youtube api detects frequent access over threshold and will deny your access for a while.
And of course you should use api gentlemanly :)
コメント