Hi there,
Just been working with your code, and I needed to support loading a Playlist via the YouTube API (http://code.google.com/apis/youtube/2.0/reference.html#Playlist_feed)
It pretty much works out of the box, except for the ID of a video, which is buried down a couple more nodes.
Normally a video ID can be found at root->entry->id, which spits out a URL to the API call for a video (ex http://gdata.youtube.com/feeds/api/videos/ww_zIiLhWpA) which the module splits up into an array and grabs the last piece (ex. ww_zIiLhWpA)
For playlists though, it's located a bit further down: root->entry->group->videoid
To differentiate between reading a playlist feed vs. reading a normal search feed, we can look at the root->category field's "term" attribute:
http://gdata.youtube.com/schemas/2007#video
http://gdata.youtube.com/schemas/2007#playlist
Parse that, and then we can decide how to search for the ID.
I did it like this...
private function parseAtom(SimpleXMLElement $sxml, FeedsImportBatch $batch, FeedsSource $source) {
$batch->title = $feed_title = (string) $sxml->title;
// Detect whether we're dealing with a playlist or just a list of videos from a search
$category = $sxml->category->attributes();
$category = explode('#',$category['term']);
$playlist = $category[1] == "playlist" ? true : false;
// Iterate over entries in feed
// TODO: This is not DRY - extract things which is same in Atom and RSS20 to common method
foreach ($sxml->entry as $entry) {
// get video ID
$arr = explode('/', $entry->id);
$id = $arr[count($arr)-1];
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
// Check if we're dealing with a playlist, and redefine $id
if ($playlist) {
$yt = $media->children('http://gdata.youtube.com/schemas/2007');
$id = (string)$yt->videoid;
}
...
If you do something like that, the module will be able to support not just video search feeds but playlist feeds as well.
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | playlist_support-1161742.patch | 1.26 KB | jacobroufa |
Comments
Comment #2
wojtha commentedPlease, provide patch against current dev.
Comment #3
janton commentedis this in the current dev version?
i need to create taxonomy based on the playlist. (if possible)
I'm not sure yet how i will do that but ok that is of course my problem :)
Comment #4
jacobroufa commentedWriting a patch against 7.x-2.0-beta1 right now, as I need this feature as well. Should have something later tonight or tomorrow.
Comment #5
jacobroufa commentedOk. So, after much annoyance with $yt->videoid printing NULL and what seems like a day wasted... 7.x-2.x-dev supports playlists out of the box, at least in Atom. I'll check to see if RSS does or not, and if not I'll add it and provide a patch. But, basically, you just have to append &v=2.1 to the end of your url and playlists are imported just fine.
Comment #6
jacobroufa commentedSee attached patch against git branch 7.x-2.x for RSS support (again, requiring &v=2.1 appended to the URL).
Not entirely unrelated to this issue - Might I suggest a basic README included with the module so one doesn't have to search the issue queue to figure out what API version to use in their query? Also, could provide some basic instruction in the use of the module.
Also, maybe this issue could be merged with #1272366: Request to support only Atom feeds for YouTube API version 2.1? The changes that Mohammed made to the dev version basically require the use of API v2.1, and this issue is not unrelated.