diff --git a/client/index.html b/client/index.html index eeb23e5..d6523e3 100644 --- a/client/index.html +++ b/client/index.html @@ -3,15 +3,160 @@ + PodGrab + + +
+

{{ .title }}

+
+
+
+
+ +
+ +
+
+
+
+{{range .podcasts}} +
+
+ {{ .Title }} +
+
+

{{.Title}}

+

{{ .Summary }}

-
    - {{range .podcasts}} -
  1. {{.Title}} -
  2. {{end}} -
+
+
+ {{ len .PodcastItems }} episodes +
+ +
+
+{{end}} +
\ No newline at end of file diff --git a/controllers/podcast.go b/controllers/podcast.go index 773922b..f0d3a64 100644 --- a/controllers/podcast.go +++ b/controllers/podcast.go @@ -20,7 +20,7 @@ type SearchByIdQuery struct { } type AddPodcastData struct { - url string `binding:"required" form:"url" json:"url"` + Url string `binding:"required" form:"url" json:"url"` } func GetAllPodcasts(c *gin.Context) { @@ -85,7 +85,7 @@ func AddPodcast(c *gin.Context) { err := c.ShouldBindJSON(&addPodcastData) if err == nil { - service.AddPodcast(addPodcastData.url) + service.AddPodcast(addPodcastData.Url) // fmt.Println(time.Unix(addPodcastData.StartDate, 0)) c.JSON(200, addPodcastData) } else { diff --git a/db/dbfunctions.go b/db/dbfunctions.go index 25d8d3b..4be04b2 100644 --- a/db/dbfunctions.go +++ b/db/dbfunctions.go @@ -61,7 +61,7 @@ func CreatePodcast(podcast *Podcast) error { } func CreatePodcastItem(podcastItem *PodcastItem) error { - tx := DB.Create(&podcastItem) + tx := DB.Omit("Podcast").Create(&podcastItem) return tx.Error } func UpdatePodcastItem(podcastItem *PodcastItem) error { diff --git a/go.mod b/go.mod index 600e2aa..83f916b 100644 --- a/go.mod +++ b/go.mod @@ -7,8 +7,10 @@ require ( github.com/gin-gonic/gin v1.6.3 github.com/gobeam/stringy v0.0.0-20200717095810-8a3637503f62 github.com/jasonlvhit/gocron v0.0.1 + github.com/microcosm-cc/bluemonday v1.0.4 github.com/satori/go.uuid v1.2.0 golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a + golang.org/x/net v0.0.0-20201002202402-0a1ea396d57c // indirect gorm.io/driver/sqlite v1.1.3 gorm.io/gorm v1.20.2 ) diff --git a/go.sum b/go.sum index 562d096..3605121 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,9 @@ github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= +github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk= +github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4= +github.com/chris-ramon/douceur v0.2.0 h1:IDMEdxlEUUBYBKE4z/mJnFyVXox+MjuEVDJNN27glkU= +github.com/chris-ramon/douceur v0.2.0/go.mod h1:wDW5xjJdeoMm1mRt4sD4c/LbF/mWdEpRXQKjTR8nIBE= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= @@ -28,6 +32,8 @@ github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY= +github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/jasonlvhit/gocron v0.0.1 h1:qTt5qF3b3srDjeOIR4Le1LfeyvoYzJlYpqvG7tJX5YU= github.com/jasonlvhit/gocron v0.0.1/go.mod h1:k9a3TV8VcU73XZxfVHCHWMWF9SOqgoku0/QlY2yvlA4= @@ -49,6 +55,8 @@ github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/ github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus= github.com/mattn/go-sqlite3 v1.14.3 h1:j7a/xn1U6TKA/PHHxqZuzh64CdtRc7rU9M+AvkOl5bA= github.com/mattn/go-sqlite3 v1.14.3/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI= +github.com/microcosm-cc/bluemonday v1.0.4 h1:p0L+CTpo/PLFdkoPcJemLXG+fpMD7pYOoDEq1axMbGg= +github.com/microcosm-cc/bluemonday v1.0.4/go.mod h1:8iwZnFn2CDDNZ0r6UXhF4xawGvzaqzCRa1n3/lO3W2w= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= @@ -69,14 +77,19 @@ github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLY golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a h1:vclmkQCjlDX5OydZ9wv8rBCcS0QyQY66Mpf/7BZbInM= golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b h1:0mm1VjtFUOIlE1SbDlwjYaDxZVDP2S5ou6y0gSgXHu8= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20201002202402-0a1ea396d57c h1:dk0ukUIHmGHqASjP0iue2261isepFCC6XRCSd1nHgDw= +golang.org/x/net v0.0.0-20201002202402-0a1ea396d57c/go.mod h1:iQL9McJNjoIa5mjH6nYTCTZXUN6RP+XW3eib7Ya3XcI= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -85,6 +98,7 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/main.go b/main.go index 12950b4..ad964f5 100644 --- a/main.go +++ b/main.go @@ -64,8 +64,30 @@ func main() { r.GET("/", func(c *gin.Context) { //var podcasts []db.Podcast podcasts := service.GetAllPodcasts() - c.HTML(http.StatusOK, "index.html", gin.H{"title": "Main website", "podcasts": podcasts}) + c.HTML(http.StatusOK, "index.html", gin.H{"title": "Podgrab", "podcasts": podcasts}) }) + r.POST( + "/", func(c *gin.Context) { + var addPodcastData controllers.AddPodcastData + err := c.ShouldBind(&addPodcastData) + + if err == nil { + + _, err = service.AddPodcast(addPodcastData.Url) + if err == nil { + c.Redirect(http.StatusFound, "/") + + } else { + + c.JSON(http.StatusBadRequest, err) + + } + } else { + // fmt.Println(err.Error()) + c.JSON(http.StatusBadRequest, err) + } + + }) go intiCron() @@ -74,7 +96,7 @@ func main() { } func intiCron() { - gocron.Every(20).Minutes().Do(service.DownloadMissingEpisodes) - gocron.Every(20).Minutes().Do(service.RefreshEpisodes) + //gocron.Every(5).Minutes().Do(service.DownloadMissingEpisodes) + gocron.Every(5).Minutes().Do(service.RefreshEpisodes) <-gocron.Start() } diff --git a/service/podcastService.go b/service/podcastService.go index 3c6b636..96c7ac7 100644 --- a/service/podcastService.go +++ b/service/podcastService.go @@ -11,14 +11,18 @@ import ( "time" "github.com/akhilrex/podgrab/db" + "github.com/microcosm-cc/bluemonday" "gorm.io/gorm" ) //Fetch is func FetchURL(url string) (PodcastData, error) { - body := makeQuery(url) + body, err := makeQuery(url) + if err != nil { + return PodcastData{}, err + } var response PodcastData - err := xml.Unmarshal(body, &response) + err = xml.Unmarshal(body, &response) return response, err } func GetAllPodcasts() *[]db.Podcast { @@ -31,15 +35,17 @@ func AddPodcast(url string) (db.Podcast, error) { data, err := FetchURL(url) if err != nil { fmt.Println("Error") - log.Fatal(err) + //log.Fatal(err) return db.Podcast{}, err } var podcast db.Podcast err = db.GetPodcastByTitleAndAuthor(data.Channel.Title, data.Channel.Author, &podcast) if errors.Is(err, gorm.ErrRecordNotFound) { + p := bluemonday.StripTagsPolicy() + podcast := db.Podcast{ Title: data.Channel.Title, - Summary: data.Channel.Summary, + Summary: p.Sanitize(data.Channel.Summary), Author: data.Channel.Author, Image: data.Channel.Image.URL, URL: url, @@ -58,7 +64,7 @@ func AddPodcastItems(podcast *db.Podcast) error { log.Fatal(err) return err } - + p := bluemonday.StripTagsPolicy() for i := 0; i < 5; i++ { obj := data.Channel.Item[i] var podcastItem db.PodcastItem @@ -69,7 +75,7 @@ func AddPodcastItems(podcast *db.Podcast) error { podcastItem = db.PodcastItem{ PodcastID: podcast.ID, Title: obj.Title, - Summary: obj.Summary, + Summary: p.Sanitize(obj.Summary), EpisodeType: obj.EpisodeType, Duration: duration, PubDate: pubDate, @@ -120,24 +126,24 @@ func RefreshEpisodes() error { return nil } -func makeQuery(url string) []byte { +func makeQuery(url string) ([]byte, error) { //link := "https://www.goodreads.com/search/index.xml?q=Good%27s+Omens&key=" + "jCmNlIXjz29GoB8wYsrd0w" //link := "https://www.goodreads.com/search/index.xml?key=jCmNlIXjz29GoB8wYsrd0w&q=Ender%27s+Game" //fmt.Println(url) req, err := http.NewRequest("GET", url, nil) if err != nil { - log.Fatal(err) + return nil, err } resp, err := http.DefaultClient.Do(req) if err != nil { - log.Fatal(err) + return nil, err } defer resp.Body.Close() fmt.Println("Response status:", resp.Status) body, err := ioutil.ReadAll(resp.Body) - return body + return body, nil }