Skip to content

Instantly share code, notes, and snippets.

@siennathesane
Created September 30, 2015 16:04
Show Gist options
  • Select an option

  • Save siennathesane/e8b2c63862bc46585b11 to your computer and use it in GitHub Desktop.

Select an option

Save siennathesane/e8b2c63862bc46585b11 to your computer and use it in GitHub Desktop.
Downloads all the wallpapers from the Imgur main gallery.
package main
/*
Made with <3 from picturesarenice and emi8ly.
*/
import (
"encoding/json"
"fmt"
"github.com/cheggaaa/pb"
"io"
"net/http"
"os"
"regexp"
"sync"
)
type ImgurData struct {
Width int16 `bson:"width" json:"width"`
Points int32 `bson:"points" json:"points"`
CommentCount int32 `bson:"comment_count" json:"comment_count"`
TopicId int32 `bson:"topic_id" json:"topic_id"`
AccountId int32 `bson:"account_id" json:"account_id"`
Ups int32 `bson:"ups" json:"ups"`
Downs int32 `bson:"downs" json:"downs"`
Bandwidth int64 `bson:"bandwidth" json:"bandwidth"`
Datetime int64 `bson:"datetime" json:"datetime"`
Score int64 `bson:"score" json:"score"`
AccountUrl string `bson:"account_url" json:"account_url"`
Topic string `bson:"topic" json:"topic"`
Link string `bson:"link" json:"link"`
Id string `bson:"_id,omitempty" json:"id"`
Description string `bson:"description" json:"description"`
CommentPreview string `bson:"comment_preview" json:"comment_preview"`
Vote string `bson:"vote" json:"vote"`
Title string `bson:"title" json:"title"`
Section string `bson:"section" json:"section"`
Favorite bool `bson:"favorite" json:"favorite"`
Is_Album bool `bson:"is_album" json:"is_album"`
Nsfw bool `bson:"nsfw" json:"nsfw"`
}
type ImgurJson struct {
Status int16 `json:"status"`
Success bool `json:"success"`
Data []ImgurData `json:"data"`
}
func downloader() (savedJson ImgurJson) {
client := &http.Client{}
req, err := http.NewRequest("GET", "https://api.imgur.com/3/gallery.json", nil)
// my API key.
req.Header.Add()
fmt.Println("downloading data from imgur.")
resp, err := client.Do(req)
fmt.Println("server status: ", resp.StatusCode)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
saveJson := ImgurJson{}
err = decoder.Decode(&saveJson)
return saveJson
}
func jsonReader(imgurJson ImgurJson) {
fmt.Println("compiling searcher.")
r, err := regexp.Compile(`wallpaper`)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
client := &http.Client{}
var wg sync.WaitGroup
for key, _ := range imgurJson.Data {
if r.MatchString(imgurJson.Data[key].Title) == true {
fmt.Println("found wallpapers at ", imgurJson.Data[key].Link)
if imgurJson.Data[key].Is_Album == true {
/*
start parsing through the album images and increment the waitgroup counter.
*/
wg.Add(1)
uri := fmt.Sprintf("https://api.imgur.com/3/album/%s/images", imgurJson.Data[key].Id)
req, err := http.NewRequest("GET", uri, nil)
req.Header.Add()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resp, err := client.Do(req)
fmt.Println("server status: ", resp.StatusCode)
defer resp.Body.Close()
//handles the new album json.
decoder := json.NewDecoder(resp.Body)
if decoder == nil {
fmt.Println("empty data from imgur for %s", imgurJson.Data[key].Link)
os.Exit(1)
}
albumJson := ImgurJson{}
err = decoder.Decode(&albumJson)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
//create new client and download each image individually.
//in theory this is faster.
go imageDownloader(albumJson, wg)
} else {
fmt.Println("not an album: ", imgurJson.Data[key].Link)
}
} else {
continue
}
}
}
func imageDownloader(albumJson ImgurJson, wg sync.WaitGroup) {
client := &http.Client{}
for image, _ := range albumJson.Data {
req, err := http.NewRequest("GET", albumJson.Data[image].Link, nil)
resp, err := client.Do(req)
fmt.Println("server status: ", resp.StatusCode)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("saving image %s", albumJson.Data[image].Id)
out, err := os.Create(albumJson.Data[image].Id)
//progress bar
header := resp.ContentLength
bar := pb.New(int(header)).SetUnits(pb.U_BYTES)
bar.Start()
reader := bar.NewProxyReader(resp.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// not closing reader as the NewProxyReader exits for us.
io.Copy(out, reader)
wg.Done()
}
}
func main() {
jsonData := downloader()
jsonReader(jsonData)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment