cmd

package
v0.0.0-...-d0d9a17 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const NotFoundSource = "not found source %v"

Variables

View Source
var AddCmd = &cobra.Command{

	Use:              "add [LINK]",
	Short:            "Add manga in local storage",
	Args:             cobra.MinimumNArgs(1),
	TraverseChildren: true,

	RunE: func(cmd *cobra.Command, args []string) error {
		sourcesData := Sources()
		db, err := storage.DatabseFactory()

		if err != nil {
			return err
		}

		selectedSource, err := cmd.Flags().GetString("source")
		if err != nil {
			return err
		}
		execSource, ok := sourcesData[selectedSource]
		if !ok {
			return fmt.Errorf(NotFoundSource, selectedSource)

		}
		res, err := execSource.Info(args[0])
		if err != nil {
			return err
		}

		if err := db.Add(*res); err != nil {
			return err
		}

		log.Println(fmt.Sprintf("Manga %v has been added in local storage", res.Title))

		return nil
	},
}
View Source
var ChapterCmd = &cobra.Command{
	Use:              "chapter [LINK]",
	Short:            "Get chapters from manga link",
	Args:             cobra.MinimumNArgs(1),
	TraverseChildren: true,
	RunE: func(cmd *cobra.Command, args []string) error {
		var res []bushido.Chapter
		var intId int64
		var link string
		var sourceStr string

		sourcesData := Sources()
		db, err := storage.DatabseFactory()
		if err != nil {
			return err
		}

		if len(args) >= 1 {
			intId, err = strconv.ParseInt(args[0], 10, 64)
			if err != nil {
				link = args[0]
				sourceStr, err = cmd.Flags().GetString("source")
				if err != nil {
					return err
				}
			} else {
				info, err := db.FindById(int(intId))
				if err != nil {
					return err
				}
				link = info.Link
				sourceStr = info.Source.ID
			}
		}

		recursiveSearch, err := cmd.Flags().GetBool("recursive")
		if err != nil {
			return err
		}
		localOnly, err := cmd.Flags().GetBool("local-only")
		if err != nil {
			return err
		}

		execSource, ok := sourcesData[sourceStr]
		if !ok {
			return fmt.Errorf(NotFoundSource, selectedSource)

		}

		if localOnly && intId != 0 {
			res, err = db.ListChaptersByContentId(int(intId))
			if err != nil {
				return err
			}
		} else {
			res, err = execSource.Chapters(link, recursiveSearch)
			if err != nil {
				return err
			}
		}

		var rows [][]string
		for _, content := range res {
			rows = append(rows, []string{strconv.Itoa(content.ID), content.Title, content.ExternalId, content.Link, content.Content.ExternalId})
		}

		table := RenderTable([]string{"ID", "Title", "id", "link", "contend_id"}, rows)
		table.Render()
		return nil
	},
}
View Source
var InfoCmd = &cobra.Command{
	Use:              "info [LINK]",
	Short:            "Get mangainfo fromb local or source",
	Args:             cobra.MinimumNArgs(1),
	TraverseChildren: true,

	RunE: func(cmd *cobra.Command, args []string) error {
		sourcesData := Sources()
		selectedSource, err := cmd.Flags().GetString("source")
		if err != nil {
			return err
		}
		execSource, ok := sourcesData[selectedSource]
		if !ok {
			return fmt.Errorf(NotFoundSource, selectedSource)

		}

		db, err := storage.DatabseFactory()
		if err != nil {
			return err
		}

		res, err := db.FindByLink(args[0])
		if err != nil {
			if err.Error() != "content not found" {
				return err
			}
			res, err = execSource.Info(args[0])
			if err != nil {
				return err
			}
		}

		table := RenderTable([]string{"ID", "Title", "Author", "Link", "source"}, [][]string{
			{strconv.Itoa(res.ID), res.Title, res.Author, res.Link, res.Source.ID},
		})

		table.Render()

		return nil
	},
}
View Source
var ListCmd = &cobra.Command{
	Use:              "list [LINK]",
	Short:            "List mangas storage in local",
	Args:             cobra.MinimumNArgs(0),
	TraverseChildren: true,

	RunE: func(cmd *cobra.Command, args []string) error {

		var searchText string
		var res []bushido.Content
		var intId int64

		db, err := storage.DatabseFactory()
		if err != nil {
			return err
		}

		if len(args) >= 1 {
			intId, err = strconv.ParseInt(args[0], 10, 64)
			fmt.Println(intId)
			if err != nil {
				searchText = args[0]
			}
		}

		if intId != 0 {
			data, err := db.FindById(int(intId))
			res = append(res, *data)
			if err != nil {
				return err
			}

		} else {
			res, err = db.ListByName(searchText)
			if err != nil {
				return err
			}
		}

		var rows [][]string
		for _, content := range res {
			rows = append(rows, []string{strconv.Itoa(content.ID), content.Title, content.Author, content.Link, content.Source.ID})
		}

		table := RenderTable([]string{"ID", "Title", "Author", "Link", "source"}, rows)

		table.Render()

		return nil
	},
}
View Source
var LocalCmd = &cobra.Command{
	Use:              "local [COMAND]",
	Short:            "Local is a collection of commands with depend of local storage",
	Long:             "Local is a collection of commands with we use to manager and consume local data",
	TraverseChildren: true,
	Run: func(cmd *cobra.Command, args []string) {

	},
}
View Source
var PageCmd = &cobra.Command{
	Use:              "page [CONTENT_ID] [CHAPTER_ID]",
	Short:            "Get pages from manga",
	Args:             cobra.MinimumNArgs(2),
	TraverseChildren: true,

	RunE: func(cmd *cobra.Command, args []string) error {
		var execSource bushido.Client

		db, err := storage.DatabseFactory()
		if err != nil {
			return err
		}

		contentId, err := strconv.ParseInt(args[0], 10, 64)
		if err != nil {
			return errors.New("first arg is not a valid content id")
		}
		chapterId, err := strconv.ParseInt(args[1], 10, 64)
		if err != nil {
			return errors.New("second arg is not a valid chapterId id")
		}
		content, err := db.FindById(int(contentId))
		if err != nil {
			return err
		}

		chapter, err := db.FindChapterById(int(chapterId))
		if err != nil {
			return err
		}

		if chapter.Content.ID != content.ID {
			return errors.New("chapter not owner by content")
		} else {
			chapter.Content = content
		}

		sourcesData := Sources()
		execSource, ok := sourcesData[content.Source.ID]
		if !ok {
			return fmt.Errorf(NotFoundSource, content.Source)
		}

		res, err := execSource.Pages(content.ExternalId, chapter.ExternalId)
		if err != nil {
			return err
		}

		persistLocal, err := cmd.Flags().GetBool("dowload")
		if err != nil {
			return err
		}

		if persistLocal {
			err = db.AppendPages(*chapter, res)
			if err != nil {
				return err
			}
		}

		var rows [][]string
		for k, content := range res {
			rows = append(rows, []string{fmt.Sprintf("%d", k), string(content)})
		}
		table := RenderTable([]string{"Number", "Link"}, rows)

		table.Render()

		return nil
	},
}
View Source
var RemoteCmd = &cobra.Command{
	Use:              "remote [COMAND]",
	Short:            "Remote is way to use sources",
	Long:             "Remote is a collection of source related commands such as search in specific manga source or get info using link as origin",
	TraverseChildren: true,
	Run: func(cmd *cobra.Command, args []string) {

	},
}
View Source
var RootCmd = &cobra.Command{
	Use:              "bushido [COMAND]",
	Short:            "Bushido is a manga sourece manageer",
	Long:             `Bushido is a manga source manager, a simple way to manager, read , sync and read mangas from diferent sourcers`,
	TraverseChildren: true,
	Run: func(cmd *cobra.Command, args []string) {

	},
}
View Source
var SearchCmd = &cobra.Command{
	Use:              "search [QUERY]",
	Short:            "Search from manga in remote",
	Args:             cobra.MinimumNArgs(1),
	TraverseChildren: true,
	RunE: func(cmd *cobra.Command, args []string) error {
		sourcesData := Sources()
		selectedSource, err := cmd.Flags().GetString("source")
		if err != nil {
			return err
		}

		var contents []bushido.Content
		if selectedSource != "" {
			execSource, ok := sourcesData[selectedSource]
			if !ok {
				return fmt.Errorf(NotFoundSource, selectedSource)
			}
			res, err := execSource.Search(args[0])
			if err != nil {
				return err
			}
			contents = append(contents, res...)
		} else {
			resultch := make(chan []bushido.Content)
			var wg sync.WaitGroup
			wg.Add(len(sourcesData))

			for _, v := range sourcesData {
				go asyncSearch(&wg, v, args[0], resultch)
			}

			go func() {
				wg.Wait()
				close(resultch)
			}()

			for res := range resultch {
				contents = append(contents, res...)
			}
		}

		var rows [][]string
		for _, content := range contents {
			rows = append(rows, []string{content.Title, content.Author, content.Link, content.Source.ID})
		}

		table := RenderTable([]string{"Title", "Author", "Link", "source"}, rows)
		table.Render()
		return nil

	},
}
View Source
var ServerCmd = &cobra.Command{
	Use:              "server",
	Short:            "Start server for self-hosted platform",
	Args:             cobra.MinimumNArgs(0),
	TraverseChildren: true,

	RunE: func(cmd *cobra.Command, args []string) error {
		httpServer := web.NewWebServer(3000)
		log.Printf("Server start runing in %s", httpServer.Addr)
		return httpServer.ListenAndServe()

	},
}
View Source
var SourceCmd = &cobra.Command{
	Use:              "source",
	Short:            "list active sources",
	Args:             cobra.MinimumNArgs(0),
	TraverseChildren: true,

	RunE: func(cmd *cobra.Command, args []string) error {
		sourcesData := Sources()
		sources := make([]string, 0, len(sourcesData))
		for k := range sourcesData {
			sources = append(sources, k)
		}

		var rows [][]string
		for k, content := range sources {
			rows = append(rows, []string{fmt.Sprintf("%d", k), content})
		}
		table := RenderTable([]string{"Number", "Source"}, rows)

		table.Render()

		return nil
	},
}
View Source
var SyncCmd = &cobra.Command{
	Use:              "sync [ID]",
	Short:            "Sync manga chapters from remote to local storage",
	Args:             cobra.MinimumNArgs(0),
	TraverseChildren: true,

	RunE: func(cmd *cobra.Command, args []string) error {

		var intId int64
		var contents []bushido.Content

		recursiveSearch, err := cmd.Flags().GetBool("recursive")
		if err != nil {
			return err
		}

		syncAll, err := cmd.Flags().GetBool("sync-all")
		if err != nil {
			return err
		}

		db, err := storage.DatabseFactory()
		if err != nil {
			return err
		}

		if len(args) >= 1 || !syncAll {
			intId, err = strconv.ParseInt(args[0], 10, 64)
			if err != nil {
				return err
			}

			info, err := db.FindById(int(intId))
			if err != nil {
				return err
			}

			contents = append(contents, *info)
		} else {
			contents, err = db.ListByName("")
			if err != nil {
				return err
			}
		}

		log.Println("contents to sync ", len(contents))

		sourcesData := Sources()

		for _, c := range contents {
			execSource, ok := sourcesData[c.Source.ID]
			if !ok {
				return fmt.Errorf(NotFoundSource, selectedSource)
			}

			chapters, err := execSource.Chapters(c.Link, recursiveSearch)
			log.Println(fmt.Printf("%v chapters to sync for %v from %v", len(chapters), c.Title, c.Source))
			if err != nil {
				return err
			}

			if err := db.AppendChapter(c, chapters); err != nil {
				return err
			}
		}

		return nil
	},
}

Functions

func RenderTable

func RenderTable(header []string, rows [][]string) *tablewriter.Table

func Sources

func Sources() map[string]bushido.Client

Types

type SourceItem

type SourceItem interface {
	New() *bushido.Source
}

Jump to

Keyboard shortcuts

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