Skip to content

Commit

Permalink
Added progression bar to episodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zexuz committed Apr 18, 2019
1 parent 69a6a3b commit d675c8d
Show file tree
Hide file tree
Showing 18 changed files with 476 additions and 39 deletions.
6 changes: 6 additions & 0 deletions Mpv.JsonIpc/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,11 @@ public async Task<TimeSpan> GetCurrentPosition()
var result = await _ipc.GetProperty<double>(Property.Position);
return TimeSpan.FromSeconds(result.Data);
}

public async Task<TimeSpan> GetDuration()
{
var result = await _ipc.GetProperty<double>(Property.Duration);
return TimeSpan.FromSeconds(result.Data);
}
}
}
1 change: 1 addition & 0 deletions Mpv.JsonIpc/IApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public interface IApi
Task<float> GetVolume();
Task PlayMedia(string path);
Task<TimeSpan> GetCurrentPosition();
Task<TimeSpan> GetDuration();
}
}
3 changes: 2 additions & 1 deletion Mpv.JsonIpc/Property.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public enum Property
{
Volume,
Pause,
Position
Position,
Duration
}
}
1 change: 1 addition & 0 deletions Mpv.JsonIpc/PropertyEnumConverterExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static class PropertyEnumConverterExtension
{Property.Volume, "volume"},
{Property.Pause, "pause"},
{Property.Position, "time-pos"},
{Property.Duration, "duration"},
};

public static string GetStringValue(this Property property)
Expand Down
2 changes: 1 addition & 1 deletion SnackTime.Core/Database/DatabaseFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace SnackTime.Core.Database
{
public class DatabaseFactory
{
private const string ConnectionString = "MediaDb";
private const string ConnectionString = "../Media.db";

public LiteDatabase GetDatabase()
{
Expand Down
1 change: 1 addition & 0 deletions SnackTime.Core/DependencyModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ protected override void Load(ContainerBuilder builder)
builder.RegisterType<EpisodeProvider>().AsSelf();

builder.RegisterType<ProcessManager>().AsSelf();
builder.RegisterType<EpisodeBuilder>().AsSelf();
}

public class SonarrConfig
Expand Down
22 changes: 22 additions & 0 deletions SnackTime.Core/Media/Episodes/EpisodeBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
using System.Collections.Generic;
using System.Linq;
using SnackTime.Core.Session;
using SnackTime.MediaServer.Models.ProtoGenerated;
using SnackTime.MediaServer.Storage.ProtoGenerated;

namespace SnackTime.Core.Media.Episodes
{
public class EpisodeBuilder
{
private readonly SessionService _sessionService;

public EpisodeBuilder(SessionService sessionService)
{
_sessionService = sessionService;
}

public List<Episode> Build(IEnumerable<SonarrSharp.Models.Episode> episodes)
{
return episodes.Select(Build)
Expand All @@ -22,6 +31,13 @@ public Episode Build(SonarrSharp.Models.Episode episode)
FileId = episode.EpisodeFileId,
};

var allSessionsForCurrentEpisode = _sessionService.GetAll()
.Where(session => session.MediaId == mediaFileId.ToString())
.ToList();

var end = allSessionsForCurrentEpisode.Max(session => (long?) session.Duration.EndPostionInSec) ?? 0;
var lastWatchedSession = allSessionsForCurrentEpisode.OrderBy(session => session.EndUTC).FirstOrDefault();

return new Episode
{
Title = GrpcStringParser.Parse(episode.Title),
Expand All @@ -31,6 +47,12 @@ public Episode Build(SonarrSharp.Models.Episode episode)
EpisideNumber = episode.EpisodeNumber,
EpisodeFileId = episode.EpisodeFileId,
PlayableId = mediaFileId.ToString(),
Progress = new Progress
{
LastWatchedUtc = lastWatchedSession?.EndUTC ?? 0,
WatchedInSec = end,
Lenght = lastWatchedSession?.MediaLenghtInSec ?? 0
},
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions SnackTime.Core/Media/Episodes/EpisodeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public class EpisodeProvider
private readonly SonarrClient _client;
private readonly EpisodeBuilder _seriesBuilder;

public EpisodeProvider(SonarrClient client)
public EpisodeProvider(SonarrClient client, EpisodeBuilder episodeBuilder)
{
_client = client;
_seriesBuilder = new EpisodeBuilder();
_seriesBuilder = episodeBuilder;
}

public async Task<List<Episode>> GetEpisodesForSeriesById(int seriesId)
Expand Down
16 changes: 11 additions & 5 deletions SnackTime.Core/Session/SessionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,29 @@ public SessionService(DatabaseFactory databaseFactory, TimeService timeService)
_timeService = timeService;
}

public MediaServer.Storage.ProtoGenerated.Session CreateNewSession(MediaFileId mediaFileId, TimeSpan? startPosition = null)
public MediaServer.Storage.ProtoGenerated.Session CreateNewSession
(
MediaFileId mediaFileId,
TimeSpan mediaDuration,
TimeSpan? startPosition = null
)
{
var duration = new Duration {EndPostionInSec = 0, StartPostionInSec = 0};
var timeWatched = new Duration {EndPostionInSec = 0, StartPostionInSec = 0};
if (startPosition.HasValue)
{
duration.StartPostionInSec = startPosition.Value.TotalSeconds;
duration.EndPostionInSec = startPosition.Value.TotalSeconds;
timeWatched.StartPostionInSec = startPosition.Value.TotalSeconds;
timeWatched.EndPostionInSec = startPosition.Value.TotalSeconds;
}

var sessionId = Guid.NewGuid().ToString("N");
return new MediaServer.Storage.ProtoGenerated.Session
{
Id = sessionId,
MediaId = mediaFileId.ToString(),
Duration = duration,
Duration = timeWatched,
StartUTC = _timeService.GetCurrentTimeAsUnixSeconds(),
EndUTC = _timeService.GetCurrentTimeAsUnixSeconds(),
MediaLenghtInSec = mediaDuration.TotalSeconds,
};
}

Expand Down
61 changes: 48 additions & 13 deletions SnackTime.MediaServer.Proto/csharp/Media.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,24 @@ public static partial class MediaReflection {
static MediaReflection() {
byte[] descriptorData = global::System.Convert.FromBase64String(
string.Concat(
"ChFwcm90by9tZWRpYS5wcm90bxIPc25hY2t0aW1lLm1lZGlhIncKBlNlcmll",
"cxIKCgJpZBgBIAEoBRINCgV0aXRsZRgCIAEoCRItCglpbWFnZXNVcmwYA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KCHByb2dyZXNzGAgg",
"ASgLMhsuc25hY2t0aW1lLnN0b3JhZ2UuUHJvZ3Jlc3MqIwoJUHJvdmlkZXJz",
"EgoKBlNvbmFychAAEgoKBlJhZGFychABQi6qAitTbmFja1RpbWUuTWVkaWFT",
"ZXJ2ZXIuTW9kZWxzLlByb3RvR2VuZXJhdGVkYgZwcm90bzM="));
descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData,
new pbr::FileDescriptor[] { },
new pbr::FileDescriptor[] { global::SnackTime.MediaServer.Storage.ProtoGenerated.StorageReflection.Descriptor, },
new pbr::GeneratedClrTypeInfo(new[] {typeof(global::SnackTime.MediaServer.Models.ProtoGenerated.Providers), }, new pbr::GeneratedClrTypeInfo[] {
new pbr::GeneratedClrTypeInfo(typeof(global::SnackTime.MediaServer.Models.ProtoGenerated.Series), global::SnackTime.MediaServer.Models.ProtoGenerated.Series.Parser, new[]{ "Id", "Title", "ImagesUrl", "Overview", "Monitored" }, null, null, null),
new pbr::GeneratedClrTypeInfo(typeof(global::SnackTime.MediaServer.Models.ProtoGenerated.ImagesUrl), global::SnackTime.MediaServer.Models.ProtoGenerated.ImagesUrl.Parser, new[]{ "Banner", "Fanart", "Poster" }, null, null, null),
new pbr::GeneratedClrTypeInfo(typeof(global::SnackTime.MediaServer.Models.ProtoGenerated.Episode), global::SnackTime.MediaServer.Models.ProtoGenerated.Episode.Parser, new[]{ "SeriesId", "EpisodeFileId", "SeasonNumber", "EpisideNumber", "Title", "Overview", "PlayableId" }, null, null, null)
new pbr::GeneratedClrTypeInfo(typeof(global::SnackTime.MediaServer.Models.ProtoGenerated.Episode), global::SnackTime.MediaServer.Models.ProtoGenerated.Episode.Parser, new[]{ "SeriesId", "EpisodeFileId", "SeasonNumber", "EpisideNumber", "Title", "Overview", "PlayableId", "Progress" }, null, null, null)
}));
}
#endregion
Expand Down Expand Up @@ -519,6 +520,7 @@ public Episode(Episode other) : this() {
title_ = other.title_;
overview_ = other.overview_;
playableId_ = other.playableId_;
progress_ = other.progress_ != null ? other.progress_.Clone() : null;
_unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields);
}

Expand Down Expand Up @@ -604,6 +606,17 @@ public string PlayableId {
}
}

/// <summary>Field number for the "progress" field.</summary>
public const int ProgressFieldNumber = 8;
private global::SnackTime.MediaServer.Storage.ProtoGenerated.Progress progress_;
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public global::SnackTime.MediaServer.Storage.ProtoGenerated.Progress Progress {
get { return progress_; }
set {
progress_ = value;
}
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public override bool Equals(object other) {
return Equals(other as Episode);
Expand All @@ -624,6 +637,7 @@ public bool Equals(Episode other) {
if (Title != other.Title) return false;
if (Overview != other.Overview) return false;
if (PlayableId != other.PlayableId) return false;
if (!object.Equals(Progress, other.Progress)) return false;
return Equals(_unknownFields, other._unknownFields);
}

Expand All @@ -637,6 +651,7 @@ public override int GetHashCode() {
if (Title.Length != 0) hash ^= Title.GetHashCode();
if (Overview.Length != 0) hash ^= Overview.GetHashCode();
if (PlayableId.Length != 0) hash ^= PlayableId.GetHashCode();
if (progress_ != null) hash ^= Progress.GetHashCode();
if (_unknownFields != null) {
hash ^= _unknownFields.GetHashCode();
}
Expand Down Expand Up @@ -678,6 +693,10 @@ public void WriteTo(pb::CodedOutputStream output) {
output.WriteRawTag(58);
output.WriteString(PlayableId);
}
if (progress_ != null) {
output.WriteRawTag(66);
output.WriteMessage(Progress);
}
if (_unknownFields != null) {
_unknownFields.WriteTo(output);
}
Expand Down Expand Up @@ -707,6 +726,9 @@ public int CalculateSize() {
if (PlayableId.Length != 0) {
size += 1 + pb::CodedOutputStream.ComputeStringSize(PlayableId);
}
if (progress_ != null) {
size += 1 + pb::CodedOutputStream.ComputeMessageSize(Progress);
}
if (_unknownFields != null) {
size += _unknownFields.CalculateSize();
}
Expand Down Expand Up @@ -739,6 +761,12 @@ public void MergeFrom(Episode other) {
if (other.PlayableId.Length != 0) {
PlayableId = other.PlayableId;
}
if (other.progress_ != null) {
if (progress_ == null) {
progress_ = new global::SnackTime.MediaServer.Storage.ProtoGenerated.Progress();
}
Progress.MergeFrom(other.Progress);
}
_unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields);
}

Expand Down Expand Up @@ -778,6 +806,13 @@ public void MergeFrom(pb::CodedInputStream input) {
PlayableId = input.ReadString();
break;
}
case 66: {
if (progress_ == null) {
progress_ = new global::SnackTime.MediaServer.Storage.ProtoGenerated.Progress();
}
input.ReadMessage(progress_);
break;
}
}
}
}
Expand Down
Loading

0 comments on commit d675c8d

Please sign in to comment.