Shrooms

YouTubeCloud

Sep 10th, 2012
931
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.08 KB | None | 0 0
  1. // Comments
  2.  
  3. // The program was coded by Andrew J S.
  4.  
  5. // The program is NOT meant to steal music off youtube, was just a personal experiment.
  6.  
  7. // The video function was removed due to the other website is now experiencing problems and un reliable.
  8.  
  9. // Thanks to youtube-mp3.org as without them this program would not exist.
  10.  
  11. using System;
  12. using System.Collections.Generic;
  13. using System.ComponentModel;
  14. using System.Data;
  15. using System.Drawing;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Windows.Forms;
  19. using System.Runtime.InteropServices;
  20. using System.Diagnostics;
  21. using System.IO;
  22. using System.Net;
  23.  
  24. namespace YouTubeCloud
  25. {
  26.     public partial class Form1 : Form
  27.     {
  28.         int seconds = 10;
  29.  
  30.         string folderPath = "C:\\YouTubeCloud";
  31.         string audioPath = "C:\\YouTubeCloud\\Audio";
  32.  
  33.         void directoryCheck()
  34.         {
  35.             try
  36.             {
  37.                 if (!Directory.Exists(folderPath)) // Check if the directory (folder) does not exist.
  38.                 {
  39.                     Directory.CreateDirectory(folderPath);
  40.                     Directory.CreateDirectory(audioPath);
  41.                 }
  42.             }
  43.             catch { }
  44.         }
  45.  
  46.         void downloadFile()
  47.         {
  48.             try
  49.             {
  50.                 HtmlElement dl_link = webBrowser1.Document.GetElementById("dl_link").All[0]; // Get all the information under the id 'dl_link'.
  51.                 string downloadUrl = dl_link.GetAttribute("href"); // Grab the link which is located in 'href' under dl_link.
  52.                 string songName = webBrowser1.Document.GetElementById("title").InnerText.Substring(7); // Get the title in the element title and remove the text 'Title: '.
  53.  
  54.                 WebClient webClient = new WebClient(); // Used to remove dialog of saving a file
  55.                 webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(downloadComplete);
  56.                 webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloadProgress);
  57.                 webClient.DownloadFileAsync(new Uri(downloadUrl), audioPath + "\\" + songName + ".mp3");
  58.             }
  59.             catch { }
  60.         }
  61.  
  62.         private void downloadProgress(object sender, DownloadProgressChangedEventArgs e)
  63.         {
  64.             progressBar1.Value = e.ProgressPercentage; // Progress bar value is the same as the percentage of the download
  65.         }
  66.  
  67.         private void downloadComplete(object sender, AsyncCompletedEventArgs e)
  68.         {
  69.             textBox1.Clear();
  70.             textBox1.Enabled = true;
  71.             button1.Enabled = true;
  72.             progressBar1.Value = 0;
  73.         }
  74.  
  75.         public Form1()
  76.         {
  77.             InitializeComponent();
  78.             directoryCheck(); // Call the function.
  79.         }
  80.  
  81.         private void Form1_Load(object sender, EventArgs e)
  82.         {
  83.             Process.Start(folderPath); // Open the folder so the user knows where it is located. This line is NOT NEEDED and is just a reminder to the user every time they open the program.
  84.         }
  85.  
  86.         private void button1_Click(object sender, EventArgs e)
  87.         {
  88.             directoryCheck(); // Call the function.
  89.  
  90.             if (textBox1.Text.Trim().Length != 0) // Determine whether the textbox is empty.
  91.             {
  92.                 webBrowser1.Document.GetElementById("youtube-url").SetAttribute("value", textBox1.Text); // Put text into textbox.
  93.                 webBrowser1.Document.GetElementById("submit").InvokeMember("onclick"); // Click button.
  94.  
  95.                 textBox1.Enabled = false;
  96.                 button1.Enabled = false;
  97.  
  98.                 timer1.Start();
  99.             }
  100.         }
  101.  
  102.         private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  103.         {
  104.             MessageBox.Show("Program created by : Andrew");
  105.         }
  106.  
  107.         private void timer1_Tick(object sender, EventArgs e)
  108.         {
  109.             seconds--;
  110.  
  111.             if (seconds == 0)
  112.             {
  113.                 timer1.Stop();
  114.                 seconds = 10;
  115.                 downloadFile();
  116.             }
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment