Created
October 29, 2015 18:01
-
-
Save jjjake/e1ca9bda667f693762c5 to your computer and use it in GitHub Desktop.
Revisions
-
jjjake created this gist
Oct 29, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,66 @@ package main import ( "os" "bufio" "crypto/tls" "net/http" "io/ioutil" "fmt" "time" ) type Resp struct { *http.Response err error } func fetch(uri string, queue chan string) { tr := &http.Transport{ TLSClientConfig: &tls.Config{}, } client := http.Client{Transport: tr} resp, err := client.Get(uri) defer resp.Body.Close() if err != nil { // re-queue queue <- uri return } r := Resp{resp, err} body, _ := ioutil.ReadAll(r.Body) fmt.Println(string(body)) } func q(itemlist string, queue chan string) { file, _ := os.Open(itemlist) defer file.Close() scanner := bufio.NewScanner(file) for scanner.Scan() { uri := "http://archive.org/metadata/" + scanner.Text() queue <- uri } return } func main() { // rate-limiter rate := time.Second / 500 throttle := time.Tick(rate) queue := make(chan string) go q("itemlist.txt", queue) for uri := range queue { <-throttle go fetch(uri, queue) } }