2008-09-11 04:40:07 +00:00
{
"translatorID" : "d3b1d34c-f8a1-43bb-9dd6-27aa6403b217" ,
"translatorType" : 4 ,
"label" : "YouTube" ,
"creator" : "Sean Takats and Michael Berkowitz" ,
"target" : "https?://[^/]*youtube\\.com\\/" ,
"minVersion" : "1.0.0rc4" ,
"maxVersion" : "" ,
"priority" : 100 ,
"inRepository" : true ,
2008-10-29 16:58:10 +00:00
"lastUpdated" : "2008-10-29 17:05:00"
2008-09-11 04:40:07 +00:00
}
function detectWeb ( doc , url ) {
var namespace = doc . documentElement . namespaceURI ;
var nsResolver = namespace ? function ( prefix ) {
if ( prefix == 'x' ) return namespace ; else return null ;
} : null ;
var xpath = '//input[@type="hidden" and @name="video_id"]' ;
if ( doc . evaluate ( xpath , doc , nsResolver , XPathResult . ANY _TYPE , null ) . iterateNext ( ) ) {
return "videoRecording" ;
}
if ( doc . evaluate ( '//div[@class="vtitle"]/a[@class="vtitlelink" and contains(@href, "/watch?v=")]' , doc , nsResolver , XPathResult . ANY _TYPE , null ) . iterateNext ( ) ) {
return "multiple" ;
}
if ( doc . evaluate ( '//div[starts-with(@class, "vtitle")]/a[contains(@href, "/watch?v=")]' , doc , nsResolver , XPathResult . ANY _TYPE , null ) . iterateNext ( ) ) {
return "multiple" ;
}
if ( doc . evaluate ( '//div[@class="vltitle"]/div[@class="vlshortTitle"]/a[contains(@href, "/watch?v=")]' , doc , nsResolver , XPathResult . ANY _TYPE , null ) . iterateNext ( ) ) {
return "multiple" ;
}
}
function doWeb ( doc , url ) {
var namespace = doc . documentElement . namespaceURI ;
var nsResolver = namespace ? function ( prefix ) {
if ( prefix == 'x' ) return namespace ; else return null ;
} : null ;
var host = doc . location . host ;
var video _ids = new Array ( ) ;
var xpath = '//input[@type="hidden" and @name="video_id"]' ;
var elmts ;
var elmt ;
elmts = doc . evaluate ( xpath , doc , nsResolver , XPathResult . ANY _TYPE , null ) ;
elmt = elmts . iterateNext ( ) ;
if ( elmt ) {
//single video
var video _id = elmt . value ;
video _ids . push ( video _id ) ;
} else {
// multiple videos
var items = new Object ( ) ;
var videoRe = /\/watch\?v=([a-zA-Z0-9-_]+)/ ;
// search results
if ( elmt = doc . evaluate ( '//div[@class="vtitle"]/a[@class="vtitlelink" and contains(@href, "/watch?v=")]' , doc , nsResolver , XPathResult . ANY _TYPE , null ) . iterateNext ( ) ) {
elmts = doc . evaluate ( '//div[@class="vtitle"]/a[@class="vtitlelink" and contains(@href, "/watch?v=")]' , doc , nsResolver , XPathResult . ANY _TYPE , null ) ;
// categories and community pages and user pages and browse pages
} else if ( doc . evaluate ( '//div[starts-with(@class, "vtitle")]/a[contains(@href, "/watch?v=")]' , doc , nsResolver , XPathResult . ANY _TYPE , null ) . iterateNext ( ) ) {
elmts = doc . evaluate ( '//div[starts-with(@class, "vtitle")]/a[contains(@href, "/watch?v=")]' , doc , nsResolver , XPathResult . ANY _TYPE , null ) ;
} else if ( doc . evaluate ( '//div[@class="vltitle"]/div[@class="vlshortTitle"]/a[contains(@href, "/watch?v=")]' , doc , nsResolver , XPathResult . ANY _TYPE , null ) . iterateNext ( ) ) {
elmts = doc . evaluate ( '//div[@class="vltitle"]/div[@class="vlshortTitle"]/a[contains(@href, "/watch?v=")]' , doc , nsResolver , XPathResult . ANY _TYPE , null ) ;
}
while ( elmt = elmts . iterateNext ( ) ) {
var title = elmt . textContent ;
title = Zotero . Utilities . trimInternal ( title ) ;
var link = elmt . href ;
var m = videoRe ( link ) ;
var video _id = m [ 1 ] ;
items [ video _id ] = title ;
}
items = Zotero . selectItems ( items ) ;
if ( ! items ) return true ;
for ( var i in items ) {
video _ids . push ( i ) ;
}
}
getData ( video _ids , host ) ;
}
function getData ( ids , host ) {
var uris = new Array ( ) ;
var url = "http://gdata.youtube.com/feeds/videos/" ;
for each ( var id in ids ) {
uris . push ( url + id ) ;
}
Zotero . Utilities . HTTP . doGet ( uris , function ( text ) {
// clean up header
text = text . replace ( /<\?xml[^>]*\?>/ , "" ) ;
text = text . replace ( /<entry[^>]*>/ , "<entry>" ) ;
// replace colons in XML tags
text = text . replace ( /<media:/g , "<media_" ) . replace ( /<\/media:/g , "</media_" ) ;
// text = text.replace(/<yt:/g, "<yt_").replace(/<\/yt:/g, "</yt_");
text = text . replace ( /yt:/g , "yt_" ) ;
text = text . replace ( /<gd:/g , "<gd_" ) . replace ( /<\/gd:/g , "</gd_" ) ;
text = text . replace ( /<\/?(georss|gml)[^>]+>/g , "" ) ;
// pad xml
text = "<zotero>" + text + "</zotero>" ;
var xml = new XML ( text ) ;
var newItem = new Zotero . Item ( "videoRecording" ) ;
var title = "" ;
var title = xml . . media _title [ 0 ] . text ( ) . toString ( ) ;
if ( xml . . media _title . length ( ) ) {
var title = Zotero . Utilities . cleanString ( xml . . media _title [ 0 ] . text ( ) . toString ( ) ) ;
if ( title == "" ) {
title = " " ;
}
newItem . title = title ;
}
if ( xml . . media _keywords . length ( ) ) {
var keywords = xml . . media _keywords [ 0 ] . text ( ) . toString ( ) ;
keywords = keywords . split ( "," ) ;
for each ( var tag in keywords ) {
newItem . tags . push ( Zotero . Utilities . trimInternal ( tag ) ) ;
}
}
if ( xml . . published . length ( ) ) {
var date = xml . . published [ 0 ] . text ( ) . toString ( ) ;
newItem . date = date . substr ( 0 , 10 ) ;
}
if ( xml . . author . name . length ( ) ) {
var author = xml . . author . name [ 0 ] . text ( ) . toString ( ) ;
newItem . creators . push ( Zotero . Utilities . cleanAuthor ( author , "contributor" , true ) ) ;
}
if ( xml . . media _player . length ( ) ) {
var url = xml . . media _player [ 0 ] . @ url . toString ( ) ;
newItem . url = url ;
newItem . attachments . push ( { title : "YouTube Link" , snapshot : false , mimeType : "text/html" , url : url } ) ;
}
if ( xml . . yt _duration . length ( ) ) {
var runningTime = xml . . yt _duration [ 0 ] . @ seconds . toString ( ) ;
newItem . runningTime = runningTime + " seconds" ;
}
if ( xml . . media _description . length ( ) ) {
newItem . abstractNote = xml . . media _description [ 0 ] . text ( ) . toString ( ) ;
}
2008-10-29 15:33:55 +00:00
//temporary fix for downloads using techcrunch
var techcrunchurl = "http://www.techcrunch.com/ytdownload3.php?url=" + encodeURIComponent ( newItem . url ) + "&submit=Get+Video" ;
Zotero . Utilities . HTTP . doGet ( techcrunchurl , function ( text ) {
var flv = text . match ( /HREF='([^']+)'/ ) ;
if ( flv [ 1 ] ) {
flv = flv [ 1 ] ;
// title parameter needs to be encoded
var title = flv . match ( /&title=([^&]+)/ ) ;
if ( title [ 1 ] ) {
title = encodeURIComponent ( title [ 1 ] ) ;
flv = flv . replace ( /&title=([^&]+)/ , title ) ;
}
newItem . attachments . push ( { url : flv , title : "YouTube Video Recording" , mimeType : "video/x-flv" } ) ;
}
2008-09-11 04:40:07 +00:00
newItem . complete ( ) ;
} , function ( ) { Zotero . done ; } ) ;
} ) ;
Zotero . wait ( ) ;
2008-10-29 15:33:55 +00:00
}
2008-09-11 04:40:07 +00:00
}