Skip to content

Instantly share code, notes, and snippets.

@random-robbie
Created June 24, 2018 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save random-robbie/35a9463bb04e1756dfe8247d7f507b5f to your computer and use it in GitHub Desktop.
Save random-robbie/35a9463bb04e1756dfe8247d7f507b5f to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"sync"
"time"
)
const (
photoLimit = 2
)
var (
// flickr returns json inside of a some wrapper, remove it
replacer = strings.NewReplacer("jsonFlickrFeed(", "", "})", "}")
)
// Flickr public response object
type Flickr struct {
Title string `json:"title"`
Link string `json:"link"`
Description string `json:"description"`
Modified time.Time `json:"modified"`
Generator string `json:"generator"`
Items []struct {
Title string `json:"title"`
Link string `json:"link"`
Media struct {
M string `json:"m"`
} `json:"media"`
DateTaken string `json:"date_taken"`
Description string `json:"description"`
Published time.Time `json:"published"`
Author string `json:"author"`
AuthorID string `json:"author_id"`
Tags string `json:"tags"`
} `json:"items"`
}
func displaylink(link string, wg *sync.WaitGroup) (buf bytes.Buffer) {
defer wg.Done()
log.Println("Downloading", link)
return
}
func main() {
client := &http.Client{Timeout: 5 * time.Second}
linkSet := make(map[string]struct{})
ch := make(chan string, 10)
var wg sync.WaitGroup
go func() {
for link := range ch {
wg.Add(2)
go func(link string) {
displaylink(link, &wg)
}(link)
}
}()
var counter int
for counter <= photoLimit {
res, err := client.Get("https://api.flickr.com/services/feeds/photos_public.gne?tags=kitten&format=json")
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
b, _ := ioutil.ReadAll(res.Body)
var data Flickr
json.Unmarshal([]byte(replacer.Replace(string(b))), &data)
for _, item := range data.Items {
if _, ok := linkSet[item.Media.M]; ok {
continue
}
linkSet[item.Media.M] = struct{}{}
ch <- item.Media.M
counter++
fmt.Printf("Number of links hit: %d\n", counter)
}
}
close(ch)
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment