-
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.
Merge pull request #444 from weiyongsheng/master
extractors/geekbang: Add basic support
- Loading branch information
Showing
4 changed files
with
155 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
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,118 @@ | ||
package geekbang | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"io/ioutil" | ||
"strings" | ||
|
||
"github.com/iawia002/annie/downloader" | ||
"github.com/iawia002/annie/request" | ||
"github.com/iawia002/annie/utils" | ||
) | ||
|
||
type geekData struct { | ||
Code int `json:"code"` | ||
Error struct { | ||
Code int `json:"code"` | ||
Msg string `json:"msg"` | ||
} `json:"error"` | ||
Data struct { | ||
Title string `json:"article_sharetitle"` | ||
VideoMediaMap map[string]struct { | ||
URL string `json:"url"` | ||
Size int64 `json:"size"` | ||
} `json:"video_media_map"` | ||
} `json:"data"` | ||
} | ||
|
||
type geekURLInfo struct { | ||
URL string | ||
Size int64 | ||
} | ||
|
||
func geekM3u8(url string) ([]geekURLInfo, error) { | ||
var ( | ||
data []geekURLInfo | ||
temp geekURLInfo | ||
size int64 | ||
err error | ||
) | ||
urls, err := utils.M3u8URLs(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, u := range urls { | ||
temp = geekURLInfo{ | ||
URL: u, | ||
Size: size, | ||
} | ||
data = append(data, temp) | ||
} | ||
return data, nil | ||
} | ||
|
||
// Extract is the main function for extracting data | ||
func Extract(url string) ([]downloader.Data, error) { | ||
var err error | ||
matches := utils.MatchOneOf(url, `https?://time.geekbang.org/course/detail/(\d+)-(\d+)`) | ||
if matches == nil { | ||
return downloader.EmptyList, errors.New("地址有误") | ||
} | ||
|
||
heanders := map[string]string{"Origin": "https://time.geekbang.org", "Content-Type": "application/json", "Referer": url} | ||
params := strings.NewReader("{\"id\":" + string(matches[2]+"}")) | ||
res, err := request.Request("POST", "https://time.geekbang.org/serv/v1/article", params, heanders) | ||
if err != nil { | ||
return downloader.EmptyList, err | ||
} | ||
|
||
body, err := ioutil.ReadAll(res.Body) | ||
defer res.Body.Close() | ||
if err != nil { | ||
return downloader.EmptyList, err | ||
} | ||
|
||
var geekData geekData | ||
err = json.Unmarshal(body, &geekData) | ||
|
||
if geekData.Code < 0 { | ||
return downloader.EmptyList, errors.New(geekData.Error.Msg) | ||
} | ||
|
||
title := geekData.Data.Title | ||
|
||
streams := make(map[string]downloader.Stream, len(geekData.Data.VideoMediaMap)) | ||
|
||
for key, media := range geekData.Data.VideoMediaMap { | ||
m3u8URLs, err := geekM3u8(media.URL) | ||
|
||
if err != nil { | ||
return downloader.EmptyList, err | ||
} | ||
|
||
urls := make([]downloader.URL, len(m3u8URLs)) | ||
for index, u := range m3u8URLs { | ||
urls[index] = downloader.URL{ | ||
URL: u.URL, | ||
Size: u.Size, | ||
Ext: "ts", | ||
} | ||
} | ||
|
||
streams[key] = downloader.Stream{ | ||
URLs: urls, | ||
Size: media.Size, | ||
} | ||
} | ||
|
||
return []downloader.Data{ | ||
{ | ||
Site: "极客时间 geekbang.org", | ||
Title: title, | ||
Type: "video", | ||
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,33 @@ | ||
package geekbang | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/iawia002/annie/config" | ||
"github.com/iawia002/annie/test" | ||
) | ||
|
||
func TestDownload(t *testing.T) { | ||
config.InfoOnly = true | ||
config.RetryTimes = 100 | ||
tests := []struct { | ||
name string | ||
args test.Args | ||
}{ | ||
{ | ||
name: "normal test", | ||
args: test.Args{ | ||
URL: "https://time.geekbang.org/course/detail/190-97203", | ||
Title: "02 | 内容综述 - 玩转webpack", | ||
Size: 38556544, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
data, err := Extract(tt.args.URL) | ||
test.CheckError(t, err) | ||
test.Check(t, tt.args, data[0]) | ||
}) | ||
} | ||
} |
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