feat: ✨ reimplementd GetTVShow handler
This commit is contained in:
@@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user