Updated video download code
continuous-integration/drone/push Build was killed Details

This commit is contained in:
Chris Plaatjes 2023-02-14 00:35:28 -05:00
parent 724e48da04
commit ae4c436a1a
2 changed files with 53 additions and 3 deletions

View File

@ -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<id>[\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}.");

View File

@ -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;
}
}
}