Skip to content

Commit

Permalink
feat(input): support video metrics (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
ykzts authored Oct 19, 2021
1 parent ebada30 commit a02b37a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ Gather channel information from [YouTube](https://www.youtube.com/) channels.
"UCnrgOD6G0y0_rcubQuICpTQ"
]

## List of videos to monitor.
videos = [
"gjoHHYnXdqs",
"OoCsY8odmpM"
]

## YouTube API key.
# api_key = ""
```
Expand All @@ -26,10 +32,22 @@ Gather channel information from [YouTube](https://www.youtube.com/) channels.
- subscribers (int)
- videos (int)
- views (int)
- youtube_video
- tags:
- id - The ID of the video
- title - The title name
- fields:
- comments (int)
- dislikes (int)
- favorites (int)
- likes (int)
- views (int)

### Example Output

```plain
youtube_channel,id=UCBR8-60-B28hp2BmDPdntcQ,title=YouTube subscribers=30400000i,videos=318i,views=2256168113i 1591499999079340600
youtube_channel,id=UCnrgOD6G0y0_rcubQuICpTQ,title=InfluxData subscribers=2740i,videos=150i,views=252193i 1591499999079340600
youtube_channel,id=UCBR8-60-B28hp2BmDPdntcQ,title=YouTube subscribers=32000000i,videos=409i,views=2509852022i 1634655873951240300
youtube_channel,id=UCnrgOD6G0y0_rcubQuICpTQ,title=InfluxData videos=261i,views=489944i,subscribers=4520i 1634655873951240300
youtube_video,id=gjoHHYnXdqs,title=InfluxDB:\ The\ time\ series\ data\ platform\ built\ for\ developers comments=0i,dislikes=1i,favorites=0i,likes=8i,views=377i 1634655874031796300
youtube_video,id=OoCsY8odmpM,title=Intro\ to\ Time\ Series\ Databases\ &\ Data\ |\ Getting\ Started\ [1\ of\ 7] comments=0i,dislikes=20i,favorites=0i,likes=514i,views=74740i 1634655874031796300
```
6 changes: 6 additions & 0 deletions etc/youtube-telegraf-plugin.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@
"UCnrgOD6G0y0_rcubQuICpTQ"
]

## List of videos to monitor.
videos = [
"gjoHHYnXdqs",
"OoCsY8odmpM"
]

## Google API key.
api_key = "${GOOGLE_API_KEY}"
45 changes: 44 additions & 1 deletion plugins/inputs/youtube/youtube.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
// YouTube - plugin main structure
type YouTube struct {
Channels []string `toml:"channels"`
Videos []string `toml:"videos"`
APIKey string `toml:"api_key"`
youtubeService *youtube.Service
}
Expand All @@ -25,6 +26,12 @@ const sampleConfig = `
"UCnrgOD6G0y0_rcubQuICpTQ"
]
## List of videos to monitor.
videos = [
"gjoHHYnXdqs",
"OoCsY8odmpM"
]
## Google API key.
# api_key = ""
`
Expand All @@ -44,7 +51,7 @@ func (y *YouTube) createYouTubeService(ctx context.Context) (*youtube.Service, e
return youtube.NewService(ctx, option.WithAPIKey(y.APIKey))
}

// Gather GitHub Metrics
// Gather YouTube Metrics
func (y *YouTube) Gather(acc telegraf.Accumulator) error {
ctx := context.Background()

Expand Down Expand Up @@ -76,6 +83,25 @@ func (y *YouTube) Gather(acc telegraf.Accumulator) error {
acc.AddFields("youtube_channel", fields, tags, now)
}

call2 := y.youtubeService.Videos.
List([]string{"snippet", "statistics"}).
Id(strings.Join(y.Videos, ",")).
MaxResults(50)

resp2, err := call2.Do()
if err != nil {
return err
}

now2 := time.Now()

for _, item := range resp2.Items {
tags := getVideoTags(item)
fields := getVideoFields(item)

acc.AddFields("youtube_video", fields, tags, now2)
}

return nil
}

Expand All @@ -94,6 +120,23 @@ func getFields(channelInfo *youtube.Channel) map[string]interface{} {
}
}

func getVideoTags(videoInfo *youtube.Video) map[string]string {
return map[string]string{
"id": videoInfo.Id,
"title": videoInfo.Snippet.Title,
}
}

func getVideoFields(videoInfo *youtube.Video) map[string]interface{} {
return map[string]interface{}{
"comments": videoInfo.Statistics.CommentCount,
"dislikes": videoInfo.Statistics.DislikeCount,
"favorites": videoInfo.Statistics.FavoriteCount,
"likes": videoInfo.Statistics.LikeCount,
"views": videoInfo.Statistics.ViewCount,
}
}

func init() {
inputs.Add("youtube", func() telegraf.Input {
return &YouTube{}
Expand Down

0 comments on commit a02b37a

Please sign in to comment.