move to ytldl 50mb limit
This commit is contained in:
parent
e921d15e04
commit
7fa7f1cf43
6 changed files with 506 additions and 108 deletions
|
@ -39,11 +39,11 @@ type RelayCaptionMode =
|
|||
| Unknown
|
||||
|
||||
let RelaySupportedContent (x: Message) =
|
||||
match x.Type with
|
||||
| MessageType.Text -> true
|
||||
| MessageType.Photo -> true
|
||||
| MessageType.Video -> true
|
||||
| _ -> false
|
||||
match x.Type with
|
||||
| MessageType.Text -> true
|
||||
| MessageType.Photo -> true
|
||||
| MessageType.Video -> true
|
||||
| _ -> false
|
||||
|
||||
let RelayCaptionType (command: string) =
|
||||
match command with
|
||||
|
@ -55,48 +55,48 @@ let RelayCaption (name: string, url: string) = $"<a href=\"{url}\">Присла
|
|||
let RelayParseMode = ParseMode.Html;
|
||||
|
||||
let RelayResolveCaption (mode: RelayCaptionMode, username: string, linkUrl: string) =
|
||||
match mode with
|
||||
| WithAuthor -> RelayCaption(username, linkUrl)
|
||||
| _ -> null
|
||||
match mode with
|
||||
| WithAuthor -> RelayCaption(username, linkUrl)
|
||||
| _ -> null
|
||||
|
||||
let public RelayMatch: HandlerRequirements = fun (u, c) ->
|
||||
UpdateIsAMessage u &&
|
||||
FromAdminChat <| (u.Message, c) &&
|
||||
HasReply u.Message &&
|
||||
RelaySupportedContent u.Message.ReplyToMessage &&
|
||||
not (RelayCaptionType u.Message.Text = RelayCaptionMode.Unknown)
|
||||
UpdateIsAMessage u &&
|
||||
FromAdminChat <| (u.Message, c) &&
|
||||
HasReply u.Message &&
|
||||
RelaySupportedContent u.Message.ReplyToMessage &&
|
||||
not (RelayCaptionType u.Message.Text = RelayCaptionMode.Unknown)
|
||||
|
||||
let public RelayHandler: Handler = fun (update, config, tg) ->
|
||||
let reply = update.Message.ReplyToMessage
|
||||
let channelId = config.chanelId
|
||||
let author = $"{reply.From.FirstName} {reply.From.LastName}"
|
||||
let captionMode = RelayCaptionType update.Message.Text
|
||||
let reply = update.Message.ReplyToMessage
|
||||
let channelId = config.chanelId
|
||||
let author = $"{reply.From.FirstName} {reply.From.LastName}"
|
||||
let captionMode = RelayCaptionType update.Message.Text
|
||||
|
||||
let photoMedia = lazy Array.get (ExtractPhotoFromMessage reply) 0
|
||||
let caption = lazy RelayResolveCaption(captionMode, author, config.relayUrl)
|
||||
let photoMedia = lazy Array.get (ExtractPhotoFromMessage reply) 0
|
||||
let caption = lazy RelayResolveCaption(captionMode, author, config.relayUrl)
|
||||
|
||||
match reply.Type with
|
||||
| MessageType.Text -> tg.ForwardMessageAsync(channelId, reply.Chat.Id, reply.MessageId)
|
||||
| MessageType.Photo -> tg.SendPhotoAsync(channelId, photoMedia.Value, caption = caption.Value,
|
||||
parseMode = RelayParseMode)
|
||||
| MessageType.Video -> tg.SendVideoAsync(channelId, reply.Video.FileId, caption = caption.Value,
|
||||
parseMode = RelayParseMode)
|
||||
| _ -> Task.CompletedTask
|
||||
match reply.Type with
|
||||
| MessageType.Text -> tg.ForwardMessageAsync(channelId, reply.Chat.Id, reply.MessageId)
|
||||
| MessageType.Photo -> tg.SendPhotoAsync(channelId, photoMedia.Value, caption = caption.Value,
|
||||
parseMode = RelayParseMode)
|
||||
| MessageType.Video -> tg.SendVideoAsync(channelId, reply.Video.FileId, caption = caption.Value,
|
||||
parseMode = RelayParseMode)
|
||||
| _ -> Task.CompletedTask
|
||||
|
||||
|
||||
// YoutubeDL repost
|
||||
|
||||
let YoutubeRepostMatchCmd = "\\ytdl"
|
||||
let public YoutubeRepostMatch: HandlerRequirements = fun (u, c) ->
|
||||
UpdateIsAMessage u &&
|
||||
FromAdminChat <| (u.Message, c) &&
|
||||
HasText <| u.Message &&
|
||||
u.Message.Text.StartsWith YoutubeRepostMatchCmd &&
|
||||
u.Message.Text.Split(' ').Length = 2
|
||||
UpdateIsAMessage u &&
|
||||
FromAdminChat <| (u.Message, c) &&
|
||||
HasText <| u.Message &&
|
||||
u.Message.Text.StartsWith YoutubeRepostMatchCmd &&
|
||||
u.Message.Text.Split(' ').Length = 2
|
||||
|
||||
let public YoutubeRepostHandler: Handler<IYoutubeDlService> = fun (yt, (u, c, tg)) ->
|
||||
task {
|
||||
let trim (x: string) = x.Trim()
|
||||
let! id = YoutubeRepostMatchCmd |> u.Message.Text.Split |> Array.last |> trim |> yt.AddJob
|
||||
do! tg.SendTextMessageAsync(c.adminChatId, id.ToString()) |> Async.AwaitTask |> Async.Ignore
|
||||
}
|
||||
task {
|
||||
let trim (x: string) = x.Trim()
|
||||
let! id = YoutubeRepostMatchCmd |> u.Message.Text.Split |> Array.last |> trim |> yt.AddJob
|
||||
do! tg.SendTextMessageAsync(c.adminChatId, id.ToString()) |> Async.AwaitTask |> Async.Ignore
|
||||
}
|
|
@ -24,7 +24,12 @@ let youtubeDlService =
|
|||
}
|
||||
|
||||
let tgService =
|
||||
TgService.createService config.chanelId youtubeDlClient botClient
|
||||
TgService.createService {
|
||||
Client = botClient
|
||||
ChannelId = config.chanelId
|
||||
YoutubeDlClient = youtubeDlClient
|
||||
AdminChatId = config.adminChatId
|
||||
}
|
||||
|
||||
YoutubeDlService.createService youtubeDlClient tgService
|
||||
|
||||
|
@ -42,15 +47,15 @@ let (|ObsoleteUpdate|RelayMatchUpdate|YoutubeRepostMatchUpdate|SkipUpdate|) (upd
|
|||
let updateHandle (bc: ITelegramBotClient) (update: Update) (ct: CancellationToken): Task =
|
||||
let tgCtx = (update, config, bc)
|
||||
match update with
|
||||
| RelayMatchUpdate ->
|
||||
| RelayMatchUpdate() ->
|
||||
Logging.logger.Information("RelayMatchUpdate")
|
||||
RelayHandler tgCtx
|
||||
| YoutubeRepostMatchUpdate ->
|
||||
| YoutubeRepostMatchUpdate() ->
|
||||
YoutubeRepostHandler <| (youtubeDlService, tgCtx)
|
||||
| ObsoleteUpdate ->
|
||||
| ObsoleteUpdate() ->
|
||||
Logging.logger.Information("Skipping obsolete update")
|
||||
Task.CompletedTask
|
||||
| SkipUpdate ->
|
||||
| SkipUpdate() ->
|
||||
Logging.logger.Information("Skipping update")
|
||||
Task.CompletedTask
|
||||
|
||||
|
|
|
@ -31,11 +31,6 @@ type YoutubeDlError = {
|
|||
message: string
|
||||
}
|
||||
|
||||
type YoutubeDlClientConfig = {
|
||||
BaseUrl: string
|
||||
Client: HttpClient
|
||||
}
|
||||
|
||||
type CreateJobResult = Result<CreateYoutubeDlJobSuccess, YoutubeDlError>
|
||||
|
||||
type CheckJobResult = Result<YoutubeDlStateResponse, YoutubeDlError>
|
||||
|
@ -47,6 +42,11 @@ type IYoutubeDlClient =
|
|||
abstract member CheckJob: externalId: Guid -> Async<CheckJobResult>
|
||||
abstract member CleanJob: externalId: Guid -> Async<CleanJobResult>
|
||||
|
||||
type YoutubeDlClientConfig = {
|
||||
BaseUrl: string
|
||||
Client: HttpClient
|
||||
}
|
||||
|
||||
[<RequireQualifiedAccess>]
|
||||
module YoutubeDlClient =
|
||||
|
||||
|
@ -192,6 +192,13 @@ module YoutubeDlClient =
|
|||
inbox.Post(CleanJob(externalId, tcs))
|
||||
tcs.Task |> Async.AwaitTask }
|
||||
|
||||
type TgServiceConfig = {
|
||||
Client: ITelegramBotClient
|
||||
ChannelId: ChatId
|
||||
AdminChatId: ChatId
|
||||
YoutubeDlClient: IYoutubeDlClient
|
||||
}
|
||||
|
||||
type ITgService =
|
||||
abstract member PostVideo: url: string * savePath: string * externalId: Guid -> unit
|
||||
|
||||
|
@ -200,7 +207,7 @@ module TgService =
|
|||
type private Msg =
|
||||
| PostVideo of url: string * savePath: string * externalId: Guid
|
||||
|
||||
let private createInbox (channelId: ChatId) (youtubeDlClient: IYoutubeDlClient) (tg: ITelegramBotClient) =
|
||||
let private createInbox (config: TgServiceConfig) =
|
||||
MailboxProcessor.Start(fun inbox ->
|
||||
let rec loop () =
|
||||
async {
|
||||
|
@ -210,20 +217,28 @@ module TgService =
|
|||
try
|
||||
Logging.logger.Information("Reading file path = {path}", savePath)
|
||||
use file = File.OpenRead(savePath)
|
||||
let input = InputOnlineFile(file, savePath)
|
||||
let caption = $"Source: {url}"
|
||||
Logging.logger.Information(
|
||||
"Sending video to channel, channelId = {channelId}, caption = {caption}",
|
||||
channelId,
|
||||
caption)
|
||||
do! tg.SendVideoAsync(channelId, input, caption = caption) |> Async.AwaitTask |> Async.Ignore
|
||||
if (file.Length / 1024L / 1024L) < 50L then
|
||||
let input = InputOnlineFile(file, Path.GetRandomFileName())
|
||||
let caption = $"Source: {url}"
|
||||
Logging.logger.Information(
|
||||
"Sending video to channel, channelId = {channelId}, caption = {caption}",
|
||||
config.ChannelId,
|
||||
caption)
|
||||
do! config.Client.SendVideoAsync(
|
||||
config.ChannelId,
|
||||
input,
|
||||
caption = caption
|
||||
)
|
||||
|> Async.AwaitTask |> Async.Ignore
|
||||
else
|
||||
do! config.Client.SendTextMessageAsync(config.AdminChatId, $"Да блять, видео вышло больше 50мб: {externalId}") |> Async.AwaitTask |> Async.Ignore
|
||||
with ex ->
|
||||
Logging.logger.Error(ex, "Failed to send video")
|
||||
finally
|
||||
Logging.logger.Information("Deleting file path = {path}", savePath)
|
||||
File.Delete(savePath)
|
||||
|
||||
match! youtubeDlClient.CleanJob(externalId) with
|
||||
match! config.YoutubeDlClient.CleanJob(externalId) with
|
||||
| Ok _ -> ()
|
||||
| Error _ -> ()
|
||||
return! loop ()
|
||||
|
@ -231,8 +246,8 @@ module TgService =
|
|||
loop ()
|
||||
)
|
||||
|
||||
let createService chatId youtubeDlClient tg =
|
||||
let inbox = createInbox chatId youtubeDlClient tg
|
||||
let createService config =
|
||||
let inbox = createInbox config
|
||||
{ new ITgService with
|
||||
member this.PostVideo(url, savePath, externalId) =
|
||||
inbox.Post(PostVideo(url, savePath, externalId)) }
|
||||
|
@ -360,6 +375,7 @@ module YoutubeDlService =
|
|||
| Ok x when x.state.Equals("Finished", StringComparison.OrdinalIgnoreCase) ->
|
||||
Logging.logger.Information("Sending post video from job = {job}", externalId)
|
||||
tgService.PostVideo(job.Url, job.SavePath, externalId)
|
||||
postCheck()
|
||||
return! loop jobQueue None
|
||||
|
||||
| Error e ->
|
||||
|
@ -389,4 +405,4 @@ module YoutubeDlService =
|
|||
member this.AddJob(url) =
|
||||
let tcs = TaskCompletionSource<_>()
|
||||
inbox.Post(AddJob(url, tcs))
|
||||
tcs.Task |> Async.AwaitTask}
|
||||
tcs.Task |> Async.AwaitTask }
|
Loading…
Add table
Add a link
Reference in a new issue