add line api

This commit is contained in:
Jay
2018-09-10 18:13:27 +08:00
parent 00147fc4d0
commit 80b9ccf613
47 changed files with 13185 additions and 5 deletions
+29
View File
@@ -0,0 +1,29 @@
package apis
import (
"io"
"net/http"
)
type RequestObj struct {
Method string
Url string
Body io.Reader
Headers map[string]string
}
func GetRequest(r RequestObj) (req *http.Request, err error) {
req, err = http.NewRequest(r.Method, r.Url, r.Body)
if err != nil {
return
}
if len(r.Headers) > 0 {
for k, v := range r.Headers {
req.Header.Set(k, v)
}
}
return
}
+104
View File
@@ -0,0 +1,104 @@
package line
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"git.trj.tw/golang/mtfosbot/module/apis"
"git.trj.tw/golang/mtfosbot/module/config"
)
type TextMessage struct {
Type string `json:"type"`
Text string `json:"text"`
}
type ImageMessage struct {
Type string `json:"type"`
OriginalContentUrl string `json:"originalContentUrl"`
PreviewImageUrl string `json:"previewImageUrl"`
}
type pushBody struct {
To string `json:"to"`
Messages []interface{} `json:"messages"`
}
type replyBody struct {
ReplyToken string `json:"replyToken"`
Messages []interface{} `json:"messages"`
}
var baseUrl = "https://api.line.me/"
func getUrl(p string) (string, bool) {
u, err := url.Parse(baseUrl)
if err != nil {
return "", false
}
ref, err := u.Parse(p)
if err != nil {
return "", false
}
str := ref.String()
return str, true
}
func getHeaders() map[string]string {
m := make(map[string]string)
conf := config.GetConf()
m["Content-Type"] = "application/json"
m["Authorization"] = fmt.Sprintf("Bearer %s", conf.Line.Access)
return m
}
// PushMessage -
func PushMessage(target string, message interface{}) {
url := "/v2/bot/message/push"
body := &pushBody{
To: target,
}
switch message.(type) {
case ImageMessage:
break
case TextMessage:
break
default:
return
}
body.Messages = append(body.Messages, message)
dataByte, err := json.Marshal(body)
if err != nil {
fmt.Println("json encoding error")
return
}
byteReader := bytes.NewReader(dataByte)
apiUrl, ok := getUrl(url)
if !ok {
fmt.Println("url parser fail")
return
}
reqObj := apis.RequestObj{
Method: "POST",
Url: apiUrl,
Headers: getHeaders(),
Body: byteReader,
}
req, err := apis.GetRequest(reqObj)
if err != nil {
return
}
_, err = http.DefaultClient.Do(req)
if err != nil {
fmt.Println("post api fail")
return
}
}
+4 -4
View File
@@ -1,14 +1,14 @@
package background
import "github.com/robfig/cron"
import (
"github.com/robfig/cron"
)
var c *cron.Cron
// SetBackground -
func SetBackground() {
c = cron.New()
c.AddFunc("0 */2 * * * *", readFacebookPage)
c.AddFunc("0 * * * * *", readFacebookPage)
c.Start()
}
func readFacebookPage() {}
+118
View File
@@ -0,0 +1,118 @@
package background
import (
"fmt"
"net/http"
"regexp"
"sort"
"strconv"
"github.com/PuerkitoBio/goquery"
"git.trj.tw/golang/mtfosbot/model"
)
var idRegex = []*regexp.Regexp{
regexp.MustCompile(`[\?|&]id\=(\d+)`),
regexp.MustCompile(`\/posts\/(\d+)`),
regexp.MustCompile(`\/photos\/.+?\/(\d+)`),
regexp.MustCompile(`\/videos\/(\d+)`),
}
type PageData struct {
ID string
Text string
Time int
Link string
}
type byTime []*PageData
func (pd byTime) Len() int { return len(pd) }
func (pd byTime) Swap(i, j int) { pd[i], pd[j] = pd[j], pd[i] }
func (pd byTime) Less(i, j int) bool { return pd[i].Time < pd[j].Time }
func readFacebookPage() {
pages, err := model.GetAllFacebookPage()
if err != nil {
return
}
for _, v := range pages {
fmt.Println("get facebook page ::: ", v.ID)
go getPageHTML(v)
}
}
func getPageHTML(page *model.FacebookPage) {
resp, err := http.Get(fmt.Sprintf("https://www.facebook.com/%s", page.ID))
if err != nil {
return
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return
}
var pageData []*PageData
doc.Find("div.userContentWrapper").Each(func(idx int, s *goquery.Selection) {
timeEl := s.Find("abbr")
time, timeExists := timeEl.Attr("data-utime")
if !timeExists {
fmt.Println("time not found")
return
}
link, linkExists := timeEl.Parent().Attr("href")
if !linkExists {
fmt.Println("link not found")
return
}
postContent := s.Find("div.userContent")
text := postContent.Text()
postID, idExists := postContent.First().Attr("id")
if !idExists {
idFlag := false
for _, v := range idRegex {
if v.MatchString(link) {
idFlag = true
m := v.FindStringSubmatch(link)
postID = m[1]
}
}
if !idFlag {
fmt.Println("id not found")
return
}
}
fmt.Printf("Time: %s / Text: %s / ID: %s \n", time, text, postID)
timeInt, err := strconv.Atoi(time)
if err != nil {
fmt.Println("convert time to int error")
return
}
re := regexp.MustCompile(`^\/`)
pageLink := fmt.Sprintf("https://www.facebook.com/%s", re.ReplaceAllString(link, ""))
data := &PageData{
ID: postID,
Text: text,
Time: timeInt,
Link: pageLink,
}
pageData = append(pageData, data)
})
if len(pageData) == 0 {
fmt.Println("no data found")
return
}
sort.Sort(sort.Reverse(byTime(pageData)))
// lastData := pageData[0]
}