diff --git a/.github/workflows/buildx.yml b/.github/workflows/buildx.yml index 9e5cfea..d7982fc 100644 --- a/.github/workflows/buildx.yml +++ b/.github/workflows/buildx.yml @@ -13,15 +13,21 @@ env: IMAGE_NAME: shanedabes/ircbot jobs: - build: + test: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v1 - push: - needs: build + - name: Lint + uses: docker://mushus/golangci-linter:1.1.2 + + - name: Test + uses: shoukoo/golang-pipeline/go1.13/test@master + + docker: + needs: test runs-on: ubuntu-latest if: github.event_name == 'push' @@ -60,3 +66,30 @@ jobs: --platform linux/amd64,linux/arm,linux/arm64/v8 \ --tag ${IMAGE_ID}:${VERSION} \ --file ./Dockerfile . + + release: + needs: test + + runs-on: ubuntu-latest + if: github.event_name == 'push' + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Unshallow + run: git fetch --prune --unshallow + + - name: Set up Go + uses: actions/setup-go@v1 + with: + go-version: 1.13.8 + + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v1 + with: + version: latest + args: release --rm-dist + key: ${{ secrets.YOUR_PRIVATE_KEY }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 275d71f..382f2e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.1.0 + +- Add qwantz (dinosaur comics) plugin + ## 1.0.0 - Initial release diff --git a/main.go b/main.go index 797b641..6cca020 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( _ "github.com/shanedabes/ircbot/plugins/clock" _ "github.com/shanedabes/ircbot/plugins/donger" _ "github.com/shanedabes/ircbot/plugins/lastfm" + _ "github.com/shanedabes/ircbot/plugins/qwantz" _ "github.com/shanedabes/ircbot/plugins/tenor" _ "github.com/shanedabes/ircbot/plugins/trakt" _ "github.com/shanedabes/ircbot/plugins/twitter" diff --git a/plugins/qwantz/qwantz.go b/plugins/qwantz/qwantz.go new file mode 100644 index 0000000..8947fd0 --- /dev/null +++ b/plugins/qwantz/qwantz.go @@ -0,0 +1,84 @@ +package qwantz + +import ( + "fmt" + "io/ioutil" + "net/http" + "regexp" + "strings" + + "github.com/go-chat-bot/bot" +) + +const ( + qwantzURL = "http://www.qwantz.com" + reg = `(http:\/\/www\.qwantz\.com\/index.php\?comic=\d+)">([^<]+)` +) + +type comic struct { + url string + desc string +} + +func (c comic) String() string { + return fmt.Sprintf("%s - %s", c.desc, c.url) +} + +func httpError(code int, status string) error { + return fmt.Errorf("status code error: %d %s", code, status) +} + +func getPage(url string) (page string, err error) { + res, err := http.Get("http://www.qwantz.com") + if err != nil { + return "", err + } + defer res.Body.Close() + + if res.StatusCode != 200 { + return "", httpError(res.StatusCode, res.Status) + } + + contents, err := ioutil.ReadAll(res.Body) + if err != nil { + return "", err + } + return string(contents), nil +} + +func extractRandComic(url string) (out comic, err error) { + page, err := getPage(url) + + if err != nil { + return out, err + } + + r, _ := regexp.Compile(reg) + rr := r.FindStringSubmatch(page) + comicURL := rr[1] + desc := rr[2] + + return comic{ + url: comicURL, + desc: strings.ReplaceAll(desc, "\n", " "), + }, nil +} + +func qwantz(command *bot.Cmd) (msg string, err error) { + comic, err := extractRandComic(qwantzURL) + + if err != nil { + return "", err + } + + return comic.String(), nil +} + +func init() { + bot.RegisterCommand( + "qwantz", + "Post random dinosaur comic", + "", + qwantz, + ) +} diff --git a/plugins/qwantz/qwantz_test.go b/plugins/qwantz/qwantz_test.go new file mode 100644 index 0000000..987eb35 --- /dev/null +++ b/plugins/qwantz/qwantz_test.go @@ -0,0 +1,18 @@ +package qwantz + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func testComic(t *testing.T) { + c := comic{ + url: "test.com", + desc: "comic desc", + } + + expected := "comic desc - test.com" + + assert.Equal(t, c.String(), expected) +}