forked from mxpv/podsync
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
262 additions
and
17 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM golang:1.20 as builder | ||
FROM golang:1.22 as builder | ||
|
||
ENV TAG="nightly" | ||
ENV COMMIT="" | ||
|
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
BINPATH := $(abspath ./bin) | ||
|
||
.PHONY: all | ||
all: build test | ||
all: build #test | ||
|
||
# | ||
# Build Podsync CLI binary | ||
|
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,109 @@ | ||
package builder | ||
|
||
import ( | ||
"context" | ||
|
||
"strconv" | ||
"time" | ||
|
||
"github.com/CuteReimu/bilibili/v2" | ||
"github.com/mxpv/podsync/pkg/feed" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/mxpv/podsync/pkg/model" | ||
) | ||
|
||
type BilibiliBuilder struct { | ||
client *bilibili.Client | ||
} | ||
|
||
func (b *BilibiliBuilder) queryFeed(feed *model.Feed, info *model.Info) error { | ||
|
||
switch info.LinkType { | ||
case model.TypeChannel: | ||
//TODO channel surpport | ||
return errors.New("Bilibili channel not supported.") | ||
case model.TypeUser: | ||
// query user info | ||
mid, err := strconv.Atoi(info.ItemID) | ||
if err != nil { | ||
return err | ||
} | ||
userCardParam := bilibili.GetUserCardParam{Mid: mid, Photo: false} | ||
userCard, err := b.client.GetUserCard(userCardParam) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
feed.Author = userCard.Card.Name | ||
feed.CoverArt = userCard.Card.Face | ||
feed.Title = userCard.Card.Name | ||
feed.Description = userCard.Card.Sign | ||
|
||
// query video collection | ||
videoParam := bilibili.GetVideoByKeywordsParam{Mid: mid, Keywords: "", Pn: feed.PageSize} | ||
videoCollection, err := b.client.GetVideoByKeywords(videoParam) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
feed.PubDate = time.Unix(int64(videoCollection.Archives[0].Pubdate), 0) | ||
|
||
for _, videoInfo := range videoCollection.Archives { | ||
bvid := videoInfo.Bvid | ||
desc, err := b.client.GetVideoDesc(bilibili.VideoParam{Bvid: bvid}) | ||
if err == nil { | ||
e := model.Episode{ | ||
ID: videoInfo.Bvid, | ||
Title: videoInfo.Title, | ||
Description: desc, | ||
Duration: int64(videoInfo.Duration), | ||
Size: int64(videoInfo.Duration * 15000), // very rough estimate | ||
VideoURL: "https://www.bilibili.com/" + videoInfo.Bvid, | ||
PubDate: time.Unix(int64(videoInfo.Pubdate), 0), | ||
Thumbnail: videoInfo.Pic, | ||
Status: model.EpisodeNew, | ||
} | ||
feed.Episodes = append(feed.Episodes, &e) | ||
} | ||
} | ||
|
||
return nil | ||
default: | ||
return errors.New("unsupported link format") | ||
} | ||
} | ||
|
||
func (b *BilibiliBuilder) Build(ctx context.Context, cfg *feed.Config) (*model.Feed, error) { | ||
info, err := ParseURL(cfg.URL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
_feed := &model.Feed{ | ||
ItemID: info.ItemID, | ||
Provider: info.Provider, | ||
LinkType: info.LinkType, | ||
Format: cfg.Format, | ||
Quality: cfg.Quality, | ||
CoverArtQuality: cfg.Custom.CoverArtQuality, | ||
PageSize: cfg.PageSize, | ||
PlaylistSort: cfg.PlaylistSort, | ||
PrivateFeed: cfg.PrivateFeed, | ||
UpdatedAt: time.Now().UTC(), | ||
ItemURL: cfg.URL, | ||
} | ||
|
||
// Query general information about feed (title, description, lang, etc) | ||
if err := b.queryFeed(_feed, &info); err != nil { | ||
return nil, err | ||
} | ||
|
||
return _feed, nil | ||
} | ||
|
||
func NewBilibiliBuilder() (*BilibiliBuilder, error) { | ||
sc := bilibili.New() | ||
|
||
return &BilibiliBuilder{client: sc}, 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,46 @@ | ||
package builder | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mxpv/podsync/pkg/feed" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBilibili_BuildFeed(t *testing.T) { | ||
builder, err := NewBilibiliBuilder() | ||
require.NoError(t, err) | ||
|
||
urls := []string{ | ||
"https://space.bilibili.com/1302298364", | ||
"https://space.bilibili.com/397490386/channel/seriesdetail?sid=1203833", | ||
} | ||
|
||
t.Run(urls[0], func(t *testing.T) { | ||
feed, err := builder.Build(testCtx, &feed.Config{URL: urls[0]}) | ||
require.NoError(t, err) | ||
|
||
assert.NotEmpty(t, feed.Title) | ||
assert.NotEmpty(t, feed.Description) | ||
assert.NotEmpty(t, feed.Author) | ||
assert.NotEmpty(t, feed.ItemURL) | ||
|
||
assert.NotZero(t, len(feed.Episodes)) | ||
|
||
for _, item := range feed.Episodes { | ||
assert.NotEmpty(t, item.Title) | ||
assert.NotEmpty(t, item.VideoURL) | ||
assert.NotZero(t, item.Duration) | ||
assert.NotEmpty(t, item.Title) | ||
assert.NotEmpty(t, item.Thumbnail) | ||
} | ||
}) | ||
|
||
t.Run(urls[1], func(t *testing.T) { | ||
_, err := builder.Build(testCtx, &feed.Config{URL: urls[1]}) | ||
require.Error(t, err, "Bilibili channel not supported.") | ||
|
||
}) | ||
|
||
} |
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
Oops, something went wrong.