-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscorer.go
92 lines (75 loc) · 2.01 KB
/
scorer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package youtube
import (
"context"
"strings"
"time"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
"github.com/forbole/juno/v5/types/config"
"github.com/desmos-labs/athena/types"
profilesscore "github.com/desmos-labs/athena/x/profiles-score"
)
var (
_ profilesscore.Scorer = &Scorer{}
)
// Scorer represents a scorers.Scorer instance to score profiles based on their Twitter statistics
type Scorer struct {
client *youtube.Service
}
// NewScorer returns a new Scorer instance
func NewScorer(junoCfg config.Config) profilesscore.Scorer {
cfgBz, err := junoCfg.GetBytes()
if err != nil {
panic(err)
}
cfg, err := ParseConfig(cfgBz)
if err != nil {
panic(err)
}
if cfg == nil {
return nil
}
service, err := youtube.NewService(context.Background(), option.WithAPIKey(cfg.APIKey))
if err != nil {
panic(err)
}
return &Scorer{
client: service,
}
}
// GetRateLimit implements Scorer
func (s *Scorer) GetRateLimit() *profilesscore.ScoreRateLimit {
return profilesscore.NewScoreRateLimit(time.Hour*24, 10000)
}
// GetScoreDetails implements Scorer
func (s *Scorer) GetScoreDetails(_ string, application string, username string) (types.ProfileScoreDetails, error) {
if !strings.EqualFold(application, "youtube") {
return nil, nil
}
channel, err := s.GetChannel(username)
if err != nil {
return nil, err
}
var linkedTime *time.Time
if channel.Status.IsLinked {
date, err := time.Parse(time.RFC3339, channel.ContentOwnerDetails.TimeLinked)
if err != nil {
return nil, err
}
linkedTime = &date
}
return NewScoreDetails(
linkedTime,
channel.Statistics.SubscriberCount,
), nil
}
// GetChannel returns the YouTube channel data of the user having the given username
func (s *Scorer) GetChannel(username string) (*youtube.Channel, error) {
call := s.client.Channels.List([]string{"contentDetails", "contentOwnerDetails", "snippet", "statistics", "status"})
call = call.ForUsername(username)
res, err := call.Do()
if err != nil {
return nil, err
}
return res.Items[0], nil
}