<commit_before>package extra

import (
	"io"
	"bufio"
	"errors"
	"fmt"
	"net/http"
	"strings"
	"unicode"

	"github.com/belak/go-seabird"
	"github.com/go-irc/irc"
)

func init() {
	seabird.RegisterPlugin("bulkcnam", newBulkCNAMPlugin)
}

type bulkCNAMPlugin struct {
	Key string
}

func newBulkCNAMPlugin(b *seabird.Bot, cm *seabird.CommandMux) error {
	p := &bulkCNAMPlugin{}

	err := b.Config("bulkcnam", p)
	if err != nil {
		return err
	}

	cm.Event("cnam", p.bulkCNAMCallback, &seabird.HelpInfo{
		Usage:       "<phone #>",
		Description: "Returns the CNAM of a phone number",
	})

	return nil
}

func (p *bulkCNAMPlugin) bulkCNAMCallback(b *seabird.Bot, m *irc.Message) {
	if !m.FromChannel() {
		return
	}

	r, err := p.BulkCNAM(m.Trailing())
	if err != nil {
		b.MentionReply(m, "Error: %s", err)
		return
	}

	b.MentionReply(m, "%s", r)
}

// This function queries the BulkCNAM API for a Phone #'s
// corresponding CNAM, and returns it
func (p *bulkCNAMPlugin) BulkCNAM(number string) (string, error) {
	for _, digit := range number {
		if !unicode.IsDigit(digit) {
			return "", errors.New("Not a phone number")
		}
	}

	resp, err := http.Get(fmt.Sprintf("http://cnam.bulkcnam.com/?id=%s&did=%s", p.Key, number))
	if err != nil {
		return "", errors.New("BulkCNAM appears to be down")
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		return "", errors.New("Server side error occurred")
	}

	in := bufio.NewReader(resp.Body)
	for {
		line, err := in.ReadString('\n')
		if err != nil && err != io.EOF {
			break
		}

		return strings.TrimSpace(line), nil
	}

	return "", errors.New("No results")
}<commit_msg>simplify CNAM plugin<commit_after>package extra

import (
	"io"
	"bufio"
	"fmt"
	"net/http"
	"strings"
	"unicode"

	"github.com/belak/go-seabird"
	"github.com/go-irc/irc"
)

func init() {
	seabird.RegisterPlugin("bulkcnam", newBulkCNAMPlugin)
}

type bulkCNAMPlugin struct {
	Key string
}

func newBulkCNAMPlugin(b *seabird.Bot, cm *seabird.CommandMux) error {
	p := &bulkCNAMPlugin{}

	err := b.Config("bulkcnam", p)
	if err != nil {
		return err
	}

	cm.Event("cnam", p.bulkCNAMCallback, &seabird.HelpInfo{
		Usage:       "<phone #>",
		Description: "Returns the CNAM of a phone number",
	})

	return nil
}

// This function queries the BulkCNAM API for a Phone #'s
// corresponding CNAM, and returns it
func (p *bulkCNAMPlugin) bulkCNAMCallback(b *seabird.Bot, m *irc.Message) {
	number := m.Trailing()

	for _, digit := range number {
		if !unicode.IsDigit(digit) {
			b.MentionReply(m, "Error: Not a phone number")
			return
		}
	}

	resp, err := http.Get(fmt.Sprintf("http://cnam.bulkcnam.com/?id=%s&did=%s", p.Key, number))
	if err != nil {
		b.MentionReply(m, "Error: BulkCNAM appears to be down")
		return
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		b.MentionReply(m, "Error: Server side error occurred")
		return
	}

	in := bufio.NewReader(resp.Body)
	line, err := in.ReadString('\n')
	if err != nil || err == io.EOF {
		b.MentionReply(m, "%s", strings.TrimSpace(line))
	} else {
		b.MentionReply(m, "Error: Something happened when parsing the response.")
	}
}
{'fraction_non_alphanumeric': 0.1609380019274012, 'fraction_numerical': 0.0019274012206874397, 'mean_word_length': 3.776073619631902, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2979603', 'n_tokens_mistral': 1313, 'n_tokens_neox': 1222, 'n_words': 291}