Posted on So 07 April 2013

Informationen über ein Youtube-Video auslesen

Um Meta-Daten (z.B. Anzahl der Likes, Anzahl der Kommentare oder Titel) über ein Youtube-Video zu bekommen nutzt man ganz einfach die Youtube data API. Gibt man folgendes in den Browser ein, sendet die API Informationen zum angegebenen Video im JSON-Format. (Das Video wird mittels ID bestimmt. Die Video-ID steht in der URL eines Video-Aufrufs nach dem watch?v= https://www.youtube.com/watch?v=\<ID>)

Browser-Eingabe: http://gdata.youtube.com/feeds/api/videos/<ID>?v=2&alt=jsonc

Für eine bessere Übersicht sorgt der URL-Anhang &prettyprint=true.

Eingabe: http://gdata.youtube.com/feeds/api/videos/<ID>?v=2&alt=jsonc&prettyprint=true

Die Ausgabe, die von der API erzeugt wird, ist nichts weiter als ein String (Zeichenkette) der dekodiert werden muss. In PHP könnte dies so aussehen:

// <ID> wird durch die Youtube-Video-ID ersetzt
$j = json_decode(file_get_contents("http://gdata.youtube.com/feeds/api/videos/<ID>?v=2&alt=jsonc"));
// Einzelne Angaben können nun mit diesem Muster ausgegeben werden
echo "Video-Title: " . $j->data->title;

In Python könnte dies so aussehen:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib2
import simplejson

# <ID> wird durch die Youtube-Video-ID ersetzt
handle = urllib2.urlopen("http://gdata.youtube.com/feeds/api/videos/<ID>?v=2&alt=jsonc")
content = handle.read()
j = simplejson.loads(content)

print "Titel: {0}".format(j["data"]["title"])
print "Aufrufe: {0}".format(j["data"]["viewCount"])

Comments


There are no comments yet.

Add a Comment

You can use the Markdown syntax to format your comment.

Comment Atom Feed

© heiko. Built using Pelican. Theme by Giulio Fidente on github. .