Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Created by SharpDevelop.
- * User: Admin
- * Date: 5/10/2021
- * Time: 8:52 PM
- *
- * This is V1.5 of the YouTube thumbnail and data downloader. It will get thumbs and video ID, title, viewCount, uploadDate and store
- * 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
- * to get the extra information that may be needed for a future offline video archival system.
- */
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Windows.Forms;
- using System.Web;
- using System.Net;
- using System.Text;
- using System.Linq;
- using System.IO;
- namespace ThumbExtractor
- {
- public partial class MainForm : Form
- {
- public WebClient happy;
- public struct VidData
- {
- public string title;
- public string ID;
- public UInt32 views;
- public DateTime date;
- }
- public List<VidData> video;
- public MainForm()
- {
- InitializeComponent();
- happy = new WebClient();
- video = new List<VidData>();
- }
- void BtnSaveClick(object sender, EventArgs e)
- {
- int count = txtThumb.Lines.Length;
- int cur = 0;
- int i = 0;
- if (txtThumb.Text == "")
- {
- MessageBox.Show("The URL (video list) is empty. Don't cha know?", "Empty", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- return;
- }
- if (txtPath.Text == "")
- {
- MessageBox.Show("You need to browse for a directory or type one in first!", "Select Folder Please", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- return;
- }
- if (!Directory.Exists(txtPath.Text))
- {
- MessageBox.Show("The directory you put in the box does not exist. Please make it first.", "Folder does not exist", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- return;
- }
- List<string> fixedLines = new List<string>();
- foreach (string line in txtThumb.Lines)
- {
- if (line != "")
- {
- if (!line.Contains("youtu.be") && !line.Contains("youtube.com"))
- {
- MessageBox.Show("There's an invalid URL in there!", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- return;
- }
- if (!line.Contains("/channel/"))
- {
- if (line.Contains("&"))
- fixedLines.Add(line.Substring(0, line.IndexOf('&')));
- else
- fixedLines.Add(line);
- }
- }
- }
- txtThumb.Lines = fixedLines.ToArray();
- txtThumb.Text = string.Join(Environment.NewLine, txtThumb.Lines.Distinct());
- count = txtThumb.Lines.Length;
- txtThumb.Refresh();
- if (!txtPath.Text.EndsWith("\\"))
- txtPath.Text += "\\";
- btnSave.Enabled = false;
- prog.Maximum = count;
- prog.Value = 0;
- video.Clear();
- foreach (string vid in txtThumb.Lines)
- {
- if (chkData.Checked) GetData(vid); else GetThumb(vid);
- cur++;
- btnSave.Text = cur.ToString() + " / " + count.ToString();
- prog.Value = cur;
- Refresh();
- }
- if (chkData.Checked)
- {
- video.Sort((x, y) => DateTime.Compare(y.date, x.date));
- string[] filed = new string[video.Count];
- for (i = 0; i < filed.Length; i++)
- filed[i] = video[i].title + "; " + video[i].ID + "; " + video[i].views + "; " + video[i].date.ToShortDateString();
- File.WriteAllLines(txtPath.Text + "1A_YouTube.txt", filed);
- btnSave.Text = "Save Data";
- }
- else
- btnSave.Text = "Save Thumbs";
- btnSave.Enabled = true;
- }
- public void GetThumb(string url)
- {
- //another way for later - provides more data: https://www.youtube.com/get_video_info?video_id=
- //but it's much slower
- string api = "https://noembed.com/embed?url=" + url;
- string ytCode;
- if (url.Contains("youtu.be"))
- ytCode = url.Substring(url.LastIndexOf('/') + 1);
- else
- ytCode = GetArgs(url, "v", '?');
- ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
- string result = happy.DownloadString(api);
- string theTitle = GetElement(ref result, "title");
- string toLoad = "https://i.ytimg.com/vi/" + ytCode + "/hqdefault.jpg";
- string fixedFile = txtPath.Text + ScrubFileName(theTitle) + ".jpg";
- happy.DownloadFile(toLoad, fixedFile);
- picPrev.ImageLocation = fixedFile;
- if (File.Exists(picPrev.ImageLocation))
- picPrev.Load();
- }
- public void GetData(string url)
- {
- string api = "https://www.youtube.com/get_video_info?video_id=";
- string ytCode;
- string scaped;
- if (url.Contains("youtu.be"))
- ytCode = url.Substring(url.LastIndexOf('/') + 1);
- else
- ytCode = GetArgs(url, "v", '?');
- api += ytCode;
- ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
- //THE DAMN THING JUST DOESN'T CONNECT SOMETIMES, SO WE JUST HAVE TO KEEP TRYING - ANYBODY UNDERSTAND THE PROBLEM?
- while (true)
- {
- try { scaped = happy.DownloadString(api); break; }
- catch { }
- }
- string result = Uri.UnescapeDataString(scaped);
- VidData curVid = new VidData();
- curVid.title = ScrubFileName(GetElement(ref result, "title"));
- curVid.views = Convert.ToUInt32(GetElement(ref result, "viewCount"));
- curVid.date = DateTime.Parse(GetElement(ref result, "publishDate"));
- curVid.ID = ytCode;
- video.Add(curVid);
- }
- public string GetElement(ref string data, string toFind)
- {
- string fullFind = "\"" + toFind + "\":\"";
- int loc = data.IndexOf(fullFind);
- loc += fullFind.Length;
- return data.Substring(loc, data.IndexOf('"', loc) - loc).Replace('+', ' ');
- }
- public string GetArgs(string args, string key, char query)
- {
- int iqs = args.IndexOf(query);
- return iqs == -1
- ? string.Empty
- : HttpUtility.ParseQueryString(iqs < args.Length - 1
- ? args.Substring(iqs + 1) : string.Empty)[key];
- }
- public string ScrubFileName(string value)
- {
- StringBuilder sb = new StringBuilder(value);
- foreach (char item in Path.GetInvalidFileNameChars())
- {
- sb.Replace(item.ToString(), "");
- }
- return sb.ToString();
- }
- void BtnHelpClick(object sender, EventArgs e)
- {
- 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\". " +
- "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 " +
- "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 " +
- "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 " +
- "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 " +
- "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 " +
- "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);
- }
- void BtnBrowseClick(object sender, EventArgs e)
- {
- if (fBrowser.ShowDialog() == DialogResult.OK)
- {
- if (!fBrowser.SelectedPath.EndsWith("\\"))
- txtPath.Text = fBrowser.SelectedPath + "\\";
- else
- txtPath.Text = fBrowser.SelectedPath;
- }
- }
- void ChkDataCheckedChanged(object sender, EventArgs e)
- {
- btnSave.Text = chkData.Checked ? "Save Data" : "Save Thumbs";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement