Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test and code cleanup #9

Merged
merged 5 commits into from
Apr 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: go

go:
- stable

before_install:
- go get -u github.com/mattn/goveralls
script:
- go test -v -race ./...
- go vet ./...
- goveralls -race -v -show -service=travis-ci
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
[![GoDoc](https://godoc.org/github.com/mrobinsn/go-newznab/newznab?status.svg)](https://godoc.org/github.com/mrobinsn/go-newznab/newznab)
[![Go Report Card](https://goreportcard.com/badge/github.com/mrobinsn/go-newznab)](https://goreportcard.com/report/github.com/mrobinsn/go-newznab)
[![Build Status](https://travis-ci.org/mrobinsn/go-newznab.svg?branch=master)](https://travis-ci.org/mrobinsn/go-newznab)
[![Coverage Status](https://coveralls.io/repos/github/mrobinsn/go-newznab/badge.svg?branch=master)](https://coveralls.io/github/mrobinsn/go-newznab?branch=master)
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)


Expand Down
47 changes: 19 additions & 28 deletions newznab/newznab.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import (
"strings"
"time"

"fmt"

log "github.com/Sirupsen/logrus"
"github.com/pkg/errors"
)

// Various constants for categories
Expand Down Expand Up @@ -173,20 +172,18 @@ func (c Client) search(vals url.Values) ([]NZB, error) {

func (c Client) process(vals url.Values, path string) ([]NZB, error) {
var nzbs []NZB
log.Debug("newznab:Client:Search: searching")
resp, err := c.getURL(c.buildURL(vals, path))
if err != nil {
return nzbs, err
}
var feed SearchResponse
err = xml.Unmarshal(resp, &feed)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to unmarshal xml feed")
}
if feed.ErrorCode != 0 {
return nil, fmt.Errorf("newznab api error %d: %s", feed.ErrorCode, feed.ErrorDesc)
return nil, errors.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{
Title: gotNZB.Title,
Expand All @@ -200,7 +197,7 @@ func (c Client) process(vals url.Values, path string) ([]NZB, error) {
switch attr.Name {
case "tvairdate":
if parsedAirDate, err := parseDate(attr.Value); err != nil {
log.Errorf("newznab:Client:Search: failed to parse tvairdate: %v", err)
log.WithError(err).WithField("tvairdate", attr.Value).Debug("newznab:Client:Search: failed to parse tvairdate")
} else {
nzb.AirDate = parsedAirDate
}
Expand Down Expand Up @@ -259,15 +256,15 @@ func (c Client) process(vals url.Values, path string) ([]NZB, error) {
nzb.CoverURL = attr.Value
case "usenetdate":
if parsedUsetnetDate, err := parseDate(attr.Value); err != nil {
log.Errorf("newznab:Client:Search: failed to parse usenetdate: %v", err)
log.WithError(err).WithField("usenetdate", attr.Value).Debug("failed to parse usenetdate")
} else {
nzb.UsenetDate = parsedUsetnetDate
}
default:
log.WithFields(log.Fields{
"name": attr.Name,
"value": attr.Value,
}).Debug("encounted unknown attribute")
}).Debug("encontered unknown attribute")
}
}
if nzb.Size == 0 {
Expand All @@ -280,7 +277,6 @@ func (c Client) process(vals url.Values, path string) ([]NZB, error) {

// PopulateComments fills in the Comments for the given NZB
func (c Client) PopulateComments(nzb *NZB) error {
log.Debug("newznab:Client:PopulateComments: getting comments")
data, err := c.getURL(c.buildURL(url.Values{
"t": []string{"comments"},
"id": []string{nzb.ID},
Expand All @@ -292,7 +288,7 @@ func (c Client) PopulateComments(nzb *NZB) error {
var resp commentResponse
err = xml.Unmarshal(data, &resp)
if err != nil {
return err
return errors.Wrap(err, "failed to unmarshal comments xml data")
}

for _, rawComment := range resp.Channel.Comments {
Expand All @@ -301,10 +297,7 @@ func (c Client) PopulateComments(nzb *NZB) error {
Content: rawComment.Description,
}
if parsedPubDate, err := time.Parse(time.RFC1123Z, rawComment.PubDate); err != nil {
log.WithFields(log.Fields{
"pub_date": rawComment.PubDate,
"err": err,
}).Error("newznab:Client:PopulateComments: failed to parse date")
log.WithError(err).WithField("pubdate", rawComment.PubDate).Debug("failed to parse comment date")
} else {
comment.PubDate = parsedPubDate
}
Expand All @@ -314,7 +307,7 @@ func (c Client) PopulateComments(nzb *NZB) error {
}

// NZBDownloadURL returns a URL to download the NZB from
func (c Client) NZBDownloadURL(nzb NZB) string {
func (c Client) NZBDownloadURL(nzb NZB) (string, error) {
return c.buildURL(url.Values{
"t": []string{"get"},
"id": []string{nzb.ID},
Expand All @@ -327,34 +320,32 @@ func (c Client) DownloadNZB(nzb NZB) ([]byte, error) {
return c.getURL(c.NZBDownloadURL(nzb))
}

func (c Client) getURL(url string) ([]byte, error) {
log.WithField("url", url).Debug("newznab:Client:getURL: getting url")
res, err := c.client.Get(url)
func (c Client) getURL(url string, err error) ([]byte, error) {
if err != nil {
return nil, err
}
res, err := c.client.Get(url)
if err != nil {
return nil, errors.Wrapf(err, "http request failed: %s", url)
}

var data []byte
data, err = ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to read response body")
}

log.WithField("num_bytes", len(data)).Debug("newznab:Client:getURL: retrieved")

return data, nil
}

func (c Client) buildURL(vals url.Values, path string) string {
func (c Client) buildURL(vals url.Values, path string) (string, error) {
parsedURL, err := url.Parse(c.apiBaseURL + path)
if err != nil {
log.WithError(err).Error("failed to parse base API url")
return ""
return "", errors.Wrap(err, "failed to parse base API url")
}

parsedURL.RawQuery = vals.Encode()
return parsedURL.String()
return parsedURL.String(), nil
}

func parseDate(date string) (time.Time, error) {
Expand All @@ -366,7 +357,7 @@ func parseDate(date string) (time.Time, error) {
return parsedTime, nil
}
}
return parsedTime, fmt.Errorf("failed to parse date %s as one of %s", date, strings.Join(formats, ", "))
return parsedTime, errors.Errorf("failed to parse date %s as one of %s", date, strings.Join(formats, ", "))
}

const (
Expand Down
Loading