Skip to content

Commit

Permalink
Drop support for non-image url
Browse files Browse the repository at this point in the history
  • Loading branch information
LarryLuTW committed Jan 8, 2019
1 parent 97245ea commit e8a9590
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 28 deletions.
13 changes: 1 addition & 12 deletions model/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package model

import (
"fmt"
"regexp"
"strings"
"time"

Expand All @@ -21,18 +20,8 @@ type Post struct {
func fetchPreviewImg(p *Post) string {
// TODO: handle error
doc, _ := goquery.NewDocument(p.Href)
imgSelector := `#main-content a[href$=".jpg"],a[href$=".png"],a[href$=".gif"],a[href*="imgur"]`
imgSelector := `#main-content a[href$=".jpg"],a[href$=".png"],a[href$=".gif"]`
imgURL, _ := doc.Find(imgSelector).Attr("href")

// https://imgur.com/8bsl72C -> https://imgur.com/8bsl72C.jpg
matched, err := regexp.MatchString("^https?://imgur.com/\\w+$", imgURL)
if err != nil {
panic(err)
}
if matched {
imgURL += ".jpg"
}

return imgURL
}

Expand Down
16 changes: 0 additions & 16 deletions model/post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,4 @@ func TestPostToBeauty(t *testing.T) {
t.Errorf("transform url error %s", b.Href)
}
}

{
p := Post{
Title: "[正妹] 這個笑容",
Href: "https://www.ptt.cc/bbs/Beauty/M.1479381920.A.D30.html",
NVote: 100,
Date: time.Now(),
}
b := p.ToBeauty()
if b.PreviewImg != "http://imgur.com/8bsl72C.jpg" {
t.Error("preview image error")
}
if b.Title != "這個笑容" {
t.Error("trim title error")
}
}
}

0 comments on commit e8a9590

Please sign in to comment.