You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
2.2 KiB
99 lines
2.2 KiB
4 years ago
|
package weibo
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"git.oa00.com/go/mysql"
|
||
|
"github.com/antchfx/htmlquery"
|
||
|
"github.com/golangkit/formatime"
|
||
|
"io"
|
||
|
"net/http"
|
||
|
"net/url"
|
||
|
"recook/internal/v2/lib/downloader"
|
||
|
"recook/internal/v2/model/weibo"
|
||
|
"strings"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
const wUrl = "https://m.weibo.cn/api/container/getIndex"
|
||
|
|
||
|
func GetJson(params url.Values) ([]byte, error) {
|
||
|
req, _ := http.NewRequest("GET", wUrl, nil)
|
||
|
req.Header.Set("user", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36")
|
||
|
req.URL.RawQuery = params.Encode()
|
||
|
c := &http.Client{}
|
||
|
|
||
|
resp, err := c.Do(req)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer resp.Body.Close()
|
||
|
data, err := io.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return data, nil
|
||
|
}
|
||
|
|
||
|
type Card struct {
|
||
|
CardType uint `json:"card_type"`
|
||
|
MBlog MBlog `json:"mblog"`
|
||
|
}
|
||
|
|
||
|
type MBlog struct {
|
||
|
ID string `json:"id"`
|
||
|
Text string `json:"text"`
|
||
|
RetweetedStatus struct {
|
||
|
ID string `json:"id"`
|
||
|
} `json:"retweeted_status"`
|
||
|
OriginalPic string `json:"original_pic"`
|
||
|
CreatedAt string `json:"created_at"`
|
||
|
}
|
||
|
|
||
|
type Message struct {
|
||
|
Ok uint `json:"ok"`
|
||
|
Data struct {
|
||
|
Cards []Card `json:"cards"`
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func ParsePage(data []byte) (res []weibo.ReunionSystem, err error) {
|
||
|
var t Message
|
||
|
err = json.Unmarshal(data, &t)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
do := downloader.New(nil, "/weibo", 10)
|
||
|
do.Start()
|
||
|
for _, v := range t.Data.Cards {
|
||
|
doc, err := htmlquery.Parse(strings.NewReader(v.MBlog.Text))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if v.CardType == 9 && strings.Contains(htmlquery.InnerText(doc), "#儿童失踪紧急发布#") {
|
||
|
|
||
|
now, _ := time.ParseInLocation(time.RubyDate, v.MBlog.CreatedAt, time.Local)
|
||
|
var temp weibo.ReunionSystem
|
||
|
mysql.Db.Table(temp.TableName()).First(&temp, "bid = ?", v.MBlog.ID)
|
||
|
if temp.ID > 0 {
|
||
|
continue
|
||
|
}
|
||
|
do.AddTask(v.MBlog.OriginalPic)
|
||
|
|
||
|
res = append(res, weibo.ReunionSystem{
|
||
|
Text: htmlquery.InnerText(doc),
|
||
|
Pic: do.GetPath(v.MBlog.OriginalPic),
|
||
|
Verify: false,
|
||
|
CreatedAt: formatime.NewSecondFrom(now),
|
||
|
BID: v.MBlog.ID,
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
do.Wait()
|
||
|
if err = mysql.Db.Create(res).Error; err != nil{
|
||
|
fmt.Println(err.Error())
|
||
|
}
|
||
|
|
||
|
return
|
||
|
}
|