Skip to content

Commit

Permalink
refactor: use one qojq syntax to get all extracted information
Browse files Browse the repository at this point in the history
  • Loading branch information
dfang committed Mar 4, 2022
1 parent edcb53a commit 5da5ebd
Showing 1 changed file with 23 additions and 37 deletions.
60 changes: 23 additions & 37 deletions extractors/xinpianchang/xinpianchang.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ func init() {
type extractor struct{}

type Video struct {
URL string `json:"url"`
Size int64 `json:"size"`
Ext string `json:"ext"`
Quality string `json:"quality"`
Title string `json:"title"`
Qualities []struct {
Quality string `json:"quality"`
Size int64 `json:"size"`
URL string `json:"url"`
Ext string `json:"ext"`
} `json:"qualities"`
}

// New returns a xinpianchang extractor.
Expand Down Expand Up @@ -53,47 +56,28 @@ func (e *extractor) Extract(url string, option extractors.Options) ([]*extractor
return nil, errors.WithStack(err)
}

streams := make(map[string]*extractors.Stream)

var m interface{}
err = json.Unmarshal([]byte(body), &m)
if err != nil {
return nil, errors.WithStack(err)
}

var title string
query1, err := gojq.Parse(".data.title")
query, err := gojq.Parse("{title: .data.title} + {qualities: [(.data.resource.progressive[] | {quality: .quality, size: .filesize, url: .url, ext: .mime})]}")
if err != nil {
return nil, errors.WithStack(err)
}
iter := query1.Run(m)
for {
v, ok := iter.Next()
if !ok {
break
}
if err, ok := v.(error); ok {
return nil, errors.WithStack(err)
}
title, _ = v.(string)
}
iter := query.Run(m)
video := Video{}

query2, err := gojq.Parse(".data.resource.progressive[] | {quality: .quality, size: .filesize, url: .url}")
if err != nil {
return nil, errors.WithStack(err)
}
iter2 := query2.Run(m)
for {
v, ok := iter2.Next()
v, ok := iter.Next()
if !ok {
break
}
if err, ok := v.(error); ok {
return nil, errors.WithStack(err)
}

video := Video{}

jsonbody, err := json.Marshal(v)
if err != nil {
return nil, errors.WithStack(err)
Expand All @@ -102,29 +86,31 @@ func (e *extractor) Extract(url string, option extractors.Options) ([]*extractor
if err := json.Unmarshal(jsonbody, &video); err != nil {
return nil, errors.WithStack(err)
}
video.Ext = "mp4"
}

stream := extractors.Stream{
Size: video.Size,
Quality: video.Quality,
streams := make(map[string]*extractors.Stream)
for _, quality := range video.Qualities {
streams[quality.Quality] = &extractors.Stream{
Size: quality.Size,
Quality: quality.Quality,
Parts: []*extractors.Part{
&extractors.Part{
URL: video.URL,
Size: video.Size,
Ext: video.Ext,
{
URL: quality.URL,
Size: quality.Size,
Ext: quality.Ext,
},
},
}
streams[video.Quality] = &stream
}

return []*extractors.Data{
{
Site: "新片场 xinpianchang.com",
Title: title,
Title: video.Title,
Type: extractors.DataTypeVideo,
Streams: streams,
URL: url,
},
}, nil

}

0 comments on commit 5da5ebd

Please sign in to comment.