ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

youtube-dl-info-builder.ts 5.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES } from '../../initializers/constants.js'
  2. import { peertubeTruncate } from '../core-utils.js'
  3. import { isUrlValid } from '../custom-validators/activitypub/misc.js'
  4. import { isArray } from '../custom-validators/misc.js'
  5. export type YoutubeDLInfo = {
  6. name?: string
  7. description?: string
  8. category?: number
  9. language?: string
  10. licence?: number
  11. nsfw?: boolean
  12. tags?: string[]
  13. thumbnailUrl?: string
  14. ext?: string
  15. originallyPublishedAtWithoutTime?: Date
  16. webpageUrl?: string
  17. urls?: string[]
  18. chapters?: {
  19. timecode: number
  20. title: string
  21. }[]
  22. }
  23. export class YoutubeDLInfoBuilder {
  24. private readonly info: any
  25. constructor (info: any) {
  26. this.info = { ...info }
  27. }
  28. getInfo () {
  29. const obj = this.buildVideoInfo(this.normalizeObject(this.info))
  30. if (obj.name && obj.name.length < CONSTRAINTS_FIELDS.VIDEOS.NAME.min) obj.name += ' video'
  31. return obj
  32. }
  33. private normalizeObject (obj: any) {
  34. const newObj: any = {}
  35. for (const key of Object.keys(obj)) {
  36. // Deprecated key
  37. if (key === 'resolution') continue
  38. const value = obj[key]
  39. if (typeof value === 'string') {
  40. newObj[key] = value.normalize()
  41. } else {
  42. newObj[key] = value
  43. }
  44. }
  45. return newObj
  46. }
  47. private buildOriginallyPublishedAt (obj: any) {
  48. let originallyPublishedAt: Date = null
  49. const uploadDateMatcher = /^(\d{4})(\d{2})(\d{2})$/.exec(obj.upload_date)
  50. if (uploadDateMatcher) {
  51. originallyPublishedAt = new Date()
  52. originallyPublishedAt.setHours(0, 0, 0, 0)
  53. const year = parseInt(uploadDateMatcher[1], 10)
  54. // Month starts from 0
  55. const month = parseInt(uploadDateMatcher[2], 10) - 1
  56. const day = parseInt(uploadDateMatcher[3], 10)
  57. originallyPublishedAt.setFullYear(year, month, day)
  58. }
  59. return originallyPublishedAt
  60. }
  61. private buildVideoInfo (obj: any): YoutubeDLInfo {
  62. return {
  63. name: this.titleTruncation(obj.title),
  64. description: this.descriptionTruncation(obj.description),
  65. category: this.getCategory(obj.categories),
  66. licence: this.getLicence(obj.license),
  67. language: this.getLanguage(obj.language),
  68. nsfw: this.isNSFW(obj),
  69. tags: this.getTags(obj.tags),
  70. thumbnailUrl: obj.thumbnail || undefined,
  71. urls: this.buildAvailableUrl(obj),
  72. originallyPublishedAtWithoutTime: this.buildOriginallyPublishedAt(obj),
  73. ext: obj.ext,
  74. webpageUrl: obj.webpage_url,
  75. chapters: isArray(obj.chapters)
  76. ? obj.chapters.map((c: { start_time: number, title: string }) => ({ timecode: c.start_time, title: c.title }))
  77. : []
  78. }
  79. }
  80. private buildAvailableUrl (obj: any) {
  81. const urls: string[] = []
  82. if (obj.url) urls.push(obj.url)
  83. if (obj.urls) {
  84. if (Array.isArray(obj.urls)) urls.push(...obj.urls)
  85. else urls.push(obj.urls)
  86. }
  87. const formats = Array.isArray(obj.formats)
  88. ? obj.formats
  89. : []
  90. for (const format of formats) {
  91. if (!format.url) continue
  92. urls.push(format.url)
  93. }
  94. const thumbnails = Array.isArray(obj.thumbnails)
  95. ? obj.thumbnails
  96. : []
  97. for (const thumbnail of thumbnails) {
  98. if (!thumbnail.url) continue
  99. urls.push(thumbnail.url)
  100. }
  101. if (obj.thumbnail) urls.push(obj.thumbnail)
  102. for (const subtitleKey of Object.keys(obj.subtitles || {})) {
  103. const subtitles = obj.subtitles[subtitleKey]
  104. if (!Array.isArray(subtitles)) continue
  105. for (const subtitle of subtitles) {
  106. if (!subtitle.url) continue
  107. urls.push(subtitle.url)
  108. }
  109. }
  110. return urls.filter(u => u && isUrlValid(u))
  111. }
  112. private titleTruncation (title: string) {
  113. return peertubeTruncate(title, {
  114. length: CONSTRAINTS_FIELDS.VIDEOS.NAME.max,
  115. separator: /,? +/,
  116. omission: ' […]'
  117. })
  118. }
  119. private descriptionTruncation (description: string) {
  120. if (!description || description.length < CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.min) return undefined
  121. return peertubeTruncate(description, {
  122. length: CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max,
  123. separator: /,? +/,
  124. omission: ' […]'
  125. })
  126. }
  127. private isNSFW (info: any) {
  128. return info?.age_limit >= 16
  129. }
  130. private getTags (tags: string[]) {
  131. if (Array.isArray(tags) === false) return []
  132. return tags
  133. .filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min)
  134. .map(t => t.normalize())
  135. .slice(0, 5)
  136. }
  137. private getLicence (licence: string) {
  138. if (!licence) return undefined
  139. if (licence.includes('Creative Commons Attribution')) return 1
  140. for (const key of Object.keys(VIDEO_LICENCES)) {
  141. const peertubeLicence = VIDEO_LICENCES[key]
  142. if (peertubeLicence.toLowerCase() === licence.toLowerCase()) return parseInt(key, 10)
  143. }
  144. return undefined
  145. }
  146. private getCategory (categories: string[]) {
  147. if (!categories) return undefined
  148. const categoryString = categories[0]
  149. if (!categoryString || typeof categoryString !== 'string') return undefined
  150. if (categoryString === 'News & Politics') return 11
  151. for (const key of Object.keys(VIDEO_CATEGORIES)) {
  152. const category = VIDEO_CATEGORIES[key]
  153. if (categoryString.toLowerCase() === category.toLowerCase()) return parseInt(key, 10)
  154. }
  155. return undefined
  156. }
  157. private getLanguage (language: string) {
  158. return VIDEO_LANGUAGES[language] ? language : undefined
  159. }
  160. }