Skip to content

Commit

Permalink
Add trivia & separate type from api
Browse files Browse the repository at this point in the history
  • Loading branch information
BarryYan committed Oct 23, 2019
1 parent 460bf5b commit 1644067
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 52 deletions.
59 changes: 12 additions & 47 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,6 @@ import (
query "github.com/PuerkitoBio/goquery"
)

// Weather site data
type Weather struct {
City string
Temp string
Weather string
Air string
Humidity string
Wind string
Limit string
Note string
}

// One site info
type One struct {
Date string
ImgURL string
Sentence string
}

// English info
type English struct {
ImgURL string
Sentence string
}

// Poem info
type Poem struct {
Title string `json:"title"`
Dynasty string `json:"dynasty"`
Author string `json:"author"`
Content []string `json:"content"`
}

// PoemRes response data
type PoemRes struct {
Status string `json:"status"`
Data struct {
Origin Poem `json:"origin"`
} `json:"data"`
}

// CreateClient a http
func CreateClient() *http.Client {
tr := &http.Transport{
Expand Down Expand Up @@ -157,12 +116,6 @@ func GetPoem() Poem {
return resJSON.Data.Origin
}

// Wallpaper data
type Wallpaper struct {
Title string
ImgURL string
}

// GetWallpaper from bing
func GetWallpaper() Wallpaper {
url := "https://bing.ioliu.cn/"
Expand All @@ -175,3 +128,15 @@ func GetWallpaper() Wallpaper {
ImgURL: imgURL,
}
}

// GetTrivia data
func GetTrivia() Trivia {
url := "http://www.lengdou.net/random"
doc := FetchHTML(url)
wrap := doc.Find(".container .media .media-body")
imgURL, _ := wrap.Find(".topic-img img").Attr("src")
return Trivia{
ImgURL: imgURL,
Description: strings.Split(wrap.Find(".topic-content").Text(), "#")[0],
}
}
54 changes: 54 additions & 0 deletions api/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package api

// Weather site data
type Weather struct {
City string
Temp string
Weather string
Air string
Humidity string
Wind string
Limit string
Note string
}

// One site info
type One struct {
Date string
ImgURL string
Sentence string
}

// English info
type English struct {
ImgURL string
Sentence string
}

// Poem info
type Poem struct {
Title string `json:"title"`
Dynasty string `json:"dynasty"`
Author string `json:"author"`
Content []string `json:"content"`
}

// PoemRes response data
type PoemRes struct {
Status string `json:"status"`
Data struct {
Origin Poem `json:"origin"`
} `json:"data"`
}

// Wallpaper data
type Wallpaper struct {
Title string
ImgURL string
}

// Trivia info
type Trivia struct {
ImgURL string
Description string
}
25 changes: 21 additions & 4 deletions daily.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<div style="max-width: 375px; margin: 20px auto;color:#444; font-size: 16px;">
<h3 >{{one.Date}}</h3>
<h3 style="text-align: center">{{weather.City}}</h3>
<div style="text-align: center;font-size: 30px;">❣️</div>
<br>
<div style="padding: 0;width: 100%;">
<div><span style="color: #6e6e6e">天气:</span>{{weather.Weather}}</div>
<div><span style="color: #6e6e6e">温度:</span>{{weather.Temp}}</div>
Expand All @@ -19,28 +21,43 @@ <h3 style="text-align: center">{{weather.City}}</h3>
<div><span style="color: #6e6e6e">限行:</span>{{weather.Limit}}</div>
<div><span style="color: #6e6e6e">提示:</span>{{weather.Note}}</div>
</div>
<br><br>
<br>
<div style="text-align: center;font-size: 30px;">📝</div>
<br>
<div>
<div><img width="100%" src="{{english.ImgURL}}"></div>
<div style="margin-top: 10px;line-height: 1.5">&emsp;&emsp;{{english.Sentence}}</div>
</div>
<br><br>
<br>
<div style="text-align: center;font-size: 30px;">📖</div>
<br>
<div style="text-align: center">
<div>{{poem.Title}}</div>
<div style="font-size: 12px">{{poem.Dynasty}} {{poem.Author}}</div>
<br>
<div>{{poem.Content}}</div>
</div>
<br><br>
<br>
<div style="text-align: center;font-size: 30px;">🔔</div>
<br>
<div>
<div><img width="100%" src="{{one.ImgURL}}"></div>
<div style="margin-top: 10px;line-height: 1.5">&emsp;&emsp;{{one.Sentence}}</div>
</div>
<br><br>
<br>
<div style="text-align: center;font-size: 30px;">🏞</div>
<br>
<div>
<div><img width="100%" src="{{wallpaper.ImgURL}}"></div>
<div style="margin-top: 10px;line-height: 1.5">&emsp;&emsp;{{wallpaper.Title}}</div>
</div>
<br>
<div style="text-align: center;font-size: 30px;">📚</div>
<br>
<div>
<div><img width="100%" src="{{trivia.ImgURL}}"></div>
<div style="margin-top: 10px;line-height: 1.5">&emsp;&emsp;{{trivia.Description}}</div>
</div>
</div>
<br><br>
</body>
Expand Down
65 changes: 64 additions & 1 deletion html.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,68 @@ package main

// HTML for email template
const HTML = `
你想看到什么内容呢?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>每日一暖, 温情一生</title>
</head>
<body>
<div style="max-width: 375px; margin: 20px auto;color:#444; font-size: 16px;">
<h3 >{{one.Date}}</h3>
<h3 style="text-align: center">{{weather.City}}</h3>
<div style="text-align: center;font-size: 30px;">❣️</div>
<br>
<div style="padding: 0;width: 100%;">
<div><span style="color: #6e6e6e">天气:</span>{{weather.Weather}}</div>
<div><span style="color: #6e6e6e">温度:</span>{{weather.Temp}}</div>
<div><span style="color: #6e6e6e">湿度:</span>{{weather.Humidity}}</div>
<div><span style="color: #6e6e6e">风向:</span>{{weather.Wind}}</div>
<div><span style="color: #6e6e6e">空气:</span>{{weather.Air}}</div>
<div><span style="color: #6e6e6e">限行:</span>{{weather.Limit}}</div>
<div><span style="color: #6e6e6e">提示:</span>{{weather.Note}}</div>
</div>
<br>
<div style="text-align: center;font-size: 30px;">📝</div>
<br>
<div>
<div><img width="100%" src="{{english.ImgURL}}"></div>
<div style="margin-top: 10px;line-height: 1.5">&emsp;&emsp;{{english.Sentence}}</div>
</div>
<br>
<div style="text-align: center;font-size: 30px;">📖</div>
<br>
<div style="text-align: center">
<div>{{poem.Title}}</div>
<div style="font-size: 12px">{{poem.Dynasty}} {{poem.Author}}</div>
<br>
<div>{{poem.Content}}</div>
</div>
<br>
<div style="text-align: center;font-size: 30px;">🔔</div>
<br>
<div>
<div><img width="100%" src="{{one.ImgURL}}"></div>
<div style="margin-top: 10px;line-height: 1.5">&emsp;&emsp;{{one.Sentence}}</div>
</div>
<br>
<div style="text-align: center;font-size: 30px;">🏞</div>
<br>
<div>
<div><img width="100%" src="{{wallpaper.ImgURL}}"></div>
<div style="margin-top: 10px;line-height: 1.5">&emsp;&emsp;{{wallpaper.Title}}</div>
</div>
<br>
<div style="text-align: center;font-size: 30px;">📚</div>
<br>
<div>
<div><img width="100%" src="{{trivia.ImgURL}}"></div>
<div style="margin-top: 10px;line-height: 1.5">&emsp;&emsp;{{trivia.Description}}</div>
</div>
</div>
<br><br>
</body>
</html>
`
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func batchSendMail() {
english := api.GetEnglish()
poem := api.GetPoem()
wallpaper := api.GetWallpaper()
trivia := api.GetTrivia()

users := getUsers("MAIL_TO")
if len(users) == 0 {
Expand All @@ -80,6 +81,7 @@ func batchSendMail() {
"english": english,
"poem": poem,
"wallpaper": wallpaper,
"trivia": trivia,
}

html := generateHTML(HTML, datas)
Expand Down

0 comments on commit 1644067

Please sign in to comment.