-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
downloading the items locally to the drive
- Loading branch information
1 parent
769b1cf
commit d32acbd
Showing
9 changed files
with
845 additions
and
31 deletions.
There are no files selected for viewing
707 changes: 707 additions & 0 deletions
707
...eShelf.Videos.Database/Migrations/20240907124519_TelegramMediaItemsDownloaded.Designer.cs
Large diffs are not rendered by default.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
...Videos/OneShelf.Videos.Database/Migrations/20240907124519_TelegramMediaItemsDownloaded.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
OneShelf.Videos/OneShelf.Videos.Telegram.Processor/Commands/DownloadAll.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters