Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils: Handle overly long strings #146

Merged
merged 2 commits into from
May 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ func (v VideoData) Download(refer string) {
}
data, ok := v.Formats[format]
if !ok {
log.Println(v)
log.Fatal("No format named " + format)
}
if data.Size == 0 {
Expand Down
4 changes: 2 additions & 2 deletions extractors/bcy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ func TestBcy(t *testing.T) {
name: "normal test",
args: test.Args{
URL: "https://bcy.net/illust/detail/38134/2048276",
Title: "牛奶",
Title: "牛奶小姐姐,草莓味的w - 半次元-二次元爱好者社区",
},
},
{
name: "normal test",
args: test.Args{
URL: "https://bcy.net/coser/detail/143767/2094010",
Title: "命运石之门助手cos预告",
Title: "phx:柠檬先行预告!牧濑红莉栖 cn:三度 - 半次元-二次元爱好者社区",
},
},
}
Expand Down
9 changes: 8 additions & 1 deletion extractors/bilibili/bilibili.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func genURL(durl []dURLData) ([]downloader.URLData, int64) {

type bilibiliOptions struct {
Bangumi bool
P int
Subtitle string
Aid string
Cid string
Expand Down Expand Up @@ -136,6 +137,7 @@ func Download(url string) {
// https://www.bilibili.com/video/av20827366/?p=2
p, _ = strconv.Atoi(pageString[1])
}
options.P = p
page := data.VideoData.Pages[p-1]
options.Aid = data.Aid
options.Cid = strconv.Itoa(page.Cid)
Expand Down Expand Up @@ -171,6 +173,7 @@ func Download(url string) {
options.Aid = data.Aid
options.Cid = strconv.Itoa(u.Cid)
options.Subtitle = u.Part
options.P = u.Page
bilibiliDownload(url, options)
}
}
Expand Down Expand Up @@ -230,7 +233,11 @@ func bilibiliDownload(url string, options bilibiliOptions) downloader.VideoData
doc := parser.GetDoc(html)
title := parser.Title(doc)
if options.Subtitle != "" {
title = fmt.Sprintf("%s %s", title, options.Subtitle)
tempTitle := fmt.Sprintf("%s %s", title, options.Subtitle)
if len([]rune(tempTitle)) > utils.MAXLENGTH {
tempTitle = fmt.Sprintf("%s P%d %s", title, options.P, options.Subtitle)
}
title = tempTitle
}
extractedData := downloader.VideoData{
Site: "哔哩哔哩 bilibili.com",
Expand Down
4 changes: 2 additions & 2 deletions extractors/instagram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ func TestInstagram(t *testing.T) {
name: "Video",
args: test.Args{
URL: "https://www.instagram.com/p/BYQ0PMWlAQY",
Title: "王薇雅🇨🇳🇺🇸 on Instagram:“我的Ins是用来分享#lifestyle 一些正能量健身旅游等,请那些负能量离我远点!谢谢😀😀BTW,我从来不否认我P图微调,谁都想展现自己最完美的一面在网上.不要再给我粉丝私信黑我了,那么空能不能多读书看报增加些涵养🙂🙂🙂”",
Title: "王薇雅🇨🇳🇺🇸 on Instagram:“我的Ins是用来分享#lifestyle 一些正能量健身旅游等,请那些负能量离我远点!谢谢😀😀BTW,我从来不...",
Size: 1469037,
},
},
{
name: "Image Single",
args: test.Args{
URL: "https://www.instagram.com/p/Bei7whzgfMq",
Title: "王薇雅🇨🇳🇺🇸 on Instagram:“Let go of what u can no longer keep. Protect what’s still worth keeping. ✨✨✨”",
Title: "王薇雅🇨🇳🇺🇸 on Instagram:“Let go of what u can no longer keep. Protect what’s sti...",
Size: 144348,
},
},
Expand Down
20 changes: 18 additions & 2 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
"github.com/iawia002/annie/request"
)

// MAXLENGTH Maximum length of file name
const MAXLENGTH = 80

// MatchOneOf match one of the patterns
func MatchOneOf(text string, patterns ...string) []string {
var (
Expand Down Expand Up @@ -47,7 +50,10 @@ func MatchAll(text, pattern string) [][]string {
func FileSize(filePath string) (int64, bool) {
file, err := os.Stat(filePath)
if err != nil {
return 0, false
if os.IsNotExist(err) {
return 0, false
}
log.Fatal(err)
}
return file.Size(), true
}
Expand All @@ -65,6 +71,16 @@ func Domain(url string) string {
return "Universal"
}

// LimitLength Handle overly long strings
func LimitLength(s string, length int) string {
const ELLIPSES = "..."
str := []rune(s)
if len(str) > length {
return string(str[:length-len(ELLIPSES)]) + ELLIPSES
}
return s
}

// FileName Converts a string to a valid filename
func FileName(name string) string {
rep := strings.NewReplacer("\n", " ", "/", " ", "|", "-", ": ", ":", ":", ":", "'", "’")
Expand All @@ -73,7 +89,7 @@ func FileName(name string) string {
rep = strings.NewReplacer("\"", " ", "?", " ", "*", " ", "\\", " ", "<", " ", ">", " ")
name = rep.Replace(name)
}
return name
return LimitLength(name, MAXLENGTH)
}

// FilePath gen valid file path
Expand Down
43 changes: 43 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,42 @@ func TestDomain(t *testing.T) {
}
}

func TestLimitLength(t *testing.T) {
type args struct {
s string
length int
}
tests := []struct {
name string
args args
want string
}{
{
name: "normal test",
args: args{
s: "你好 hello",
length: 8,
},
want: "你好 hello",
},
{
name: "normal test",
args: args{
s: "你好 hello",
length: 6,
},
want: "你好 ...",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := LimitLength(tt.args.s, tt.args.length); got != tt.want {
t.Errorf("LimitLength() = %v, want %v", got, tt.want)
}
})
}
}

func TestFileName(t *testing.T) {
type args struct {
name string
Expand All @@ -190,6 +226,13 @@ func TestFileName(t *testing.T) {
},
want: "hello:world",
},
{
name: "overly long strings test",
args: args{
name: "super 超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长", // length 81
},
want: "super 超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级长超级...",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down