code
This commit is contained in:
commit
fc6930c0f1
11
README.md
Normal file
11
README.md
Normal file
@ -0,0 +1,11 @@
|
||||
### 用Go实现的一套抓取固定站点漫画的命令行
|
||||
|
||||
编译方式:
|
||||
```shell
|
||||
go build -o anime.exe
|
||||
```
|
||||
|
||||
使用方式示例:
|
||||
```shell
|
||||
anime.exe -u /data/1616/88/121_01.jpg -m 25 -o D:\anime
|
||||
```
|
9
go.mod
Normal file
9
go.mod
Normal file
@ -0,0 +1,9 @@
|
||||
module anime_crawler
|
||||
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/spf13/cobra v1.6.1 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
)
|
11
go.sum
Normal file
11
go.sum
Normal file
@ -0,0 +1,11 @@
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
|
||||
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
38
src/cmd/lib.go
Normal file
38
src/cmd/lib.go
Normal file
@ -0,0 +1,38 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
url string
|
||||
max int
|
||||
output string
|
||||
host = "https://img4.qy0.ru"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.PersistentFlags().StringVarP(&url, "url", "u", "", "除域名外的链接")
|
||||
rootCmd.PersistentFlags().IntVarP(&max, "max", "m", 1, "图片最大值")
|
||||
rootCmd.PersistentFlags().StringVarP(&output, "output", "o", "anime", "设置漫画抓取结果的保存位置,默认为当前用户的主目录下的 anime 文件夹")
|
||||
}
|
||||
|
||||
func IfPathNotExistDoMkdir(path string) error {
|
||||
_, err := os.Stat(path)
|
||||
if err != nil || os.IsNotExist(err) {
|
||||
err := os.MkdirAll(path, 0777)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
15
src/cmd/root.go
Normal file
15
src/cmd/root.go
Normal file
@ -0,0 +1,15 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "anime",
|
||||
Short: "Anime crawler written by go",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
start()
|
||||
fmt.Println("抓取完成")
|
||||
},
|
||||
}
|
58
src/cmd/start.go
Normal file
58
src/cmd/start.go
Normal file
@ -0,0 +1,58 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
|
||||
func start() {
|
||||
err := IfPathNotExistDoMkdir(output)
|
||||
if err != nil {
|
||||
fmt.Println("输出目录创建失败")
|
||||
return
|
||||
}
|
||||
|
||||
for i := 1; i <= max; i++ {
|
||||
wg.Add(1)
|
||||
go get(i)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func get(num int) {
|
||||
defer wg.Done()
|
||||
fileName := strconv.Itoa(num) + ".jpg"
|
||||
imgSlice := strings.Split(url, "_")
|
||||
imgUrl := host + imgSlice[0] + "_" + fmt.Sprintf("%02d", num) + ".jpg"
|
||||
fmt.Println("获取图片:" + imgUrl)
|
||||
|
||||
resp, err := http.Get(imgUrl)
|
||||
if err != nil {
|
||||
fmt.Println(fileName, "图片获取失败:", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
reader := bufio.NewReaderSize(resp.Body, 32*1024)
|
||||
file, err := os.Create(output + "/" + fileName)
|
||||
if err != nil {
|
||||
fmt.Println(fileName, "图片创建失败:", err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = io.Copy(file, reader)
|
||||
if err != nil {
|
||||
fmt.Println(fileName, "图片输出失败:", err)
|
||||
return
|
||||
}
|
||||
}
|
7
src/main.go
Normal file
7
src/main.go
Normal file
@ -0,0 +1,7 @@
|
||||
package main
|
||||
|
||||
import "anime_crawler/src/cmd"
|
||||
|
||||
func main() {
|
||||
cmd.Execute()
|
||||
}
|
Loading…
Reference in New Issue
Block a user