Skip to content

Commit

Permalink
Add image amount in each beauty
Browse files Browse the repository at this point in the history
  • Loading branch information
LarryLuTW committed Jan 8, 2019
1 parent e8a9590 commit 77248ba
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
6 changes: 3 additions & 3 deletions mail/mail.html
Expand Up @@ -79,14 +79,14 @@ <h1 class="title">Daily Beauty 表特日報</h1>
<div class="list-title">本日精選</div>
{{ range $.Beauties }}
<a class="item" href="{{.Href}}">
<h2>{{ .Title }}</h2>
<h2>{{ .Title }}({{ .NImage }}圖)</h2>
<img class="preview" src="{{.PreviewImg}}" />
</a>
{{ end }}

<div class="list-title">隨機推薦</div>
<a class="item" href="{{ $.RandomBeauty.Href }}">
<h2>{{ $.RandomBeauty.Title }}</h2>
<h2>{{ $.RandomBeauty.Title }}({{ $.RandomBeauty.NImage }}圖)</h2>
<img class="preview" src="{{ $.RandomBeauty.PreviewImg }}" />
</a>

Expand All @@ -96,7 +96,7 @@ <h2>{{ $.RandomBeauty.Title }}</h2>
如果不想再收到了可以
<a
class="unsubscribe"
href="https://daily-beauty.xyz/api/unsubscribe?token={{$.Token}}"
href="https://daily-beauty.xyz/api/unsubscribe?token={{ $.Token }}"
>取消訂閱</a
>
</div>
Expand Down
1 change: 1 addition & 0 deletions model/beauty.go
Expand Up @@ -3,6 +3,7 @@ package model
// Beauty is a struct from getDailyBeauties api
type Beauty struct {
NVote int
NImage int
Title string
Href string
PreviewImg string
Expand Down
17 changes: 16 additions & 1 deletion model/post.go
Expand Up @@ -25,7 +25,20 @@ func fetchPreviewImg(p *Post) string {
return imgURL
}

// [正妹] 大橋未久 -> 大橋未久
// fetchImageAmount get the amount of images in a post
func fetchImageAmount(p *Post) int {
// TODO: handle error
doc, _ := goquery.NewDocument(p.Href)
doc.Find("div.push").Each(func(i int, s *goquery.Selection) {
// remove push comment
s.Remove()
})
imgSelector := `#main-content a[href$=".jpg"],a[href$=".png"],a[href$=".gif"]`
nImage := doc.Find(imgSelector).Size()
return nImage
}

// "[正妹] 大橋未久" -> "大橋未久"
func trimTitlePrefix(title string) string {
return strings.TrimPrefix(title, "[正妹] ")
}
Expand All @@ -41,8 +54,10 @@ func transformURL(pttURL string) string {
// ToBeauty transform a Post to a Beauty
func (p *Post) ToBeauty() Beauty {
previewImg := fetchPreviewImg(p)
nImage := fetchImageAmount(p)
return Beauty{
NVote: p.NVote,
NImage: nImage,
Title: trimTitlePrefix(p.Title),
Href: transformURL(p.Href),
PreviewImg: previewImg,
Expand Down

0 comments on commit 77248ba

Please sign in to comment.