cmd

package
v0.0.0-...-d10ae67 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2021 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var IndexCmd = &cobra.Command{
	Use:     "index",
	Aliases: []string{"i"},
	Short:   "index prestashop images into image match.",
	Long:    "index prestashop images into image match.",
	Run: func(cmd *cobra.Command, args []string) {

		if !dryRun {
			var err error

			dsn := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=utf8mb4&collation=utf8mb4_unicode_ci&parseTime=True&loc=Local", dbUser, dbPass, dbHost, dbPort, dbName)
			db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
				NamingStrategy: schema.NamingStrategy{
					TablePrefix:   dbTablePrefix,
					SingularTable: true,
					NameReplacer:  strings.NewReplacer("ID", "Id"),
				},
			})
			if err != nil {
				log.Fatal(err)
			}
		}

		if dbDrop {
			if db.Migrator().HasTable(&Match{}) {
				db.Migrator().DropTable(&Match{})
			}
		}

		db.Migrator().AutoMigrate(&Match{})

		query := `SELECT   i.id_image as id_image,
						   il.id_lang as id_lang,
						   i.id_product as id_product,
					       i.id_shop as id_shop,
					       il.legend as legend,
					       pl.link_rewrite as link_rewrite
					FROM ` + dbTablePrefix + `image_shop i 
					LEFT JOIN ` + dbTablePrefix + `product_lang pl ON pl.id_product=i.id_product AND pl.id_lang=1
					LEFT JOIN ` + dbTablePrefix + `image_lang il ON il.id_image=i.id_image AND il.id_lang=1`

		var images []*Image
		db.Raw(query).Scan(&images)

		t := throttler.New(2, len(images))

		for _, image := range images {

			go func(i *Image) error {

				defer t.Done(nil)

				imageJson, err := json.Marshal(image)
				if err != nil {
					log.Warn(err)
					return err
				}

				imgPrefixPath := buildFolderForImage(filepath.Join("img", "p"), i.IdImage)
				mediaUrl := filepath.Join(imgPrefixPath, fmt.Sprintf("%d.jpg", i.IdImage))

				fingerprint, err := getMd5File(filepath.Join(psDir, mediaUrl))
				if err != nil {
					log.Warn(err)
					return err
				}

				params := &Add{
					Url:      psDomain + "/" + mediaUrl,
					Filepath: mediaUrl,
					Metadata: string(imageJson),
				}

				var existsImage Match
				err = db.Where("fingerprint = ? ", fingerprint).First(&existsImage).Error
				if !errors.Is(err, gorm.ErrRecordNotFound) {
					log.Infoln("Skipping: ", i.IdImage)
					return nil
				}

				httpClient := &http.Client{}

				summaryBase := sling.New().Base(matchAddr).Client(httpClient)
				req, err := summaryBase.New().Post("add").BodyForm(params).Request()
				if err != nil {
					log.Warn(err)
					return err
				}

				resp, err := httpClient.Do(req)
				if err != nil {
					log.Warn(err)
					return err
				}
				defer resp.Body.Close()
				body, err := ioutil.ReadAll(resp.Body)
				if err != nil {
					log.Warn(err)
					return err
				}

				// parsing response
				var response Response
				err = json.Unmarshal(body, &response)
				if err != nil {
					log.Warn(err)
					return err
				}

				pp.Println(response)
				if response.Status != "ok" {
					log.Infoln("params:", params)
					log.Warn("Something went wrong while indexing the media.")
				} else {
					log.Infoln("Submitted media >> ", i.IdImage)
					img := &Match{
						IdProduct:   i.IdProduct,
						IdImage:     i.IdImage,
						IdShop:      i.IdShop,
						Filepath:    params.Filepath,
						Fingerprint: fingerprint,
					}
					if err := db.Create(&img).Error; err != nil {
						log.Fatal(err)
					}
				}

				return nil

			}(image)
			t.Throttle()

		}

		if t.Err() != nil {

			for i, err := range t.Errs() {
				log.Printf("error #%d: %s", i, err)
			}
			log.Fatal(t.Err())
		}

	},
}
View Source
var RootCmd = &cobra.Command{
	Use:   "ps2match",
	Short: "ps2match is an helper to index prestashop images into image match.",
	Long:  `ps2match is an helper to index prestashop images into image match.`,
}

RootCmd is the root command for ovh-qa

View Source
var SearchCmd = &cobra.Command{
	Use:     "search",
	Aliases: []string{"s"},
	Short:   "Searches for a similar image in the database. Scores range from 0 to 100, with 100 being a perfect match.",
	Long:    "Searches for a similar image in the database. Scores range from 0 to 100, with 100 being a perfect match.",
	Run: func(cmd *cobra.Command, args []string) {

		if matchImageUrl != "" && matchImagePath != "" {
			log.Fatal("Error, you cannot use url and image argument at the same time.")
		}

		params := &Search{
			AllOrientations: matchAllOrientations,
		}

		if matchImageUrl != "" {
			params.Url = matchImageUrl
		}

		if matchImagePath != "" {
			params.Image = matchImagePath
		}

		pp.Println("params", params)

		httpClient := &http.Client{}

		searchBase := sling.New().Base(matchAddr).Client(httpClient)
		req, err := searchBase.New().Post("search").BodyForm(params).Request()
		if err != nil {
			log.Fatal(err)
		}

		resp, err := httpClient.Do(req)
		if err != nil {
			log.Fatal(err)
		}
		defer resp.Body.Close()
		body, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			log.Fatal(err)
		}

		// parsing response
		var response MatchSearch
		err = json.Unmarshal(body, &response)
		if err != nil {
			log.Fatal(err)
		}

		pp.Println(response)

	},
}

Functions

func Execute

func Execute()

Execute adds all child commands to the root command and sets flags appropriately.

Types

type Add

type Add struct {
	Url      string      `url:"url,omitempty"`
	Filepath string      `url:"filepath,omitempty"`
	Metadata interface{} `url:"metadata,omitempty"`
}

type Image

type Image struct {
	IdLang      uint   `json:"id_lang,omitempty"`
	IdShop      uint   `json:"id_shop,omitempty"`
	IdProduct   uint   `json:"id_product,omitempty"`
	IdImage     uint   `json:"id_image,omitempty"`
	LinkRewrite string `json:"link_rewrite,omitempty"`
	Legend      string `json:"legend,omitempty"`
}

type Match

type Match struct {
	gorm.Model
	IdProduct   uint
	IdImage     uint
	IdShop      uint
	Fingerprint string
	Filepath    string
	Mimetype    string
}

type MatchSearch

type MatchSearch struct {
	Error  []interface{}  `json:"error"`
	Method string         `json:"method"`
	Result []ResultSearch `json:"result"`
	Status string         `json:"status"`
}

type Response

type Response struct {
	Status string   `json:"status,omitempty"`
	Error  []string `json:"error,omitempty"`
	Method string   `json:"method,omitempty"`
	Result []string `json:"result,omitempty"`
}

type ResultSearch

type ResultSearch struct {
	Filepath string  `json:"filepath"`
	Score    float64 `json:"score"`
}
type Search struct {
	Url             string `url:"url,omitempty"`
	Image           string `url:"image,omitempty"`
	AllOrientations bool   `url:"all_orientations,omitempty"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL