addet some fixes and styling

This commit is contained in:
pika 2024-12-09 22:46:57 +01:00
parent 859c6d328e
commit cc92bc3e26
3 changed files with 127 additions and 4 deletions

View file

@ -3,7 +3,10 @@ package scraper
import (
"context"
"fmt"
"time"
// "log"
"html"
"strings"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
@ -22,6 +25,93 @@ type Video struct {
// Replace with your actual API key
const API_KEY = "AIzaSyAzsihRkp8mYTOXLOkVN09yTqld9TJ4Nts"
func formatViews(count uint64) string {
switch {
case count >= 1000000000:
return fmt.Sprintf("%.1fB views", float64(count)/1000000000)
case count >= 1000000:
return fmt.Sprintf("%.1fM views", float64(count)/1000000)
case count >= 1000:
return fmt.Sprintf("%.1fK views", float64(count)/1000)
default:
return fmt.Sprintf("%d views", count)
}
}
func formatDuration(duration string) string {
// Remove PT from the start
duration = strings.TrimPrefix(duration, "PT")
var result strings.Builder
// Handle hours
if i := strings.Index(duration, "H"); i != -1 {
result.WriteString(duration[:i])
result.WriteString(":")
duration = duration[i+1:]
}
// Handle minutes
if i := strings.Index(duration, "M"); i != -1 {
minutes := duration[:i]
if len(minutes) == 1 {
result.WriteString("0")
}
result.WriteString(minutes)
result.WriteString(":")
duration = duration[i+1:]
} else if result.Len() > 0 {
result.WriteString("00:")
}
// Handle seconds
if i := strings.Index(duration, "S"); i != -1 {
seconds := duration[:i]
if len(seconds) == 1 {
result.WriteString("0")
}
result.WriteString(seconds)
} else {
result.WriteString("00")
}
return result.String()
}
func formatUploadDate(uploadDate string) string {
t, err := time.Parse(time.RFC3339, uploadDate)
if err != nil {
return uploadDate
}
now := time.Now()
diff := now.Sub(t)
days := int(diff.Hours() / 24)
formattedDate := t.Format("02-01-2006")
// If video is less than 30 days old, add "X days ago"
if days < 30 {
var timeAgo string
switch {
case days == 0:
hours := int(diff.Hours())
if hours == 0 {
timeAgo = "just now"
} else {
timeAgo = fmt.Sprintf("%dh ago", hours)
}
case days == 1:
timeAgo = "1 day ago"
default:
timeAgo = fmt.Sprintf("%d days ago", days)
}
return fmt.Sprintf("%s (%s)", formattedDate, timeAgo)
}
return formattedDate
}
func FetchVideos(query string) ([]Video, error) {
ctx := context.Background()
youtubeService, err := youtube.NewService(ctx, option.WithAPIKey(API_KEY))
@ -70,8 +160,10 @@ func FetchVideos(query string) ([]Video, error) {
// Update videos with additional information
for i, stat := range statsResponse.Items {
if i < len(videos) {
videos[i].Duration = stat.ContentDetails.Duration
videos[i].Views = fmt.Sprintf("%s views", stat.Statistics.ViewCount)
videos[i].Duration = formatDuration(stat.ContentDetails.Duration)
videos[i].Views = formatViews(stat.Statistics.ViewCount)
videos[i].Title = html.UnescapeString(videos[i].Title)
videos[i].UploadDate = formatUploadDate(videos[i].UploadDate)
}
}