java youtube verbinden!

wintersun

Mitglied
hallo leute,

Ich bin programieranfänger und hätte gerne wissen, wie man die vidoes auf youtube mit java Code suchen?
d.h.Ich gebe zum Beispiel ein String search = "irgendwas" und wenn ich mein Program kompiliere, möchte ich, dass mein Programm mit youtube verbindet und mir die Vidoes ausgibt..

Danke im Vorasu!
 
Danke für deine Antowrt,
Ich habe die folgende Code schon gefunden aber ich bekomme eine Fehlermeldung beim YouTubeService, weil ich ClienID, und developer_key dazu schreiben muss.
Ich weiß nicht wo ich es genau finde.

Java:
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gdata.client.GoogleService;
import com.google.gdata.client.Service;
import com.google.gdata.client.media.MediaService;
import com.google.gdata.client.youtube.YouTubeQuery;
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.geo.impl.GeoRssWhere;
import com.google.gdata.data.media.mediarss.MediaKeywords;
import com.google.gdata.data.media.mediarss.MediaPlayer;
import com.google.gdata.data.media.mediarss.MediaThumbnail;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.VideoFeed;
import com.google.gdata.data.youtube.YouTubeMediaContent;
import com.google.gdata.data.youtube.YouTubeMediaGroup;
import com.google.gdata.data.youtube.YouTubeMediaRating;
import com.google.gdata.data.youtube.YtPublicationState;
import com.google.gdata.data.youtube.YtStatistics;
import com.google.gdata.model.gd.Rating;
import com.google.gdata.util.ServiceException;

public class VideoSearch {

	public static void main( String args [] ) throws IOException, ServiceException {
		
		YouTubeService service = new YouTubeService(ClientID,developer_key); // !ERROR 
		
		YouTubeQuery query = 
			new YouTubeQuery(new URL("http://gdata.youtube.com/feeds/api/videos"));
		query.setFullTextQuery("Hallo World");
		
		VideoFeed videoFeed = service.query(query, VideoFeed.class);
		printEntireVideoFeed(service, videoFeed, false);
		

		 
	}
	public static void printEntireVideoFeed(YouTubeService service, 
			VideoFeed videoFeed, boolean detailed) throws MalformedURLException, 
			IOException, ServiceException {
		do {
			printVideoFeed(videoFeed, detailed);
			if(videoFeed.getNextLink() != null) {
				videoFeed = service.getFeed(new URL(videoFeed.getNextLink().getHref()), 
						VideoFeed.class);
			}
			else {
				videoFeed = null;
			}
		}
		while(videoFeed != null);
	}

	public static void printVideoFeed(VideoFeed videoFeed, boolean detailed) {
		for(VideoEntry videoEntry : videoFeed.getEntries() ) {
			printVideoEntry(videoEntry, detailed);
		}
	}

	public static void printVideoEntry(VideoEntry videoEntry, boolean detailed) {
		System.out.println("Title: " + videoEntry.getTitle().getPlainText());

		if(videoEntry.isDraft()) {
			System.out.println("Video is not live");
			YtPublicationState pubState = videoEntry.getPublicationState();
			if(pubState.getState() == YtPublicationState.State.PROCESSING) {
				System.out.println("Video is still being processed.");
			}
			else if(pubState.getState() == YtPublicationState.State.REJECTED) {
				System.out.print("Video has been rejected because: ");
				System.out.println(pubState.getDescription());
				System.out.print("For help visit: ");
				System.out.println(pubState.getHelpUrl());
			}
			else if(pubState.getState() == YtPublicationState.State.FAILED) {
				System.out.print("Video failed uploading because: ");
				System.out.println(pubState.getDescription());
				System.out.print("For help visit: ");
				System.out.println(pubState.getHelpUrl());
			}
		}

		if(videoEntry.getEditLink() != null) {
			System.out.println("Video is editable by current user.");
		}

		if(detailed) {

			YouTubeMediaGroup mediaGroup = videoEntry.getMediaGroup();

			System.out.println("Uploaded by: " + mediaGroup.getUploader());

			System.out.println("Video ID: " + mediaGroup.getVideoId());
			System.out.println("Description: " + 
					mediaGroup.getDescription().getPlainTextContent());

			MediaPlayer mediaPlayer = mediaGroup.getPlayer();
			System.out.println("Web Player URL: " + mediaPlayer.getUrl());
			MediaKeywords keywords = mediaGroup.getKeywords();
			System.out.print("Keywords: ");
			for(String keyword : keywords.getKeywords()) {
				System.out.print(keyword + ",");
			}

			GeoRssWhere location = videoEntry.getGeoCoordinates();
			if(location != null) {
				System.out.println("Latitude: " + location.getLatitude());
				System.out.println("Longitude: " + location.getLongitude());
			}

			com.google.gdata.data.extensions.Rating rating = videoEntry.getRating();
			if(rating != null) {
				System.out.println("Average rating: " + rating.getAverage());
			}

			YtStatistics stats = videoEntry.getStatistics();
			if(stats != null ) {
				System.out.println("View count: " + stats.getViewCount());
			}
			System.out.println();

			System.out.println("\tThumbnails:");
			for(MediaThumbnail mediaThumbnail : mediaGroup.getThumbnails()) {
				System.out.println("\t\tThumbnail URL: " + mediaThumbnail.getUrl());
				System.out.println("\t\tThumbnail Time Index: " +
						mediaThumbnail.getTime());
				System.out.println();
			}

			System.out.println("\tMedia:");
			for(YouTubeMediaContent mediaContent : mediaGroup.getYouTubeContents()) {
				System.out.println("\t\tMedia Location: "+ mediaContent.getUrl());
				System.out.println("\t\tMedia Type: "+ mediaContent.getType());
				System.out.println("\t\tDuration: " + mediaContent.getDuration());
				System.out.println();
			}

			for(YouTubeMediaRating mediaRating : mediaGroup.getYouTubeRatings()) {
				System.out.println("Video restricted in the following countries: " +
						mediaRating.getCountries().toString());
			}
		}
	}

}

Danke im Voraus!
Lg.
 
Wenn du nur suchen möchtest und nicht irgendetwas updaten oder hochladen etc. brauchst du dich nicht unbedingt zu authentifizieren, da reicht es glaube ich aus wenn du den Service wie folgt erstellst:
[c]YouTubeService service = new YouTubeService("");[/c]

Ob du dadurch noch weiter eingeschränkt bist, weiß ich jetzt so aus dem Kopf auch nicht mehr, das steht aber sicherlich auch irgendwo auf der Seite, funktionieren müsste es so aber dennoch erstmal ^^
 
Hi,

Danke für deine schnelle Antwort. 🙂

Was meinst du dass es erstmal funtionieren muss?
Ich hab das Service auch ohne Id, und key probiert aber dann bekomme ich die folgende Fehlermeldung.

Java:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/MessagingException
	at youtube.VideoSearch.main(VideoSearch.java:34)
Caused by: java.lang.ClassNotFoundException: javax.mail.MessagingException
	at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)
	... 1 more

Danke im Voraus!
 
Hi,

Danke es funtioniert..🙂
Ich habe aber noch eine Frege bitte.
Wenn ich jetzt mein Programm kompiliere, dann bekomme ich nur Titel von dem Suchwort in der Console.
Ich möchte aber direkt mit dem Browser verbinden ( z.B.: firefox, exprorer, safari..usw.)
dh. Wenn ich mein program einschalte dann will ich einfach mit youtube verbinden und dann die Videos was es in YouTube gibt sehen.

Könnte jemand auch noch helfen, wie ich das machen soll?

Danke im Voraus
Lg.
 
Ich möchte aber direkt mit dem Browser verbinden ( z.B.: firefox, exprorer, safari..usw.)
dh. Wenn ich mein program einschalte dann will ich einfach mit youtube verbinden und dann die Videos was es in YouTube gibt sehen.

Kannst du nicht vllt ein wenig detaillierter beschreiben was du vorhast?
Denn so wie man das jetzt interpretieren könnte, würde evtl. auch so etwas (grob gesagt) ausreichen wie:
Java:
	public static void main(String[] args) throws Exception {
		String query = "java hello world";
		if (Desktop.isDesktopSupported()) {
			query = query.replace(" ", "%20");
			Desktop.getDesktop().browse(
					new URI("http://www.youtube.com/results?search_query=" + query));
		}
	}

und man bräuchte die API dann gar nicht ?!
 
Hallo zusammen,

Ich hätte noch eine Frage dazu und zwar würde ich gerne wissen wie ich folgendes Code in Gui erstellen will.
d.h. : Wenn ich das Code kommpiliere, wird es mit dem Browser verbinden und dann sehe ich einfach die gesuchten Videos, wie viel es gibt.
Ich möchte aber in Gui die gesuchte Videos sehen, nicht in Browser.
Ich würde deswegen wissen wie ich es machen soll?

Hier ist es Code:
Java:
public static void main(String[] args) throws Exception {
       String query = "java hello world";
        
        if (Desktop.isDesktopSupported()) {
            query = query.replace(" ", "%50");
            // for Youtube..
            Desktop.getDesktop().browse(new URI("http://www.youtube.com/results?search_query=" + query));
        
        }
    }

Danke im Voraus!

Lg.
 
Zuletzt bearbeitet:

Zurück
Oben