From ae4c436a1a9042d6fcba99c56ad9916653439b01 Mon Sep 17 00:00:00 2001 From: Chris Plaatjes Date: Tue, 14 Feb 2023 00:35:28 -0500 Subject: [PATCH] Updated video download code --- Program.cs | 19 +++++++++++++++++-- VidDownload.cs | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/Program.cs b/Program.cs index 1749d3e..b41e853 100644 --- a/Program.cs +++ b/Program.cs @@ -60,12 +60,27 @@ namespace TeleTok return; var chatId = message.Chat.Id; + var messageText = update.Message.Text; - bool isUri = Uri.IsWellFormedUriString(message.ToString(), UriKind.RelativeOrAbsolute); + string videoPath; + // Checks if the text contains a valid URL + bool isUri = Uri.IsWellFormedUriString(messageText, UriKind.RelativeOrAbsolute); + + // Passes the url along to the video downloader if it is valid AND a tiktok link if (isUri) { - Regex isTikTok = new Regex(@"(?x)(http(s)?:\/\/)?(?:www|m)\.(?:tiktok.com)\/(?:v|embed|trending)(?:\/)?(?:\?shareId=)?(?P[\da-z]+)", RegexOptions.Singleline); + if(messageText.Contains("tiktok.com")) + { + videoPath = VidDownload.TikTokUrl(messageText); + + Message ttVideo = await botClient.SendVideoAsync( + chatId: chatId, + videoPath: videoPath, + supportsStreaming: true, + cancellationToken: cancellationToken + ); + } } Console.WriteLine($"Received a '{messageText}' message in chat {chatId}."); diff --git a/VidDownload.cs b/VidDownload.cs index 291eff0..f5aa893 100644 --- a/VidDownload.cs +++ b/VidDownload.cs @@ -5,6 +5,41 @@ namespace TeleTok { public class VidDownload { - + static void TikTokURL(string videourl) + { + string url = videourl; + + if(url.Contains("vm.tiktok.com")) + { + url = UnshortenUrl(url); + } + + var proc = new Process + { + // Function downloads tiktok urls passed from the listener + StartInfo = new ProcessStartInfo + { + FileName = "/bin/bash", + Arguments = "-c \"tiktok-scraper --filepath /app/videos -d " + videourl + "\"", + UseShellExecute = false, + RedirectStandardOutput = true, + CreateNoWindow = true + } + }; + + proc.Start(); + proc.WaitForExit(); + + } + + static string UnshortenUrl(string videourl) + { + HttpWebRequest req = (HttpWebRequest)WebRequest.Create(videourl); + req.AllowAutoRedirect = false; + var resp = req.GetResponse(); + string realUrl = resp.Headers["Location"]; + + return realUrl; + } } } \ No newline at end of file