Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wangbjun committed Jan 17, 2022
1 parent ee1cd14 commit 8717a94
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
20 changes: 14 additions & 6 deletions api/huya.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var ErrorNotExist = errors.New("未开播或不存在")
func New() Huya {
return Huya{
httpClient: http.Client{
Timeout: time.Second * 5,
Timeout: time.Second * 3,
},
cache: make(map[string][]ResultUrl, 20),
}
Expand All @@ -31,12 +31,20 @@ type ResultUrl struct {
Url string
}

func (r *Huya) GetRealUrl(roomId string) ([]ResultUrl, error) {
// 直接从缓存里面取
// GetRealUrlFromCache 从缓存里面取
func (r *Huya) GetRealUrlFromCache(roomId string) ([]ResultUrl, error) {
cacheUrl, ok := r.cache[roomId]
if ok {
return cacheUrl, nil
}
realUrl, err := r.GetRealUrl(roomId)
if err != nil {
return nil, err
}
return realUrl, nil
}

func (r *Huya) GetRealUrl(roomId string) ([]ResultUrl, error) {
roomUrl := "https://m.huya.com/" + roomId
request, err := http.NewRequest("GET", roomUrl, nil)
if err != nil {
Expand All @@ -60,12 +68,12 @@ func (r *Huya) GetRealUrl(roomId string) ([]ResultUrl, error) {
if submatch == nil || len(submatch) < 2 {
return nil, errors.New("查询失败")
}
resultUrl, err := extractUrl(submatch[1])
realUrl, err := extractUrl(submatch[1])
if err != nil {
return nil, err
}
r.cache[roomId] = resultUrl
return resultUrl, nil
r.cache[roomId] = realUrl
return realUrl, nil
}

func extractUrl(content string) ([]ResultUrl, error) {
Expand Down
5 changes: 2 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func (app *App) updateStatus() {
time.Sleep(time.Millisecond * 300)
}
app.updateRecents()
app.saveRecent()
app.Window.Content().Refresh()
log.Println("update live status finished")
<-ticker.C
Expand Down Expand Up @@ -149,10 +150,8 @@ func (app *App) openRoom(room Room) {
if !isExisted {
app.recents = append(app.recents, &Room{Id: room.Id, Remark: room.Remark})
}
go app.updateRecents()
go app.saveRecent()
// 获取直播地址
urls, err := app.api.GetRealUrl(room.Id)
urls, err := app.api.GetRealUrlFromCache(room.Id)
if err != nil {
app.alert(err.Error())
return
Expand Down

0 comments on commit 8717a94

Please sign in to comment.