|
| 1 | +package gelbooru |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +const ( |
| 9 | + PostURL = "https://gelbooru.com/index.php?page=post&s=view&id=%d" |
| 10 | +) |
| 11 | + |
| 12 | +type ( |
| 13 | + Post struct { |
| 14 | + Id int `json:"id"` |
| 15 | + CreatedAt string `json:"created_at"` |
| 16 | + Score int `json:"score"` |
| 17 | + Width int `json:"width"` |
| 18 | + Height int `json:"height"` |
| 19 | + Md5 string `json:"md5"` |
| 20 | + Directory string `json:"directory"` |
| 21 | + Image string `json:"image"` |
| 22 | + Rating string `json:"rating"` |
| 23 | + Source string `json:"source"` |
| 24 | + Change int `json:"change"` |
| 25 | + Owner string `json:"owner"` |
| 26 | + CreatorId int `json:"creator_id"` |
| 27 | + ParentId int `json:"parent_id"` |
| 28 | + Sample int `json:"sample"` |
| 29 | + PreviewHeight int `json:"preview_height"` |
| 30 | + PreviewWidth int `json:"preview_width"` |
| 31 | + Tags string `json:"tags"` |
| 32 | + Title string `json:"title"` |
| 33 | + HasNotes string `json:"has_notes"` |
| 34 | + HasComments string `json:"has_comments"` |
| 35 | + FileUrl string `json:"file_url"` |
| 36 | + PreviewUrl string `json:"preview_url"` |
| 37 | + SampleUrl string `json:"sample_url"` |
| 38 | + SampleHeight int `json:"sample_height"` |
| 39 | + SampleWidth int `json:"sample_width"` |
| 40 | + Status string `json:"status"` |
| 41 | + PostLocked int `json:"post_locked"` |
| 42 | + HasChildren string `json:"has_children"` |
| 43 | + } |
| 44 | + |
| 45 | + Response struct { |
| 46 | + Attributes struct { |
| 47 | + Limit int `json:"limit"` |
| 48 | + Offset int `json:"offset"` |
| 49 | + Count int `json:"count"` |
| 50 | + } `json:"@attributes"` |
| 51 | + Post []Post `json:"post"` |
| 52 | + } |
| 53 | +) |
| 54 | + |
| 55 | +func (p *Post) FileURL() string { |
| 56 | + if p.SampleUrl != "" { |
| 57 | + return p.SampleUrl |
| 58 | + } |
| 59 | + return p.FileUrl |
| 60 | +} |
| 61 | + |
| 62 | +func (p *Post) DirectURL() string { |
| 63 | + if p.FileUrl != "" { |
| 64 | + return p.FileUrl |
| 65 | + } |
| 66 | + return p.SampleUrl |
| 67 | +} |
| 68 | + |
| 69 | +func (p *Post) IsNSFW() bool { |
| 70 | + if p.Rating == "questionable" || p.Rating == "explicit" { |
| 71 | + return true |
| 72 | + } |
| 73 | + return false |
| 74 | +} |
| 75 | + |
| 76 | +func (p *Post) Caption() string { |
| 77 | + var sb strings.Builder |
| 78 | + |
| 79 | + sb.WriteString(fmt.Sprintf("🔗 <a href=\"%s\">Post #%d</a> - ", p.PostURL(), p.Id)) |
| 80 | + sb.WriteString(fmt.Sprintf("🖼️ <a href=\"%s\">Direktlink</a>", p.DirectURL())) |
| 81 | + if p.Source != "" { |
| 82 | + sb.WriteString(fmt.Sprintf(" - 🌐 <a href=\"%s\">Quelle</a>\n", p.Source)) |
| 83 | + } |
| 84 | + |
| 85 | + return sb.String() |
| 86 | +} |
| 87 | + |
| 88 | +// AltCaption is used when the media is too big or invalid type |
| 89 | +func (p *Post) AltCaption() string { |
| 90 | + var sb strings.Builder |
| 91 | + |
| 92 | + sb.WriteString(fmt.Sprintf("%s\n", p.DirectURL())) |
| 93 | + if p.IsNSFW() { |
| 94 | + sb.WriteString("️🔞 <b>NSFW</b> - ") |
| 95 | + } |
| 96 | + sb.WriteString(fmt.Sprintf("🔗 <a href=\"%s\">Post #%d</a>", p.PostURL(), p.Id)) |
| 97 | + if p.Source != "" { |
| 98 | + sb.WriteString(fmt.Sprintf(" - 🌐 <a href=\"%s\">Quelle</a>\n", p.Source)) |
| 99 | + } |
| 100 | + |
| 101 | + return sb.String() |
| 102 | +} |
| 103 | + |
| 104 | +func (p *Post) PostURL() string { |
| 105 | + return fmt.Sprintf(PostURL, p.Id) |
| 106 | +} |
| 107 | + |
| 108 | +func (p *Post) IsImage() bool { |
| 109 | + if strings.HasSuffix(p.FileURL(), ".jpg") || |
| 110 | + strings.HasSuffix(p.FileURL(), ".jpeg") || |
| 111 | + strings.HasSuffix(p.FileURL(), ".png") || |
| 112 | + strings.HasSuffix(p.FileURL(), ".webp") { |
| 113 | + return true |
| 114 | + } |
| 115 | + return false |
| 116 | +} |
| 117 | + |
| 118 | +func (p *Post) IsVideo() bool { |
| 119 | + if strings.HasSuffix(p.FileURL(), ".mp4") || |
| 120 | + strings.HasSuffix(p.FileURL(), ".webm") { |
| 121 | + return true |
| 122 | + } |
| 123 | + return false |
| 124 | +} |
| 125 | + |
| 126 | +func (p *Post) IsGIF() bool { |
| 127 | + if strings.HasSuffix(p.FileURL(), ".gif") { |
| 128 | + return true |
| 129 | + } |
| 130 | + return false |
| 131 | +} |
0 commit comments