Skip to content

Commit

Permalink
Add kizu option
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin MS committed Feb 8, 2019
1 parent f7f575f commit d4a7759
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/getcards.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/getprice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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.
Expand Down
14 changes: 10 additions & 4 deletions libs/yyt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion libs/yyt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down

0 comments on commit d4a7759

Please sign in to comment.