Added duration for music files O.O

This commit is contained in:
Kwoth
2016-10-19 12:40:43 +02:00
parent 4428c9d90f
commit 13bac49acc
5 changed files with 85 additions and 5 deletions

View File

@@ -243,6 +243,31 @@ namespace NadekoBot.Modules.Music.Classes
});
}
internal async Task UpdateSongDurationsAsync()
{
var curSong = CurrentSong;
var toUpdate = playlist.Where(s => s.SongInfo.ProviderType == MusicType.Normal &&
s.TotalLength == TimeSpan.Zero);
if (curSong != null)
toUpdate = toUpdate.Append(curSong);
var ids = toUpdate.Select(s => s.SongInfo.Query.Substring(s.SongInfo.Query.LastIndexOf("?v=") + 3))
.Distinct();
var durations = await NadekoBot.Google.GetVideoDurationsAsync(ids);
toUpdate.ForEach(s =>
{
foreach (var kvp in durations)
{
if (s.SongInfo.Query.EndsWith(kvp.Key))
{
s.TotalLength = kvp.Value;
return;
}
}
});
}
public void Destroy()
{
actionQueue.Enqueue(async () =>

View File

@@ -38,7 +38,14 @@ namespace NadekoBot.Modules.Music.Classes
public string PrettyCurrentTime()
{
var time = TimeSpan.FromSeconds(bytesSent / 3840 / 50);
return $"【{(int)time.TotalMinutes}m {time.Seconds}s】";
var str = $"【{(int)time.TotalMinutes}m {time.Seconds}s】**/** ";
if (TotalLength == TimeSpan.Zero)
str += "**?**";
else if (TotalLength == TimeSpan.MaxValue)
str += "**∞**";
else
str += $"【{(int)TotalLength.TotalMinutes}m {TotalLength.Seconds}s】";
return str;
}
const int milliseconds = 20;
@@ -60,6 +67,8 @@ namespace NadekoBot.Modules.Music.Classes
}
}
public TimeSpan TotalLength { get; set; } = TimeSpan.Zero;
public Song(SongInfo songInfo)
{
this.SongInfo = songInfo;
@@ -263,7 +272,8 @@ namespace NadekoBot.Modules.Music.Classes
Provider = "Radio Stream",
ProviderType = musicType,
Query = query
});
})
{ TotalLength = TimeSpan.MaxValue };
}
if (SoundCloud.Default.IsSoundCloudLink(query))
{
@@ -275,7 +285,8 @@ namespace NadekoBot.Modules.Music.Classes
Uri = svideo.StreamLink,
ProviderType = musicType,
Query = svideo.TrackLink,
});
})
{ TotalLength = TimeSpan.FromMilliseconds(svideo.Duration) };
}
if (musicType == MusicType.Soundcloud)
@@ -288,7 +299,8 @@ namespace NadekoBot.Modules.Music.Classes
Uri = svideo.StreamLink,
ProviderType = MusicType.Normal,
Query = svideo.TrackLink,
});
})
{ TotalLength = TimeSpan.FromMilliseconds(svideo.Duration) };
}
var link = (await NadekoBot.Google.GetVideosByKeywordsAsync(query).ConfigureAwait(false)).FirstOrDefault();

View File

@@ -141,6 +141,12 @@ namespace NadekoBot.Modules.Music
var currentSong = musicPlayer.CurrentSong;
if (currentSong == null)
return;
if (currentSong.TotalLength == TimeSpan.Zero)
{
await musicPlayer.UpdateSongDurationsAsync().ConfigureAwait(false);
}
var toSend = $"🎵`Now Playing` {currentSong.PrettyName} " + $"{currentSong.PrettyCurrentTime()}\n";
if (musicPlayer.RepeatSong)
toSend += "🔂";
@@ -168,6 +174,11 @@ namespace NadekoBot.Modules.Music
var currentSong = musicPlayer.CurrentSong;
if (currentSong == null)
return;
if (currentSong.TotalLength == TimeSpan.Zero)
{
await musicPlayer.UpdateSongDurationsAsync().ConfigureAwait(false);
}
await channel.SendMessageAsync($"🎵`Now Playing` {currentSong.PrettyName} " +
$"{currentSong.PrettyCurrentTime()}").ConfigureAwait(false);
}