Ejemplo n.º 1
0
func processXML(sess rets.Requester, ctx context.Context, req rets.SearchRequest, elem string, output *string) {
	w := os.Stdout
	if *output != "" {
		os.MkdirAll(*output, 0777)
		f, _ := os.Create(*output + "/results.xml")
		w = f
		defer f.Close()
	}

	if elem == "" {
		elem = ".*Listing$"
	}
	rgx, _ := regexp.Compile(elem)
	match := func(t xml.StartElement) bool {
		return rgx.MatchString(t.Name.Local)
	}
	// loop over all the pages we need
	for {
		fmt.Printf("Querying next page: %v\n", req)
		result, err := rets.StandardXMLSearch(sess, ctx, req)
		if err != nil {
			panic(err)
		}
		switch result.Response.Code {
		case rets.StatusOK:
			// we got some daters
		case rets.StatusNoRecords:
			return
		case rets.StatusSearchError:
			fallthrough
		default: // shit hit the fan
			panic(errors.New(result.Response.Text))
		}
		count := 0
		_, more, err := result.ForEach(match, func(row io.ReadCloser, err error) error {
			if err != nil {
				return err
			}
			count++
			io.Copy(w, row)
			return err
		})
		if err != nil {
			panic(err)
		}
		result.Close()
		if err != nil {
			panic(err)
		}
		if !more {
			return
		}
		if req.Offset == 0 {
			req.Offset = 1
		}
		req.Offset = req.Offset + count
	}
}
Ejemplo n.º 2
0
func processCompact(sess rets.Requester, ctx context.Context, req rets.SearchRequest, output *string) {
	w := csv.NewWriter(os.Stdout)
	if *output != "" {
		os.MkdirAll(*output, 0777)
		f, _ := os.Create(*output + "/results.csv")
		defer f.Close()
		w = csv.NewWriter(f)
	}
	defer w.Flush()

	// loop over all the pages we need
	for {
		fmt.Printf("Querying next page: %v\n", req)
		result, err := rets.SearchCompact(sess, ctx, req)
		if err != nil {
			panic(err)
		}
		switch result.Response.Code {
		case rets.StatusOK:
			// we got some daters
		case rets.StatusNoRecords:
			return
		case rets.StatusSearchError:
			fallthrough
		default: // shit hit the fan
			panic(errors.New(result.Response.Text))
		}
		count := 0
		if count == 0 {
			w.Write(result.Columns)
		}
		hasMoreRows, err := result.ForEach(func(row rets.Row, err error) error {
			if err != nil {
				return err
			}
			w.Write(row)
			count++
			return err
		})
		result.Close()
		if err != nil {
			panic(err)
		}
		if !hasMoreRows {
			return
		}
		if req.Offset == 0 {
			req.Offset = 1
		}
		req.Offset = req.Offset + count
	}
}