-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
143 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
## 1.1.0 | ||
|
||
- Add qwantz (dinosaur comics) plugin | ||
|
||
## 1.0.0 | ||
|
||
- Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |