I'm trying to get open graph metadata from an URL using nodejs (with cheerio), using the code bellow.
I have this thing to fill: var result={};
for (var ogCounter = 0; ogCounter < metalist.length; ogCounter++) {
if (!utils.isEmpty(metalist[ogCounter].attribs.property) && !utils.isEmpty(metalist[ogCounter].attribs.content)) {
if (metalist[ogCounter].attribs.property.indexOf('og') == 0) {
var ogname = metalist[ogCounter].attribs.property.split(':');
var property = ogname[1];
var content = metalist[ogCounter].attribs.content;
if (utils.isEmpty(result[property])) {
result[property] = content;
} else {
if (result[property].push) {
result[property].push(content);
} else {
result[property] = [result[property], content];
}
}
}
}
}
After I populate the result I converted in a JSon and with this code I get something like:
type: "video",
image: "http://i3.ytimg.com/vi/fWNaR-rxAic/mqdefault.jpg",
video: [
"http://www.youtube.com/v/fWNaR-rxAic?version=3&autohide=1",
"application/x-shockwave-flash",
"1920",
"1080"
]
But the thing I want is something like:
type: "video",
image: "http://i3.ytimg.com/vi/fWNaR-rxAic/mqdefault.jpg",
video: {
"http://www.youtube.com/v/fWNaR-rxAic?version=3&autohide=1",
{
type:"application/x-shockwave-flash",
width:"1920",
height:"1080"
}
}
I'm trying this "if" but it doesn't work:
if (utils.isEmpty(result[property])) {
result[property] = content;
} else {
if (result[property].push) {
result[property].push(content);
} else {
var subresult={};
subresult[name[2]]=content;
subresult[property]=result[property] ;
result[property] = subresult;
}
}
I don't want to cycle all the meta 2 times and I'm not good with javascript and nodejs function... Any suggestion? Thanks