Skip to content

Commit

Permalink
Check for newznab api errors, attempt to parse date in RFC3339 if RFC…
Browse files Browse the repository at this point in the history
…1123Z failed.
  • Loading branch information
imba3r committed May 6, 2017
1 parent 300929a commit d5b18f7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 20 deletions.
25 changes: 21 additions & 4 deletions newznab/newznab.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"time"

"fmt"

log "github.com/Sirupsen/logrus"
)

Expand Down Expand Up @@ -181,6 +183,9 @@ func (c Client) process(vals url.Values, path string) ([]NZB, error) {
if err != nil {
return nil, err
}
if feed.ErrorCode != 0 {
return nil, fmt.Errorf("newznab api error %d: %s", feed.ErrorCode, feed.ErrorDesc)
}
log.WithField("num", len(feed.Channel.NZBs)).Debug("newznab:Client:Search: found NZBs")
for _, gotNZB := range feed.Channel.NZBs {
nzb := NZB{
Expand All @@ -194,8 +199,8 @@ func (c Client) process(vals url.Values, path string) ([]NZB, error) {
for _, attr := range gotNZB.Attributes {
switch attr.Name {
case "tvairdate":
if parsedAirDate, err := time.Parse(time.RFC1123Z, attr.Value); err != nil {
log.Errorf("newznab:Client:Search: failed to parse date: %v: %v", attr.Value, err)
if parsedAirDate, err := parseDate(attr.Value); err != nil {
log.Errorf("newznab:Client:Search: failed to parse tvairdate: %v", err)
} else {
nzb.AirDate = parsedAirDate
}
Expand Down Expand Up @@ -253,8 +258,8 @@ func (c Client) process(vals url.Values, path string) ([]NZB, error) {
case "coverurl":
nzb.CoverURL = attr.Value
case "usenetdate":
if parsedUsetnetDate, err := time.Parse(time.RFC1123Z, attr.Value); err != nil {
log.Errorf("newznab:Client:Search: failed to parse date: %v: %v", attr.Value, err)
if parsedUsetnetDate, err := parseDate(attr.Value); err != nil {
log.Errorf("newznab:Client:Search: failed to parse usenetdate: %v", err)
} else {
nzb.UsenetDate = parsedUsetnetDate
}
Expand Down Expand Up @@ -352,6 +357,18 @@ func (c Client) buildURL(vals url.Values, path string) string {
return parsedURL.String()
}

func parseDate(date string) (time.Time, error) {
formats := []string{time.RFC3339, time.RFC1123Z}
var parsedTime time.Time
var err error
for _, format := range formats {
if parsedTime, err = time.Parse(format, date); err == nil {
return parsedTime, nil
}
}
return parsedTime, fmt.Errorf("failed to parse date %s as one of %s", date, strings.Join(formats, ", "))
}

const (
apiPath = "/api"
rssPath = "/rss"
Expand Down
32 changes: 18 additions & 14 deletions newznab/newznab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,15 @@ func TestUsenetCrawlerClient(t *testing.T) {

Convey("Handle errors", func() {

Convey("Return an error for an invalid search", func() {
Convey("Return an error for an invalid search.", func() {
_, err := client.SearchWithTVDB(categories, 1234, 9, 2)
So(err, ShouldNotBeNil)
})

Convey("Return an error for invalid api usage.", func() {
_, err := client.SearchWithTVDB(categories, 5678, 9, 2)
So(err.Error(), ShouldEqual, "newznab api error 100: Invalid API Key")
})
})

Convey("When getting TV show information", func() {
Expand All @@ -91,10 +96,6 @@ func TestUsenetCrawlerClient(t *testing.T) {
Convey("When given a category and a tvrage id", func() {
results, err := client.SearchWithTVRage(categories, 2870, 10, 1)

//for _, result := range results {
// log.Info(result.JSONString())
//}

Convey("A valid result is returned.", func() {
So(err, ShouldBeNil)
So(len(results), ShouldBeGreaterThan, 0)
Expand Down Expand Up @@ -136,8 +137,6 @@ func TestUsenetCrawlerClient(t *testing.T) {
})

Convey("When getting movie information", func() {
categories := []int{CategoryMovieHD}

Convey("Given multiple categories and an IMDB id", func() {
cats := []int{
CategoryMovieHD,
Expand All @@ -155,7 +154,8 @@ func TestUsenetCrawlerClient(t *testing.T) {
})

Convey("Given a single category and an IMDB id", func() {
results, err := client.SearchWithIMDB(categories, "0364569")
cats := []int{CategoryMovieHD}
results, err := client.SearchWithIMDB(cats, "0364569")

So(err, ShouldBeNil)
So(len(results), ShouldBeGreaterThan, 0)
Expand All @@ -164,31 +164,26 @@ func TestUsenetCrawlerClient(t *testing.T) {

Convey("An IMDB id.", func() {
imdbAttr := results[0].IMDBID

So(imdbAttr, ShouldEqual, "0364569")
})

Convey("An IMDB title.", func() {
imdbAttr := results[0].IMDBTitle

So(imdbAttr, ShouldEqual, "Oldboy")
})

Convey("An IMDB year.", func() {
imdbAttr := results[0].IMDBYear

So(imdbAttr, ShouldEqual, 2003)
})

Convey("An IMDB score.", func() {
imdbAttr := results[0].IMDBScore

So(imdbAttr, ShouldEqual, 8.4)
})

Convey("A cover URL.", func() {
imdbAttr := results[0].CoverURL

So(imdbAttr, ShouldEqual, "https://dognzb.cr/content/covers/movies/thumbs/364569.jpg")
})
})
Expand Down Expand Up @@ -217,6 +212,16 @@ func TestUsenetCrawlerClient(t *testing.T) {
So(title, ShouldEqual, "030517-VSHS0101720WDA20H264V")
})

Convey("An airdate with RFC1123Z format is parsed.", func() {
year := results[7].AirDate.Year()
So(year, ShouldEqual, 2017)
})

Convey("An usenetdate with RFC3339 format is parsed.", func() {
year := results[7].UsenetDate.Year()
So(year, ShouldEqual, 2017)
})

})

Convey("I can load the RSS feed up to a given NZB ID.", func() {
Expand All @@ -237,7 +242,6 @@ func TestUsenetCrawlerClient(t *testing.T) {
})

Convey("I can load the RSS feed up to a given NZB ID but will stop after N tries", func() {

results, err := client.LoadRSSFeedUntilNZBID(categories, num, "does-not-exist", 2)

Convey("100 results with 2 requests were fetched.", func() {
Expand Down
6 changes: 4 additions & 2 deletions newznab/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ func (c Comment) JSONString() string {

// SearchResponse is a RSS version of the response.
type SearchResponse struct {
Version string `xml:"version,attr"`
Channel struct {
Version string `xml:"version,attr"`
ErrorCode int `xml:"code,attr"`
ErrorDesc string `xml:"description,attr"`
Channel struct {
Title string `xml:"title"`
Link struct {
Href string `xml:"href,attr"`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<error code="100" description="Invalid API Key"/>

0 comments on commit d5b18f7

Please sign in to comment.