From dd3853af7be010cac85c331162eb4d4ece8d4530 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20P=C3=A9rez?= Date: Wed, 6 Nov 2024 02:19:26 +0100 Subject: [PATCH] feat: :sparkles: reimplementd GetTVShow handler --- core/cmd/main.go | 2 +- core/cmd/routes.go | 2 + .../migrations/001_rating_schema.up.sql | 3 +- {docker => core/docker}/docker-compose.yml | 1 + core/internal/handlers/tvshow.go | 54 +++++-------------- core/internal/scraper/tvshow.go | 18 +++---- db/docker-compose.yml | 15 ------ db/schema.dbml | 32 ----------- db/schema.sql | 29 ---------- 9 files changed, 28 insertions(+), 128 deletions(-) rename {docker => core/docker}/docker-compose.yml (94%) delete mode 100644 db/docker-compose.yml delete mode 100644 db/schema.dbml delete mode 100644 db/schema.sql diff --git a/core/cmd/main.go b/core/cmd/main.go index 0a19bd4..d486845 100644 --- a/core/cmd/main.go +++ b/core/cmd/main.go @@ -18,7 +18,7 @@ import ( "github.com/zepyrshut/rating-orama/internal/repository" ) -const version = "0.2.0-beta.20241116" +const version = "0.2.0-beta.20241116-4" var app *config.App diff --git a/core/cmd/routes.go b/core/cmd/routes.go index cc671ff..32375a6 100644 --- a/core/cmd/routes.go +++ b/core/cmd/routes.go @@ -11,6 +11,8 @@ func Router(h *handlers.Handlers, app *config.App) *gin.Engine { gin.SetMode(app.AppInfo.GinMode) r := gin.New() + r.GET("/tvshow", h.GetTVShow) + // app.Use(recover.New()) // app.Static("/js", "./views/js") diff --git a/core/database/migrations/001_rating_schema.up.sql b/core/database/migrations/001_rating_schema.up.sql index 6316344..4a8abc7 100644 --- a/core/database/migrations/001_rating_schema.up.sql +++ b/core/database/migrations/001_rating_schema.up.sql @@ -8,7 +8,8 @@ create table if not exists tv_show ( updated_at timestamp not null default (now()) ); -create index if not exists idx_tv_show_title on "tv_show" ("title"); +create index if not exists idx_tv_show_name on "tv_show" ("name"); +create index if not exists idx_tv_show_tt_imdb on "tv_show" ("tt_imdb"); create index if not exists idx_tv_show_updated_at on "tv_show" ("updated_at"); create table if not exists episodes ( diff --git a/docker/docker-compose.yml b/core/docker/docker-compose.yml similarity index 94% rename from docker/docker-compose.yml rename to core/docker/docker-compose.yml index 9a18590..d35009d 100644 --- a/docker/docker-compose.yml +++ b/core/docker/docker-compose.yml @@ -1,5 +1,6 @@ version: '3' +// TODO: update docker-compose services: core: container_name: core-ratingorama diff --git a/core/internal/handlers/tvshow.go b/core/internal/handlers/tvshow.go index 1ae52e6..041e16d 100644 --- a/core/internal/handlers/tvshow.go +++ b/core/internal/handlers/tvshow.go @@ -1,46 +1,20 @@ package handlers -//func (hq *Handlers) GetAllChapters(c *fiber.Ctx) error { -// tvShow := models.TvShow{} +import ( + "log/slog" + "net/http" -// ttShowID := c.Query("id") + "github.com/gin-gonic/gin" + "github.com/zepyrshut/rating-orama/internal/scraper" +) -// if ttShowID[0:2] == "tt" { -// ttShowID = ttShowID[2:] -// } +func (hq *Handlers) GetTVShow(c *gin.Context) { + ttShowID := c.Query("ttid") + slog.Info("GetTVShow", "ttid", ttShowID) -// exist := hq.DB.CheckIfTvShowExists(ttShowID) + seasons := scraper.ScrapeSeasons(ttShowID) -// if !exist { -// url := fmt.Sprintf(hq.App.Environment.HarvesterApi, ttShowID) -// response, _ := http.Get(url) -// body, _ := io.ReadAll(response.Body) -// err := json.Unmarshal(body, &tvShow) -// if err != nil { -// hq.App.Error(err.Error()) -// return c.Status(http.StatusInternalServerError).JSON(err) -// } -// err = hq.DB.InsertTvShow(tvShow) -// if err != nil { -// hq.App.Error(err.Error()) -// return c.Status(http.StatusInternalServerError).JSON(err) -// } -// } - -// tvShow, err := hq.DB.FetchTvShow(ttShowID) -// if err != nil { -// hq.App.Error(err.Error()) -// return c.Status(http.StatusInternalServerError).JSON(err) -// } - -// tvShowJSON, err := json.Marshal(tvShow) -// if err != nil { -// hq.App.Error(err.Error()) -// return c.Status(http.StatusInternalServerError).JSON(err) -// } - -// return c.Render("charts", fiber.Map{ -// "TvShow": tvShow, -// "TvShowJSON": string(tvShowJSON), -// }) -//} + c.JSON(http.StatusOK, gin.H{ + "seasons": seasons, + }) +} diff --git a/core/internal/scraper/tvshow.go b/core/internal/scraper/tvshow.go index 0851e72..0088896 100644 --- a/core/internal/scraper/tvshow.go +++ b/core/internal/scraper/tvshow.go @@ -29,12 +29,13 @@ const episodesSelector = "section.sc-1e7f96be-0.ZaQIL" const nextSeasonButtonSelector = "#next-season-btn" const imdbEpisodesURL = "https://www.imdb.com/title/%s/episodes?season=%d" -func scrapeSeasons(ttImdb string) { +func ScrapeSeasons(ttImdb string) []Season { c := colly.NewCollector( colly.AllowedDomains("imdb.com", "www.imdb.com"), ) - var allEpisodes []Episode + //var allEpisodes []Episode + var allSeasons []Season var seasons []int c.OnHTML("ul.ipc-tabs a[data-testid='tab-season-entry']", func(e *colly.HTMLElement) { @@ -61,26 +62,23 @@ func scrapeSeasons(ttImdb string) { episodeCollector.OnHTML(episodesSelector, func(e *colly.HTMLElement) { seasonEpisodes := extractEpisodesFromSeason(e.Text) - allEpisodes = append(allEpisodes, seasonEpisodes...) + allSeasons = append(allSeasons, seasonEpisodes) + //allEpisodes = append(allEpisodes, seasonEpisodes...) }) for _, seasonNum := range uniqueSeasons { seasonURL := fmt.Sprintf(imdbEpisodesURL, ttImdb, seasonNum) - slog.Info("visiting %s", seasonURL) + slog.Info("visiting season", "url", seasonURL) episodeCollector.Visit(seasonURL) } episodeCollector.Wait() - - // fmt.Println("Total de episodios:", len(allEpisodes)) - // for _, episode := range allEpisodes { - // fmt.Printf("Temporada %d, Episodio %d: %s\n", episode.Season, episode.Episode, episode.Name) - // } - // TODO: Save to DB }) c.Visit("https://www.imdb.com/title/tt0903747/episodes") c.Wait() + + return allSeasons } func extractEpisodesFromSeason(data string) Season { diff --git a/db/docker-compose.yml b/db/docker-compose.yml deleted file mode 100644 index 45732cd..0000000 --- a/db/docker-compose.yml +++ /dev/null @@ -1,15 +0,0 @@ -version: '3' - -services: - postgres: - image: postgres:15.2-alpine - container_name: postgres-dev - restart: always - environment: - POSTGRES_USER: postgres - POSTGRES_PASSWORD: postgres - volumes: - - ./schema.sql:/docker-entrypoint-initdb.d/schema.sql - - ./data:/var/lib/postgresql/data - ports: - - 5432:5432 \ No newline at end of file diff --git a/db/schema.dbml b/db/schema.dbml deleted file mode 100644 index 7336557..0000000 --- a/db/schema.dbml +++ /dev/null @@ -1,32 +0,0 @@ -Table tv_show { - show_id integer [pk] - title varchar [not null] - runtime integer [not null] - popularity integer [not null, default: 0] - created_at timestamp [not null, default: `now()`] - updated_at timestamp [not null, default: `now()`] - - - indexes { - show_id - title - updated_at - } -} - -Table episodes { - episode_id integer [pk] - tv_show_id integer [not null] - season_number integer [not null] - title varchar [not null] - number int [not null] - aired date [not null] - avg_rating decimal [not null] - votes int [not null] - - indexes { - avg_rating - } -} - -Ref: "tv_show"."show_id" < "episodes"."tv_show_id" diff --git a/db/schema.sql b/db/schema.sql deleted file mode 100644 index e724fc4..0000000 --- a/db/schema.sql +++ /dev/null @@ -1,29 +0,0 @@ -CREATE TABLE IF NOT EXISTS "tv_show" ( - "show_id" integer PRIMARY KEY, - "title" varchar NOT NULL, - "runtime" integer NOT NULL, - "popularity" integer NOT NULL DEFAULT 0, - "created_at" timestamp NOT NULL DEFAULT (now()), - "updated_at" timestamp NOT NULL DEFAULT (now()) -); - -CREATE TABLE IF NOT EXISTS "episodes" ( - "episode_id" integer PRIMARY KEY, - "tv_show_id" integer NOT NULL, - "season_number" integer NOT NULL, - "title" varchar NOT NULL, - "number" int NOT NULL, - "aired" date NOT NULL, - "avg_rating" numeric NOT NULL, - "votes" int NOT NULL -); - -CREATE INDEX IF NOT EXISTS idx_tv_show_show_id ON "tv_show" ("show_id"); - -CREATE INDEX IF NOT EXISTS idx_tv_show_title ON "tv_show" ("title"); - -CREATE INDEX IF NOT EXISTS idx_tv_show_updated_at ON "tv_show" ("updated_at"); - -CREATE INDEX IF NOT EXISTS idx_episodes_avg_rating ON "episodes" ("avg_rating"); - -ALTER TABLE "episodes" ADD FOREIGN KEY ("tv_show_id") REFERENCES "tv_show" ("show_id");