Skip to content

Commit

Permalink
feat: print better error message when error (iawia002#1037)
Browse files Browse the repository at this point in the history
* feat: print better error message when error

* update

* εˆ†η»„ import

* ι‡ζ–°εˆ†η»„ import

* update
  • Loading branch information
axetroy authored Feb 25, 2022
1 parent 1394471 commit 0c6b593
Show file tree
Hide file tree
Showing 43 changed files with 312 additions and 254 deletions.
5 changes: 3 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,10 @@ func New() *cli.App {
if err := download(c, videoURL); err != nil {
fmt.Fprintf(
color.Output,
"Downloading %s error:\n%s\n",
color.CyanString("%s", videoURL), color.RedString("%v", err),
"Downloading %s error:\n",
color.CyanString("%s", videoURL),
)
fmt.Printf("%+v\n", err)
isErr = true
}
}
Expand Down
21 changes: 11 additions & 10 deletions downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"time"

"github.com/cheggaaa/pb/v3"
"github.com/pkg/errors"

"github.com/iawia002/lux/extractors"
"github.com/iawia002/lux/request"
Expand Down Expand Up @@ -113,7 +114,7 @@ func (downloader *Downloader) writeFile(url string, file *os.File, headers map[s
// So don't worry about memory.
written, copyErr := io.Copy(barWriter, res.Body)
if copyErr != nil && copyErr != io.EOF {
return written, fmt.Errorf("file copy error: %s", copyErr)
return written, errors.Errorf("file copy error: %s", copyErr)
}
return written, nil
}
Expand Down Expand Up @@ -415,20 +416,20 @@ func readDirAllFilePart(filePath, filename, extname string) ([]*FilePartMeta, er
dirPath := filepath.Dir(filePath)
dir, err := os.Open(dirPath)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
defer dir.Close() // nolint
fns, err := dir.Readdir(0)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
var metas []*FilePartMeta
reg := regexp.MustCompile(fmt.Sprintf("%s.%s.part.+", regexp.QuoteMeta(filename), extname))
for _, fn := range fns {
if reg.MatchString(fn.Name()) {
meta, err := parseFilePartMeta(path.Join(dirPath, fn.Name()), fn.Size())
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
metas = append(metas, meta)
}
Expand All @@ -444,20 +445,20 @@ func parseFilePartMeta(filepath string, fileSize int64) (*FilePartMeta, error) {
size := binary.Size(*meta)
file, err := os.OpenFile(filepath, os.O_RDWR, 0666)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
defer file.Close() // nolint
var buf [512]byte
readSize, err := file.ReadAt(buf[0:size], 0)
if err != nil && err != io.EOF {
return nil, err
return nil, errors.WithStack(err)
}
if readSize < size {
return nil, fmt.Errorf("the file has been broked, please delete all part files and re-download")
return nil, errors.Errorf("the file has been broked, please delete all part files and re-download")
}
err = binary.Read(bytes.NewBuffer(buf[:size]), binary.LittleEndian, meta)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
savedSize := fileSize - int64(binary.Size(meta))
meta.Cur = meta.Start + savedSize
Expand Down Expand Up @@ -545,7 +546,7 @@ func (downloader *Downloader) aria2(title string, stream *extractors.Stream) err
// Download download urls
func (downloader *Downloader) Download(data *extractors.Data) error {
if len(data.Streams) == 0 {
return fmt.Errorf("no streams in title %s", data.Title)
return errors.Errorf("no streams in title %s", data.Title)
}

sortedStreams := genSortedStreams(data.Streams)
Expand All @@ -566,7 +567,7 @@ func (downloader *Downloader) Download(data *extractors.Data) error {
}
stream, ok := data.Streams[streamName]
if !ok {
return fmt.Errorf("no stream named %s", streamName)
return errors.Errorf("no stream named %s", streamName)
}

if !downloader.option.Silent {
Expand Down
13 changes: 7 additions & 6 deletions extractors/acfun/acfun.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"regexp"

jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"

"github.com/iawia002/lux/extractors"
"github.com/iawia002/lux/parser"
Expand Down Expand Up @@ -37,15 +38,15 @@ func New() extractors.Extractor {
func (e *extractor) Extract(URL string, option extractors.Options) ([]*extractors.Data, error) {
html, err := request.GetByte(URL, referer, nil)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

epDatas := make([]*episodeData, 0)

if option.Playlist {
list, err := resolvingEpisodes(html)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
items := utils.NeedDownloadList(option.Items, option.ItemStart, option.ItemEnd, len(list.Episodes))

Expand All @@ -55,7 +56,7 @@ func (e *extractor) Extract(URL string, option extractors.Options) ([]*extractor
} else {
bgData, _, err := resolvingData(html)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
epDatas = append(epDatas, &bgData.episodeData)
}
Expand Down Expand Up @@ -151,12 +152,12 @@ func resolvingData(html []byte) (*bangumiData, *videoInfo, error) {
groups := pattern.FindSubmatch(html)
err := jsoniter.Unmarshal(groups[1], bgData)
if err != nil {
return nil, nil, err
return nil, nil, errors.WithStack(err)
}

err = jsoniter.UnmarshalFromString(bgData.CurrentVideoInfo.KsPlayJSON, vInfo)
if err != nil {
return nil, nil, err
return nil, nil, errors.WithStack(err)
}
return bgData, vInfo, nil
}
Expand All @@ -168,7 +169,7 @@ func resolvingEpisodes(html []byte) (*episodeList, error) {
groups := pattern.FindSubmatch(html)
err := jsoniter.Unmarshal(groups[1], list)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
return list, nil
}
15 changes: 8 additions & 7 deletions extractors/bcy/bcy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package bcy

import (
"encoding/json"
"fmt"
"strings"

"github.com/pkg/errors"

"github.com/iawia002/lux/extractors"
"github.com/iawia002/lux/parser"
"github.com/iawia002/lux/request"
Expand Down Expand Up @@ -36,25 +37,25 @@ func New() extractors.Extractor {
func (e *extractor) Extract(url string, option extractors.Options) ([]*extractors.Data, error) {
html, err := request.Get(url, url, nil)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

// parse json data
rep := strings.NewReplacer(`\"`, `"`, `\\`, `\`)
realURLs := utils.MatchOneOf(html, `JSON.parse\("(.+?)"\);`)
if realURLs == nil || len(realURLs) < 2 {
return nil, extractors.ErrURLParseFailed
return nil, errors.WithStack(extractors.ErrURLParseFailed)
}
jsonString := rep.Replace(realURLs[1])

var data bcyData
if err = json.Unmarshal([]byte(jsonString), &data); err != nil {
return nil, fmt.Errorf("json unmarshal failed, err: %v", err)
return nil, errors.WithStack(err)
}

doc, err := parser.GetDoc(html)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
title := strings.Replace(parser.Title(doc), " - εŠζ¬‘ε…ƒ banciyuan - ACG爱ε₯½θ€…η€ΎεŒΊ", "", -1)

Expand All @@ -63,12 +64,12 @@ func (e *extractor) Extract(url string, option extractors.Options) ([]*extractor
for _, img := range data.Detail.PostData.Multi {
size, err := request.Size(img.OriginalPath, url)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
totalSize += size
_, ext, err := utils.GetNameAndExt(img.OriginalPath)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
parts = append(parts, &extractors.Part{
URL: img.OriginalPath,
Expand Down
17 changes: 9 additions & 8 deletions extractors/bilibili/bilibili.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package bilibili

import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"time"

"github.com/pkg/errors"

"github.com/iawia002/lux/extractors"
"github.com/iawia002/lux/parser"
"github.com/iawia002/lux/request"
Expand Down Expand Up @@ -49,7 +50,7 @@ func genAPI(aid, cid, quality int, bvid string, bangumi bool, cookie string) (st
return "", err
}
if t.Code != 0 {
return "", fmt.Errorf("cookie error: %s", t.Message)
return "", errors.Errorf("cookie error: %s", t.Message)
}
utoken = t.Data.Token
}
Expand Down Expand Up @@ -94,7 +95,7 @@ func extractBangumi(url, html string, extractOption extractors.Options) ([]*extr
var data bangumiData
err := json.Unmarshal([]byte(dataString), &data)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
if !extractOption.Playlist {
aid := data.EpInfo.Aid
Expand Down Expand Up @@ -158,15 +159,15 @@ func getMultiPageData(html string) (*multiPage, error) {
}
err := json.Unmarshal([]byte(multiPageDataString[1]), &data)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
return &data, nil
}

func extractNormalVideo(url, html string, extractOption extractors.Options) ([]*extractors.Data, error) {
pageData, err := getMultiPageData(html)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
if !extractOption.Playlist {
// handle URL that has a playlist, mainly for unified titles
Expand All @@ -183,7 +184,7 @@ func extractNormalVideo(url, html string, extractOption extractors.Options) ([]*
}

if len(pageData.VideoData.Pages) < p || p < 1 {
return nil, extractors.ErrURLParseFailed
return nil, errors.WithStack(extractors.ErrURLParseFailed)
}

page := pageData.VideoData.Pages[p-1]
Expand Down Expand Up @@ -246,7 +247,7 @@ func (e *extractor) Extract(url string, option extractors.Options) ([]*extractor
var err error
html, err := request.Get(url, referer, nil)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

// set thread number to 1 manually to avoid http 412 error
Expand Down Expand Up @@ -421,7 +422,7 @@ func subtitleTransform(body []byte) ([]byte, error) {
captionData := bilibiliSubtitleFormat{}
err := json.Unmarshal(body, &captionData)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

for i := 0; i < len(captionData.Body); i++ {
Expand Down
19 changes: 10 additions & 9 deletions extractors/douyin/douyin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package douyin

import (
"encoding/json"
"errors"
"net/http"
"strings"

"github.com/pkg/errors"

"github.com/iawia002/lux/extractors"
"github.com/iawia002/lux/request"
"github.com/iawia002/lux/utils"
Expand All @@ -29,7 +30,7 @@ func (e *extractor) Extract(url string, option extractors.Options) ([]*extractor
if strings.Contains(url, "v.douyin.com") {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
c := http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
Expand All @@ -38,7 +39,7 @@ func (e *extractor) Extract(url string, option extractors.Options) ([]*extractor
}
resp, err := c.Do(req)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
url = resp.Header.Get("location")
}
Expand All @@ -48,16 +49,16 @@ func (e *extractor) Extract(url string, option extractors.Options) ([]*extractor
return nil, errors.New("unable to get video ID")
}
if itemIds == nil || len(itemIds) < 2 {
return nil, extractors.ErrURLParseFailed
return nil, errors.WithStack(extractors.ErrURLParseFailed)
}
itemId := itemIds[len(itemIds)-1]
jsonData, err := request.Get("https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids="+itemId, url, nil)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
var douyin douyinData
if err = json.Unmarshal([]byte(jsonData), &douyin); err != nil {
return nil, err
return nil, errors.WithStack(err)
}

urlData := make([]*extractors.Part, 0)
Expand All @@ -70,12 +71,12 @@ func (e *extractor) Extract(url string, option extractors.Options) ([]*extractor
realURL := img.URLList[len(img.URLList)-1]
size, err := request.Size(realURL, url)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
totalSize += size
_, ext, err := utils.GetNameAndExt(realURL)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
urlData = append(urlData, &extractors.Part{
URL: realURL,
Expand All @@ -88,7 +89,7 @@ func (e *extractor) Extract(url string, option extractors.Options) ([]*extractor
realURL := "https://aweme.snssdk.com/aweme/v1/play/?video_id=" + douyin.ItemList[0].Video.PlayAddr.URI + "&ratio=720p&line=0"
totalSize, err = request.Size(realURL, url)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}
urlData = append(urlData, &extractors.Part{
URL: realURL,
Expand Down
Loading

0 comments on commit 0c6b593

Please sign in to comment.