Skip to content

Commit

Permalink
add background random video search task
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima committed Jan 29, 2024
1 parent 1d77715 commit 8d3c362
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 59 deletions.
121 changes: 62 additions & 59 deletions app/handlers/random_hander.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,64 @@ func (s *Router) RememberSeen(conn *sqlite.Conn, visitorId string, videoId strin
return nil
}

func (s *Router) FindRandomVideos() (map[string]*Video, error) {
results := make(map[string]*Video)

searchResult, err := s.ytr.Search("inurl:" + youtube.RandomYoutubeVideoId())
if err != nil {
return nil, err
}
if searchResult == nil || len(searchResult.Items) == 0 {
return results, nil
}

for _, item := range searchResult.Items {
video := Video{}
video.ID = item.Id.VideoId
video.Title = item.Snippet.Title
parsed, err := time.Parse(time.RFC3339, item.Snippet.PublishedAt)
if err == nil {
video.UploadedAt = parsed.Unix()
}
results[item.Id.VideoId] = &video
}

ids := make([]string, len(results))
for _, video := range results {
ids = append(ids, video.ID)
}

videoInfoResult, err := s.ytr.VideosInfo(ids)
if err != nil {
return nil, err
}
for _, item := range videoInfoResult.Items {
video := results[item.Id]
video.Category, _ = strconv.ParseInt(item.Snippet.CategoryId, 10, 64)
video.Views, _ = strconv.ParseInt(item.Statistics.ViewCount, 10, 64)
}

for videoID, video := range results {
short, err := s.ytr.IsShort(video.ID, video.UploadedAt)
if err != nil {
delete(results, videoID)
log.Printf("error defining short (%s): %s", video.ID, err.Error())
} else {
video.Vertical = short
}
}

// clear out fucking gaming videos trash i hate it
for videoId, video := range results {
if video.Category == 20 {
delete(results, videoId)
}
}

return results, nil

}

func (s *Router) GetRandom(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", "application/json")
Expand All @@ -252,10 +310,7 @@ func (s *Router) GetRandom(w http.ResponseWriter, r *http.Request) {
visitor := r.Header.Get("visitor")
params := r.URL.Query()

ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second*60))
defer cancel()

conn := s.db.Get(ctx)
conn := s.db.Get(context.Background())
defer s.db.Put(conn)

searchCriteria := ParseQueryParams(params)
Expand Down Expand Up @@ -287,65 +342,13 @@ func (s *Router) GetRandom(w http.ResponseWriter, r *http.Request) {
// go ask youtube api for random video
var found bool
for !found && r.Context().Err() == nil {
results := make(map[string]*Video)

searchResult, err := s.ytr.Search("inurl:" + youtube.RandomYoutubeVideoId())

results, err := s.FindRandomVideos()
if err != nil {
log.Println("GetRandom:", err.Error())
w.WriteHeader(http.StatusBadGateway)
encoder.Encode(Message{"couldn't find video"})
return
}
if searchResult == nil {
continue
}
nItems := len(searchResult.Items)
if nItems == 0 {
continue
}
log.Printf("%d videos found", nItems)

for _, item := range searchResult.Items {
video := Video{}
video.ID = item.Id.VideoId
video.Title = item.Snippet.Title
parsed, err := time.Parse(time.RFC3339, item.Snippet.PublishedAt)
if err == nil {
video.UploadedAt = parsed.Unix()
}
results[item.Id.VideoId] = &video
}

ids := make([]string, len(results))
for _, video := range results {
ids = append(ids, video.ID)
}

videoInfoResult, err := s.ytr.VideosInfo(ids)
if err != nil {
w.WriteHeader(http.StatusBadGateway)
encoder.Encode(Message{"couldn't find video"})
return
}
for _, item := range videoInfoResult.Items {
video := results[item.Id]
video.Category, _ = strconv.ParseInt(item.Snippet.CategoryId, 10, 64)
video.Views, _ = strconv.ParseInt(item.Statistics.ViewCount, 10, 64)
}

for _, video := range results {
short, err := s.ytr.IsShort(video.ID, video.UploadedAt)
if err != nil {
log.Println("error defining short:", err.Error())
}
video.Vertical = short
}

// clear out fucking gaming videos trash i hate it
for videoId, video := range results {
if video.Category == 20 {
delete(results, videoId)
}
}

for _, video := range results {
if searchCriteria.CheckVideo(video) {
Expand Down
21 changes: 21 additions & 0 deletions app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ func main() {
Handler: handler,
}

// search random video in background
go func() {
for range time.NewTicker(time.Hour).C {

results, err := handler.FindRandomVideos()
if err != nil {
log.Println("background random search:", err.Error())
continue
}

conn := db.Get(context.Background())
err = handler.StoreVideos(conn, results)
if err != nil {
log.Println("background random search: couldn't store found videos:", err.Error())
} else {
log.Println(len(results), "background random search: found videos stored")
}
db.Put(conn)
}
}()

// serve 80
go func() {
if err := server.ListenAndServe(); err != http.ErrServerClosed {
Expand Down

0 comments on commit 8d3c362

Please sign in to comment.