Skip to content

Commit

Permalink
feat: unified the lanzou hosename and remove download client.
Browse files Browse the repository at this point in the history
  • Loading branch information
syhily committed Nov 15, 2022
1 parent 1967205 commit ad10221
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 132 deletions.
12 changes: 0 additions & 12 deletions internal/driver/aliyun/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ var (

type Aliyun struct {
client *client.Client
downloader *client.Client
tokenCache *TokenResp
refreshToken string
}
Expand All @@ -42,19 +41,8 @@ func New(c *client.Config, refreshToken string) (*Aliyun, error) {
// Set extra middleware for cleaning up the header.
cl.SetPreRequestHook(removeContentType)

c2, err := client.New(&client.Config{
HTTPS: true,
UserAgent: c.UserAgent,
Proxy: c.Proxy,
ConfigRoot: c.ConfigRoot,
})
if err != nil {
return nil, err
}

return &Aliyun{
client: cl,
downloader: c2,
refreshToken: refreshToken,
}, nil
}
Expand Down
11 changes: 9 additions & 2 deletions internal/driver/aliyun/share.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package aliyun

import "io"
import (
"io"

"github.com/bookstairs/bookhunter/internal/log"
)

// AnonymousShare will try to access the share without the user information.
func (ali *Aliyun) AnonymousShare(shareID string) (*ShareInfoResp, error) {
Expand Down Expand Up @@ -140,9 +144,12 @@ func (ali *Aliyun) DownloadURL(shareToken string, shareID string, fileID string)
}

func (ali *Aliyun) DownloadFile(downloadURL string) (io.ReadCloser, int64, error) {
resp, err := ali.downloader.R().
log.Debugf("Start to download file from aliyun drive: %s", downloadURL)

resp, err := ali.client.R().
SetDoNotParseResponse(true).
Get(downloadURL)
response := resp.RawResponse

return response.Body, response.ContentLength, err
}
49 changes: 49 additions & 0 deletions internal/driver/lanzou/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package lanzou

import (
"io"

"github.com/bookstairs/bookhunter/internal/client"
"github.com/bookstairs/bookhunter/internal/log"
)

type Drive struct {
client *client.Client
}

// hostname is used to return a valid lanzou host.
func hostname() string {
// We may add a roundrobin list in the future.
return "lanzoux.com"
}

func NewDrive(config *client.Config) (*Drive, error) {
cl, err := client.New(&client.Config{
HTTPS: true,
Host: hostname(),
UserAgent: config.UserAgent,
Proxy: config.Proxy,
ConfigRoot: config.ConfigRoot,
})

if err != nil {
return nil, err
}

cl.Client.
SetHeader("Accept-Language", "zh-CN,zh;q=0.9").
SetHeader("Referer", cl.BaseURL)

return &Drive{client: cl}, nil
}

func (l Drive) DownloadFile(downloadURL string) (io.ReadCloser, int64, error) {
log.Debugf("Start to download file from aliyun drive: %s", downloadURL)

resp, err := l.client.R().
SetDoNotParseResponse(true).
Get(downloadURL)
response := resp.RawResponse

return response.Body, response.ContentLength, err
}
59 changes: 59 additions & 0 deletions internal/driver/lanzou/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package lanzou

import "regexp"

var (
lxReg = regexp.MustCompile(`'lx':(\d+),`)
fidReg = regexp.MustCompile(`'fid':(\d+),`)
uidReg = regexp.MustCompile(`'uid':'(\d+)',`)
repReg = regexp.MustCompile(`'rep':'(\d+)',`)
upReg = regexp.MustCompile(`'up':(\d+),`)
lsReg = regexp.MustCompile(`'ls':(\d+),`)
tVar = regexp.MustCompile(`'t':(\S+),`)
kVar = regexp.MustCompile(`'k':(\S+),`)

dirURLRe = regexp.MustCompile(`(?m)https?://[a-zA-Z0-9-]*?\.?lanzou[a-z]\.com/(/s/)?b[a-zA-Z0-9]{7,}/?`)
fileURLRe = regexp.MustCompile(`(?m)https?://[a-zA-Z0-9-]*?\.?lanzou[a-z]\.com/(/s/)?i[a-zA-Z0-9]{7,}/?`)

find1Re = regexp.MustCompile(`(?m)url\s:\s+'(.*?)',\n\t+data\s:\s+'(.*?)'\+pwd,`)
find2Re = regexp.MustCompile(`(?m)<iframe.*?src="(/fn\?\w{10,})"\s.*>`)
find2TitleRe = regexp.MustCompile(`(?m)<title>(.*?)\s-\s蓝奏云</title>`)

htmlNoteRe = regexp.MustCompile(`(?m)<!--.+?-->|\s+//\s*.+`)
jsNoteRe = regexp.MustCompile(`(?m)(.+?[,;])\s*//.+`)
)

type (
Response struct {
Code int64 `json:"code"`
Data ResponseData `json:"data"`
Msg string `json:"msg"`
}

ResponseData struct {
Name string `json:"name"`
URL string `json:"url"`
}

Dom struct {
Zt int `json:"zt"`
Dom string `json:"dom"`
URL string `json:"url"`
Inf interface{} `json:"inf"`
}

FileList struct {
Zt int `json:"zt"`
Info string `json:"info"`
Text []struct {
Icon string `json:"icon"`
T int `json:"t"`
ID string `json:"id"`
NameAll string `json:"name_all"`
Size string `json:"size"`
Time string `json:"time"`
Duan string `json:"duan"`
PIco int `json:"p_ico"`
} `json:"text"`
}
)

0 comments on commit ad10221

Please sign in to comment.