Skip to content
Permalink
Browse files

Post Spotify links if GPM music link is posted

botbot gains another useless skill!
  • Loading branch information...
golf1052 committed May 14, 2019
1 parent 401db19 commit a229ec1747f926f4f7fc2425480f9d2caaed7ab9
Showing with 96 additions and 22 deletions.
  1. +96 −22 botbot/Client.cs
@@ -20,6 +20,7 @@
using botbot.Controllers;
using System.IO.Pipelines;
using System.Buffers;
using Reverb;

namespace botbot
{
@@ -95,6 +96,7 @@ static Client()
SoundcloudApi soundcloud;
SpotifyApi spotify;
SpotifyGet2018Albums spotifyGet2018Albums;
SpotifyClient spotifyClient;

HttpClient httpClient;

@@ -111,6 +113,7 @@ public Client(Settings settings, ILogger<Client> logger)
soundcloud = new SoundcloudApi();
spotify = new SpotifyApi();
spotifyGet2018Albums = new SpotifyGet2018Albums(SendSlackMessage);
spotifyClient = new SpotifyClient(Secrets.SpotifyClientId, Secrets.SpotifyClientSecret, Secrets.SpotifyRedirectUrl);
this.logger = logger;
}

@@ -132,6 +135,7 @@ public async Task Connect(Uri uri)
await webSocket.ConnectAsync(uri, CancellationToken.None);
slackUsers = await slackCore.UsersList();
slackChannels = await slackCore.ChannelsList(1);
await spotifyClient.RequestAccessToken();
//await soundcloud.Auth();
Task.Run(() => CheckTypings());
Task.Run(() => SendTypings(GetChannelIdByName(settings.TestingChannel)));
@@ -373,29 +377,16 @@ private async void Client_MessageReceived(object sender, SlackMessageEventArgs e
{
return;
}
if (channel == GetChannelIdByName(settings.TechChannel))

string threadTimestamp = null;
if (newMessage["thread_ts"] != null)
{
foreach (JObject attachment in attachments)
{
string url = (string)attachment["title_link"];
if (string.IsNullOrEmpty(url))
{
return;
}
SearchItem hackerNewsItem = await HackerNewsApi.Search(url);
if (hackerNewsItem == null)
{
return;
}

// Check if link was posted in a thread, if so reply to the thread
string threadTimestamp = null;
if (newMessage["thread_ts"] != null)
{
threadTimestamp = (string)newMessage["thread_ts"];
}
await SendSlackMessage($"From Hacker News\nTitle: {hackerNewsItem.Title}\nPoints: {hackerNewsItem.Points}\nComments: {hackerNewsItem.NumComments}\nLink: {hackerNewsItem.GetUrl()}", channel, threadTimestamp);
}
threadTimestamp = (string)newMessage["thread_ts"];
}

foreach (JObject attachment in attachments)
{
await ProcessAttachment(newMessage, attachment, channel, threadTimestamp);
}
//else if (channel == "C0ANB9SMV" || channel == "G0L8C7Q6L") // radio
//{
@@ -630,6 +621,89 @@ public async Task<List<long>> ProcessRadio()
return ids;
}

private async Task ProcessAttachment(JObject newMessage, JObject attachment, string channel, string threadTimestamp)
{
await ProcessGooglePlayMusicAttachment(newMessage, attachment, channel, threadTimestamp);
await ProcessHackerNewsAttachment(newMessage, attachment, channel, threadTimestamp);
}

private async Task ProcessGooglePlayMusicAttachment(JObject newMessage, JObject attachment, string channel, string threadTimestamp)
{
string serviceName = (string)attachment["service_name"];
string originalUrl = (string)attachment["original_url"];
if (serviceName == "play.google.com" && originalUrl.Contains("/music/"))
{
string title = (string)attachment["title"];
string[] splitTitle = title.Split("-");
string guessTitle = string.Empty;
string guessArtist = string.Empty;
if (splitTitle.Length > 0)
{
guessTitle = splitTitle[0].Trim();
}
if (splitTitle.Length > 1)
{
guessArtist = splitTitle[1].Trim();
}

if (spotifyClient.AccessTokenExpiresAt <= DateTimeOffset.UtcNow)
{
await spotifyClient.RefreshAccessToken();
}
var searchResult = await spotifyClient.Search(title,
new List<SpotifyConstants.SpotifySearchTypes> { SpotifyConstants.SpotifySearchTypes.Album, SpotifyConstants.SpotifySearchTypes.Track });
var firstAlbum = searchResult.Albums.Items.FirstOrDefault();
var firstTrack = searchResult.Tracks.Items.FirstOrDefault();
string spotifyLink = string.Empty;
if (firstAlbum != null)
{
var artist = firstAlbum.Artists.FirstOrDefault();
if (artist?.Name == guessArtist && firstAlbum.Name == guessTitle)
{
if (firstAlbum.ExternalUrls.ContainsKey("spotify"))
{
spotifyLink = firstAlbum.ExternalUrls["spotify"];
}
}
}

if (string.IsNullOrEmpty(spotifyLink) && firstTrack != null)
{
var artist = firstTrack.Artists.FirstOrDefault();
if (artist?.Name == guessArtist && firstTrack.Name == guessTitle)
{
if (firstTrack.ExternalUrls.ContainsKey("spotify"))
{
spotifyLink = firstTrack.ExternalUrls["spotify"];
}
}
}

if (!string.IsNullOrEmpty(spotifyLink))
{
await SendSlackMessage($"Spotify Link: {spotifyLink}", channel, threadTimestamp);
}
}
}

private async Task ProcessHackerNewsAttachment(JObject newMessage, JObject attachment, string channel, string threadTimestamp)
{
if (channel == GetChannelIdByName(settings.TechChannel))
{
string url = (string)attachment["title_link"];
if (string.IsNullOrEmpty(url))
{
return;
}
SearchItem hackerNewsItem = await HackerNewsApi.Search(url);
if (hackerNewsItem == null)
{
return;
}
await SendSlackMessage($"From Hacker News\nTitle: {hackerNewsItem.Title}\nPoints: {hackerNewsItem.Points}\nComments: {hackerNewsItem.NumComments}\nLink: {hackerNewsItem.GetUrl()}", channel, threadTimestamp);
}
}

private async Task CalculateReactions(string channel)
{
List<SlackEvent> reactions = new List<SlackEvent>();

0 comments on commit a229ec1

Please sign in to comment.
You can’t perform that action at this time.