Skip to content

Instantly share code, notes, and snippets.

@bycoffe
Created November 18, 2014 19:50
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 bycoffe/010213650ec9eadcfa00 to your computer and use it in GitHub Desktop.
Save bycoffe/010213650ec9eadcfa00 to your computer and use it in GitHub Desktop.
Get the first 50 pages of polls from the Pollster database
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