Skip to content

Commit

Permalink
service 4 separation
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-zhur committed Sep 25, 2024
1 parent c9470da commit 6ef4054
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
5 changes: 3 additions & 2 deletions OneShelf.Videos/OneShelf.Videos.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Extensions.Hosting;
using OneShelf.Videos.BusinessLogic;
using OneShelf.Videos.BusinessLogic.Services;
using OneShelf.Videos.BusinessLogic.Services.Live;
using OneShelf.Videos.Database;

var builder = Host.CreateApplicationBuilder();
Expand All @@ -15,12 +16,12 @@
var service1 = host.Services.GetRequiredService<Service1>();
var service2 = host.Services.GetRequiredService<Service2>();
var service3 = host.Services.GetRequiredService<Service3>();
var service4 = host.Services.GetRequiredService<Service4>();
var service4 = host.Services.GetRequiredService<LiveDownloader>();

await using var videosDatabase = host.Services.GetRequiredService<VideosDatabase>();
await videosDatabase.Database.MigrateAsync();

await service4.Try();
await service4.Try(false);

//await videosDatabase.CreateMissingTopics();
//await videosDatabase.UpdateMessagesTopics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.Options;
using OneShelf.Videos.BusinessLogic.Models;
using OneShelf.Videos.BusinessLogic.Services;
using OneShelf.Videos.BusinessLogic.Services.Live;
using OneShelf.Videos.Database;
using Polly;

Expand All @@ -20,7 +21,7 @@ public static IServiceCollection AddVideosBusinessLogic(this IServiceCollection
.AddScoped<Service1>()
.AddScoped<Service2>()
.AddScoped<Service3>()
.AddScoped<Service4>()
.AddScoped<LiveDownloader>()
.AddSingleton<TelegramLoggerInitializer>()
.AddScoped<VideosDatabaseOperations>()
.AddMyGooglePhotos();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
Expand All @@ -12,14 +10,14 @@
using WTelegram;
using Document = TL.Document;

namespace OneShelf.Videos.BusinessLogic.Services;
namespace OneShelf.Videos.BusinessLogic.Services.Live;

public class Service4(IOptions<VideosOptions> options, ILogger<Service4> logger, VideosDatabase videosDatabase, TelegramLoggerInitializer _) : IDisposable
public class LiveDownloader(IOptions<VideosOptions> options, ILogger<LiveDownloader> logger, VideosDatabase videosDatabase, TelegramLoggerInitializer _) : IDisposable
{
private byte[]? _session;
private readonly AsyncLock _databaseLock = new();

public async Task Try()
public async Task Try(bool downloadMissing)
{
await using var client = await Login();

Expand All @@ -29,11 +27,16 @@ public async Task Try()
{
var chat = allChats.chats[chatId];

await ProcessChat(client, chat);
var topics = await ProcessChat(client, chat);

if (downloadMissing)
{
await Download(client, chat, topics);
}
}
}

private async Task ProcessChat(Client client, ChatBase chat)
private async Task<Dictionary<int, (string name, List<(Document? document, Photo? photo, Message message, string mediaFlags)> media)>> ProcessChat(Client client, ChatBase chat)
{
var inputPeer = chat.ToInputPeer();

Expand All @@ -44,11 +47,9 @@ private async Task ProcessChat(Client client, ChatBase chat)

var topics = await GetTopicsAndMedia(messages);

//await client.DownloadFileAsync(topics[0].media[0].document.ToFileLocation(topics[0].media[0].document.LargestThumbSize), outputStream)

await Save(chat, topics);

await Download(client, chat, topics);
return topics;
}

private async Task Download(Client client, ChatBase chat, Dictionary<int, (string name, List<(Document? document, Photo? photo, Message message, string mediaFlags)> media)> topics)
Expand Down Expand Up @@ -184,7 +185,7 @@ private async Task Save(ChatBase chat, Dictionary<int, (string name, List<(Docum
liveMedia.MediaType = media.message.media.GetType().ToString();
liveMedia.MediaFlags = media.mediaFlags;

if (media.document is {} document)
if (media.document is { } document)
{
liveMedia.Type = LiveMediaType.Document;
liveMedia.MediaDate = document.date;
Expand All @@ -205,7 +206,7 @@ private async Task Save(ChatBase chat, Dictionary<int, (string name, List<(Docum
liveMedia.VideoFlags = attributeVideo.flags.ToString();
}
}
else if (media.photo is {} photo)
else if (media.photo is { } photo)
{
liveMedia.Type = LiveMediaType.Photo;
liveMedia.MediaDate = photo.date;
Expand Down Expand Up @@ -325,7 +326,7 @@ private async Task<Client> Login()
? await File.ReadAllBytesAsync(options.Value.TelegramAuthPath)
: null,
x => _session = x);

var myself = await client.LoginUserIfNeeded();

logger.LogInformation("We are logged-in as {myself} (id {id})", myself, myself.id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Microsoft.Extensions.Logging;

namespace OneShelf.Videos.BusinessLogic.Services;
namespace OneShelf.Videos.BusinessLogic.Services.Live;

public class TelegramLoggerInitializer
{
public TelegramLoggerInitializer(ILogger<Service4> logger)
public TelegramLoggerInitializer(ILogger<LiveDownloader> logger)
{
WTelegram.Helpers.Log = (logLevel, message) => logger.Log((LogLevel)logLevel, message);
}
Expand Down

0 comments on commit 6ef4054

Please sign in to comment.