code styling improvements

This commit is contained in:
Pedro Pérez 2024-11-06 22:18:54 +01:00
parent 8c1f642e33
commit 3d71a16633
4 changed files with 9 additions and 36 deletions

View File

@ -2,6 +2,7 @@ package main
import (
"encoding/gob"
"errors"
"gopher-toolbox/config"
"log/slog"
"net/http"
@ -45,11 +46,11 @@ func migrateDB() {
}
err = m.Up()
if err != nil && err != migrate.ErrNoChange {
if err != nil && !errors.Is(err, migrate.ErrNoChange) {
slog.Error("cannot migrate", "error", err)
panic(err)
}
if err == migrate.ErrNoChange {
if errors.Is(err, migrate.ErrNoChange) {
slog.Info("migration has no changes")
}

View File

@ -33,5 +33,5 @@ select avg(avg_rating) from "episodes"
where tv_show_id = $1 and season = $2;
-- name: SeasonMedianRating :one
-- select percentile_cont(0.5) within group (order by avg_rating) from "episodes"
-- where tv_show_id = $1 and season = $2;
select percentile_cont(0.5) within group (order by avg_rating) from "episodes"
where tv_show_id = $1 and season = $2;

View File

@ -28,7 +28,7 @@ type Episode struct {
func (e Episode) ToEpisodeParams(tvShowID int32) sqlc.CreateEpisodesParams {
var date pgtype.Date
date.Scan(e.Released)
_ = date.Scan(e.Released)
return sqlc.CreateEpisodesParams{
TvShowID: tvShowID,
@ -82,7 +82,7 @@ func ScrapeEpisodes(ttImdb string) (string, []Episode) {
c.OnScraped(func(r *colly.Response) {
seasonMap := make(map[int]bool)
uniqueSeasons := []int{}
var uniqueSeasons []int
slog.Info("scraped seasons", "seasons", seasons)
for _, seasonNum := range seasons {
if !seasonMap[seasonNum] {
@ -103,13 +103,13 @@ func ScrapeEpisodes(ttImdb string) (string, []Episode) {
for _, seasonNum := range uniqueSeasons {
seasonURL := fmt.Sprintf(imdbEpisodesURL, ttImdb, seasonNum)
slog.Info("visiting season", "url", seasonURL)
episodeCollector.Visit(seasonURL)
_ = episodeCollector.Visit(seasonURL)
}
episodeCollector.Wait()
})
c.Visit(fmt.Sprintf(visitURL, ttImdb))
_ = c.Visit(fmt.Sprintf(visitURL, ttImdb))
c.Wait()
slog.Info("scraped all seasons", "length", len(allSeasons))

View File

@ -1,28 +0,0 @@
package utils
import "time"
// TODO: Move to toolbox
func TimeParser(timeString string) (time.Time, error) {
if len(timeString) == 1 {
return time.Time{}, nil
}
if len(timeString) == 4 {
return time.Parse("2006", timeString)
}
if len(timeString) == 9 {
return time.Parse("Jan. 2006", timeString)
}
if len(timeString) == 10 {
return time.Parse("2 Jan 2006", timeString)
}
if len(timeString) == 11 {
if timeString[5:6] == "." {
return time.Parse("2 Jan. 2006", timeString)
} else {
return time.Parse("2 Jan 2006", timeString)
}
}
return time.Parse("2 Jan. 2006", timeString)
}