Created
January 25, 2014 19:30
-
-
Save xiam/8622073 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"menteslibres.net/gosexy/resource" | |
"menteslibres.net/gosexy/rest" | |
"menteslibres.net/gosexy/to" | |
"net/url" | |
"sync" | |
"time" | |
) | |
type RedditResponse struct { | |
Data struct { | |
Children []struct { | |
Data Post `json:"data"` | |
} `json:"children"` | |
} `json:"data"` | |
} | |
type Post struct { | |
Thumbnail string `json:"thumbnail"` | |
Ups uint `json:"ups"` | |
Downs uint `json:"downs"` | |
Url string `json:"url"` | |
Title string `json:"title"` | |
CreatedUTC Time `json:"created_utc"` | |
} | |
type Time struct { | |
time.Time | |
} | |
func (self *Time) UnmarshalJSON(data []byte) error { | |
// Time values are returned like 123456789.0, that does not unmarshal nicely | |
// into a float, integer nor a time.Time, so we had to unmarshal it by | |
// wrapping time.Time and adding a custom unmarshaler. | |
self.Time = time.Unix(0, 0).UTC() | |
self.Time = self.Time.Add(to.Duration(string(data) + "s")) | |
return nil | |
} | |
func main() { | |
var err error | |
data := RedditResponse{} | |
err = rest.Get(&data, "http://www.reddit.com/r/pics.json", url.Values{"limit": {"100"}}) | |
var wg sync.WaitGroup | |
if err == nil { | |
for _, child := range data.Data.Children { | |
wg.Add(1) | |
go func(url string) { | |
log.Printf("Trying to download %s.\n", url) | |
file, err := resource.Download(url, "downloads") | |
if err != nil { | |
log.Printf("Download: %s\n", err.Error()) | |
} | |
log.Printf("Wrote: %s\n", file) | |
wg.Done() | |
}(child.Data.Url) | |
/* | |
fmt.Println("Title: ", child.Data.Title, "(ups: ", child.Data.Ups, ", downs: ", child.Data.Downs, ")") | |
fmt.Println("URL: ", child.Data.Url) | |
fmt.Printf("Date: %v\n", child.Data.CreatedUTC) | |
fmt.Println("--") | |
*/ | |
} | |
} else { | |
log.Printf("Get error: %s\n", err.Error()) | |
} | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment