Skip to content

Commit

Permalink
feat: support imgur.io domains
Browse files Browse the repository at this point in the history
These URLs follow the same format as their .com counterparts. They
currently do not seem to be documented by imgur anyway.
  • Loading branch information
leon-richardt authored and jbreitbart committed Oct 20, 2022
1 parent 3402ebf commit b883cba
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions fromURL.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,64 @@ type GenericInfo struct {
Limit *RateLimit
}

var directURLPatterns = []string{
"://i.imgur.com/",
"://i.imgur.io/",
}

var albumURLPatterns = []string{
"://imgur.com/a/",
"://m.imgur.com/a/",
"://imgur.io/a/",
"://m.imgur.io/a/",
}

var galleryURLPatterns = []string{
"://imgur.com/gallery/",
"://m.imgur.com/gallery/",
"://imgur.io/gallery/",
"://m.imgur.io/gallery/",
}

var imageURLPatterns = []string{
"://imgur.com/",
"://m.imgur.com/",
"://imgur.io/",
"://m.imgur.io/",
}

func matchesSlice(url string, validFormats []string) bool {
for _, format := range validFormats {
if strings.Contains(url, format) {
return true
}
}

return false
}

// GetInfoFromURL tries to query imgur based on information identified in the URL.
// returns image/album info, status code of the request, error
func (client *Client) GetInfoFromURL(url string) (*GenericInfo, int, error) {
url = strings.TrimSpace(url)

// https://i.imgur.com/<id>.jpg -> image
if strings.Contains(url, "://i.imgur.com/") {
if matchesSlice(url, directURLPatterns) {
return client.directImageURL(url)
}

// https://imgur.com/a/<id> -> album
if strings.Contains(url, "://imgur.com/a/") || strings.Contains(url, "://m.imgur.com/a/") {
if matchesSlice(url, albumURLPatterns) {
return client.albumURL(url)
}

// https://imgur.com/gallery/<id> -> gallery album
if strings.Contains(url, "://imgur.com/gallery/") || strings.Contains(url, "://m.imgur.com/gallery/") {
if matchesSlice(url, galleryURLPatterns) {
return client.galleryURL(url)
}

// https://imgur.com/<id> -> image
if strings.Contains(url, "://imgur.com/") || strings.Contains(url, "://m.imgur.com/") {
if matchesSlice(url, imageURLPatterns) {
return client.imageURL(url)
}

Expand Down

0 comments on commit b883cba

Please # to comment.