change to go fiber and update selectors
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"ron"
|
||||
|
||||
"gopher-toolbox/app"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/zepyrshut/rating-orama/internal/repository"
|
||||
)
|
||||
|
||||
@@ -22,38 +21,10 @@ func New(app *app.App, q repository.ExtendedQuerier) *Handlers {
|
||||
}
|
||||
}
|
||||
|
||||
func (hq *Handlers) ToBeImplemented(c *ron.CTX, ctx context.Context) {
|
||||
c.JSON(http.StatusOK, ron.Data{
|
||||
"message": "To be implemented",
|
||||
})
|
||||
func (hq *Handlers) ToBeImplemented(c *fiber.Ctx) error {
|
||||
return c.Status(http.StatusNotImplemented).JSON("not implemented")
|
||||
}
|
||||
|
||||
<<<<<<< Updated upstream
|
||||
func (hq *Handlers) Ping(c *gin.Context) {
|
||||
slog.Info("ping", RequestID, c.Request.Context().Value(RequestID))
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
=======
|
||||
func (hq *Handlers) Ping(c *ron.CTX, ctx context.Context) {
|
||||
c.JSON(http.StatusOK, ron.Data{
|
||||
>>>>>>> Stashed changes
|
||||
"message": "pong",
|
||||
})
|
||||
func (hq *Handlers) Ping(c *fiber.Ctx) error {
|
||||
return c.JSON("pong")
|
||||
}
|
||||
|
||||
// // TODO: Extract to toolbox
|
||||
// func handleQueryError(c *gin.Context, err error, errorMap map[string]string, logMessage string, defaultErrorMessage string) bool {
|
||||
// if err != nil {
|
||||
// for key, message := range errorMap {
|
||||
// if strings.Contains(err.Error(), key) {
|
||||
// slog.Error(logMessage, "error", message, RequestID, c.Request.Context().Value(RequestID))
|
||||
// c.JSON(http.StatusConflict, gin.H{"error": message})
|
||||
// return true
|
||||
// }
|
||||
// }
|
||||
|
||||
// slog.Error(logMessage, "error", err.Error(), RequestID, c.Request.Context().Value(RequestID))
|
||||
// c.JSON(http.StatusInternalServerError, gin.H{"error": defaultErrorMessage})
|
||||
// return true
|
||||
// }
|
||||
// return false
|
||||
// }
|
||||
|
||||
@@ -1,100 +1,63 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gopher-toolbox/app"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"ron"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"github.com/zepyrshut/rating-orama/internal/scraper"
|
||||
"github.com/zepyrshut/rating-orama/internal/sqlc"
|
||||
)
|
||||
|
||||
func (hq *Handlers) GetTVShow(c *ron.CTX, ctx context.Context) {
|
||||
func (hq *Handlers) GetTVShow(c *fiber.Ctx) error {
|
||||
ttShowID := c.Query("ttid")
|
||||
//slog.Info("", "ttid", ttShowID, RequestID, ctx.Value(RequestID))
|
||||
|
||||
var title string
|
||||
var scraperEpisodes []scraper.Episode
|
||||
var sqlcEpisodes []sqlc.Episode
|
||||
|
||||
tvShow, err := hq.queries.CheckTVShowExists(ctx, ttShowID)
|
||||
tvShow, err := hq.queries.CheckTVShowExists(c.Context(), ttShowID)
|
||||
if err != nil {
|
||||
title, scraperEpisodes = scraper.ScrapeEpisodes(ttShowID)
|
||||
<<<<<<< Updated upstream
|
||||
|
||||
sqlcEpisodes, err = hq.Queries.CreateTvShowWithEpisodes(c, sqlc.CreateTVShowParams{
|
||||
=======
|
||||
// TODO: make transactional
|
||||
ttShow, err := hq.queries.CreateTVShow(ctx, sqlc.CreateTVShowParams{
|
||||
>>>>>>> Stashed changes
|
||||
ttShow, err := hq.queries.CreateTVShow(c.Context(), sqlc.CreateTVShowParams{
|
||||
TtImdb: ttShowID,
|
||||
Name: title,
|
||||
}, scraperEpisodes)
|
||||
})
|
||||
if err != nil {
|
||||
<<<<<<< Updated upstream
|
||||
slog.Error("failed to create tv show with episodes", "ttid", ttShowID, "error", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrorCreating})
|
||||
return
|
||||
=======
|
||||
slog.Error("failed to create tv show", "ttid", ttShowID, "error", err)
|
||||
c.JSON(http.StatusInternalServerError, ron.Data{"error": app.ErrorCreating})
|
||||
return c.SendStatus(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
slog.Info("ttshowid", "id", ttShow.ID)
|
||||
for _, episode := range scraperEpisodes {
|
||||
sqlcEpisodesParams := episode.ToEpisodeParams(ttShow.ID)
|
||||
sqlcEpisode, err := hq.queries.CreateEpisodes(ctx, sqlcEpisodesParams)
|
||||
sqlcEpisode, err := hq.queries.CreateEpisodes(c.Context(), sqlcEpisodesParams)
|
||||
if err != nil {
|
||||
slog.Error("failed to create episodes", "ttid", ttShowID, "error", err)
|
||||
c.JSON(http.StatusInternalServerError, ron.Data{"error": app.ErrorCreating})
|
||||
return
|
||||
return c.SendStatus(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
sqlcEpisodes = append(sqlcEpisodes, sqlcEpisode)
|
||||
>>>>>>> Stashed changes
|
||||
}
|
||||
|
||||
slog.Info("scraped seasons", "ttid", ttShowID, "title", title)
|
||||
} else {
|
||||
title = tvShow.Name
|
||||
sqlcEpisodes, err = hq.queries.GetEpisodes(ctx, tvShow.ID)
|
||||
sqlcEpisodes, err = hq.queries.GetEpisodes(c.Context(), tvShow.ID)
|
||||
if err != nil {
|
||||
slog.Error("failed to get episodes", "ttid", ttShowID, "error", err)
|
||||
c.JSON(http.StatusInternalServerError, ron.Data{"error": app.ErrorGetting})
|
||||
return
|
||||
}
|
||||
|
||||
<<<<<<< Updated upstream
|
||||
if err := hq.Queries.IncreasePopularity(c, ttShowID); err != nil {
|
||||
slog.Error("failed to increase popularity", "ttid", ttShowID, "error", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrorUpdating})
|
||||
return
|
||||
return c.SendStatus(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
hq.queries.IncreasePopularity(c.Context(), ttShowID)
|
||||
slog.Info("tv show exists", "ttid", ttShowID, "title", tvShow.Name)
|
||||
}
|
||||
|
||||
tvShowMedian, _ := hq.Queries.TvShowMedianRating(c, sqlcEpisodes[0].TvShowID)
|
||||
tvShowAverage, _ := hq.Queries.TvShowAverageRating(c, sqlcEpisodes[0].TvShowID)
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"popularity": tvShow.Popularity,
|
||||
"title": title,
|
||||
"seasons": sqlcEpisodes,
|
||||
"tvShowMedian": tvShowMedian,
|
||||
"tvShowAverage": tvShowAverage,
|
||||
=======
|
||||
hq.queries.IncreasePopularity(ctx, ttShowID)
|
||||
slog.Info("tv show exists", "ttid", ttShowID, "title", tvShow.Name)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, ron.Data{
|
||||
return c.JSON(fiber.Map{
|
||||
"popularity": tvShow.Popularity,
|
||||
"title": title,
|
||||
"seasons": sqlcEpisodes,
|
||||
>>>>>>> Stashed changes
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package scraper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"log/slog"
|
||||
"regexp"
|
||||
"sort"
|
||||
@@ -10,6 +9,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
|
||||
"github.com/gocolly/colly"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/zepyrshut/rating-orama/internal/sqlc"
|
||||
@@ -47,14 +48,15 @@ const (
|
||||
seasonsSelector = "ul.ipc-tabs a[data-testid='tab-season-entry']"
|
||||
episodeCardSelector = "article.sc-f8507e90-1.cHtpvn.episode-item-wrapper"
|
||||
seasonEpisodeAndTitleSelector = "div.ipc-title__text"
|
||||
releasedDateSelector = "span.sc-ccd6e31b-10.dYquTu"
|
||||
plotSelector = "div.sc-ccd6e31b-11.cVKeME"
|
||||
releasedDateSelector = "span.sc-f2169d65-10.bYaARM"
|
||||
plotSelector = "div.ipc-html-content-inner-div"
|
||||
starRatingSelector = "span.ipc-rating-star--rating"
|
||||
voteCountSelector = "span.ipc-rating-star--voteCount"
|
||||
imdbEpisodesURL = "https://www.imdb.com/title/%s/episodes/?season=%d"
|
||||
visitURL = "https://www.imdb.com/title/%s/episodes"
|
||||
)
|
||||
|
||||
|
||||
func ScrapeEpisodes(ttImdb string) (string, []Episode) {
|
||||
c := colly.NewCollector(
|
||||
colly.AllowedDomains("imdb.com", "www.imdb.com"),
|
||||
|
||||
Reference in New Issue
Block a user