Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // v4.0
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- using System.Net;
- using System.IO;
- using System.Collections.Specialized;
- namespace TikTok_Bot
- {
- class Program
- {
- public static void Main(string[] args)
- {
- bool devMode = true;
- //string filePathMain = @"C:\Users\Ben\Desktop\temp\code\TikTok (Bot)\for k\";
- string filePathMain = @"C:\Active Bots\TikTok Bot self\";
- //dev time settings options in initializeSettingsVar()
- if (!devMode)
- filePathMain = AppDomain.CurrentDomain.BaseDirectory;
- settingsStruct settings = new settingsStruct();
- initializeSettingsVar(filePathMain, devMode, ref settings);
- Thread.Sleep(settings.startDelaySeconds * 1000);
- List<tikTokPersonsStruct> tikTokPersonsList = new List<tikTokPersonsStruct>();
- initializeTikTokPersonsList(filePathMain, ref tikTokPersonsList);
- int indexPerson = 0;
- bool newVideoAvailable = false;
- bool anyNewVideoAvailable = false;
- while (true)
- {
- Thread.Sleep(settings.checkFrequencySeconds * 1000);
- anyNewVideoAvailable = false;
- for (indexPerson = 0; indexPerson < tikTokPersonsList.Count; indexPerson++)
- {
- getVideoUrlCurrent(filePathMain, indexPerson, ref tikTokPersonsList, settings);
- newVideoAvailable = checkNewVideo(filePathMain, indexPerson, ref tikTokPersonsList, settings);
- if (newVideoAvailable)
- {
- anyNewVideoAvailable = true;
- createDiscordPost(filePathMain, indexPerson, ref tikTokPersonsList, settings);
- }
- }
- if (!settings.postToDiscord && !anyNewVideoAvailable)
- break;
- }
- }
- public static void createDiscordPost(string filePathMain, int indexPerson, ref List<tikTokPersonsStruct> tikTokPersonsList, settingsStruct settings)
- {
- int indexUrl = 0;
- for (indexUrl = 0; indexUrl < tikTokPersonsList[indexPerson].videoUrlCurrentList.Count; indexUrl++)
- {
- DateTime lastVideoTime = DateTime.Parse(tikTokPersonsList[indexPerson].videoUrlCurrentList[indexUrl].Substring(19, 19));
- DateTime currentTime = DateTime.Parse(tikTokPersonsList[indexPerson].timeStamp);
- int result = DateTime.Compare(lastVideoTime, currentTime);
- if (result == 1)
- break;
- }
- string embedHtml = "";
- string videoTitle = "";
- string videoWatermarkUrl = "";
- int indexTitleStart = 0;
- int indexUrlStart = 0;
- int attempts = 0;
- bool success = false;
- while (!success && attempts < 20)
- {
- try
- {
- Thread.Sleep(settings.webConnectPause * 1000);
- HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("https://www.tiktok.com/embed/" + tikTokPersonsList[indexPerson].videoUrlCurrentList[indexUrl].Substring(0, 19));
- myRequest.Method = "GET";
- myRequest.UserAgent = "Mozilla/5.0";
- myRequest.Timeout = settings.webHtmlDownloadTimeout * 1000;
- myRequest.ReadWriteTimeout = settings.webHtmlDownloadTimeout * 1000;
- WebResponse myResponse = myRequest.GetResponse();
- StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
- embedHtml = sr.ReadToEnd();
- sr.Close();
- myResponse.Close();
- indexTitleStart = 0;
- indexUrlStart = 0;
- for (int i = 0; i < embedHtml.Length; i++)
- {
- if (embedHtml.Substring(i, 11) == ":{\"urls\":[\"")
- indexUrlStart = i + 11;
- if (embedHtml.Substring(i, 13) == "\"],\"videoMeta")
- {
- videoWatermarkUrl = embedHtml.Substring(indexUrlStart, i - indexUrlStart);
- }
- if (embedHtml.Substring(i, 11) == "class=\"text")
- indexTitleStart = i + 13;
- if (embedHtml.Substring(i, 19) == "</div></a><a href=\"" && embedHtml.Substring(i, 21) != "</div></a><a href=\"/@")
- {
- videoTitle = embedHtml.Substring(indexTitleStart, i - indexTitleStart);
- success = true;
- break;
- }
- }
- }
- catch (Exception e)
- {
- writoToErrorLog(filePathMain, e.ToString(), " <489652> ");
- }
- attempts++;
- }
- if (!success)
- return;
- string api2VideoSourceUrl = getApi2VideoSourceUrl(videoWatermarkUrl, filePathMain, tikTokPersonsList, indexPerson, indexUrl, settings);
- if (api2VideoSourceUrl == "")
- return;
- string videoSourceUrl = "";
- attempts = 0;
- bool fileCorrupted = true;
- while (fileCorrupted && attempts < 20)
- {
- try
- {
- Thread.Sleep(settings.webConnectPause * 1000);
- HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(api2VideoSourceUrl);
- myRequest.UserAgent = "Mozilla/5.0";
- myRequest.Timeout = settings.webHtmlDownloadTimeout * 1000;
- myRequest.ReadWriteTimeout = settings.webHtmlDownloadTimeout * 1000;
- using (WebResponse myResponse = (HttpWebResponse)myRequest.GetResponse())
- {
- videoSourceUrl = myResponse.ResponseUri.ToString();
- }
- }
- catch (Exception e)
- {
- writoToErrorLog(filePathMain, e.ToString(), " <845612> ");
- }
- try
- {
- Thread.Sleep(settings.webConnectPause * 1000);
- HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(videoSourceUrl);
- myRequest.Timeout = settings.webVideoDownloadTimeout * 1000;
- myRequest.ReadWriteTimeout = settings.webVideoDownloadTimeout * 1000;
- using (var myResponse = (HttpWebResponse)myRequest.GetResponse())
- {
- using (Stream file = File.Create(filePathMain + @"TikTok Videos\" + tikTokPersonsList[indexPerson].videoUrlCurrentList[indexUrl].Substring(0, 19) + ".mp4"))
- {
- myResponse.GetResponseStream().CopyTo(file);
- }
- }
- long fileSize = new FileInfo(filePathMain + @"TikTok Videos\" + tikTokPersonsList[indexPerson].videoUrlCurrentList[indexUrl].Substring(0, 19) + ".mp4").Length;
- if (fileSize > 50000)
- fileCorrupted = false;
- }
- catch (Exception e)
- {
- writoToErrorLog(filePathMain, e.ToString(), " <987984> ");
- }
- attempts++;
- }
- if (fileCorrupted)
- return;
- success = false;
- string watermarkString = "";
- if (tikTokPersonsList[indexPerson].discordWatermark == "discordWatermark=true")
- watermarkString = "watermark";
- if (settings.postToDiscord == true)
- {
- long fileSize = new FileInfo(filePathMain + @"TikTok Videos\" + watermarkString + tikTokPersonsList[indexPerson].videoUrlCurrentList[indexUrl].Substring(0, 19) + ".mp4").Length;
- if (fileSize < 8000000)
- {
- try
- {
- using (WebClient webClient = new WebClient())
- {
- var valuePairs = new NameValueCollection
- {
- {"content", videoTitle + " <https://m.tiktok.com/v/" + tikTokPersonsList[indexPerson].videoUrlCurrentList[indexUrl].Substring(0, 19) + ">"}
- };
- for (int i = 0; i < tikTokPersonsList[indexPerson].webhooksList.Count; i++)
- {
- Thread.Sleep(settings.webConnectPause * 1000);
- webClient.UploadValues(tikTokPersonsList[indexPerson].webhooksList[i], valuePairs);
- }
- }
- }
- catch (Exception e)
- {
- writoToErrorLog(filePathMain, e.ToString(), " <564123> ");
- }
- try
- {
- using (WebClient webClient = new WebClient())
- {
- for (int i = 0; i < tikTokPersonsList[indexPerson].webhooksList.Count; i++)
- {
- Thread.Sleep(settings.webConnectPause * 1000);
- webClient.UploadFile(tikTokPersonsList[indexPerson].webhooksList[i], filePathMain + @"TikTok Videos\" + watermarkString + tikTokPersonsList[indexPerson].videoUrlCurrentList[indexUrl].Substring(0, 19) + ".mp4");
- }
- }
- success = true;
- }
- catch (Exception e)
- {
- writoToErrorLog(filePathMain, e.ToString(), " <654165> ");
- }
- }
- else
- {
- try
- {
- using (WebClient webClient = new WebClient())
- {
- var valuePairs = new NameValueCollection
- {
- {"content", "https://m.tiktok.com/v/" + tikTokPersonsList[indexPerson].videoUrlCurrentList[indexUrl].Substring(0, 19)}
- };
- for (int i = 0; i < tikTokPersonsList[indexPerson].webhooksList.Count; i++)
- {
- Thread.Sleep(settings.webConnectPause * 1000);
- webClient.UploadValues(tikTokPersonsList[indexPerson].webhooksList[i], valuePairs);
- }
- }
- success = true;
- }
- catch (Exception e)
- {
- writoToErrorLog(filePathMain, e.ToString(), " <987891> ");
- }
- }
- }
- else
- success = true;
- if (!success)
- return;
- watermarkString = "";
- if (tikTokPersonsList[indexPerson].folderWatermark == "folderWatermark=true")
- watermarkString = "watermark";
- try
- {
- try
- {
- if (!File.Exists(tikTokPersonsList[indexPerson].folderLocation + Convert.ToString(tikTokPersonsList[indexPerson].fileNameNumber + 1) + "-.mp4"))
- {
- File.Copy(filePathMain + @"TikTok Videos\" + watermarkString + tikTokPersonsList[indexPerson].videoUrlCurrentList[indexUrl].Substring(0, 19) + ".mp4", tikTokPersonsList[indexPerson].folderLocation + Convert.ToString(tikTokPersonsList[indexPerson].fileNameNumber + 1) + "-.mp4");
- }
- }
- catch
- {
- }
- File.Delete(filePathMain + @"TikTok Videos\" + "watermark" + tikTokPersonsList[indexPerson].videoUrlCurrentList[indexUrl].Substring(0, 19) + ".mp4");
- File.Delete(filePathMain + @"TikTok Videos\" + tikTokPersonsList[indexPerson].videoUrlCurrentList[indexUrl].Substring(0, 19) + ".mp4");
- var tikTokPersonsVar = tikTokPersonsList[indexPerson];
- tikTokPersonsVar.timeStamp = tikTokPersonsList[indexPerson].videoUrlCurrentList[indexUrl].Substring(19, 19);
- tikTokPersonsVar.fileNameNumber++;
- tikTokPersonsList[indexPerson] = tikTokPersonsVar;
- updateTikTokPersonsFile(filePathMain, tikTokPersonsList);
- }
- catch (Exception e)
- {
- writoToErrorLog(filePathMain, e.ToString(), " <894412> ");
- }
- }
- public static void updateTikTokPersonsFile(string filePathMain, List<tikTokPersonsStruct> tikTokPersonsList)
- {
- using (StreamWriter fileWrite = new StreamWriter(filePathMain + "TikTok Persons.txt"))
- {
- for (int indexPerson = 0; indexPerson < tikTokPersonsList.Count; indexPerson++)
- {
- fileWrite.WriteLine(tikTokPersonsList[indexPerson].username);
- fileWrite.WriteLine(tikTokPersonsList[indexPerson].timeStamp);
- fileWrite.WriteLine(tikTokPersonsList[indexPerson].discordWatermark);
- fileWrite.WriteLine(tikTokPersonsList[indexPerson].folderWatermark);
- fileWrite.WriteLine(Convert.ToString(tikTokPersonsList[indexPerson].fileNameNumber));
- fileWrite.WriteLine(tikTokPersonsList[indexPerson].folderLocation);
- for (int i = 0; i < tikTokPersonsList[indexPerson].webhooksList.Count; i++)
- {
- fileWrite.Write(tikTokPersonsList[indexPerson].webhooksList[i] + ", ");
- }
- fileWrite.WriteLine();
- }
- }
- }
- public static string getApi2VideoSourceUrl(string videoWatermarkUrl, string filePathMain, List<tikTokPersonsStruct> tikTokPersonsList, int indexPerson, int indexUrl, settingsStruct settings)
- {
- bool webSuccess = false;
- int webAttempts = 0;
- while (!webSuccess && webAttempts < 20)
- {
- try
- {
- Thread.Sleep(settings.webConnectPause * 1000);
- HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(videoWatermarkUrl);
- myRequest.Timeout = settings.webVideoDownloadTimeout * 1000;
- myRequest.ReadWriteTimeout = settings.webVideoDownloadTimeout * 1000;
- using (var myResponse = (HttpWebResponse)myRequest.GetResponse())
- {
- using (Stream file = File.Create(filePathMain + @"TikTok Videos\watermark" + tikTokPersonsList[indexPerson].videoUrlCurrentList[indexUrl].Substring(0, 19) + ".mp4"))
- {
- myResponse.GetResponseStream().CopyTo(file);
- webSuccess = true;
- }
- }
- }
- catch (Exception e)
- {
- writoToErrorLog(filePathMain, e.ToString(), " <485451> ");
- }
- webAttempts++;
- }
- string api2VideoSourceUrl = "";
- string mp4File = "";
- try
- {
- using (StreamReader sr = new StreamReader(filePathMain + @"TikTok Videos\watermark" + tikTokPersonsList[indexPerson].videoUrlCurrentList[indexUrl].Substring(0, 19) + ".mp4"))
- {
- mp4File = sr.ReadToEnd();
- }
- for (int i = 0; i < mp4File.Length - 35; i++)
- {
- if (mp4File.Substring(i, 4) == "vid:")
- {
- api2VideoSourceUrl = "https://api2.musical.ly/aweme/v1/play/?video_id=" + mp4File.Substring(i + 4, 32) + "&improve_bitrate=1";
- break;
- }
- }
- }
- catch (Exception e)
- {
- writoToErrorLog(filePathMain, e.ToString(), " <515231> ");
- }
- mp4File = "";
- return api2VideoSourceUrl;
- }
- public static bool checkNewVideo(string filePathMain, int indexPerson, ref List<tikTokPersonsStruct> tikTokPersonsList, settingsStruct settings)
- {
- if (tikTokPersonsList[indexPerson].videoUrlCurrentList.Count == 0)
- return false;
- try
- {
- DateTime latestVideoTime = DateTime.Parse(tikTokPersonsList[indexPerson].videoUrlCurrentList[tikTokPersonsList[indexPerson].videoUrlCurrentList.Count - 1].Substring(19, 19));
- DateTime lastPostTime = DateTime.Parse(tikTokPersonsList[indexPerson].timeStamp);
- int result = DateTime.Compare(latestVideoTime, lastPostTime);
- if (result <= 0)
- return false;
- else
- return true;
- }
- catch (Exception e)
- {
- writoToErrorLog(filePathMain, e.ToString(), " <321446> ");
- return false;
- }
- }
- public static void getVideoUrlCurrent(string filePathMain, int indexPerson, ref List<tikTokPersonsStruct> tikTokPersonsList, settingsStruct settings)
- {
- string mainHtml = "";
- try
- {
- Thread.Sleep(settings.webConnectPause * 1000);
- HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("https://www.tiktok.com/@" + tikTokPersonsList[indexPerson].username);
- myRequest.Method = "GET";
- myRequest.UserAgent = "Mozilla/5.0";
- myRequest.Timeout = settings.webHtmlDownloadTimeout * 1000;
- myRequest.ReadWriteTimeout = settings.webHtmlDownloadTimeout * 1000;
- WebResponse myResponse = myRequest.GetResponse();
- StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
- mainHtml = sr.ReadToEnd();
- sr.Close();
- myResponse.Close();
- string time = "";
- tikTokPersonsList[indexPerson].videoUrlCurrentList.Clear();
- for (int i = 0; i < mainHtml.Length - 31 && tikTokPersonsList[indexPerson].videoUrlCurrentList.Count < 9; i++)
- {
- if (mainHtml.Substring(i, 6) == "upload")
- {
- time = mainHtml.Substring(i + 13, 19);
- }
- if (mainHtml.Substring(i, 6) == "embed/")
- {
- tikTokPersonsList[indexPerson].videoUrlCurrentList.Add(mainHtml.Substring(i + 6, 19) + time);
- }
- }
- tikTokPersonsList[indexPerson].videoUrlCurrentList.Reverse();
- }
- catch (Exception e)
- {
- writoToErrorLog(filePathMain, e.ToString(), " <231222> ");
- }
- }
- public static void initializeTikTokPersonsList(string filePathMain, ref List<tikTokPersonsStruct> tikTokPersonsList)
- {
- string[] tikTokPersonsArr = File.ReadAllLines(filePathMain + "TikTok Persons.txt");
- tikTokPersonsList.Clear();
- var tikTokPersonsVar = new tikTokPersonsStruct();
- for (int i = 0; i < tikTokPersonsArr.Length - 1; i += 7)
- {
- tikTokPersonsVar.username = tikTokPersonsArr[i];
- tikTokPersonsVar.timeStamp = tikTokPersonsArr[i + 1];
- tikTokPersonsVar.discordWatermark = tikTokPersonsArr[i + 2];
- tikTokPersonsVar.folderWatermark = tikTokPersonsArr[i + 3];
- tikTokPersonsVar.fileNameNumber = Convert.ToInt32(tikTokPersonsArr[i + 4]);
- tikTokPersonsVar.folderLocation = tikTokPersonsArr[i + 5];
- if (tikTokPersonsVar.folderLocation.Substring(tikTokPersonsVar.folderLocation.Length - 1, 1) != @"\")
- {
- tikTokPersonsVar.folderLocation += @"\";
- }
- tikTokPersonsVar.webhooksList = new List<string>();
- tikTokPersonsVar.webhooksList.Clear();
- tikTokPersonsVar.webhooksList = (parseWebhooksList(tikTokPersonsArr[i + 6]));
- tikTokPersonsVar.videoUrlCurrentList = new List<string>();
- tikTokPersonsVar.videoUrlCurrentList.Clear();
- tikTokPersonsList.Add(tikTokPersonsVar);
- }
- }
- public static void writoToErrorLog(string filePathMain, string errorMessage, string errorID)
- {
- long fileSize = new FileInfo(filePathMain + "error log.txt").Length;
- if (fileSize > 50000000)
- {
- using (StreamWriter fileWrite = new StreamWriter(filePathMain + "error log.txt"))
- {
- fileWrite.Write("");
- }
- }
- using (StreamWriter fileWrite = File.AppendText(filePathMain + "error log.txt"))
- {
- fileWrite.WriteLine(DateTime.UtcNow.ToString("yyyy/MM/dd HH:mm:ss") + errorID + errorMessage);
- }
- }
- public static List<string> parseWebhooksList(string webhooksString)
- {
- List<string> webhooksList = new List<string>();
- webhooksList.Clear();
- string individualWebhook = "";
- for (int i = 0; i < webhooksString.Length - 35; i++)
- {
- if (webhooksString.Substring(i, 36) == "https://discordapp.com/api/webhooks/")
- {
- individualWebhook = webhooksString.Substring(i + 36, 87);
- webhooksList.Add("https://discordapp.com/api/webhooks/" + individualWebhook);
- }
- }
- return webhooksList;
- }
- public static void initializeSettingsVar(string filePathMain, bool devMode, ref settingsStruct settings)
- {
- string[] settingsArr = File.ReadAllLines(filePathMain + "settings.txt");
- settings.startDelaySeconds = Convert.ToInt32(settingsArr[0]);
- settings.checkFrequencySeconds = Convert.ToInt32(settingsArr[1]);
- settings.webConnectPause = Convert.ToInt32(settingsArr[2]);
- settings.webHtmlDownloadTimeout = Convert.ToInt32(settingsArr[3]);
- settings.webVideoDownloadTimeout = Convert.ToInt32(settingsArr[4]);
- if (settingsArr[5] == "postToDiscord=true")
- settings.postToDiscord = true;
- else
- settings.postToDiscord = false;
- if (devMode)
- {
- settings.startDelaySeconds = 0;
- settings.checkFrequencySeconds = 0;
- settings.webConnectPause = 0;
- settings.webHtmlDownloadTimeout = 5;
- settings.webVideoDownloadTimeout = 10;
- }
- }
- public struct settingsStruct
- {
- public int startDelaySeconds;
- public int checkFrequencySeconds;
- public int webConnectPause;
- public int webHtmlDownloadTimeout;
- public int webVideoDownloadTimeout;
- public bool postToDiscord;
- }
- public struct tikTokPersonsStruct
- {
- public string username;
- public string timeStamp;
- public string discordWatermark;
- public string folderWatermark;
- public int fileNameNumber;
- public string folderLocation;
- public List<string> webhooksList;
- public List<string> videoUrlCurrentList;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement