<commit_before>package main

import (
	"bufio"
	"encoding/json"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"os"
	"runtime"
	"strings"
	"sync"
	"time"

	"github.com/cenkalti/backoff"
)

type Result struct {
	Status    string
	URL       string
	Took      float64
	Locations []string
	Epoch     int64
}

func (r Result) String() string {
	return fmt.Sprintf("%s\t%0.4f\t%d\t%s\t%s\t", r.Status, r.Took, r.Epoch, r.URL, strings.Join(r.Locations, "|"))
}

type URLValue struct {
	Format string `json:"format"`
	Value  string `json:"value"`
}

type Value struct {
	Index     int             `json:"index"`
	Type      string          `json:"type"`
	Data      json.RawMessage `json:"data"`
	TTL       int             `json:"ttl"`
	Timestamp string          `json:"timestamp"`
}

type APIResponse struct {
	Code   int     `json:"responseCode"`
	Handle string  `json:"handle"`
	Values []Value `json:"values"`
}

func worker(queue chan *url.URL, out chan Result, wg *sync.WaitGroup) {
	defer wg.Done()
	for u := range queue {
		r := retrieve(u)
		out <- r
	}
}

func sink(out chan Result, done chan bool) {
	for r := range out {
		fmt.Println(r)
	}
	done <- true
}

// retrieve will try to GET and parse a DOI API response and will always
// return a Result, which will contain status (either HTTP or internal error designations)
func retrieve(target *url.URL) Result {

	rt := http.DefaultTransport
	var req *http.Request

	err := backoff.Retry(func() (e error) {
		req, e = http.NewRequest("GET", target.String(), nil)
		return
	}, backoff.NewExponentialBackOff())

	if err != nil {
		return Result{Status: "E_REQ", URL: target.String(), Took: 0, Epoch: time.Now().Unix()}
	}

	var resp *http.Response

	start := time.Now()
	err = backoff.Retry(func() (e error) {
		resp, e = rt.RoundTrip(req)
		if e != nil {
			log.Printf("retrying %s", req.URL.String())
		}
		return e
	}, backoff.NewExponentialBackOff())
	elapsed := time.Since(start)

	if err != nil {
		return Result{Status: "E_REQ", URL: target.String(), Took: elapsed.Seconds(), Epoch: time.Now().Unix()}
	}

	body, err := ioutil.ReadAll(resp.Body)

	if err != nil {
		return Result{Status: "E_READ", URL: target.String(), Took: elapsed.Seconds(), Epoch: time.Now().Unix()}
	}

	resp.Body.Close()

	var ar APIResponse
	err = json.Unmarshal(body, &ar)
	if err != nil {
		return Result{Status: "E_JSON", URL: target.String(), Took: elapsed.Seconds(), Epoch: time.Now().Unix()}
	}

	result := Result{Status: resp.Status, URL: target.String(), Took: elapsed.Seconds(), Epoch: time.Now().Unix()}

	for _, value := range ar.Values {
		if value.Type == "URL" {
			var v URLValue
			err := json.Unmarshal(value.Data, &v)
			if err != nil {
				return Result{Status: "E_JSON", URL: target.String(), Took: elapsed.Seconds(), Epoch: time.Now().Unix()}
			}
			result.Locations = append(result.Locations, v.Value)
		}
	}
	return result
}

func main() {

	numWorkers := flag.Int("w", runtime.NumCPU(), "number of workers")

	flag.Parse()

	runtime.GOMAXPROCS(*numWorkers)

	reader := bufio.NewReader(os.Stdin)

	queue := make(chan *url.URL)
	out := make(chan Result)<commit_msg>add version<commit_after>package main

import (
	"bufio"
	"encoding/json"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"os"
	"runtime"
	"strings"
	"sync"
	"time"

	"github.com/cenkalti/backoff"
)

const Version = "0.1.1"

type Result struct {
	Status    string
	URL       string
	Took      float64
	Locations []string
	Epoch     int64
}

func (r Result) String() string {
	return fmt.Sprintf("%s\t%0.4f\t%d\t%s\t%s\t", r.Status, r.Took, r.Epoch, r.URL, strings.Join(r.Locations, "|"))
}

type URLValue struct {
	Format string `json:"format"`
	Value  string `json:"value"`
}

type Value struct {
	Index     int             `json:"index"`
	Type      string          `json:"type"`
	Data      json.RawMessage `json:"data"`
	TTL       int             `json:"ttl"`
	Timestamp string          `json:"timestamp"`
}

type APIResponse struct {
	Code   int     `json:"responseCode"`
	Handle string  `json:"handle"`
	Values []Value `json:"values"`
}

func worker(queue chan *url.URL, out chan Result, wg *sync.WaitGroup) {
	defer wg.Done()
	for u := range queue {
		r := retrieve(u)
		out <- r
	}
}

func sink(out chan Result, done chan bool) {
	for r := range out {
		fmt.Println(r)
	}
	done <- true
}

// retrieve will try to GET and parse a DOI API response and will always
// return a Result, which will contain status (either HTTP or internal error designations)
func retrieve(target *url.URL) Result {

	rt := http.DefaultTransport
	var req *http.Request

	err := backoff.Retry(func() (e error) {
		req, e = http.NewRequest("GET", target.String(), nil)
		return
	}, backoff.NewExponentialBackOff())

	if err != nil {
		return Result{Status: "E_REQ", URL: target.String(), Took: 0, Epoch: time.Now().Unix()}
	}

	var resp *http.Response

	start := time.Now()
	err = backoff.Retry(func() (e error) {
		resp, e = rt.RoundTrip(req)
		if e != nil {
			log.Printf("retrying %s", req.URL.String())
		}
		return e
	}, backoff.NewExponentialBackOff())
	elapsed := time.Since(start)

	if err != nil {
		return Result{Status: "E_REQ", URL: target.String(), Took: elapsed.Seconds(), Epoch: time.Now().Unix()}
	}

	body, err := ioutil.ReadAll(resp.Body)

	if err != nil {
		return Result{Status: "E_READ", URL: target.String(), Took: elapsed.Seconds(), Epoch: time.Now().Unix()}
	}

	resp.Body.Close()

	var ar APIResponse
	err = json.Unmarshal(body, &ar)
	if err != nil {
		return Result{Status: "E_JSON", URL: target.String(), Took: elapsed.Seconds(), Epoch: time.Now().Unix()}
	}

	result := Result{Status: resp.Status, URL: target.String(), Took: elapsed.Seconds(), Epoch: time.Now().Unix()}

	for _, value := range ar.Values {
		if value.Type == "URL" {
			var v URLValue
			err := json.Unmarshal(value.Data, &v)
			if err != nil {
				return Result{Status: "E_JSON", URL: target.String(), Took: elapsed.Seconds(), Epoch: time.Now().Unix()}
			}
			result.Locations = append(result.Locations, v.Value)
		}
	}
	return result
}

func main() {

	numWorkers := flag.Int("w", runtime.NumCPU(), "number of workers")
	version := flag.Bool("v", false, "prints current program version")

	flag.Parse()

	if *version {
		fmt.Println(Version)
		os.Exit(0)
	}

	runtime.GOMAXPROCS(*numWorkers)

	reader := bufio.NewReader(os.Stdin)

	queue := make(chan *url.URL)
	out := make(chan Result)
{'fraction_non_alphanumeric': 0.16375818011841695, 'fraction_numerical': 0.0028046120286693674, 'mean_word_length': 3.4700557103064065, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4357022', 'n_tokens_mistral': 2453, 'n_tokens_neox': 2336, 'n_words': 614}