Skip to content

Commit

Permalink
Fixed sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
Zexuz committed Apr 22, 2019
1 parent d0d19c2 commit 68593d8
Show file tree
Hide file tree
Showing 37 changed files with 1,887 additions and 909 deletions.
14 changes: 14 additions & 0 deletions Mpv.JsonIpc/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ public async Task<TimeSpan> GetCurrentPosition()
public async Task<TimeSpan> GetDuration()
{
var result = await _ipc.GetProperty<double>(Property.Duration);

int counter = 0;
while (Math.Abs(result.Data) < 1)
{
await Task.Delay(100);
result = await _ipc.GetProperty<double>(Property.Duration);

counter++;
if (counter > 20)
{
break;
}
}

return TimeSpan.FromSeconds(result.Data);
}
}
Expand Down
15 changes: 5 additions & 10 deletions SnackTime.Core/DependencyModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ protected override void Load(ContainerBuilder builder)

builder.RegisterType<SettingsService>().AsSelf();
builder.RegisterType<SettingsRepo>().AsSelf();

builder.RegisterType<LocalSessionRepo>().As<ILocalSessionRepo>();

builder.RegisterType<TimeService>().AsSelf();
builder.RegisterType<SessionService>().AsSelf();
builder.RegisterType<SessionFactory>().AsSelf();
builder.RegisterType<Queue<Item>>().AsSelf().SingleInstance();

builder.RegisterType<EpisodeFileLookupProvider>().AsSelf();
Expand All @@ -29,13 +31,6 @@ protected override void Load(ContainerBuilder builder)
builder.RegisterType<ProcessManager>().AsSelf();
builder.RegisterType<EpisodeBuilder>().AsSelf();
}

public class SonarrConfig
{
public string Host { get; set; }
public int Port { get; set; }
public string ApiKey { get; set; }
}
}

public class SonarrFactory
Expand All @@ -55,9 +50,9 @@ public SonarrClient GetClient()

var settings = _settingsService.Get();

if (!string.IsNullOrWhiteSpace(settings.MediaServerAddress))
if (!string.IsNullOrWhiteSpace(settings.Remote.MediaServerAddress))
{
host = settings.MediaServerAddress;
host = settings.Remote.MediaServerAddress;
}


Expand Down
36 changes: 36 additions & 0 deletions SnackTime.Core/LocalSessionRepo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using SnackTime.Core.Database;
using SnackTime.Core.Session;

namespace SnackTime.Core
{
public class LocalSessionRepo : ILocalSessionRepo, IRemoteSessionRepo
{
private readonly DatabaseFactory _databaseFactory;

public LocalSessionRepo(DatabaseFactory databaseFactory)
{
_databaseFactory = databaseFactory;
}

public Task UpsertSession(MediaServer.Storage.ProtoGenerated.Session session)
{
using (var db = _databaseFactory.GetRepository())
{
db.Upsert(session);
}

return Task.CompletedTask;
}

public Task<List<MediaServer.Storage.ProtoGenerated.Session>> GetAll()
{
using (var db = _databaseFactory.GetRepository())
{
var sessions = db.Fetch<MediaServer.Storage.ProtoGenerated.Session>();
return Task.FromResult(sessions);
}
}
}
}
21 changes: 13 additions & 8 deletions SnackTime.Core/Media/Episodes/EpisodeBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SnackTime.Core.Session;
using SnackTime.MediaServer.Models.ProtoGenerated;
using SnackTime.MediaServer.Storage.ProtoGenerated;
Expand All @@ -8,34 +9,38 @@ namespace SnackTime.Core.Media.Episodes
{
public class EpisodeBuilder
{
private readonly SessionService _sessionService;
private readonly ISessionRepoFactory _sessionRepoFactory;

public EpisodeBuilder(SessionService sessionService)
public EpisodeBuilder(ISessionRepoFactory sessionRepoFactory)
{
_sessionService = sessionService;
_sessionRepoFactory = sessionRepoFactory;
}

public List<Episode> Build(IEnumerable<SonarrSharp.Models.Episode> episodes)
public async Task<List<Episode>> Build(IEnumerable<SonarrSharp.Models.Episode> episodes)
{
return episodes.Select(Build)
var tasks = episodes.Select(Build).ToList();
await Task.WhenAll(tasks);

return tasks.Select(task => task.Result)
.OrderByDescending(episode => episode.SeasonNumber)
.ToList();
}

public Episode Build(SonarrSharp.Models.Episode episode)
public async Task<Episode> Build(SonarrSharp.Models.Episode episode)
{
var mediaFileId = new MediaFileId
{
Provider = Providers.Sonarr,
MediaId = episode.SeriesId,
FileId = episode.EpisodeFileId,
};
var sessionRepo = await _sessionRepoFactory.GetRepo();

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

var lastSession = allSessionsForCurrentEpisode.OrderBy(session => session.EndUTC).FirstOrDefault();
var lastSession = allSessionsForCurrentEpisode.OrderByDescending(session => session.EndUTC).FirstOrDefault();

return new Episode
{
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 @@ -19,13 +19,13 @@ public EpisodeProvider(SonarrFactory sonarrFactory, EpisodeBuilder episodeBuilde
public async Task<List<Episode>> GetEpisodesForSeriesById(int seriesId)
{
var episodes = await _client.Episode.GetEpisodes(seriesId);
return _seriesBuilder.Build(episodes);
return await _seriesBuilder.Build(episodes);
}

public async Task<Episode> GetEpisodeById(int id)
{
var episodes = await _client.Episode.GetEpisode(id);
return _seriesBuilder.Build(episodes);
return await _seriesBuilder.Build(episodes);
}
}
}
19 changes: 19 additions & 0 deletions SnackTime.Core/Session/ISessionRepo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SnackTime.Core.Session
{
public interface ISessionRepo
{
Task UpsertSession(MediaServer.Storage.ProtoGenerated.Session session);
Task<List<MediaServer.Storage.ProtoGenerated.Session>> GetAll();
}

public interface ILocalSessionRepo : ISessionRepo
{
}

public interface IRemoteSessionRepo : ISessionRepo
{
}
}
11 changes: 11 additions & 0 deletions SnackTime.Core/Session/ISessionRepoFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Threading.Tasks;

namespace SnackTime.Core.Session
{
public interface ISessionRepoFactory
{
Task<ISessionRepo> GetRepo();
}


}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using SnackTime.Core.Database;
using SnackTime.MediaServer.Service.Session;
using SnackTime.MediaServer.Storage.ProtoGenerated;

namespace SnackTime.Core.Session
{
public class SessionService
public class SessionFactory
{
private readonly DatabaseFactory _databaseFactory;
private readonly TimeService _timeService;
private readonly TimeService _timeService;

public SessionService(DatabaseFactory databaseFactory, TimeService timeService)
public SessionFactory(TimeService timeService)
{
_databaseFactory = databaseFactory;
_timeService = timeService;
}

Expand All @@ -30,32 +29,16 @@ public MediaServer.Storage.ProtoGenerated.Session CreateNewSession
timeWatched.EndPostionInSec = startPosition.Value.TotalSeconds;
}

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

public void UpsertSession(MediaServer.Storage.ProtoGenerated.Session session)
{
using (var db = _databaseFactory.GetRepository())
{
db.Upsert(session);
}
}

public IList<MediaServer.Storage.ProtoGenerated.Session> GetAll()
{
using (var db = _databaseFactory.GetRepository())
{
return db.Fetch<MediaServer.Storage.ProtoGenerated.Session>();
}
return session;
}
}
}
16 changes: 15 additions & 1 deletion SnackTime.Core/Settings/SettingsService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.Extensions.Logging;
using SnackTime.App.Settings.ProtoGenerated;
using SonarrSharp.Models;

namespace SnackTime.Core.Settings
{
Expand All @@ -25,7 +27,19 @@ public void Save(App.Settings.ProtoGenerated.Settings settings)

public App.Settings.ProtoGenerated.Settings Get()
{
return _settingsRepo.Get()?.Value ?? new App.Settings.ProtoGenerated.Settings();
var setting = _settingsRepo.Get()?.Value ?? new App.Settings.ProtoGenerated.Settings();

if (setting.Remote == null)
{
setting.Remote = new Remote();
}

if (setting.System == null)
{
setting.System = new LocalSystem();
}

return setting;
}
}
}
10 changes: 5 additions & 5 deletions SnackTime.MediaServer.Proto/csharp-grpc/File-serviceGrpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
namespace SnackTime.MediaServer.Service.File {
public static partial class File
{
static readonly string __ServiceName = "snacktime.series.file.File";
static readonly string __ServiceName = "snacktime.file.service.File";

static readonly grpc::Marshaller<global::SnackTime.MediaServer.Service.File.DownloadFileRequest> __Marshaller_snacktime_series_file_DownloadFileRequest = grpc::Marshallers.Create((arg) => global::Google.Protobuf.MessageExtensions.ToByteArray(arg), global::SnackTime.MediaServer.Service.File.DownloadFileRequest.Parser.ParseFrom);
static readonly grpc::Marshaller<global::SnackTime.MediaServer.Service.File.ResponseDownloadFile> __Marshaller_snacktime_series_file_ResponseDownloadFile = grpc::Marshallers.Create((arg) => global::Google.Protobuf.MessageExtensions.ToByteArray(arg), global::SnackTime.MediaServer.Service.File.ResponseDownloadFile.Parser.ParseFrom);
static readonly grpc::Marshaller<global::SnackTime.MediaServer.Service.File.DownloadFileRequest> __Marshaller_snacktime_file_service_DownloadFileRequest = grpc::Marshallers.Create((arg) => global::Google.Protobuf.MessageExtensions.ToByteArray(arg), global::SnackTime.MediaServer.Service.File.DownloadFileRequest.Parser.ParseFrom);
static readonly grpc::Marshaller<global::SnackTime.MediaServer.Service.File.ResponseDownloadFile> __Marshaller_snacktime_file_service_ResponseDownloadFile = grpc::Marshallers.Create((arg) => global::Google.Protobuf.MessageExtensions.ToByteArray(arg), global::SnackTime.MediaServer.Service.File.ResponseDownloadFile.Parser.ParseFrom);

static readonly grpc::Method<global::SnackTime.MediaServer.Service.File.DownloadFileRequest, global::SnackTime.MediaServer.Service.File.ResponseDownloadFile> __Method_Download = new grpc::Method<global::SnackTime.MediaServer.Service.File.DownloadFileRequest, global::SnackTime.MediaServer.Service.File.ResponseDownloadFile>(
grpc::MethodType.ServerStreaming,
__ServiceName,
"Download",
__Marshaller_snacktime_series_file_DownloadFileRequest,
__Marshaller_snacktime_series_file_ResponseDownloadFile);
__Marshaller_snacktime_file_service_DownloadFileRequest,
__Marshaller_snacktime_file_service_ResponseDownloadFile);

/// <summary>Service descriptor</summary>
public static global::Google.Protobuf.Reflection.ServiceDescriptor Descriptor
Expand Down
21 changes: 11 additions & 10 deletions SnackTime.MediaServer.Proto/csharp/FileDownload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ public static partial class FileDownloadReflection {
static FileDownloadReflection() {
byte[] descriptorData = global::System.Convert.FromBase64String(
string.Concat(
"CiZwcm90by9maWxlLXNlcnZpY2UvZmlsZS1kb3dubG9hZC5wcm90bxIVc25h",
"Y2t0aW1lLnNlcmllcy5maWxlIioKE0Rvd25sb2FkRmlsZVJlcXVlc3QSEwoL",
"bWVkaWFGaWxlSWQYASABKAkisAEKFFJlc3BvbnNlRG93bmxvYWRGaWxlEjEK",
"B3N0YXJ0ZWQYASABKAsyHi5zbmFja3RpbWUuc2VyaWVzLmZpbGUuU3RhcnRl",
"ZEgAEjAKCHByb2dyZXNzGAIgASgLMhwuc25hY2t0aW1lLnNlcmllcy5maWxl",
"LkNodW5rSAASKwoEZG9uZRgDIAEoCzIbLnNuYWNrdGltZS5zZXJpZXMuZmls",
"ZS5Eb25lSABCBgoEdHlwZSJBCgdTdGFydGVkEg4KBmxlbmdodBgBIAEoARIU",
"CgxzaXplUGVyQ2h1bmsYAiABKAUSEAoIRmlsZU5hbWUYAyABKAkiGAoFQ2h1",
"bmsSDwoHQ29udGVudBgBIAEoDCIUCgREb25lEgwKBEhhc2gYASABKAlCJaoC",
"IlNuYWNrVGltZS5NZWRpYVNlcnZlci5TZXJ2aWNlLkZpbGViBnByb3RvMw=="));
"CiZwcm90by9maWxlLXNlcnZpY2UvZmlsZS1kb3dubG9hZC5wcm90bxIWc25h",
"Y2t0aW1lLmZpbGUuc2VydmljZSIqChNEb3dubG9hZEZpbGVSZXF1ZXN0EhMK",
"C21lZGlhRmlsZUlkGAEgASgJIrMBChRSZXNwb25zZURvd25sb2FkRmlsZRIy",
"CgdzdGFydGVkGAEgASgLMh8uc25hY2t0aW1lLmZpbGUuc2VydmljZS5TdGFy",
"dGVkSAASMQoIcHJvZ3Jlc3MYAiABKAsyHS5zbmFja3RpbWUuZmlsZS5zZXJ2",
"aWNlLkNodW5rSAASLAoEZG9uZRgDIAEoCzIcLnNuYWNrdGltZS5maWxlLnNl",
"cnZpY2UuRG9uZUgAQgYKBHR5cGUiQQoHU3RhcnRlZBIOCgZsZW5naHQYASAB",
"KAESFAoMc2l6ZVBlckNodW5rGAIgASgFEhAKCEZpbGVOYW1lGAMgASgJIhgK",
"BUNodW5rEg8KB0NvbnRlbnQYASABKAwiFAoERG9uZRIMCgRIYXNoGAEgASgJ",
"QiWqAiJTbmFja1RpbWUuTWVkaWFTZXJ2ZXIuU2VydmljZS5GaWxlYgZwcm90",
"bzM="));
descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData,
new pbr::FileDescriptor[] { },
new pbr::GeneratedClrTypeInfo(null, new pbr::GeneratedClrTypeInfo[] {
Expand Down
12 changes: 6 additions & 6 deletions SnackTime.MediaServer.Proto/csharp/FileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public static partial class FileServiceReflection {
static FileServiceReflection() {
byte[] descriptorData = global::System.Convert.FromBase64String(
string.Concat(
"CiVwcm90by9maWxlLXNlcnZpY2UvZmlsZS1zZXJ2aWNlLnByb3RvEhVzbmFj",
"a3RpbWUuc2VyaWVzLmZpbGUaJnByb3RvL2ZpbGUtc2VydmljZS9maWxlLWRv",
"d25sb2FkLnByb3RvMm8KBEZpbGUSZwoIRG93bmxvYWQSKi5zbmFja3RpbWUu",
"c2VyaWVzLmZpbGUuRG93bmxvYWRGaWxlUmVxdWVzdBorLnNuYWNrdGltZS5z",
"ZXJpZXMuZmlsZS5SZXNwb25zZURvd25sb2FkRmlsZSIAMAFCJaoCIlNuYWNr",
"VGltZS5NZWRpYVNlcnZlci5TZXJ2aWNlLkZpbGViBnByb3RvMw=="));
"CiVwcm90by9maWxlLXNlcnZpY2UvZmlsZS1zZXJ2aWNlLnByb3RvEhZzbmFj",
"a3RpbWUuZmlsZS5zZXJ2aWNlGiZwcm90by9maWxlLXNlcnZpY2UvZmlsZS1k",
"b3dubG9hZC5wcm90bzJxCgRGaWxlEmkKCERvd25sb2FkEisuc25hY2t0aW1l",
"LmZpbGUuc2VydmljZS5Eb3dubG9hZEZpbGVSZXF1ZXN0Giwuc25hY2t0aW1l",
"LmZpbGUuc2VydmljZS5SZXNwb25zZURvd25sb2FkRmlsZSIAMAFCJaoCIlNu",
"YWNrVGltZS5NZWRpYVNlcnZlci5TZXJ2aWNlLkZpbGViBnByb3RvMw=="));
descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData,
new pbr::FileDescriptor[] { global::SnackTime.MediaServer.Service.File.FileDownloadReflection.Descriptor, },
new pbr::GeneratedClrTypeInfo(null, null));
Expand Down
Loading

0 comments on commit 68593d8

Please sign in to comment.