Created
November 18, 2014 19:50
-
-
Save bycoffe/010213650ec9eadcfa00 to your computer and use it in GitHub Desktop.
Get the first 50 pages of polls from the Pollster database
This file contains 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 ( | |
"fmt" | |
"github.com/bycoffe/pollster" | |
"strconv" | |
) | |
func getPolls(pages []string) []pollster.Poll { | |
ch := make(chan []pollster.Poll) | |
polls := []pollster.Poll{} | |
per_page := 10 | |
for _, page := range pages { | |
go func(page string) { | |
polls := pollster.Polls(map[string]string{"page": page}) | |
ch <- polls | |
}(page) | |
} | |
for { | |
select { | |
case r := <-ch: | |
polls = append(polls, r...) | |
if len(polls) == len(pages)*per_page { | |
return polls | |
} | |
} | |
} | |
return polls | |
} | |
func main() { | |
numPages := 50 | |
var pages []string | |
for i := 0; i < numPages; i++ { | |
pages = append(pages, strconv.Itoa(i)) | |
if len(pages) == numPages { | |
results := getPolls(pages) | |
for _, poll := range results { | |
fmt.Println(poll.Pollster) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment