<commit_before>package search

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"os/exec"
	"strconv"

	"github.com/PuerkitoBio/goquery"
	"github.com/fatih/color"
	"github.com/wasanx25/sreq/config"
	"github.com/wasanx25/sreq/history"
)

// Content is structure that scraping content from Qiita
type Content struct {
	ID    string
	Title string
	Desc  string
}

func search(argument string, pagenation int, sort string) ([]*Content, error) {
	doc, err := goquery.NewDocument(config.GetPageURL(argument, sort, strconv.Itoa(pagenation)))
	if err != nil {
		return nil, err
	}

	var contents []*Content

	doc.Find(".searchResult").Each(func(_ int, s *goquery.Selection) {
		itemID, _ := s.Attr("data-uuid")
		title := s.Find(".searchResult_itemTitle a").Text()
		desc := s.Find(".searchResult_snippet").Text()

		content := &Content{
			ID:    itemID,
			Title: title,
			Desc:  desc,
		}

		contents = append(contents, content)
	})

	return contents, nil
}

func viewList(contents []*Content) {
	for num, content := range contents {
		fmt.Print(color.YellowString(strconv.Itoa(num) + " -> "))
		fmt.Println(content.Title)
		fmt.Println(color.GreenString(content.Desc))
		fmt.Print("\n")
	}
	if len(contents) == 10 {
		fmt.Println(color.YellowString("n -> ") + "next page")
	}
	fmt.Print("SELECT > ")
}

func scan(contents []*Content, argument string, lynx bool) bool {
	var num string
	if _, err := fmt.Scanf("%s", &num); err != nil {
		fmt.Println(err)
	}

	if num == "n" {
		return false
	}

	index, _ := strconv.Atoi(num)
	target := contents[index]

	resp, err := http.Get(config.GetAPIURL(target.ID))
	if err != nil {
		fmt.Println(err)
	}
	defer resp.Body.Close()

	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println(err)
	}
	var qiita *config.Qiita
	json.Unmarshal(b, &qiita)

	writeHistory(qiita, argument)

	if lynx {
		openFile(qiita.HTML, "/tmp/sreq.html", "lynx", "-display_charset=utf-8", "-assume_charset=utf-8")
		return true
	}

	openFile(qiita.Markdown, "/tmp/sreq.txt", "less")
	return true
}

func openFile(body string, file string, cmdName ...string) {
	text := []byte(body)
	ioutil.WriteFile(file, text, os.ModePerm)
	cmdName = append(cmdName, file)
	cmd := exec.Command(cmdName[0], cmdName[1:]...)
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Run()
}

func writeHistory(content *config.Qiita, argument string) {
	var snippets history.Snippets
	snippets.Load()
	url := content.URL
	newSnippet := history.Snippet{
		SearchKeyword: argument,
		URL:           url,
		Title:         content.Title,
	}
	snippets.Snippets = append(snippets.Snippets, newSnippet)
	if err := snippets.Save(); err != nil {
		fmt.Printf("Failed. %v", err)
		os.Exit(2)
	}
}
<commit_msg>Reset search.go<commit_after>package search

// import (
// 	"encoding/json"
// 	"fmt"
// 	"io/ioutil"
// 	"net/http"
// 	"os"
// 	"os/exec"
// 	"strconv"
//
// 	"github.com/PuerkitoBio/goquery"
// 	"github.com/fatih/color"
// 	"github.com/wasanx25/sreq/config"
// 	"github.com/wasanx25/sreq/history"
// )
//
// // Content is structure that scraping content from Qiita
// type Content struct {
// 	ID    string
// 	Title string
// 	Desc  string
// }
//
// func search(argument string, pagenation int, sort string) ([]*Content, error) {
// 	doc, err := goquery.NewDocument(config.GetPageURL(argument, sort, strconv.Itoa(pagenation)))
// 	if err != nil {
// 		return nil, err
// 	}
//
// 	var contents []*Content
//
// 	doc.Find(".searchResult").Each(func(_ int, s *goquery.Selection) {
// 		itemID, _ := s.Attr("data-uuid")
// 		title := s.Find(".searchResult_itemTitle a").Text()
// 		desc := s.Find(".searchResult_snippet").Text()
//
// 		content := &Content{
// 			ID:    itemID,
// 			Title: title,
// 			Desc:  desc,
// 		}
//
// 		contents = append(contents, content)
// 	})
//
// 	return contents, nil
// }
//
// func viewList(contents []*Content) {
// 	for num, content := range contents {
// 		fmt.Print(color.YellowString(strconv.Itoa(num) + " -> "))
// 		fmt.Println(content.Title)
// 		fmt.Println(color.GreenString(content.Desc))
// 		fmt.Print("\n")
// 	}
// 	if len(contents) == 10 {
// 		fmt.Println(color.YellowString("n -> ") + "next page")
// 	}
// 	fmt.Print("SELECT > ")
// }
//
// func scan(contents []*Content, argument string, lynx bool) bool {
// 	var num string
// 	if _, err := fmt.Scanf("%s", &num); err != nil {
// 		fmt.Println(err)
// 	}
//
// 	if num == "n" {
// 		return false
// 	}
//
// 	index, _ := strconv.Atoi(num)
// 	target := contents[index]
//
// 	resp, err := http.Get(config.GetAPIURL(target.ID))
// 	if err != nil {
// 		fmt.Println(err)
// 	}
// 	defer resp.Body.Close()
//
// 	b, err := ioutil.ReadAll(resp.Body)
// 	if err != nil {
// 		fmt.Println(err)
// 	}
// 	var qiita *config.Qiita
// 	json.Unmarshal(b, &qiita)
//
// 	writeHistory(qiita, argument)
//
// 	if lynx {
// 		openFile(qiita.HTML, "/tmp/sreq.html", "lynx", "-display_charset=utf-8", "-assume_charset=utf-8")
// 		return true
// 	}
//
// 	openFile(qiita.Markdown, "/tmp/sreq.txt", "less")
// 	return true
// }
//
// func openFile(body string, file string, cmdName ...string) {
// 	text := []byte(body)
// 	ioutil.WriteFile(file, text, os.ModePerm)
// 	cmdName = append(cmdName, file)
// 	cmd := exec.Command(cmdName[0], cmdName[1:]...)
// 	cmd.Stdin = os.Stdin
// 	cmd.Stdout = os.Stdout
// 	cmd.Run()
// }
//
// func writeHistory(content *config.Qiita, argument string) {
// 	var snippets history.Snippets
// 	snippets.Load()
// 	url := content.URL
// 	newSnippet := history.Snippet{
// 		SearchKeyword: argument,
// 		URL:           url,
// 		Title:         content.Title,
// 	}
// 	snippets.Snippets = append(snippets.Snippets, newSnippet)
// 	if err := snippets.Save(); err != nil {
// 		fmt.Printf("Failed. %v", err)
// 		os.Exit(2)
// 	}
// }

{'fraction_non_alphanumeric': 0.1954978354978355, 'fraction_numerical': 0.0038095238095238095, 'mean_word_length': 2.939972714870396, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '4963459', 'n_tokens_mistral': 2412, 'n_tokens_neox': 2287, 'n_words': 433}