From d4a775908ab2ca6f99d3e81d01cb757115fc4a64 Mon Sep 17 00:00:00 2001 From: Kevin MS Date: Fri, 8 Feb 2019 16:27:19 +0100 Subject: [PATCH] Add kizu option --- cmd/getcards.go | 2 +- cmd/getprice.go | 2 +- cmd/root.go | 2 ++ libs/yyt.go | 14 ++++++++++---- libs/yyt_test.go | 2 +- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/cmd/getcards.go b/cmd/getcards.go index 277b0c9..6a81584 100644 --- a/cmd/getcards.go +++ b/cmd/getcards.go @@ -50,7 +50,7 @@ var getcardsCmd = &cobra.Command{ fmt.Printf(err.Error()) } defer out.Close() - cardMap := mylib.GetCards(Series) + cardMap := mylib.GetCards(Series, Kizu) b, errMarshal := json.Marshal(cardMap) if errMarshal != nil { fmt.Println(errMarshal) diff --git a/cmd/getprice.go b/cmd/getprice.go index f256c4d..51115b6 100644 --- a/cmd/getprice.go +++ b/cmd/getprice.go @@ -32,7 +32,7 @@ var getpriceCmd = &cobra.Command{ fmt.Println("getprice called") for _, serie := range Series { var price = 0 - cardMap := mylib.GetCards([]string{serie}) + cardMap := mylib.GetCards([]string{serie}, Kizu) for _, infos := range cardMap { for _, rarity := range goodRarity { if rarity == infos.Rarity { diff --git a/cmd/root.go b/cmd/root.go index 20653a9..d6f515c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -25,6 +25,7 @@ import ( var cfgFile string var Series []string +var Kizu bool // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ @@ -58,6 +59,7 @@ func init() { // will be global for your application. rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.ycli.yaml)") rootCmd.PersistentFlags().StringArrayVarP(&Series, "series", "s", []string{}, "Default fetch all series") + rootCmd.PersistentFlags().BoolVarP(&Kizu, "kizu", "k", false, "Get only damaged informations") // Cobra also supports local flags, which will only run // when this action is called directly. diff --git a/libs/yyt.go b/libs/yyt.go index 5a90ce9..52b6410 100644 --- a/libs/yyt.go +++ b/libs/yyt.go @@ -85,6 +85,9 @@ func fetchCards(url string, tmpCardChan chan waitCard) { var wg sync.WaitGroup fmt.Println(url) images, errCard := goquery.NewDocument(url) + if errCard != nil { + fmt.Println(errCard) + } yytSetCode := images.Find("input[name='item[ver]']").AttrOr("value", "undefined") // fetch normal cards images.Find("li:not([class*=rarity_S-]).card_unit").Each(func(cardI int, cardLi *goquery.Selection) { @@ -99,14 +102,11 @@ func fetchCards(url string, tmpCardChan chan waitCard) { v.EBFoil = true tmpCardChan <- waitCard{Card: v, Wg: &wg} }) - if errCard != nil { - fmt.Println(errCard) - } wg.Wait() } // GetCards function -func GetCards(series []string) map[string]Card { +func GetCards(series []string, kizu bool) map[string]Card { // Get cards fmt.Println("getcards") fetchChannel := make(chan bool, maxLength) @@ -139,6 +139,9 @@ func GetCards(series []string) map[string]Card { } doc.Find(filter).Each(func(i int, s *goquery.Selection) { url, has := s.Attr("href") + if kizu { + url = url + "&kizu=1" + } if has { fetchChannel <- true wg.Add(1) @@ -151,6 +154,9 @@ func GetCards(series []string) map[string]Card { }) } else { for _, url := range series { + if kizu { + url = url + "&kizu=1" + } wg.Add(1) fetchChannel <- true go func(url string) { diff --git a/libs/yyt_test.go b/libs/yyt_test.go index 5dc8d86..3f7302b 100644 --- a/libs/yyt_test.go +++ b/libs/yyt_test.go @@ -7,7 +7,7 @@ import ( const NbCard = 117 func TestImport(t *testing.T) { - cardMap := GetCards([]string{"tmhmovie"}) + cardMap := GetCards([]string{"tmhmovie"}, false) if len(cardMap) != NbCard { t.Errorf("Errors in parsing expected %d but got %d", NbCard, len(cardMap)) }