initial commit

This commit is contained in:
pika 2024-12-09 21:59:16 +01:00
commit 859c6d328e
8 changed files with 572 additions and 0 deletions

79
scraper/scraper.go Normal file
View file

@ -0,0 +1,79 @@
package scraper
import (
"context"
"fmt"
// "log"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
)
type Video struct {
Title string
URL string
Channel string
Duration string
Views string
Thumbnail string
UploadDate string
}
// Replace with your actual API key
const API_KEY = "AIzaSyAzsihRkp8mYTOXLOkVN09yTqld9TJ4Nts"
func FetchVideos(query string) ([]Video, error) {
ctx := context.Background()
youtubeService, err := youtube.NewService(ctx, option.WithAPIKey(API_KEY))
if err != nil {
return nil, fmt.Errorf("error creating YouTube client: %w", err)
}
// Make the search request
call := youtubeService.Search.List([]string{"snippet"}).
Q(query).
MaxResults(25).
Type("video").
VideoDuration("any")
response, err := call.Do()
if err != nil {
return nil, fmt.Errorf("error making search request: %w", err)
}
var videos []Video
for _, item := range response.Items {
video := Video{
Title: item.Snippet.Title,
URL: fmt.Sprintf("https://www.youtube.com/watch?v=%s", item.Id.VideoId),
Channel: item.Snippet.ChannelTitle,
Thumbnail: item.Snippet.Thumbnails.Default.Url,
UploadDate: item.Snippet.PublishedAt,
}
videos = append(videos, video)
}
// Get additional video details (duration, views) in a single request
videoIds := make([]string, len(response.Items))
for i, item := range response.Items {
videoIds[i] = item.Id.VideoId
}
// Get video statistics
statsCall := youtubeService.Videos.List([]string{"contentDetails", "statistics"}).
Id(videoIds...)
statsResponse, err := statsCall.Do()
if err != nil {
return nil, fmt.Errorf("error fetching video details: %w", err)
}
// 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)
}
}
return videos, nil
}