169 lines
3 KiB
Go
169 lines
3 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
var version = "v1.0.0"
|
|
|
|
/*
|
|
TODO
|
|
|
|
- platform
|
|
- login
|
|
- logout
|
|
- pull
|
|
- download
|
|
|
|
*/
|
|
|
|
func main() {
|
|
flags := []cli.Flag{
|
|
&cli.StringFlag{
|
|
Name: "db",
|
|
Usage: "database file path",
|
|
Value: "gadl.db",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "platform",
|
|
Usage: "platform type",
|
|
Value: "google-play-books",
|
|
},
|
|
}
|
|
|
|
app := &cli.App{
|
|
Name: "gadl",
|
|
Usage: "Manga Downloader",
|
|
Version: version,
|
|
Flags: flags,
|
|
Commands: cli.Commands{
|
|
&cli.Command{
|
|
Name: "install",
|
|
Usage: "install system dependencies for browser",
|
|
Flags: flags,
|
|
Action: func(c *cli.Context) error {
|
|
return installBrowser()
|
|
},
|
|
},
|
|
&cli.Command{
|
|
Name: "login",
|
|
Flags: flags,
|
|
Action: func(c *cli.Context) error {
|
|
db := newDatabase(c.String("db"))
|
|
browser := runBrowser(runOptions{db: db, headless: false})
|
|
platform := c.String("platform")
|
|
_, err := browser.loadBrowserContext(platform)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
return browser.stop()
|
|
},
|
|
},
|
|
&cli.Command{
|
|
Name: "add",
|
|
Flags: flags,
|
|
Action: func(c *cli.Context) error {
|
|
db := newDatabase(c.String("db"))
|
|
lib := newLibrary(newLibraryOptions{db})
|
|
|
|
for _, url := range c.Args().Slice() {
|
|
b := book{
|
|
// TODO: url to platform
|
|
Platform: c.String("platform"),
|
|
ReaderURL: url,
|
|
}
|
|
|
|
if err := lib.add(b); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
},
|
|
},
|
|
&cli.Command{
|
|
Name: "delete",
|
|
Flags: flags,
|
|
Action: func(c *cli.Context) error {
|
|
db := newDatabase(c.String("db"))
|
|
lib := newLibrary(newLibraryOptions{db})
|
|
|
|
for _, id := range c.Args().Slice() {
|
|
if err := lib.delete(id); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
},
|
|
},
|
|
&cli.Command{
|
|
Name: "list",
|
|
Flags: flags,
|
|
Action: func(c *cli.Context) error {
|
|
db := newDatabase(c.String("db"))
|
|
lib := newLibrary(newLibraryOptions{db})
|
|
|
|
bs, err := lib.getBooks()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
for _, b := range *bs {
|
|
json, err := json.Marshal(&b)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
fmt.Println(string(json))
|
|
}
|
|
|
|
return nil
|
|
},
|
|
},
|
|
&cli.Command{
|
|
Name: "view",
|
|
Flags: flags,
|
|
Action: func(c *cli.Context) error {
|
|
db := newDatabase(c.String("db"))
|
|
lib := newLibrary(newLibraryOptions{db})
|
|
|
|
for _, id := range c.Args().Slice() {
|
|
b, err := lib.get(id)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
json, err := json.Marshal(&b)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
fmt.Println(string(json))
|
|
}
|
|
|
|
return nil
|
|
},
|
|
},
|
|
&cli.Command{
|
|
Name: "download",
|
|
Flags: flags,
|
|
Action: func(c *cli.Context) error {
|
|
newDatabase(c.String("db"))
|
|
|
|
return nil
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|