-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update readme * update readme * add xinpianchang extractor * update readme and github action * refactor: use one qojq syntax to get all extracted information * update
- Loading branch information
Showing
5 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: xinpianchang | ||
|
||
on: | ||
push: | ||
paths: | ||
- "extractors/xinpianchang/*.go" | ||
- ".github/workflows/stream_xinpianchang.yml" | ||
pull_request: | ||
paths: | ||
- "extractors/xinpianchang/*.go" | ||
- ".github/workflows/stream_xinpianchang.yml" | ||
schedule: | ||
# run ci weekly | ||
- cron: "0 0 * * 0" | ||
|
||
jobs: | ||
test: | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
matrix: | ||
go: ["1.16"] | ||
os: [ubuntu-latest] | ||
name: ${{ matrix.os }} | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-go@v2 | ||
with: | ||
go-version: ${{ matrix.go }} | ||
|
||
- name: Test | ||
run: go test -timeout 5m -race -coverpkg=./... -coverprofile=coverage.txt github.com/iawia002/lux/extractors/xinpianchang |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package xinpianchang | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/itchyny/gojq" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/iawia002/lux/extractors" | ||
"github.com/iawia002/lux/request" | ||
) | ||
|
||
func init() { | ||
extractors.Register("xinpianchang", New()) | ||
} | ||
|
||
type extractor struct{} | ||
|
||
type Video struct { | ||
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. | ||
func New() extractors.Extractor { | ||
return &extractor{} | ||
} | ||
|
||
// Extract is the main function to extract the data. | ||
func (e *extractor) Extract(url string, option extractors.Options) ([]*extractors.Data, error) { | ||
headers := map[string]string{ | ||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:98.0) Gecko/20100101 Firefox/98.0", | ||
} | ||
|
||
html, err := request.Get(url, url, headers) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
r1 := regexp.MustCompile(`vid = \"(.+?)\";`) | ||
r2 := regexp.MustCompile(`modeServerAppKey = \"(.+?)\";`) | ||
|
||
vid := r1.FindSubmatch([]byte(html))[1] | ||
appKey := r2.FindSubmatch([]byte(html))[1] | ||
|
||
video_url := fmt.Sprintf("https://mod-api.xinpianchang.com/mod/api/v2/media/%s?appKey=%s", string(vid), string(appKey)) | ||
body, err := request.Get(video_url, url, headers) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
var m interface{} | ||
err = json.Unmarshal([]byte(body), &m) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
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 := query.Run(m) | ||
video := Video{} | ||
|
||
for { | ||
v, ok := iter.Next() | ||
if !ok { | ||
break | ||
} | ||
if err, ok := v.(error); ok { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
jsonbody, err := json.Marshal(v) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
if err := json.Unmarshal(jsonbody, &video); err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
} | ||
|
||
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{ | ||
{ | ||
URL: quality.URL, | ||
Size: quality.Size, | ||
Ext: strings.Split(quality.Ext, "/")[1], | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
return []*extractors.Data{ | ||
{ | ||
Site: "新片场 xinpianchang.com", | ||
Title: video.Title, | ||
Type: extractors.DataTypeVideo, | ||
Streams: streams, | ||
URL: url, | ||
}, | ||
}, nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package xinpianchang | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/iawia002/lux/extractors" | ||
"github.com/iawia002/lux/test" | ||
) | ||
|
||
func TestDownload(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args test.Args | ||
}{ | ||
{ | ||
name: "test 1", | ||
args: test.Args{ | ||
URL: "https://www.xinpianchang.com/a10880684?from=ArticlePageSimilar", | ||
Title: "超炫酷视觉系创意短片《遗留》", | ||
Quality: "720p", | ||
Size: 79595290, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
data, err := New().Extract(tt.args.URL, extractors.Options{}) | ||
test.CheckError(t, err) | ||
test.Check(t, tt.args, data[0]) | ||
}) | ||
} | ||
} |