Skip to content

Commit

Permalink
downloading the items locally to the drive
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-zhur committed Sep 7, 2024
1 parent 769b1cf commit d32acbd
Show file tree
Hide file tree
Showing 9 changed files with 845 additions and 31 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace OneShelf.Videos.Database.Migrations.VideosDatabaseMigrations
{
/// <inheritdoc />
public partial class TelegramMediaItemsDownloaded : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "DownloadedFileName",
table: "TelegramMedia",
type: "nvarchar(max)",
nullable: true);

migrationBuilder.AddColumn<string>(
name: "DownloadedThumbnailFileName",
table: "TelegramMedia",
type: "nvarchar(max)",
nullable: true);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "DownloadedFileName",
table: "TelegramMedia");

migrationBuilder.DropColumn(
name: "DownloadedThumbnailFileName",
table: "TelegramMedia");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,12 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<DateTime>("CreatedOn")
.HasColumnType("datetime2");

b.Property<string>("DownloadedFileName")
.HasColumnType("nvarchar(max)");

b.Property<string>("DownloadedThumbnailFileName")
.HasColumnType("nvarchar(max)");

b.Property<int?>("Duration")
.HasColumnType("int");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class TelegramMedia

public int? HandlerMessageId { get; set; }

public string? DownloadedFileName { get; set; }
public string? DownloadedThumbnailFileName { get; set; }

public required DateTime CreatedOn { get; set; }
public required DateTime TelegramPublishedOn { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using OneShelf.Common;
using OneShelf.Telegram.Model.CommandAttributes;
using OneShelf.Telegram.Model.Ios;
using OneShelf.Telegram.Services.Base;
using OneShelf.Videos.BusinessLogic.Models;
using OneShelf.Videos.Database;
using OneShelf.Videos.Database.Models;
using OneShelf.Videos.Telegram.Processor.Model;
using Telegram.BotAPI;
using Telegram.BotAPI.AvailableMethods;
using Telegram.BotAPI.UpdatingMessages;
using File = System.IO.File;

namespace OneShelf.Videos.Telegram.Processor.Commands;

[AdminCommand("download_all", "Скачать", "Скачать с хендлерами")]
public class DownloadAll : Command
{
private readonly VideosDatabase _videosDatabase;
private readonly IOptions<TelegramOptions> _telegramOptions;
private readonly IOptions<VideosOptions> _videoOptions;
private readonly TelegramBotClient _botClient;
private readonly HttpClient _httpClient;

public DownloadAll(Io io, VideosDatabase videosDatabase, IOptions<TelegramOptions> telegramOptions, HttpClient httpClient, IOptions<VideosOptions> videoOptions)
: base(io)
{
_videosDatabase = videosDatabase;
_telegramOptions = telegramOptions;
_httpClient = httpClient;
_videoOptions = videoOptions;
_botClient = new(_telegramOptions.Value.Token);
}

protected override async Task ExecuteQuickly()
{
var items = await _videosDatabase.TelegramMedia
.Where(x => x.HandlerMessageId.HasValue && x.DownloadedFileName == null).ToListAsync();

if (!items.Any())
{
Io.WriteLine("Нечего скачивать.");
return;
}

Scheduled(Background(items));
}

private async Task Background(List<TelegramMedia> items)
{
var progress = await _botClient.SendMessageAsync(Io.UserId, items.Count.ToString());
var progressUpdated = DateTime.Now;

foreach (var (telegramMedia, i) in items.WithIndices())
{
telegramMedia.DownloadedFileName = await Save(telegramMedia, false);
await _videosDatabase.SaveChangesAsync();

if (telegramMedia.ThumbnailFileId != null)
{
telegramMedia.DownloadedThumbnailFileName = await Save(telegramMedia, true);
await _videosDatabase.SaveChangesAsync();
}

if ((DateTime.Now - progressUpdated).TotalSeconds > 5 || i == items.Count - 1)
{
await _botClient.EditMessageTextAsync(progress.Chat.Id, progress.MessageId, $"{i + 1}/{items.Count}");
progressUpdated = DateTime.Now;
}
}
}

private async Task<string> Save(TelegramMedia telegramMedia, bool isThumbnail)
{
var file = await _botClient.GetFileAsync(isThumbnail ? telegramMedia.ThumbnailFileId! : telegramMedia.FileId);
Console.WriteLine(file.FilePath);
var response = await _httpClient.GetAsync($"https://api.telegram.org/file/bot{_telegramOptions.Value.Token}/{file.FilePath}");
var bytes = await response.Content.ReadAsByteArrayAsync();
Console.WriteLine(bytes.Length);
var name = $"{DateTime.Now.Ticks}_{telegramMedia.FileName}";
await File.WriteAllBytesAsync(Path.Combine(_videoOptions.Value.BasePath, "_uploaded", isThumbnail ? "thumbnails" : ".", name), bytes);
return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using OneShelf.Telegram.Model.Ios;
using OneShelf.Telegram.Services.Base;
using OneShelf.Videos.Database;
using OneShelf.Videos.Database.Models;
using OneShelf.Videos.Telegram.Processor.Model;
using Telegram.BotAPI;
using Telegram.BotAPI.AvailableMethods;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
using Telegram.BotAPI.AvailableMethods;
using Telegram.BotAPI.AvailableTypes;
using Telegram.BotAPI.GettingUpdates;
using Telegram.BotAPI.UpdatingMessages;
using File = System.IO.File;

namespace OneShelf.Videos.Telegram.Processor.PipelineHandlers;

Expand All @@ -22,18 +20,14 @@ public class VideosCollector : PipelineHandler
private readonly VideosDatabase _videosDatabase;
private readonly IOptions<TelegramOptions> _telegramOptions;
private readonly Scope _scope;
private readonly HttpClient _httpClient;
private readonly IOptions<VideosOptions> _videoOptions;
private readonly VideosCollectorMemory _videosCollectorMemory;

public VideosCollector(IScopedAbstractions scopedAbstractions, VideosDatabase videosDatabase, IOptions<TelegramOptions> telegramOptions, Scope scope, HttpClient httpClient, IOptions<VideosOptions> videoOptions, VideosCollectorMemory videosCollectorMemory)
public VideosCollector(IScopedAbstractions scopedAbstractions, VideosDatabase videosDatabase, IOptions<TelegramOptions> telegramOptions, Scope scope, VideosCollectorMemory videosCollectorMemory)
: base(scopedAbstractions)
{
_videosDatabase = videosDatabase;
_telegramOptions = telegramOptions;
_scope = scope;
_httpClient = httpClient;
_videoOptions = videoOptions;
_videosCollectorMemory = videosCollectorMemory;
}

Expand Down Expand Up @@ -165,12 +159,12 @@ protected override async Task<bool> HandleSync(Update update)
await _videosDatabase.SaveChangesAsync();
}

QueueApi(null, api => Respond(api, update, telegramMedia, alreadyResponded));
QueueApi(null, api => Respond(api, update, alreadyResponded));

return true;
}

private async Task Respond(TelegramBotClient api, Update update, TelegramMedia telegramMedia, bool alreadyResponded)
private async Task Respond(TelegramBotClient api, Update update, bool alreadyResponded)
{
if (!alreadyResponded)
{
Expand All @@ -179,26 +173,5 @@ await api.SetMessageReactionAsync(
update.Message.MessageId,
[new ReactionTypeEmoji("👀")]);
}

//telegramMedia.DownloadedFileName = await Save(api, telegramMedia, false);
//await _videosDatabase.SaveChangesAsync();

//if (telegramMedia.ThumbnailFileId != null)
//{
// telegramMedia.DownloadedThumbnailFileName = await Save(api, telegramMedia, true);
// await _videosDatabase.SaveChangesAsync();
//}
}

//private async Task<string> Save(TelegramBotClient api, TelegramMedia telegramMedia, bool isThumbnail)
//{
// var file = await api.GetFileAsync(isThumbnail ? telegramMedia.ThumbnailFileId! : telegramMedia.FileId);
// Console.WriteLine(file.FilePath);
// var response = await _httpClient.GetAsync($"https://api.telegram.org/file/bot{_telegramOptions.Value.Token}/{file.FilePath}");
// var bytes = await response.Content.ReadAsByteArrayAsync();
// Console.WriteLine(bytes.Length);
// var name = $"{DateTime.Now.Ticks}_{telegramMedia.FileName}";
// await File.WriteAllBytesAsync(Path.Combine(_videoOptions.Value.BasePath, "_uploaded", isThumbnail ? "thumbnails" : ".", name), bytes);
// return name;
//}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static IServiceCollection AddProcessor(this IServiceCollection services,
.AddCommand<GetFileSize>()
.AddCommand<ListAlbums>()
.AddCommand<ShowHandlers>()
.AddCommand<DownloadAll>()

.AddPipelineHandlerInOrder<UpdatesCollector>()
.AddPipelineHandlerInOrder<VideosCollector>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public List<List<Type>> GetCommandsGrid() => [
],
[
typeof(ShowHandlers),
typeof(DownloadAll),
],
[
typeof(ViewChats),
Expand Down

0 comments on commit d32acbd

Please sign in to comment.