2018-07-28 00:56:56 +02:00
|
|
|
const logger = require('winston');
|
2018-07-30 22:48:47 +02:00
|
|
|
const lbryUri = require('../../utils/lbryUri');
|
2018-07-30 23:22:02 +02:00
|
|
|
|
|
|
|
const getOEmbedDataForChannel = require('./getOEmbedDataForChannel');
|
|
|
|
const getOEmbedDataForAsset = require('./getOEmbedDataForAsset');
|
|
|
|
|
2018-07-28 03:40:57 +02:00
|
|
|
|
2018-07-30 22:48:47 +02:00
|
|
|
const parseSpeechUrl = (url) => {
|
2018-07-28 03:40:57 +02:00
|
|
|
// parse the request url
|
|
|
|
const componentsRegex = new RegExp(
|
|
|
|
'([^:/?#]+:\/\/)'+
|
|
|
|
'([^/?#]*)' +
|
|
|
|
'(\/)' +
|
2018-07-30 19:02:08 +02:00
|
|
|
'([^/?#]*)' +
|
2018-07-28 03:40:57 +02:00
|
|
|
'(\/)' +
|
2018-07-30 19:02:08 +02:00
|
|
|
'([^/?#]*)'
|
2018-07-28 03:40:57 +02:00
|
|
|
);
|
|
|
|
const [proto, protocol, domain, slashOne, paramOne, slashTwo, paramTwo] = componentsRegex
|
|
|
|
.exec(url)
|
|
|
|
.map(match => match || null);
|
2018-07-30 22:48:47 +02:00
|
|
|
|
2018-07-28 03:40:57 +02:00
|
|
|
logger.debug(`${protocol}, ${domain}, ${slashOne}, ${paramOne}, ${slashTwo}, ${paramTwo}`);
|
|
|
|
|
2018-07-30 22:48:47 +02:00
|
|
|
return {
|
|
|
|
paramOne,
|
|
|
|
paramTwo,
|
|
|
|
}
|
|
|
|
};
|
2018-07-28 03:40:57 +02:00
|
|
|
|
2018-07-30 22:48:47 +02:00
|
|
|
const getOEmbedData = (req, res) => {
|
2018-07-28 03:40:57 +02:00
|
|
|
|
2018-07-30 22:48:47 +02:00
|
|
|
const { query: { url, format } } = req;
|
|
|
|
logger.debug('req url', url);
|
|
|
|
logger.debug('req format', format);
|
2018-07-28 03:40:57 +02:00
|
|
|
|
2018-07-30 22:48:47 +02:00
|
|
|
const { paramOne, paramTwo } = parseSpeechUrl(url);
|
2018-07-30 23:22:02 +02:00
|
|
|
|
2018-07-30 22:48:47 +02:00
|
|
|
let claimName, isChannel, channelName, channelClaimId, claimId;
|
|
|
|
|
|
|
|
if (paramTwo) {
|
|
|
|
({ isChannel, channelName, channelClaimId, claimId } = lbryUri.parseIdentifier(paramOne));
|
|
|
|
({ claimName } = lbryUri.parseClaim(paramTwo));
|
|
|
|
} else {
|
2018-07-30 23:22:02 +02:00
|
|
|
({ isChannel, channelName, channelClaimId } = lbryUri.parseIdentifier(paramOne));
|
2018-07-30 22:48:47 +02:00
|
|
|
if (!isChannel ) {
|
|
|
|
({ claimName } = lbryUri.parseClaim(paramOne));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isChannel && !paramTwo) {
|
2018-07-30 23:22:02 +02:00
|
|
|
getOEmbedDataForChannel(channelName, channelClaimId)
|
|
|
|
.then(data => {
|
2018-07-30 23:50:10 +02:00
|
|
|
if (format === 'xml'){
|
|
|
|
return res.status(503).json({
|
|
|
|
success: false,
|
|
|
|
message: 'xml format is not implemented yet',
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return res.status(200).json(data);
|
|
|
|
}
|
2018-07-30 22:48:47 +02:00
|
|
|
})
|
2018-07-30 23:22:02 +02:00
|
|
|
.catch((error) => {
|
|
|
|
return res.status(404).json({
|
|
|
|
success: false,
|
|
|
|
message: error,
|
2018-07-30 22:48:47 +02:00
|
|
|
});
|
|
|
|
})
|
2018-07-30 23:22:02 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
getOEmbedDataForAsset(channelName, channelClaimId, claimName, claimId)
|
|
|
|
.then(data => {
|
2018-07-30 23:50:10 +02:00
|
|
|
if (format === 'xml'){
|
|
|
|
return res.status(503).json({
|
|
|
|
success: false,
|
|
|
|
message: 'xml format is not implemented yet',
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return res.status(200).json(data);
|
|
|
|
}
|
2018-07-30 22:48:47 +02:00
|
|
|
})
|
2018-07-30 23:22:02 +02:00
|
|
|
.catch((error) => {
|
|
|
|
return res.status(404).json({
|
|
|
|
success: false,
|
|
|
|
message: error,
|
|
|
|
});
|
|
|
|
})
|
2018-07-30 22:48:47 +02:00
|
|
|
}
|
2018-07-28 00:56:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = getOEmbedData;
|