Skip to content

Commit

Permalink
improve: Switch URL to WeiboIntl share API.
Browse files Browse the repository at this point in the history
  • Loading branch information
hguandl committed Apr 14, 2021
1 parent 33193fd commit bacca60
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dr-feeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// Version is current `git describe --tags` infomation.
var Version string = "v2.3.2"
var Version string = "v2.3.3"

func consume(ch chan common.NotifyPayload, notifiers []notifier.Notifier) {
for {
Expand Down
49 changes: 48 additions & 1 deletion watcher/weibo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const safariUA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6)" +
" AppleWebKit/605.1.15 (KHTML, like Gecko)" +
" Version/14.0.3 Safari/605.1.15"

const iOSWeiboIntlUA = "WeiboOverseas/4.2.6 (iPhone; iOS 14.4.2; Scale/3.00)"

type weiboWatcher struct {
uid uint64
updateTime time.Time
Expand Down Expand Up @@ -55,6 +57,47 @@ func (watcher weiboWatcher) weiboAPI() string {
)
}

func (watcher weiboWatcher) weiboIntlShareAPI(weiboID string) string {
return fmt.Sprintf("https://weibointl.api.weibo.cn/portal.php"+
"?a=get_share_url"+
"&ct=weibo"+
"&lang=zh-Hans"+
"&uid=%d"+
"&weibo_id=%s",
watcher.uid,
weiboID,
)
}

func (watcher weiboWatcher) weiboIntlShareURL(weiboID string) (string, error) {
var err error = nil
var shareURL string
c := colly.NewCollector(
colly.UserAgent(iOSWeiboIntlUA),
)

c.OnError(func(_ *colly.Response, e error) {
err = e
})

c.OnResponse(func(r *colly.Response) {
var resp weiboIntlResp
err = json.Unmarshal(r.Body, &resp)
if err != nil {
return
}
if resp.Retcode != 0 {
err = fmt.Errorf("%s", resp.Info)
return
}
shareURL = resp.Data.URL
})

c.Visit(watcher.weiboIntlShareAPI(weiboID))
c.Wait()
return shareURL, err
}

func (watcher *weiboWatcher) setup() error {
var err error = nil
c := colly.NewCollector(
Expand Down Expand Up @@ -152,7 +195,11 @@ func (watcher weiboWatcher) parseContent() (common.NotifyPayload, bool) {
}

picURL := weibo.PicURL
pageURL := fmt.Sprintf("%s/%s", "https://m.weibo.cn/status", weibo.ID)
pageURL, err := watcher.weiboIntlShareURL(weibo.ID)
if err != nil {
log.Println(err)
pageURL = fmt.Sprintf("%s/%s", "https://m.weibo.cn/status", weibo.ID)
}

var pageInfo pageInfo
mapstructure.Decode(weibo.PageInfo, &pageInfo)
Expand Down
8 changes: 8 additions & 0 deletions watcher/weibomodels.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ type pageInfo struct {
} `mapstructure:"page_pic"`
PageURL string `mapstructure:"page_url"`
}

type weiboIntlResp struct {
Data struct {
URL string `json:"url"`
} `json:"data"`
Info string `json:"info"`
Retcode int `json:"retcode"`
}

0 comments on commit bacca60

Please sign in to comment.