Skip to content

Commit

Permalink
revert artworks
Browse files Browse the repository at this point in the history
  • Loading branch information
VTGare committed Nov 17, 2024
1 parent e155fd3 commit 20d9935
Show file tree
Hide file tree
Showing 12 changed files with 404 additions and 393 deletions.
108 changes: 69 additions & 39 deletions artworks/artstation/artstation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package artstation
import (
"encoding/json"
"fmt"
"github.com/VTGare/boe-tea-go/artworks/embed"
"net/http"
"regexp"
"strconv"
Expand Down Expand Up @@ -41,7 +40,6 @@ type ArtstationResponse struct {
HideAsAdult bool `json:"hide_as_adult,omitempty"`
VisibleOnArtstation bool `json:"visible_on_artstation,omitempty"`

artworkID string
AIGenerated bool
CreatedAt time.Time `json:"created_at,omitempty"`
}
Expand Down Expand Up @@ -73,32 +71,40 @@ type Category struct {
}

func New() artworks.Provider {
r := regexp.MustCompile(`(?i)https:\/\/(?:www\.)?artstation\.com\/artwork\/([\w\-]+)`)

return &Artstation{
regex: regexp.MustCompile(`(?i)https://(?:www\.)?artstation\.com/artwork/([\w\-]+)`),
regex: r,
}
}

func (as *Artstation) Find(id string) (artworks.Artwork, error) {
return artworks.NewError(as, func() (artworks.Artwork, error) {
reqURL := fmt.Sprintf("https://www.artstation.com/projects/%v.json", id)
resp, err := http.Get(reqURL)
if err != nil {
return nil, err
}
artwork, err := as._find(id)
if err != nil {
return nil, artworks.NewError(as, err)
}

defer resp.Body.Close()
return artwork, nil
}

res := &ArtstationResponse{}
err = json.NewDecoder(resp.Body).Decode(res)
if err != nil {
return nil, err
}
func (as *Artstation) _find(id string) (artworks.Artwork, error) {
reqURL := fmt.Sprintf("https://www.artstation.com/projects/%v.json", id)
resp, err := http.Get(reqURL)
if err != nil {
return nil, err
}

defer resp.Body.Close()

res.artworkID = id
res.AIGenerated = artworks.IsAIGenerated(res.Tags...)
res := &ArtstationResponse{}
err = json.NewDecoder(resp.Body).Decode(res)
if err != nil {
return nil, err
}

return res, nil
})
res.AIGenerated = artworks.IsAIGenerated(res.Tags...)

return res, nil
}

func (as *Artstation) Match(url string) (string, bool) {
Expand Down Expand Up @@ -129,8 +135,13 @@ func (artwork *ArtstationResponse) StoreArtwork() *store.Artwork {
}

func (artwork *ArtstationResponse) MessageSends(footer string, tagsEnabled bool) ([]*discordgo.MessageSend, error) {
if len(artwork.Assets) == 0 {
eb := embeds.NewBuilder()
var (
length = len(artwork.Assets)
pages = make([]*discordgo.MessageSend, 0, length)
eb = embeds.NewBuilder()
)

if length == 0 {
eb.Title("❎ An error has occured.")
eb.Description("Artwork has been deleted or the ID does not exist.")
eb.Footer(footer, "")
Expand All @@ -140,32 +151,51 @@ func (artwork *ArtstationResponse) MessageSends(footer string, tagsEnabled bool)
}, nil
}

eb := &embed.Embed{
Title: artwork.Title,
Username: artwork.User.Name,
FieldName1: "Likes",
FieldValue1: strconv.Itoa(artwork.LikesCount),
FieldName2: "Views",
FieldValue2: []string{strconv.Itoa(artwork.ViewsCount)},
URL: artwork.Permalink,
Timestamp: artwork.CreatedAt,
Footer: footer,
AIGenerated: artwork.AIGenerated,
if length > 1 {
eb.Title(fmt.Sprintf("%v by %v | Page %v / %v", artwork.Title, artwork.User.Name, 1, length))
} else {
eb.Title(fmt.Sprintf("%v by %v", artwork.Title, artwork.User.Name))
}

if tagsEnabled {
eb.Tags = bluemonday.StrictPolicy().Sanitize(artwork.Description)
desc := bluemonday.StrictPolicy().Sanitize(artwork.Description)
eb.Description(artworks.EscapeMarkdown(desc))
}

for _, image := range artwork.Assets {
eb.Images = append(eb.Images, image.ImageURL)
eb.URL(artwork.URL()).
AddField("Likes", strconv.Itoa(artwork.LikesCount), true).
AddField("Views", strconv.Itoa(artwork.ViewsCount), true).
Timestamp(artwork.CreatedAt)

if footer != "" {
eb.Footer(footer, "")
}

return eb.ToEmbed(), nil
}
if artwork.AIGenerated {
eb.AddField("⚠️ Disclaimer", "This artwork is AI-generated.")
}

eb.Image(artwork.Assets[0].ImageURL)
pages = append(pages, &discordgo.MessageSend{Embeds: []*discordgo.MessageEmbed{eb.Finalize()}})
if length > 1 {
for ind, image := range artwork.Assets[1:] {
eb := embeds.NewBuilder()

eb.Title(fmt.Sprintf("%v by %v | Page %v / %v", artwork.Title, artwork.User.Name, ind+2, length)).
Image(image.ImageURL).
URL(artwork.URL()).
Timestamp(artwork.CreatedAt)

if footer != "" {
eb.Footer(footer, "")
}

eb.AddField("Likes", strconv.Itoa(artwork.LikesCount), true)
pages = append(pages, &discordgo.MessageSend{Embeds: []*discordgo.MessageEmbed{eb.Finalize()}})
}
}

func (artwork *ArtstationResponse) ArtworkID() string {
return artwork.artworkID
return pages, nil
}

func (artwork *ArtstationResponse) URL() string {
Expand Down
4 changes: 2 additions & 2 deletions artworks/artstation/artstation_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ func TestArtstation(t *testing.T) {
var _ = DescribeTable(
"Match Artstation URL",
func(url string, expectedID string, expectedResult bool) {
provider := artstation.New()
as := artstation.New()

id, ok := provider.Match(url)
id, ok := as.Match(url)
Expect(id).To(BeEquivalentTo(expectedID))
Expect(ok).To(BeEquivalentTo(expectedResult))
},
Expand Down
49 changes: 45 additions & 4 deletions artworks/artworks.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package artworks

import (
"errors"
"fmt"
"regexp"
"strings"

"github.com/VTGare/boe-tea-go/store"
"github.com/bwmarrin/discordgo"
"strings"
)

type Provider interface {
Expand All @@ -15,12 +19,49 @@ type Provider interface {
type Artwork interface {
StoreArtwork() *store.Artwork
MessageSends(footer string, tags bool) ([]*discordgo.MessageSend, error)
ArtworkID() string
URL() string
Len() int
}

func IsAIGenerated(content ...string) bool {
type Error struct {
provider string
cause error
}

func (e *Error) Error() string {
return fmt.Sprintf("provider %v returned an error: %v", e.provider, e.cause.Error())
}

func (e *Error) Unwrap() error {
return e.cause
}

func NewError(p Provider, err error) error {
return &Error{
provider: fmt.Sprintf("%T", p),
cause: err,
}
}

// Common errors
var (
ErrArtworkNotFound = errors.New("artwork not found")
ErrRateLimited = errors.New("provider rate limited")
)

func EscapeMarkdown(content string) string {
contents := strings.Split(content, "\n")
regex := regexp.MustCompile("^#{1,3}")

for i, line := range contents {
if regex.MatchString(line) {
contents[i] = "\\" + line
}
}
return strings.Join(contents, "\n")
}

func IsAIGenerated(contents ...string) bool {
aiTags := []string{
"aiart",
"aigenerated",
Expand All @@ -32,7 +73,7 @@ func IsAIGenerated(content ...string) bool {
"stablediffusion",
}

for _, tag := range content {
for _, tag := range contents {
for _, test := range aiTags {
if strings.EqualFold(tag, test) {
return true
Expand Down
Loading

0 comments on commit 20d9935

Please sign in to comment.