Skip to content

Commit

Permalink
refactor: reduced number of internal packages
Browse files Browse the repository at this point in the history
  • Loading branch information
leonidboykov committed Oct 2, 2018
1 parent 835ac77 commit 6baac30
Show file tree
Hide file tree
Showing 21 changed files with 137 additions and 153 deletions.
40 changes: 17 additions & 23 deletions board/board.go → board.go
@@ -1,15 +1,10 @@
package board
package getmoe

import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"time"

"github.com/leonidboykov/getmoe"
"github.com/leonidboykov/getmoe/conf"
"github.com/leonidboykov/getmoe/provider"
)

// Board related errors
Expand All @@ -21,32 +16,31 @@ var (

// Board holds data for API access
type Board struct {
Provider getmoe.Provider
Provider Provider
httpClient *http.Client
}

// New creates a new Board with provided configuration
func New(config conf.BoardConfiguration) (*Board, error) {
if config.Provider.Name == "" {
return nil, ErrProviderNotSpecified
}
p, ok := provider.Providers[config.Provider.Name]
if !ok {
return nil, fmt.Errorf(ErrProviderNotFound, config.Provider.Name)
}
// NewBoard creates a new board with provided configuration
func NewBoard(provider Provider) *Board {
// func New(config getmoe.BoardConfiguration) (*Board, error) {
// if config.Provider.Name == "" {
// return nil, ErrProviderNotSpecified
// }
// p, ok := provider.Providers[config.Provider.Name]
// if !ok {
// return nil, fmt.Errorf(ErrProviderNotFound, config.Provider.Name)
// }

board := &Board{
Provider: p(config.Provider),
return &Board{
Provider: provider,
httpClient: &http.Client{
Timeout: 30 * time.Second,
},
}

return board, nil
}

// Request gets images by tags
func (b *Board) Request() ([]getmoe.Post, error) {
func (b *Board) Request() ([]Post, error) {
req, err := b.Provider.PageRequest()
if err != nil {
return nil, err
Expand All @@ -72,8 +66,8 @@ func (b *Board) Request() ([]getmoe.Post, error) {
}

// RequestAll checks all pages
func (b *Board) RequestAll() ([]getmoe.Post, error) {
var pages []getmoe.Post
func (b *Board) RequestAll() ([]Post, error) {
var pages []Post
for {
b.Provider.NextPage()
page, err := b.Request()
Expand Down
54 changes: 0 additions & 54 deletions board/boards.go

This file was deleted.

11 changes: 5 additions & 6 deletions cmd/getmoe/get.go
Expand Up @@ -7,9 +7,8 @@ import (
"github.com/urfave/cli"

"github.com/leonidboykov/getmoe"
"github.com/leonidboykov/getmoe/board"
"github.com/leonidboykov/getmoe/conf"
"github.com/leonidboykov/getmoe/utils"
"github.com/leonidboykov/getmoe/internal/helper"
"github.com/leonidboykov/getmoe/provider"
)

var getCommand = cli.Command{
Expand Down Expand Up @@ -48,18 +47,18 @@ func getAction(ctx *cli.Context) error {
passwordFlag := ctx.String("password")
quietFlag := ctx.GlobalBool("quiet")

board, ok := board.AvailableBoards[srcFlag]
board, ok := provider.AvailableBoards[srcFlag]
if !ok {
fmt.Printf("There is no %s source specified\n", srcFlag)
os.Exit(1)
}

board.Provider.Auth(conf.AuthConfiguration{
board.Provider.Auth(getmoe.AuthConfiguration{
Login: loginFlag,
Password: passwordFlag,
})

board.Provider.BuildRequest(conf.RequestConfiguration{
board.Provider.BuildRequest(getmoe.RequestConfiguration{
Tags: tagFlag,
})

Expand Down
2 changes: 1 addition & 1 deletion conf/configuration.go → configuration.go
@@ -1,4 +1,4 @@
package conf
package getmoe

import (
"io/ioutil"
Expand Down
6 changes: 3 additions & 3 deletions conf/configuration_test.go → configuration_test.go
@@ -1,11 +1,11 @@
package conf_test
package getmoe_test

import (
"testing"

"gopkg.in/yaml.v2"

"github.com/leonidboykov/getmoe/conf"
"github.com/leonidboykov/getmoe"
)

var testProviders = []byte(`
Expand All @@ -24,7 +24,7 @@ providers:
`)

func TestProviders(t *testing.T) {
var config conf.GlobalConfiguration
var config getmoe.GlobalConfiguration
if err := yaml.Unmarshal(testProviders, &config); err != nil {
t.Error(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/hash/sha1.go
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
)

// Sha1 builds Sha1 hash with proper salt
// Sha1 builds sha1 hash with proper salt
func Sha1(value, salt string) string {
value = fmt.Sprintf(salt, value)
hash := sha1.New()
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion post.go
Expand Up @@ -7,7 +7,7 @@ import (
"path"
"time"

"github.com/leonidboykov/getmoe/utils"
"github.com/leonidboykov/getmoe/internal/helper"
)

// Post contains post data, represents intersection of *boorus post structs
Expand Down
6 changes: 2 additions & 4 deletions provider.go
Expand Up @@ -2,14 +2,12 @@ package getmoe

import (
"net/http"

"github.com/leonidboykov/getmoe/conf"
)

// Provider describes Board provider
type Provider interface {
Auth(conf.AuthConfiguration)
BuildRequest(conf.RequestConfiguration)
Auth(AuthConfiguration)
BuildRequest(RequestConfiguration)
NextPage()
PageRequest() (*http.Request, error)
Parse([]byte) ([]Post, error)
Expand Down
34 changes: 34 additions & 0 deletions provider/boards.go
@@ -0,0 +1,34 @@
package provider

import (
"net/url"

"github.com/leonidboykov/getmoe"
"github.com/leonidboykov/getmoe/provider/danbooru"
"github.com/leonidboykov/getmoe/provider/gelbooru"
"github.com/leonidboykov/getmoe/provider/moebooru"
"github.com/leonidboykov/getmoe/provider/sankaku"
)

// AvailableBoards is a list of predefined boards.
var AvailableBoards = map[string]*getmoe.Board{
"yande.re": getmoe.NewBoard(moebooru.New(getmoe.ProviderConfiguration{
URL: getmoe.URLString{URL: url.URL{Host: "yande.re"}},
})),
"konachan.com": getmoe.NewBoard(moebooru.New(getmoe.ProviderConfiguration{
URL: getmoe.URLString{URL: url.URL{Host: "konachan.com"}},
PasswordSalt: "So-I-Heard-You-Like-Mupkids-?--%s--",
})),
"gelbooru.com": getmoe.NewBoard(gelbooru.New(getmoe.ProviderConfiguration{
URL: getmoe.URLString{URL: url.URL{Host: "gelbooru.com"}},
})),
"danbooru.donmai.us": getmoe.NewBoard(danbooru.New(getmoe.ProviderConfiguration{
URL: getmoe.URLString{URL: url.URL{Host: "danbooru.donmai.us"}},
})),
"chan.sankakucomplex.com": getmoe.NewBoard(sankaku.New(getmoe.ProviderConfiguration{
URL: getmoe.URLString{URL: url.URL{Host: "capi-beta.sankakucomplex.com"}},
})),
"idol.sankakucomplex.com": getmoe.NewBoard(sankaku.New(getmoe.ProviderConfiguration{
URL: getmoe.URLString{URL: url.URL{Host: "iapi.sankakucomplex.com"}},
})),
}
7 changes: 3 additions & 4 deletions provider/danbooru/provider.go
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/imdario/mergo"

"github.com/leonidboykov/getmoe"
"github.com/leonidboykov/getmoe/conf"
"github.com/leonidboykov/getmoe/internal/query"
)

Expand Down Expand Up @@ -45,7 +44,7 @@ type Provider struct {
}

// New creates a new moebooru provider with configuration
func New(config conf.ProviderConfiguration) *Provider {
func New(config getmoe.ProviderConfiguration) *Provider {
provider := &Provider{
URL: &config.URL.URL,
PasswordSalt: config.PasswordSalt,
Expand All @@ -59,12 +58,12 @@ func New(config conf.ProviderConfiguration) *Provider {
}

// Auth builds query based on AuthConfiguration
func (p *Provider) Auth(config conf.AuthConfiguration) {
func (p *Provider) Auth(config getmoe.AuthConfiguration) {
p.URL.User = url.UserPassword(config.Login, config.APIKey)
}

// BuildRequest builds query based on RequestConfiguration
func (p *Provider) BuildRequest(config conf.RequestConfiguration) {
func (p *Provider) BuildRequest(config getmoe.RequestConfiguration) {
q := p.URL.Query()
query.Array(&q, "tags", config.Tags)
query.Int(&q, "limit", p.PostsLimit)
Expand Down
7 changes: 3 additions & 4 deletions provider/gelbooru/provider.go
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/imdario/mergo"

"github.com/leonidboykov/getmoe"
"github.com/leonidboykov/getmoe/conf"
"github.com/leonidboykov/getmoe/internal/query"
)

Expand Down Expand Up @@ -46,7 +45,7 @@ type Provider struct {
}

// New creates a new moebooru provider with configuration
func New(config conf.ProviderConfiguration) *Provider {
func New(config getmoe.ProviderConfiguration) *Provider {
provider := &Provider{
URL: &config.URL.URL,
PasswordSalt: config.PasswordSalt,
Expand All @@ -60,12 +59,12 @@ func New(config conf.ProviderConfiguration) *Provider {
}

// Auth builds query based on AuthConfiguration
func (p *Provider) Auth(config conf.AuthConfiguration) {
func (p *Provider) Auth(config getmoe.AuthConfiguration) {
// No auth required
}

// BuildRequest builds query based on RequestConfiguration
func (p *Provider) BuildRequest(config conf.RequestConfiguration) {
func (p *Provider) BuildRequest(config getmoe.RequestConfiguration) {
q := p.URL.Query()
query.Array(&q, "tags", config.Tags)
query.Int(&q, "limit", p.PostsLimit)
Expand Down
7 changes: 3 additions & 4 deletions provider/moebooru/provider.go
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/imdario/mergo"

"github.com/leonidboykov/getmoe"
"github.com/leonidboykov/getmoe/conf"
"github.com/leonidboykov/getmoe/internal/hash"
"github.com/leonidboykov/getmoe/internal/query"
)
Expand Down Expand Up @@ -48,7 +47,7 @@ type Provider struct {
}

// New creates a new moebooru provider with configuration
func New(config conf.ProviderConfiguration) *Provider {
func New(config getmoe.ProviderConfiguration) *Provider {
provider := &Provider{
URL: &config.URL.URL,
PasswordSalt: config.PasswordSalt,
Expand All @@ -62,7 +61,7 @@ func New(config conf.ProviderConfiguration) *Provider {
}

// Auth builds query based on AuthConfiguration
func (p *Provider) Auth(config conf.AuthConfiguration) {
func (p *Provider) Auth(config getmoe.AuthConfiguration) {
var login, password, hashedPassword = config.Login, config.Password, config.HashedPassword
q := p.URL.Query()
if login != "" {
Expand All @@ -78,7 +77,7 @@ func (p *Provider) Auth(config conf.AuthConfiguration) {
}

// BuildRequest builds query based on RequestConfiguration
func (p *Provider) BuildRequest(config conf.RequestConfiguration) {
func (p *Provider) BuildRequest(config getmoe.RequestConfiguration) {
q := p.URL.Query()
query.Array(&q, "tags", config.Tags)
query.Int(&q, "limit", p.PostsLimit)
Expand Down
11 changes: 5 additions & 6 deletions provider/providers.go
Expand Up @@ -2,28 +2,27 @@ package provider

import (
"github.com/leonidboykov/getmoe"
"github.com/leonidboykov/getmoe/conf"
"github.com/leonidboykov/getmoe/provider/danbooru"
"github.com/leonidboykov/getmoe/provider/gelbooru"
"github.com/leonidboykov/getmoe/provider/moebooru"
"github.com/leonidboykov/getmoe/provider/sankaku"
)

type factory func(conf.ProviderConfiguration) getmoe.Provider
type factory func(getmoe.ProviderConfiguration) getmoe.Provider

func newDanbooru(config conf.ProviderConfiguration) getmoe.Provider {
func newDanbooru(config getmoe.ProviderConfiguration) getmoe.Provider {
return danbooru.New(config)
}

func newGelbooru(config conf.ProviderConfiguration) getmoe.Provider {
func newGelbooru(config getmoe.ProviderConfiguration) getmoe.Provider {
return gelbooru.New(config)
}

func newMoebooru(config conf.ProviderConfiguration) getmoe.Provider {
func newMoebooru(config getmoe.ProviderConfiguration) getmoe.Provider {
return moebooru.New(config)
}

func newSankaku(config conf.ProviderConfiguration) getmoe.Provider {
func newSankaku(config getmoe.ProviderConfiguration) getmoe.Provider {
return sankaku.New(config)
}

Expand Down

0 comments on commit 6baac30

Please sign in to comment.