2018-07-27 15:56:56 -07:00
|
|
|
const logger = require('winston');
|
2018-08-01 17:31:20 -07:00
|
|
|
const lbryUri = require('../../../../utils/lbryUri');
|
2018-07-30 14:22:02 -07:00
|
|
|
|
|
|
|
const getOEmbedDataForChannel = require('./getOEmbedDataForChannel');
|
|
|
|
const getOEmbedDataForAsset = require('./getOEmbedDataForAsset');
|
2018-07-30 16:50:36 -07:00
|
|
|
const parseSpeechUrl = require('./parseSpeechUrl');
|
2018-07-27 18:40:57 -07:00
|
|
|
|
2018-07-30 13:48:47 -07:00
|
|
|
const getOEmbedData = (req, res) => {
|
|
|
|
const { query: { url, format } } = req;
|
|
|
|
logger.debug('req url', url);
|
|
|
|
logger.debug('req format', format);
|
2018-07-27 18:40:57 -07:00
|
|
|
|
2018-07-30 13:48:47 -07:00
|
|
|
const { paramOne, paramTwo } = parseSpeechUrl(url);
|
2018-07-30 14:22:02 -07:00
|
|
|
|
2018-07-30 13:48:47 -07:00
|
|
|
let claimName, isChannel, channelName, channelClaimId, claimId;
|
|
|
|
|
|
|
|
if (paramTwo) {
|
|
|
|
({ isChannel, channelName, channelClaimId, claimId } = lbryUri.parseIdentifier(paramOne));
|
|
|
|
({ claimName } = lbryUri.parseClaim(paramTwo));
|
|
|
|
} else {
|
2018-07-30 14:22:02 -07:00
|
|
|
({ isChannel, channelName, channelClaimId } = lbryUri.parseIdentifier(paramOne));
|
2018-07-31 16:43:08 -07:00
|
|
|
if (!isChannel) {
|
2018-07-30 13:48:47 -07:00
|
|
|
({ claimName } = lbryUri.parseClaim(paramOne));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isChannel && !paramTwo) {
|
2018-07-30 14:22:02 -07:00
|
|
|
getOEmbedDataForChannel(channelName, channelClaimId)
|
|
|
|
.then(data => {
|
2018-07-31 16:43:08 -07:00
|
|
|
if (format === 'xml') {
|
2018-07-30 14:50:10 -07:00
|
|
|
return res.status(503).json({
|
|
|
|
success: false,
|
|
|
|
message: 'xml format is not implemented yet',
|
2018-07-31 16:43:08 -07:00
|
|
|
});
|
2018-07-30 14:50:10 -07:00
|
|
|
} else {
|
|
|
|
return res.status(200).json(data);
|
|
|
|
}
|
2018-07-30 13:48:47 -07:00
|
|
|
})
|
2018-07-30 14:22:02 -07:00
|
|
|
.catch((error) => {
|
|
|
|
return res.status(404).json({
|
|
|
|
success: false,
|
|
|
|
message: error,
|
2018-07-30 13:48:47 -07:00
|
|
|
});
|
2018-07-31 16:43:08 -07:00
|
|
|
});
|
2018-07-30 14:22:02 -07:00
|
|
|
} else {
|
|
|
|
getOEmbedDataForAsset(channelName, channelClaimId, claimName, claimId)
|
|
|
|
.then(data => {
|
2018-07-31 16:43:08 -07:00
|
|
|
if (format === 'xml') {
|
2018-07-30 14:50:10 -07:00
|
|
|
return res.status(503).json({
|
|
|
|
success: false,
|
|
|
|
message: 'xml format is not implemented yet',
|
2018-07-31 16:43:08 -07:00
|
|
|
});
|
2018-07-30 14:50:10 -07:00
|
|
|
} else {
|
|
|
|
return res.status(200).json(data);
|
|
|
|
}
|
2018-07-30 13:48:47 -07:00
|
|
|
})
|
2018-07-30 14:22:02 -07:00
|
|
|
.catch((error) => {
|
|
|
|
return res.status(404).json({
|
|
|
|
success: false,
|
|
|
|
message: error,
|
|
|
|
});
|
2018-07-31 16:43:08 -07:00
|
|
|
});
|
2018-07-30 13:48:47 -07:00
|
|
|
}
|
2018-07-27 15:56:56 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = getOEmbedData;
|