Skip to content

Instantly share code, notes, and snippets.

@jjjake
Last active May 11, 2016 01:04
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 jjjake/8799d9d35640d00eed1f to your computer and use it in GitHub Desktop.
Save jjjake/8799d9d35640d00eed1f to your computer and use it in GitHub Desktop.
iamine in golang
package main
import (
"sync"
"bytes"
"time"
"bufio"
"fmt"
"github.com/sethgrid/pester"
"os"
"log"
"encoding/json"
"io/ioutil"
)
var logger *log.Logger
var err_logger *log.Logger
func init() {
logger = log.New(os.Stdout, "", 0)
err_logger = log.New(os.Stderr, "", 0)
}
type Error struct {
Message string `json:"error"`
Id string `json:"id"`
}
func log_error(id string, message error) {
e := &Error{Id: id, Message: fmt.Sprintf("%s", message)}
b, _ := json.Marshal(e)
go err_logger.Println(string(b))
}
func mine(id string, client *pester.Client, outChan chan string, wg *sync.WaitGroup) {
url := fmt.Sprintf("https://archive.org/metadata/%s", id)
defer wg.Done()
resp, err := client.Get(url)
defer resp.Body.Close()
if err != nil {
log_error(id, err)
} else {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log_error(id, err)
}
buffer := new(bytes.Buffer)
if err := json.Compact(buffer, body); err != nil {
log_error(id, err)
} else {
go logger.Println(buffer)
}
}
}
func main() {
var wg sync.WaitGroup
inChan, outChan := make(chan string), make(chan string)
done := make(chan struct{})
rate := time.Second / 500
throttle := time.Tick(rate)
// Use pester for retry support.
client := pester.New()
client.MaxRetries = 5
client.Backoff = pester.ExponentialBackoff
wg.Add(1)
go func() {
defer wg.Done()
input := bufio.NewScanner(os.Stdin)
for input.Scan() {
inChan <- input.Text()
}
close(done)
}()
DONE:
for {
select {
case inStr := <-inChan:
<-throttle
wg.Add(1)
go mine(inStr, client, outChan, &wg)
case <-done:
wg.Wait()
break DONE
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment