feat: reimplementd GetTVShow handler

This commit is contained in:
Pedro Pérez 2024-11-06 02:19:26 +01:00
parent cba9dd3ffc
commit dd3853af7b
9 changed files with 28 additions and 128 deletions

View File

@ -18,7 +18,7 @@ import (
"github.com/zepyrshut/rating-orama/internal/repository" "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 var app *config.App

View File

@ -11,6 +11,8 @@ func Router(h *handlers.Handlers, app *config.App) *gin.Engine {
gin.SetMode(app.AppInfo.GinMode) gin.SetMode(app.AppInfo.GinMode)
r := gin.New() r := gin.New()
r.GET("/tvshow", h.GetTVShow)
// app.Use(recover.New()) // app.Use(recover.New())
// app.Static("/js", "./views/js") // app.Static("/js", "./views/js")

View File

@ -8,7 +8,8 @@ create table if not exists tv_show (
updated_at timestamp not null default (now()) 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 index if not exists idx_tv_show_updated_at on "tv_show" ("updated_at");
create table if not exists episodes ( create table if not exists episodes (

View File

@ -1,5 +1,6 @@
version: '3' version: '3'
// TODO: update docker-compose
services: services:
core: core:
container_name: core-ratingorama container_name: core-ratingorama

View File

@ -1,46 +1,20 @@
package handlers package handlers
//func (hq *Handlers) GetAllChapters(c *fiber.Ctx) error { import (
// tvShow := models.TvShow{} "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" { func (hq *Handlers) GetTVShow(c *gin.Context) {
// ttShowID = ttShowID[2:] ttShowID := c.Query("ttid")
// } slog.Info("GetTVShow", "ttid", ttShowID)
// exist := hq.DB.CheckIfTvShowExists(ttShowID) seasons := scraper.ScrapeSeasons(ttShowID)
// if !exist { c.JSON(http.StatusOK, gin.H{
// url := fmt.Sprintf(hq.App.Environment.HarvesterApi, ttShowID) "seasons": seasons,
// 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),
// })
//}

View File

@ -29,12 +29,13 @@ const episodesSelector = "section.sc-1e7f96be-0.ZaQIL"
const nextSeasonButtonSelector = "#next-season-btn" const nextSeasonButtonSelector = "#next-season-btn"
const imdbEpisodesURL = "https://www.imdb.com/title/%s/episodes?season=%d" const imdbEpisodesURL = "https://www.imdb.com/title/%s/episodes?season=%d"
func scrapeSeasons(ttImdb string) { func ScrapeSeasons(ttImdb string) []Season {
c := colly.NewCollector( c := colly.NewCollector(
colly.AllowedDomains("imdb.com", "www.imdb.com"), colly.AllowedDomains("imdb.com", "www.imdb.com"),
) )
var allEpisodes []Episode //var allEpisodes []Episode
var allSeasons []Season
var seasons []int var seasons []int
c.OnHTML("ul.ipc-tabs a[data-testid='tab-season-entry']", func(e *colly.HTMLElement) { 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) { episodeCollector.OnHTML(episodesSelector, func(e *colly.HTMLElement) {
seasonEpisodes := extractEpisodesFromSeason(e.Text) seasonEpisodes := extractEpisodesFromSeason(e.Text)
allEpisodes = append(allEpisodes, seasonEpisodes...) allSeasons = append(allSeasons, seasonEpisodes)
//allEpisodes = append(allEpisodes, seasonEpisodes...)
}) })
for _, seasonNum := range uniqueSeasons { for _, seasonNum := range uniqueSeasons {
seasonURL := fmt.Sprintf(imdbEpisodesURL, ttImdb, seasonNum) seasonURL := fmt.Sprintf(imdbEpisodesURL, ttImdb, seasonNum)
slog.Info("visiting %s", seasonURL) slog.Info("visiting season", "url", seasonURL)
episodeCollector.Visit(seasonURL) episodeCollector.Visit(seasonURL)
} }
episodeCollector.Wait() 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.Visit("https://www.imdb.com/title/tt0903747/episodes")
c.Wait() c.Wait()
return allSeasons
} }
func extractEpisodesFromSeason(data string) Season { func extractEpisodesFromSeason(data string) Season {

View File

@ -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

View File

@ -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"

View File

@ -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");