Advertisement
OgreVorbis

YouTube thumbs and data downloader V1.5

May 13th, 2021
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.60 KB | None | 0 0
  1. /*
  2.  * Created by SharpDevelop.
  3.  * User: Admin
  4.  * Date: 5/10/2021
  5.  * Time: 8:52 PM
  6.  *
  7.  * This is V1.5 of the YouTube thumbnail and data downloader. It will get thumbs and video ID, title, viewCount, uploadDate and store
  8.  * them to jpgs and an index file called 1A_YouTube.txt. It's meant to be used along with a downloader like mediahuman or youtube-dl
  9.  * to get the extra information that may be needed for a future offline video archival system.
  10.  */
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Drawing;
  14. using System.Windows.Forms;
  15. using System.Web;
  16. using System.Net;
  17. using System.Text;
  18. using System.Linq;
  19. using System.IO;
  20.  
  21. namespace ThumbExtractor
  22. {
  23.     public partial class MainForm : Form
  24.     {
  25.         public WebClient happy;
  26.        
  27.         public struct VidData
  28.         {
  29.             public string title;
  30.             public string ID;
  31.             public UInt32 views;
  32.             public DateTime date;
  33.         }
  34.         public List<VidData> video;
  35.        
  36.         public MainForm()
  37.         {
  38.             InitializeComponent();
  39.             happy = new WebClient();
  40.             video = new List<VidData>();
  41.         }
  42.         void BtnSaveClick(object sender, EventArgs e)
  43.         {
  44.             int count = txtThumb.Lines.Length;
  45.             int cur = 0;
  46.             int i = 0;
  47.            
  48.             if (txtThumb.Text == "")
  49.             {
  50.                 MessageBox.Show("The URL (video list) is empty. Don't cha know?", "Empty", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  51.                 return;
  52.             }
  53.             if (txtPath.Text == "")
  54.             {
  55.                 MessageBox.Show("You need to browse for a directory or type one in first!", "Select Folder Please", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  56.                 return;
  57.             }
  58.             if (!Directory.Exists(txtPath.Text))
  59.             {
  60.                 MessageBox.Show("The directory you put in the box does not exist. Please make it first.", "Folder does not exist", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  61.                 return;
  62.             }
  63.             List<string> fixedLines = new List<string>();
  64.             foreach (string line in txtThumb.Lines)
  65.             {
  66.                 if (line != "")
  67.                 {
  68.                     if (!line.Contains("youtu.be") && !line.Contains("youtube.com"))
  69.                     {
  70.                         MessageBox.Show("There's an invalid URL in there!", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  71.                         return;
  72.                     }
  73.                     if (!line.Contains("/channel/"))
  74.                     {
  75.                         if (line.Contains("&"))
  76.                             fixedLines.Add(line.Substring(0, line.IndexOf('&')));
  77.                         else
  78.                             fixedLines.Add(line);
  79.                     }
  80.                 }
  81.             }
  82.             txtThumb.Lines = fixedLines.ToArray();
  83.             txtThumb.Text = string.Join(Environment.NewLine, txtThumb.Lines.Distinct());
  84.             count = txtThumb.Lines.Length;
  85.             txtThumb.Refresh();
  86.            
  87.             if (!txtPath.Text.EndsWith("\\"))
  88.                 txtPath.Text += "\\";
  89.            
  90.             btnSave.Enabled = false;
  91.             prog.Maximum = count;
  92.             prog.Value = 0;
  93.             video.Clear();
  94.            
  95.             foreach (string vid in txtThumb.Lines)
  96.             {
  97.                 if (chkData.Checked) GetData(vid); else GetThumb(vid);
  98.                 cur++;
  99.                 btnSave.Text = cur.ToString() + " / " + count.ToString();
  100.                 prog.Value = cur;
  101.                 Refresh();
  102.             }
  103.             if (chkData.Checked)
  104.             {
  105.                 video.Sort((x, y) => DateTime.Compare(y.date, x.date));
  106.                 string[] filed = new string[video.Count];
  107.                 for (i = 0; i < filed.Length; i++)
  108.                     filed[i] = video[i].title + "; " + video[i].ID + "; " + video[i].views + "; " + video[i].date.ToShortDateString();
  109.                 File.WriteAllLines(txtPath.Text + "1A_YouTube.txt", filed);
  110.                 btnSave.Text = "Save Data";
  111.             }
  112.             else
  113.                 btnSave.Text = "Save Thumbs";
  114.             btnSave.Enabled = true;
  115.         }
  116.        
  117.         public void GetThumb(string url)
  118.         {
  119.             //another way for later - provides more data: https://www.youtube.com/get_video_info?video_id=
  120.             //but it's much slower
  121.            
  122.             string api = "https://noembed.com/embed?url=" + url;
  123.             string ytCode;
  124.             if (url.Contains("youtu.be"))
  125.                 ytCode = url.Substring(url.LastIndexOf('/') + 1);
  126.             else
  127.                 ytCode = GetArgs(url, "v", '?');
  128.             ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
  129.             string result = happy.DownloadString(api);
  130.             string theTitle = GetElement(ref result, "title");
  131.             string toLoad = "https://i.ytimg.com/vi/" + ytCode + "/hqdefault.jpg";
  132.             string fixedFile = txtPath.Text + ScrubFileName(theTitle) + ".jpg";
  133.             happy.DownloadFile(toLoad, fixedFile);
  134.             picPrev.ImageLocation = fixedFile;
  135.             if (File.Exists(picPrev.ImageLocation))
  136.                 picPrev.Load();
  137.         }
  138.        
  139.         public void GetData(string url)
  140.         {
  141.             string api = "https://www.youtube.com/get_video_info?video_id=";
  142.             string ytCode;
  143.             string scaped;
  144.             if (url.Contains("youtu.be"))
  145.                 ytCode = url.Substring(url.LastIndexOf('/') + 1);
  146.             else
  147.                 ytCode = GetArgs(url, "v", '?');
  148.             api += ytCode;
  149.             ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
  150.             //THE DAMN THING JUST DOESN'T CONNECT SOMETIMES, SO WE JUST HAVE TO KEEP TRYING - ANYBODY UNDERSTAND THE PROBLEM?
  151.             while (true)
  152.             {
  153.                 try { scaped = happy.DownloadString(api); break; }
  154.                 catch { }
  155.             }
  156.             string result = Uri.UnescapeDataString(scaped);
  157.             VidData curVid = new VidData();
  158.             curVid.title = ScrubFileName(GetElement(ref result, "title"));
  159.             curVid.views = Convert.ToUInt32(GetElement(ref result, "viewCount"));
  160.             curVid.date = DateTime.Parse(GetElement(ref result, "publishDate"));
  161.             curVid.ID = ytCode;
  162.             video.Add(curVid);
  163.         }
  164.        
  165.         public string GetElement(ref string data, string toFind)
  166.         {
  167.             string fullFind = "\"" + toFind + "\":\"";
  168.             int loc = data.IndexOf(fullFind);
  169.             loc += fullFind.Length;
  170.             return data.Substring(loc, data.IndexOf('"', loc) - loc).Replace('+', ' ');
  171.         }
  172.  
  173.         public string GetArgs(string args, string key, char query)
  174.         {
  175.             int iqs = args.IndexOf(query);
  176.             return iqs == -1
  177.                 ? string.Empty
  178.                 : HttpUtility.ParseQueryString(iqs < args.Length - 1
  179.                     ? args.Substring(iqs + 1) : string.Empty)[key];
  180.         }
  181.        
  182.         public string ScrubFileName(string value)
  183.         {
  184.            StringBuilder sb = new StringBuilder(value);
  185.            foreach (char item in Path.GetInvalidFileNameChars())
  186.            {
  187.               sb.Replace(item.ToString(), "");
  188.            }
  189.            return sb.ToString();
  190.         }
  191.         void BtnHelpClick(object sender, EventArgs e)
  192.         {
  193.             MessageBox.Show("You paste a list of youtube video links in the box. For getting large batches of links, use the firefox add-on called \"copy selected links\". " +
  194.                             "You then select all the videos (if more than about 50, then do it in segments) and copy them. If there are duplicates or erronious ones, this " +
  195.                             "software should remove them when you press the button. It will also remove any parameters other than the video link/ID. The images will be stored " +
  196.                             "in the directory you select with the browse button and will be named with the title of the video. Save data will save all the videos info like " +
  197.                             "title, date, ID, etc. to a file called 1A_YouTube.txt located in the same directory. This is intended to be used in conjunction with a good downloader " +
  198.                             "like mediahuman or youtube-dl which will get the youtube videos or audio files to go along with this.\nSomeday I may make a full youtube archival " +
  199.                             "indexer. If you have a request, you can send a message on my website.\n\n(C) 2021 DosaidSoft.com V" + Application.ProductVersion, "Help and About", MessageBoxButtons.OK, MessageBoxIcon.Information);
  200.         }
  201.         void BtnBrowseClick(object sender, EventArgs e)
  202.         {
  203.             if (fBrowser.ShowDialog() == DialogResult.OK)
  204.             {
  205.                 if (!fBrowser.SelectedPath.EndsWith("\\"))
  206.                     txtPath.Text = fBrowser.SelectedPath + "\\";
  207.                 else
  208.                     txtPath.Text = fBrowser.SelectedPath;
  209.             }
  210.         }
  211.         void ChkDataCheckedChanged(object sender, EventArgs e)
  212.         {
  213.             btnSave.Text = chkData.Checked ? "Save Data" : "Save Thumbs";
  214.         }
  215.     }
  216. }
  217.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement