Skip to content

Commit

Permalink
[lastfm] adds youtube link to [p]lf np, adds [p]lf yt
Browse files Browse the repository at this point in the history
  • Loading branch information
Seklfreak committed Mar 5, 2018
1 parent 1b16f8c commit c2a2fc0
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 12 deletions.
3 changes: 2 additions & 1 deletion _assets/i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@
"toptracks-embed-title": "%s Top Tracks",
"set-username-success": "You set `%s` as your username. <:blobsalute:317043033004703744>\n(This is saved across servers.)",
"no-stats-available": "No stats available on this server yet. <a:ablobweary:394026914479865856>",
"embed-footer-imageurl": "https://i.imgur.com/p8wijg4.png"
"embed-footer-imageurl": "https://i.imgur.com/p8wijg4.png",
"lastfm-no-youtube": "YouTube is currently not available.\nPlease try again later."
},
"weather": {
"address-not-found": "I can't find the location you are looking for. <:blobthinking:317028940885524490>",
Expand Down
64 changes: 62 additions & 2 deletions modules/plugins/lastfm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/Seklfreak/Robyul2/cache"
"github.com/Seklfreak/Robyul2/helpers"
"github.com/Seklfreak/Robyul2/metrics"
"github.com/Seklfreak/Robyul2/services/youtube"
"github.com/bradfitz/slice"
"github.com/bwmarrin/discordgo"
"github.com/dustin/go-humanize"
Expand All @@ -20,8 +21,9 @@ import (
type LastFm struct{}

const (
lastfmHexColor = "#d51007"
lastfmFriendlyUser = "https://www.last.fm/user/%s"
lastfmHexColor = "#d51007"
lastfmFriendlyUser = "https://www.last.fm/user/%s"
lastfmYouTubeFriendlyUrl = "https://youtu.be/%s"
)

var (
Expand Down Expand Up @@ -399,12 +401,70 @@ func (m *LastFm) Action(command string, content string, msg *discordgo.Message,
Inline: false,
})
}
if youtube.HasYouTubeService() {
searchResult, err := youtube.GetYouTubeService().SearchQuerySingle(
[]string{lastTrack.Artist.Name, lastTrack.Name}, "video")
helpers.RelaxLog(err)
if err == nil && searchResult != nil && searchResult.Snippet != nil {
lastTrackEmbed.Description += "\navailable on [YouTube](" + fmt.Sprintf(lastfmYouTubeFriendlyUrl, searchResult.Id.VideoId) + ")"
lastTrackEmbed.Footer.Text += " and YouTube"
}
}
_, err = helpers.SendEmbed(msg.ChannelID, lastTrackEmbed)
helpers.RelaxEmbed(err, msg.ChannelID, msg.ID)
} else {
helpers.SendMessage(msg.ChannelID, helpers.GetText("plugins.lastfm.no-recent-tracks"))
return
}
case "yt", "youtube":
if !youtube.HasYouTubeService() {
helpers.SendMessage(msg.ChannelID, helpers.GetText("lastfm.no-youtube"))
return
}
if len(args) >= 2 {
lastfmUsername = args[1]
targetUser, err := helpers.GetUserFromMention(lastfmUsername)
if err == nil {
lastfmUsername = m.getLastFmUsername(targetUser.ID)
}
}

channel, err := helpers.GetChannel(msg.ChannelID)
helpers.Relax(err)

if lastfmUsername == "" {
helpers.SendMessage(msg.ChannelID, helpers.GetTextF("plugins.lastfm.too-few", helpers.GetPrefixForServer(channel.GuildID)))
return
}
session.ChannelTyping(msg.ChannelID)
lastfmRecentTracks, err := lastfmClient.User.GetRecentTracks(lastfm.P{
"limit": 2,
"user": lastfmUsername,
})
metrics.LastFmRequests.Add(1)
if err != nil {
if e, ok := err.(*lastfm.LastfmError); ok {
helpers.SendMessage(msg.ChannelID, fmt.Sprintf("Error: `%s`", e.Message))
return
}
}
if lastfmRecentTracks.Total > 0 {
lastTrack := lastfmRecentTracks.Tracks[0]
searchResult, err := youtube.GetYouTubeService().SearchQuerySingle(
[]string{lastTrack.Artist.Name, lastTrack.Name}, "video")
helpers.RelaxLog(err)
if err != nil || searchResult == nil || searchResult.Snippet == nil {
helpers.SendMessage(msg.ChannelID, helpers.GetText("lastfm.no-youtube"))
return
}
messageContent := "**" + searchResult.Snippet.Title + "** on " + searchResult.Snippet.ChannelTitle + "\n"
messageContent += fmt.Sprintf(lastfmYouTubeFriendlyUrl, searchResult.Id.VideoId)
_, err = helpers.SendMessage(msg.ChannelID, messageContent)
helpers.RelaxEmbed(err, msg.ChannelID, msg.ID)
} else {
helpers.SendMessage(msg.ChannelID, helpers.GetText("plugins.lastfm.no-recent-tracks"))
return
}
case "topalbums", "topalbum":
timeLookup := "overall"
timeString := "all time"
Expand Down
7 changes: 4 additions & 3 deletions modules/plugins/youtube/feeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ import (
"sync/atomic"
"time"

youtubeService "github.com/Seklfreak/Robyul2/services/youtube"

"github.com/Seklfreak/Robyul2/helpers"
"github.com/Seklfreak/Robyul2/models"
"github.com/Seklfreak/Robyul2/modules/plugins/youtube/service"
"github.com/bwmarrin/discordgo"
rethink "github.com/gorethink/gorethink"
"github.com/sirupsen/logrus"
)

type feeds struct {
service *service.Service
service *youtubeService.Service
running uint32
}

func (f *feeds) Init(e *service.Service) {
func (f *feeds) Init(e *youtubeService.Service) {
if e == nil {
helpers.Relax(fmt.Errorf("feeds loop initialize failed"))
}
Expand Down
5 changes: 3 additions & 2 deletions modules/plugins/youtube/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (

"github.com/Seklfreak/Robyul2/helpers"
"github.com/Seklfreak/Robyul2/models"
"github.com/Seklfreak/Robyul2/modules/plugins/youtube/service"
youtubeService "github.com/Seklfreak/Robyul2/services/youtube"
"github.com/bwmarrin/discordgo"
"github.com/dustin/go-humanize"
)

type Handler struct {
service service.Service
service youtubeService.Service
feedsLoop feeds
}

Expand All @@ -39,6 +39,7 @@ func (h *Handler) Init(session *discordgo.Session) {

h.service.Init(youtubeConfigFileName)
h.feedsLoop.Init(&h.service)
youtubeService.SetYouTubeService(&h.service)
}

func (h *Handler) Action(command string, content string, msg *discordgo.Message, session *discordgo.Session) {
Expand Down
35 changes: 35 additions & 0 deletions services/youtube/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package youtube

import (
"errors"
"sync"
)

var (
youtubeServiceClient *Service
youtubeServiceClientMutex sync.RWMutex
)

func SetYouTubeService(s *Service) {
youtubeServiceClientMutex.Lock()
youtubeServiceClient = s
youtubeServiceClientMutex.Unlock()
}

func HasYouTubeService() bool {
if youtubeServiceClient == nil {
return false
}
return true
}

func GetYouTubeService() *Service {
youtubeServiceClientMutex.RLock()
defer youtubeServiceClientMutex.RUnlock()

if youtubeServiceClient == nil {
panic(errors.New("tried to get youtube service before cache#SetYouTubeService() was called"))
}

return youtubeServiceClient
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package service
package youtube

import (
"sync"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package service
package youtube

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package service
package youtube

import "regexp"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package service
package youtube

import "testing"

Expand Down

1 comment on commit c2a2fc0

@Seklfreak
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.