Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 29 additions & 19 deletions src/helpers/formatVideo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,45 @@ import getDateFromText from './getDateFromText'

export default async function formatVideo(video: any, speedDate: boolean = false) {
try {
// title formating
video.original_title = video.title
// Title formatting: ensure we always have a string
const titleStr = typeof video.title === 'string' ? video.title.trim() : ''
video.original_title = titleStr

if (video.title.split('-').length === 1) {
// Split on hyphen to check if there's an artist part
const parts = titleStr.split('-')
if (parts.length === 1) {
video.artist = ''
video.title = titleStr
} else {
// Extract "Artist - Title" using a regex that handles both "-" and "–"
const match = titleStr.match(/^([^–-]+)[–-](.+)$/)
if (match) {
video.artist = match[1].trim()
video.title = match[2].trim()
} else {
// Fallback if regex doesn’t match as expected
video.artist = ''
video.title = titleStr
}
}
else {
let splited = video.original_title.match(/([^,]*)-(.*)/)
video.artist = splited[1]
video.title = splited[2]
}
// Date formating
let publishedAt: Date = new Date(Date.now())

// Date formatting
let publishedAt: Date
if (speedDate) {
publishedAt = getDateFromText(video.uploadDate)
}
else {
} else {
publishedAt = await getVideoDate(video.id)
}

return {
id: video.id,
original_title: video.original_title.trim(),
title: video.title.trim(),
artist: video.artist.trim(),
original_title: video.original_title,
title: video.title,
artist: video.artist,
duration: video.duration,
publishedAt: publishedAt,
publishedAt
}
} catch (e) {
console.log('format video failed')
console.log(e)
} catch (err) {
console.error('formatVideo failed:', err)
}
}