Skip to content

Instantly share code, notes, and snippets.

@jbuda
Last active December 16, 2015 11:38
Show Gist options
  • Save jbuda/5428631 to your computer and use it in GitHub Desktop.
Save jbuda/5428631 to your computer and use it in GitHub Desktop.
Saving YouTube/Vimeo thumbnails into Mura
<!--- get a list of videos that need embeddeding --->
<cfparam name="range" default="1" />
<cfparam name="relativePath" default="/_SITEID_/assets/Image/video-thumbs/" />
<cfparam name="saveDirectory" default="#expandPath(relativePath)#" />
<cfparam name="siteID" default="_SITEID_" />
<cfscript>
// create the save directory if it doesn't exist
if (!directoryExists(saveDirectory))
directoryCreate(saveDirectory);
feed = application.feedManager.read('');
feed.setSiteID(siteID);
feed.setMaxItems(range);
feed.addAdvancedParam(relationship='OR',condition='CONTAINS',field='External Video',criteria='YouTube');
feed.addAdvancedParam(relationship='OR',condition='CONTAINS',field='External Video',criteria='vimeo');
feedRs = application.feedManager.getFeed(feed);
children = application.serviceFactory.getBean('contentIterator');
children.setQuery(feedRs);
for (i=1;i<=children.recordCount();i++) {
item = children.next();
// name of the extended attribute in mura
embedStr = item.getValue('External Video');
videoSite = "";
imageUrl = "";
// YouTube : find location of video id
idStartPosition = refindnocase('(?=embed/)(.*?)(?=["|?])',embedStr,1,true);
// if we have no start position check for Vimeo
if (idStartPosition.pos[1] eq 0) {
idStartPosition = refindnocase('(?=video/)(.*?)(?=["|?])',embedStr,1,true);
videoSite = "Vimeo";
} else {
videoSite = "YouTube";
}
// extract the id string from the embedcode
videoId = listLast(mid(embedStr,idStartPosition.pos[1],idStartPosition.len[1]),"/");
// check video locations and load depending on location
if (videoSite == "YouTube") {
// create the image url
imageUrl = "http://img.youtube.com/vi/"&videoId&"/1.jpg";
} else if (videoSite == "Vimeo") {
vimeoXml = "http://vimeo.com/api/v2/video/"&videoId&".xml";
// load in the xml from vimeo api
xmlHttp = new http();
xmlHttp.setMethod("GET");
xmlHttp.setUrl(vimeoxml);
// get the content of the request and parse the xml
xmlstringResult = xmlHttp.send().getPrefix().fileContent;
if (isXml(xmlstringResult)) {
xmlResult = xmlParse(xmlstringResult);
// locate the thumbnail in the xml
thumbnailSrc = xmlsearch(xmlResult,'//thumbnail_medium');
if (!arrayIsEmpty(thumbnailSrc)) {
imageUrl = thumbnailSrc[1].xmlText;
}
} else {
writedump(var=vimeoxml,label="Video not found");
}
}
// set up the save path for the image
filename = videoId&".jpg";
// ensure that the file dosen't already exist
if (!fileExists(saveDirectory&filename) && imageUrl != "") {
// build the http request
imgHttp = new http();
imgHttp.setMethod("get");
imgHttp.setUrl(imageUrl);
imgHttp.setFile(filename);
imgHttp.setPath(saveDirectory)
imgHttpResult = imgHttp.send().getPrefix();
}
}
</cfscript>
<b>done</b>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment