Source: vimeo-html.js

  1. const utils = require('../Utils/__defaultUtils.js')
  2. const vimeoTrack = require('./vimeo-player')
  3. /**
  4. * @class htmlVimeo -> Html Vimeo Handler Class for Handling Basic Un-Official Extraction and Parsing of Vimeo Video Metadata and Stream Readable
  5. */
  6. class htmlVimeo extends vimeoTrack {
  7. /**
  8. * @static
  9. * html() -> Html 5 Player Fetch for Vimeo Url
  10. * @param {string} rawUrl raw Vimeo Video Url for the Extraction
  11. * @param {object} __scrapperOptions scrapping Options for raw Fetch Method
  12. * @returns {Promise<vimeoTrack>} Returns Instance of Vimeo with properties of Data
  13. */
  14. static async html(rawUrl, __scrapperOptions = htmlVimeo.__scrapperOptions) {
  15. if (
  16. !(
  17. rawUrl &&
  18. typeof rawUrl === 'string' &&
  19. rawUrl !== '' &&
  20. htmlVimeo.__test(rawUrl)
  21. )
  22. )
  23. throw new TypeError(
  24. 'Vimeo Internal Error : Invalid Url is Provided for Extraction',
  25. )
  26. try {
  27. __scrapperOptions = {
  28. ...htmlVimeo.__scrapperOptions,
  29. ...__scrapperOptions,
  30. htmlOptions: {
  31. ...htmlVimeo.__scrapperOptions?.htmlOptions,
  32. ...__scrapperOptions?.htmlOptions,
  33. },
  34. fetchOptions: {
  35. ...htmlVimeo.__scrapperOptions?.fetchOptions,
  36. ...__scrapperOptions?.fetchOptions,
  37. },
  38. }
  39. if (
  40. htmlVimeo.__vimeoPlayerRegex?.find(
  41. (regex) => regex && regex.test(rawUrl),
  42. )
  43. )
  44. return await htmlVimeo.__htmlFetch(rawUrl, __scrapperOptions)
  45. const rawResponse = await utils.__rawfetchBody(
  46. rawUrl,
  47. __scrapperOptions?.htmlOptions,
  48. )
  49. if (
  50. !(rawResponse && typeof rawResponse === 'string' && rawResponse !== '')
  51. )
  52. return undefined
  53. const rawJsonResponse = JSON.parse(
  54. `{${
  55. rawResponse
  56. ?.split('<meta ')
  57. ?.filter((raw) => raw && raw?.includes('content='))
  58. ?.map((raw) => {
  59. try {
  60. raw = raw?.split('>')?.[0]
  61. if (!(raw && typeof raw === 'string' && raw !== ''))
  62. return undefined
  63. else if (raw?.startsWith('property=')) {
  64. return (
  65. `${raw
  66. ?.slice(
  67. raw?.indexOf('property=') + 'property='.length,
  68. raw?.indexOf('content='),
  69. )
  70. ?.replace('og:', '')
  71. ?.replace('al:', '')
  72. ?.replace('twitter:', '')
  73. ?.replace(/[~`!@#$%^&*()+={}\[\];:<>.,\/\\\?-_]/g, '_')
  74. ?.trim()
  75. }:${
  76. raw
  77. ?.slice(raw?.indexOf('content=') + 'content='.length)
  78. ?.trim()}`
  79. )
  80. } else if (raw?.startsWith('name=')) {
  81. return (
  82. `${raw
  83. ?.slice(
  84. raw?.indexOf('name=') + 'name='.length,
  85. raw?.indexOf('content='),
  86. )
  87. ?.replace('og:', '')
  88. ?.replace('al:', '')
  89. ?.replace('twitter:', '')
  90. ?.replace(/[~`!@#$%^&*()+={}\[\];:<>.,\/\\\?-]/g, '_')
  91. ?.trim()
  92. }:${
  93. raw
  94. ?.slice(raw?.indexOf('content=') + 'content='.length)
  95. ?.trim()}`
  96. )
  97. } else return undefined
  98. } catch {
  99. return undefined
  100. }
  101. })
  102. ?.filter(Boolean)
  103. ?.join(',')
  104. }}`,
  105. )
  106. return await htmlVimeo.__htmlFetch(
  107. rawJsonResponse?.url,
  108. __scrapperOptions,
  109. rawJsonResponse,
  110. )
  111. } catch (rawError) {
  112. if (__scrapperOptions?.ignoreError) return utils.__errorHandling(rawError)
  113. else throw rawError
  114. }
  115. }
  116. }
  117. module.exports = htmlVimeo