Skip to content
This repository has been archived by the owner on Nov 18, 2024. It is now read-only.

Commit

Permalink
adds support for flickr photos and albums #9
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Winkler committed Dec 6, 2016
1 parent 9647c1a commit f3e31be
Show file tree
Hide file tree
Showing 2 changed files with 284 additions and 1 deletion.
132 changes: 131 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,20 @@ var (
RegexpUrlImgurAlbum *regexp.Regexp
RegexpUrlGoogleDrive *regexp.Regexp
RegexpUrlPossibleTistorySite *regexp.Regexp
RegexpUrlFlickrPhoto *regexp.Regexp
RegexpUrlFlickrAlbum *regexp.Regexp
dg *discordgo.Session
DownloadTistorySites bool
interactiveChannelLinkTemp map[string]string
DiscordUserId string
myDB *db.DB
historyCommandActive map[string]string
MaxDownloadRetries int
flickrApiKey string
)

const (
VERSION string = "1.13.5"
VERSION string = "1.14"
DATABASE_DIR string = "database"
RELEASE_URL string = "https://github.com/Seklfreak/discord-image-downloader-go/releases/latest"
RELEASE_API_URL string = "https://api.github.com/repos/Seklfreak/discord-image-downloader-go/releases/latest"
Expand All @@ -63,6 +66,8 @@ const (
REGEXP_URL_IMGUR_ALBUM string = `^http(s?):\/\/imgur\.com\/a\/[A-Za-z0-9]+$`
REGEXP_URL_GOOGLEDRIVE string = `^http(s?):\/\/drive\.google\.com\/file\/d\/[^/]+\/view$`
REGEXP_URL_POSSIBLE_TISTORY_SITE string = `^http(s)?:\/\/[0-9a-zA-Z\.-]+\/(m\/)?(photo\/)?[0-9]+$`
REGEXP_URL_FLICKR_PHOTO string = `^http(s)?:\/\/(www\.)?flickr\.com\/photos\/([0-9]+)@([A-Z0-9]+)\/([0-9]+)(\/)?(\/in\/album-([0-9]+)(\/)?)?$`
REGEXP_URL_FLICKR_ALBUM string = `^http(s)?:\/\/(www\.)?flickr\.com\/photos\/([0-9]+)@([A-Z0-9]+)\/albums\/(with\/)?([0-9]+)(\/)?$`
)

type GfycatObject struct {
Expand Down Expand Up @@ -99,6 +104,7 @@ func main() {
cfg.Section("channels").NewKey("channelid1", "C:\\full\\path\\1")
cfg.Section("channels").NewKey("channelid2", "C:\\full\\path\\2")
cfg.Section("channels").NewKey("channelid3", "C:\\full\\path\\3")
cfg.Section("flickr").NewKey("api key", "yourflickrapikey")
err = cfg.SaveTo("config.ini")

if err != nil {
Expand Down Expand Up @@ -129,6 +135,7 @@ func main() {
InteractiveChannelWhitelist = cfg.Section("interactive channels").KeysHash()
interactiveChannelLinkTemp = make(map[string]string)
historyCommandActive = make(map[string]string)
flickrApiKey = cfg.Section("flickr").Key("api key").MustString("yourflickrapikey")

RegexpUrlTwitter, err = regexp.Compile(REGEXP_URL_TWITTER)
if err != nil {
Expand Down Expand Up @@ -175,6 +182,16 @@ func main() {
fmt.Println("Regexp error", err)
return
}
RegexpUrlFlickrPhoto, err = regexp.Compile(REGEXP_URL_FLICKR_PHOTO)
if err != nil {
fmt.Println("Regexp error", err)
return
}
RegexpUrlFlickrAlbum, err = regexp.Compile(REGEXP_URL_FLICKR_ALBUM)
if err != nil {
fmt.Println("Regexp error", err)
return
}

if cfg.Section("auth").HasKey("token") {
dg, err = discordgo.New(cfg.Section("auth").Key("token").String())
Expand Down Expand Up @@ -287,6 +304,22 @@ func getDownloadLinks(url string) map[string]string {
return links
}
}
if RegexpUrlFlickrPhoto.MatchString(url) {
links, err := getFlickrPhotoUrls(url)
if err != nil {
fmt.Println("flickr photo url failed, ", url, ",", err)
} else if len(links) > 0 {
return links
}
}
if RegexpUrlFlickrAlbum.MatchString(url) {
links, err := getFlickrAlbumUrls(url)
if err != nil {
fmt.Println("flickr album url failed, ", url, ",", err)
} else if len(links) > 0 {
return links
}
}
if DownloadTistorySites {
if RegexpUrlPossibleTistorySite.MatchString(url) {
links, err := getPossibleTistorySiteUrls(url)
Expand Down Expand Up @@ -683,6 +716,103 @@ func getGoogleDriveUrls(url string) (map[string]string, error) {
}
}

type FlickrPhotoSizeObject struct {
Label string `json:"label"`
Width int `json:"width,int,string"`
Height int `json:"height,int,string"`
Source string `json:"source"`
URL string `json:"url"`
Media string `json:"media"`
}

type FlickrPhotoObject struct {
Sizes struct {
Canblog int `json:"canblog"`
Canprint int `json:"canprint"`
Candownload int `json:"candownload"`
Size []FlickrPhotoSizeObject `json:"size"`
} `json:"sizes"`
Stat string `json:"stat"`
}

func getFlickrUrlFromPhotoId(photoId string) string {
reqUrl := fmt.Sprintf("https://www.flickr.com/services/rest/?format=json&nojsoncallback=1&method=%s&api_key=%s&photo_id=%s",
"flickr.photos.getSizes", flickrApiKey, photoId)
flickrPhoto := new(FlickrPhotoObject)
getJson(reqUrl, flickrPhoto)
var bestSize FlickrPhotoSizeObject
for _, size := range flickrPhoto.Sizes.Size {
if bestSize.Label == "" {
bestSize = size
} else {
if size.Width > bestSize.Width || size.Height > bestSize.Height {
bestSize = size
}
}
}
return bestSize.Source
}

func getFlickrPhotoUrls(url string) (map[string]string, error) {
if flickrApiKey == "" || flickrApiKey == "yourflickrapikey" {
return nil, errors.New("invalid flickr api key set")
}
matches := RegexpUrlFlickrPhoto.FindStringSubmatch(url)
photoId := matches[5]
if photoId == "" {
return nil, errors.New("unable to get photo id from url")
}
return map[string]string{getFlickrUrlFromPhotoId(photoId): ""}, nil
}

type FlickrAlbumObject struct {
Photoset struct {
ID string `json:"id"`
Primary string `json:"primary"`
Owner string `json:"owner"`
Ownername string `json:"ownername"`
Photo []struct {
ID string `json:"id"`
Secret string `json:"secret"`
Server string `json:"server"`
Farm int `json:"farm"`
Title string `json:"title"`
Isprimary string `json:"isprimary"`
Ispublic int `json:"ispublic"`
Isfriend int `json:"isfriend"`
Isfamily int `json:"isfamily"`
} `json:"photo"`
Page int `json:"page"`
PerPage int `json:"per_page"`
Perpage int `json:"perpage"`
Pages int `json:"pages"`
Total string `json:"total"`
Title string `json:"title"`
} `json:"photoset"`
Stat string `json:"stat"`
}

func getFlickrAlbumUrls(url string) (map[string]string, error) {
if flickrApiKey == "" || flickrApiKey == "yourflickrapikey" {
return nil, errors.New("invalid flickr api key set")
}
matches := RegexpUrlFlickrAlbum.FindStringSubmatch(url)
albumId := matches[6]
if albumId == "" {
return nil, errors.New("unable to get album id from url")
}
reqUrl := fmt.Sprintf("https://www.flickr.com/services/rest/?format=json&nojsoncallback=1&method=%s&api_key=%s&photoset_id=%s&per_page=500",
"flickr.photosets.getPhotos", flickrApiKey, albumId)
flickrAlbum := new(FlickrAlbumObject)
getJson(reqUrl, flickrAlbum)
links := make(map[string]string)
for _, photo := range flickrAlbum.Photoset.Photo {
links[getFlickrUrlFromPhotoId(photo.ID)] = ""
}
fmt.Printf("[%s] Found flickr album with %d images (url: %s)\n", time.Now().Format(time.Stamp), len(links), url)
return links, nil
}

func getPossibleTistorySiteUrls(url string) (map[string]string, error) {
client := new(http.Client)
request, err := http.NewRequest("HEAD", url, nil)
Expand Down
153 changes: 153 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"gopkg.in/ini.v1"
"os"
"reflect"
"regexp"
"strings"
Expand All @@ -17,6 +19,15 @@ func init() {
RegexpUrlImgurAlbum, _ = regexp.Compile(REGEXP_URL_IMGUR_ALBUM)
RegexpUrlGoogleDrive, _ = regexp.Compile(REGEXP_URL_GOOGLEDRIVE)
RegexpUrlPossibleTistorySite, _ = regexp.Compile(REGEXP_URL_POSSIBLE_TISTORY_SITE)
RegexpUrlFlickrPhoto, _ = regexp.Compile(REGEXP_URL_FLICKR_PHOTO)
RegexpUrlFlickrAlbum, _ = regexp.Compile(REGEXP_URL_FLICKR_ALBUM)
flickrApiKey = os.Getenv("FLICKR_API_KEY")

var err error
cfg, err := ini.Load("config.ini")
if err == nil {
flickrApiKey = cfg.Section("flickr").Key("api key").MustString("yourflickrapikey")
}
}

type urlsTestpair struct {
Expand Down Expand Up @@ -278,3 +289,145 @@ func TestGetPossibleTistorySiteUrls(t *testing.T) {
}
}
}

var getFlickrUrlFromPhotoIdTests = []map[string]string{
{
"value": "31065043320",
"result": "https://farm6.staticflickr.com/5521/31065043320_cd03a9a448_b.jpg",
},
}

func TestGetFlickrUrlFromPhotoId(t *testing.T) {
for _, pair := range getFlickrUrlFromPhotoIdTests {
v := getFlickrUrlFromPhotoId(pair["value"])
if v != pair["result"] {
t.Errorf("For %s, expected %s, got %s", pair["value"], pair["result"], v)
}
}
}

var getFlickrPhotoUrlsTests = []urlsTestpair{
{
"https://www.flickr.com/photos/137385017@N08/31065043320/in/album-72157677350305446/",
map[string]string{
"https://farm6.staticflickr.com/5521/31065043320_cd03a9a448_b.jpg": "",
},
},
{
"https://www.flickr.com/photos/137385017@N08/31065043320/in/album-72157677350305446",
map[string]string{
"https://farm6.staticflickr.com/5521/31065043320_cd03a9a448_b.jpg": "",
},
},
{
"https://www.flickr.com/photos/137385017@N08/31065043320/",
map[string]string{
"https://farm6.staticflickr.com/5521/31065043320_cd03a9a448_b.jpg": "",
},
},
{
"https://www.flickr.com/photos/137385017@N08/31065043320",
map[string]string{
"https://farm6.staticflickr.com/5521/31065043320_cd03a9a448_b.jpg": "",
},
},
}

func TestGetFlickrPhotoUrls(t *testing.T) {
for _, pair := range getFlickrPhotoUrlsTests {
v, err := getFlickrPhotoUrls(pair.value)
if err != nil {
t.Errorf("For %v, expected %v, got %v", pair.value, nil, err)
}
if !reflect.DeepEqual(v, pair.result) {
t.Errorf("For %s, expected %s, got %s", pair.value, pair.result, v)
}
}
}

var getFlickrAlbumUrlsTests = []urlsTestpair{
{
"https://www.flickr.com/photos/137385017@N08/albums/72157677350305446/",
map[string]string{
"https://farm6.staticflickr.com/5521/31065043320_cd03a9a448_b.jpg": "",
"https://farm6.staticflickr.com/5651/31434767515_49f88ee12e_b.jpg": "",
"https://farm6.staticflickr.com/5750/31434766825_529fd08071_b.jpg": "",
"https://farm6.staticflickr.com/5811/31319456971_37c8c4708a_b.jpg": "",
"https://farm6.staticflickr.com/5494/30627074913_b7f810fc26_b.jpg": "",
"https://farm6.staticflickr.com/5539/31065042720_d76f643b28_b.jpg": "",
"https://farm6.staticflickr.com/5813/31434765285_94b85d5e8c_b.jpg": "",
"https://farm6.staticflickr.com/5600/31065044090_eca63bd5a5_b.jpg": "",
"https://farm6.staticflickr.com/5733/31434764435_350825477e_b.jpg": "",
"https://farm6.staticflickr.com/5715/30627073573_b86e4b2c22_b.jpg": "",
"https://farm6.staticflickr.com/5758/31289864222_5e3cca7e72_b.jpg": "",
"https://farm6.staticflickr.com/5801/30627076673_5a32f3e562_b.jpg": "",
"https://farm6.staticflickr.com/5538/31319458901_088858d7f1_b.jpg": "",
},
},
{
"https://www.flickr.com/photos/137385017@N08/albums/72157677350305446",
map[string]string{
"https://farm6.staticflickr.com/5521/31065043320_cd03a9a448_b.jpg": "",
"https://farm6.staticflickr.com/5651/31434767515_49f88ee12e_b.jpg": "",
"https://farm6.staticflickr.com/5750/31434766825_529fd08071_b.jpg": "",
"https://farm6.staticflickr.com/5811/31319456971_37c8c4708a_b.jpg": "",
"https://farm6.staticflickr.com/5494/30627074913_b7f810fc26_b.jpg": "",
"https://farm6.staticflickr.com/5539/31065042720_d76f643b28_b.jpg": "",
"https://farm6.staticflickr.com/5813/31434765285_94b85d5e8c_b.jpg": "",
"https://farm6.staticflickr.com/5600/31065044090_eca63bd5a5_b.jpg": "",
"https://farm6.staticflickr.com/5733/31434764435_350825477e_b.jpg": "",
"https://farm6.staticflickr.com/5715/30627073573_b86e4b2c22_b.jpg": "",
"https://farm6.staticflickr.com/5758/31289864222_5e3cca7e72_b.jpg": "",
"https://farm6.staticflickr.com/5801/30627076673_5a32f3e562_b.jpg": "",
"https://farm6.staticflickr.com/5538/31319458901_088858d7f1_b.jpg": "",
},
},
{
"https://www.flickr.com/photos/137385017@N08/albums/with/72157677350305446/",
map[string]string{
"https://farm6.staticflickr.com/5521/31065043320_cd03a9a448_b.jpg": "",
"https://farm6.staticflickr.com/5651/31434767515_49f88ee12e_b.jpg": "",
"https://farm6.staticflickr.com/5750/31434766825_529fd08071_b.jpg": "",
"https://farm6.staticflickr.com/5811/31319456971_37c8c4708a_b.jpg": "",
"https://farm6.staticflickr.com/5494/30627074913_b7f810fc26_b.jpg": "",
"https://farm6.staticflickr.com/5539/31065042720_d76f643b28_b.jpg": "",
"https://farm6.staticflickr.com/5813/31434765285_94b85d5e8c_b.jpg": "",
"https://farm6.staticflickr.com/5600/31065044090_eca63bd5a5_b.jpg": "",
"https://farm6.staticflickr.com/5733/31434764435_350825477e_b.jpg": "",
"https://farm6.staticflickr.com/5715/30627073573_b86e4b2c22_b.jpg": "",
"https://farm6.staticflickr.com/5758/31289864222_5e3cca7e72_b.jpg": "",
"https://farm6.staticflickr.com/5801/30627076673_5a32f3e562_b.jpg": "",
"https://farm6.staticflickr.com/5538/31319458901_088858d7f1_b.jpg": "",
},
},
{
"https://www.flickr.com/photos/137385017@N08/albums/with/72157677350305446",
map[string]string{
"https://farm6.staticflickr.com/5521/31065043320_cd03a9a448_b.jpg": "",
"https://farm6.staticflickr.com/5651/31434767515_49f88ee12e_b.jpg": "",
"https://farm6.staticflickr.com/5750/31434766825_529fd08071_b.jpg": "",
"https://farm6.staticflickr.com/5811/31319456971_37c8c4708a_b.jpg": "",
"https://farm6.staticflickr.com/5494/30627074913_b7f810fc26_b.jpg": "",
"https://farm6.staticflickr.com/5539/31065042720_d76f643b28_b.jpg": "",
"https://farm6.staticflickr.com/5813/31434765285_94b85d5e8c_b.jpg": "",
"https://farm6.staticflickr.com/5600/31065044090_eca63bd5a5_b.jpg": "",
"https://farm6.staticflickr.com/5733/31434764435_350825477e_b.jpg": "",
"https://farm6.staticflickr.com/5715/30627073573_b86e4b2c22_b.jpg": "",
"https://farm6.staticflickr.com/5758/31289864222_5e3cca7e72_b.jpg": "",
"https://farm6.staticflickr.com/5801/30627076673_5a32f3e562_b.jpg": "",
"https://farm6.staticflickr.com/5538/31319458901_088858d7f1_b.jpg": "",
},
},
}

func TestGetFlickrAlbumUrls(t *testing.T) {
for _, pair := range getFlickrAlbumUrlsTests {
v, err := getFlickrAlbumUrls(pair.value)
if err != nil {
t.Errorf("For %v, expected %v, got %v", pair.value, nil, err)
}
if !reflect.DeepEqual(v, pair.result) {
t.Errorf("For %s, expected %s, got %s", pair.value, pair.result, v)
}
}
}

0 comments on commit f3e31be

Please sign in to comment.