Skip to content

Code refactoring #36

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

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# http://editorconfig.org
# EditorConfig is awesome: https://EditorConfig.org

root = true

Expand All @@ -16,7 +16,7 @@ indent_size = 4

[*.md]
trim_trailing_whitespace = false
indent_size = 4
indent_size = 2

[Makefile]
indent_style = tab
Expand Down
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 2

updates:
- package-ecosystem: gomod
directory: /
schedule:
interval: daily
- package-ecosystem: github-actions
directory: /
schedule:
interval: weekly
36 changes: 36 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Build
on: [ push, pull_request ]
jobs:
build:
name: Test
strategy:
matrix:
os: [ ubuntu-latest, macos-latest, windows-latest ]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.16.x
- name: Test
run: go test ./...
release:
name: Release
runs-on: ubuntu-latest
needs: build
if: startsWith(github.ref, 'refs/tags')
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.16.x
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v1
with:
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28 changes: 18 additions & 10 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,13 @@ getmoe
coverage.txt
getmoe.yaml
getmoe.yml
getmoe_cache.json

### GoReleaser ###
/dist

# Created by https://www.gitignore.io/api/go,code,macos,linux,windows
# Edit at https://www.gitignore.io/?templates=go,code,macos,linux,windows

### Code ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
# Created by https://www.toptal.com/developers/gitignore/api/go,visualstudiocode,macos,linux,windows
# Edit at https://www.toptal.com/developers/gitignore?templates=go,visualstudiocode,macos,linux,windows

### Go ###
# Binaries for programs and plugins
Expand Down Expand Up @@ -63,6 +57,7 @@ getmoe.yml
# Icon must end with two \r
Icon


# Thumbnails
._*

Expand All @@ -82,6 +77,19 @@ Network Trash Folder
Temporary Items
.apdisk

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace

### VisualStudioCode Patch ###
# Ignore all local history of files
.history
.ionide

### Windows ###
# Windows thumbnail cache files
Thumbs.db
Expand All @@ -108,4 +116,4 @@ $RECYCLE.BIN/
# Windows shortcuts
*.lnk

# End of https://www.gitignore.io/api/go,code,macos,linux,windows
# End of https://www.toptal.com/developers/gitignore/api/go,visualstudiocode,macos,linux,windows
22 changes: 0 additions & 22 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2017-2019 Leonid Boykov
Copyright (c) 2017-2021 Leonid Boykov

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
72 changes: 14 additions & 58 deletions board.go
Original file line number Diff line number Diff line change
@@ -1,85 +1,41 @@
package getmoe

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

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

// NewBoard creates a new board with provided configuration.
func NewBoard(providerName string, config BoardConfiguration) (*Board, error) {
providersMu.RLock()
provider, ok := providers[providerName]
providersMu.RUnlock()
if !ok {
return nil, fmt.Errorf("getmoe: unknown provider %s", providerName)
}

provider.New(config.Provider)
return &Board{
Provider: provider,
httpClient: &http.Client{
Timeout: 10 * time.Second,
},
}, nil
}

// NewBoardWithProvider creates a new board with provided configuration.
func NewBoardWithProvider(provider Provider) *Board {
return &Board{
Provider: provider,
httpClient: &http.Client{
Timeout: 10 * time.Second,
},
}
Name string
provider Provider
}

// Request gets images by tags.
func (b *Board) Request() ([]Post, error) {
req, err := b.Provider.PageRequest()
if err != nil {
return nil, err
}

resp, err := b.httpClient.Do(req)
if err != nil {
// NewBoard creates a new Board.
func NewBoard(name string, config BoardConfiguration) (*Board, error) {
if err := applyPresets(config.Settings, &config.Provider); err != nil {
return nil, err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
provider, err := NewProvider(config.Provider.Name, config.Provider)
if err != nil {
return nil, err
}

page, err := b.Provider.Parse(body)
if err != nil {
return nil, err
}

return page, nil
return &Board{
Name: name,
provider: provider,
}, nil
}

// RequestAll checks all pages.
func (b *Board) RequestAll() ([]Post, error) {
func (b *Board) RequestAll(tags Tags) ([]Post, error) {
var pages []Post
currentPage := 0
for {
b.Provider.NextPage()
page, err := b.Request()
page, err := b.provider.RequestPage(tags, currentPage)
if err != nil {
return pages, err
}
if len(page) == 0 {
break
}
pages = append(pages, page...)
currentPage++
}
return pages, nil
}
49 changes: 49 additions & 0 deletions cmd/getmoe/downloader/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package downloader

import (
"encoding/json"
"errors"
"os"
)

// cacheValue stores information about downloaded image.
type cacheValue struct {
Board string `json:"board,omitempty"`
ID int `json:"id,omitempty"`
URL string `json:"url,omitempty"`
}

// loadCache reads cache from local file.
func (d *Downloader) loadCache(in string) error {
var cacheMap map[string]cacheValue
file, err := os.Open(in)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
defer file.Close()
if err := json.NewDecoder(file).Decode(&cacheMap); err != nil {
return err
}
for key, value := range cacheMap {
d.cache.Store(key, value)
}
return nil
}

// saveCache writes cache to a local file.
func (d *Downloader) saveCache(out string) error {
cacheMap := make(map[string]cacheValue)
d.cache.Range(func(key, value interface{}) bool {
cacheMap[key.(string)] = value.(cacheValue)
return true
})
file, err := os.Create(out)
if err != nil {
return err
}
defer file.Close()
if err := json.NewEncoder(file).Encode(cacheMap); err != nil {
return err
}
return nil
}
Loading