Advertisement
Guest User

Soundcloud Downloader

a guest
Jun 17th, 2011
1,738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using System.Threading;
  11. using System.Net;
  12. using System.Text.RegularExpressions;
  13.  
  14.  
  15.  
  16.  
  17. namespace SoundcloudDownloader
  18. {
  19.     public partial class Form1 : Form
  20.     {
  21.         MatchCollection links;
  22.         MatchCollection nameOfTracks;
  23.         List<string> listOfLinks = new List<string>();
  24.         List<int> selectedTracks = new List<int>();
  25.  
  26.         public Form1()
  27.         {
  28.             InitializeComponent();
  29.            
  30.         }
  31.  
  32.  
  33.         private void Form1_Load(object sender, EventArgs e)
  34.         {
  35.             CheckForIllegalCrossThreadCalls = false;
  36.             btnDownloadAll.Enabled = false;
  37.             btnDownloadSelected.Enabled = false;
  38.        
  39.         }
  40.  
  41.         private void btnDownload_Click(object sender, EventArgs e)
  42.         {
  43.             if (folderBrowserDialog1.SelectedPath == "")
  44.             {
  45.                 MessageBox.Show("Set Download Folder!");
  46.                
  47.             }
  48.             else if(txtLink.Text == "Enter Soundcloud Link" || txtLink.Text == "")
  49.             {
  50.                 MessageBox.Show("Enter Soundcloud Link.");
  51.             }
  52.             else
  53.             {
  54.                 GetTracks();
  55.                 btnDownloadAll.Enabled = true;
  56.                 btnDownloadSelected.Enabled = true;
  57.             }
  58.         }
  59.  
  60.         private void btnBrowse_Click(object sender, EventArgs e)
  61.         {
  62.            
  63.             folderBrowserDialog1.ShowDialog();
  64.             txtDownloadLocation.Text = folderBrowserDialog1.SelectedPath.ToString();
  65.            
  66.         }
  67.  
  68.         private void btnGetTracks_Click(object sender, EventArgs e)
  69.         {
  70.             MethodInvoker simpleTest = new MethodInvoker(DownloadAllTracks);
  71.            
  72.            
  73.                 simpleTest.BeginInvoke(null, null);          
  74.            
  75.         }
  76.  
  77.  
  78.  
  79.  
  80.  
  81.         void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  82.         {
  83.             double bytesIn = double.Parse(e.BytesReceived.ToString());
  84.             double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
  85.             double percentage = bytesIn / totalBytes * 100;
  86.  
  87.             progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());
  88.         }
  89.  
  90.         void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
  91.  
  92.    {
  93.  
  94.       // MessageBox.Show("Track Downloaded");
  95.            
  96.    }
  97.  
  98.         private void progressBar1_Click(object sender, EventArgs e)
  99.         {
  100.  
  101.         }
  102.  
  103.         void GetTracks()
  104.         {
  105.             // used to build entire input
  106.             StringBuilder sb = new StringBuilder();
  107.  
  108.             // used on each read operation
  109.             byte[] buf = new byte[8192];
  110.  
  111.             // prepare the web page we will be asking for
  112.             HttpWebRequest request = (HttpWebRequest)
  113.                 WebRequest.Create(txtLink.Text.ToString());
  114.  
  115.             // execute the request
  116.             HttpWebResponse response = (HttpWebResponse)
  117.                 request.GetResponse();
  118.  
  119.  
  120.             // we will read data via the response stream
  121.             Stream resStream = response.GetResponseStream();
  122.  
  123.             string tempString = null;
  124.             int count = 0;
  125.  
  126.             do
  127.             {
  128.                 // fill the buffer with data
  129.                 count = resStream.Read(buf, 0, buf.Length);
  130.  
  131.                 // make sure we read some data
  132.                 if (count != 0)
  133.                 {
  134.                     // translate from bytes to ASCII text
  135.                     tempString = Encoding.ASCII.GetString(buf, 0, count);
  136.  
  137.                     // continue building the string
  138.                     sb.Append(tempString);
  139.                 }
  140.             }
  141.             while (count > 0); // any more data to read?
  142.  
  143.             // print out page source
  144.  
  145.             string searchString = sb.ToString();
  146.  
  147.  
  148.             links = Regex.Matches(searchString, "((http:[/][/])(media.soundcloud.com/stream/)([a-z]|[A-Z]|[0-9]|[/.]|[~]|[?]|[_]|[=])*)");
  149.            
  150.             nameOfTracks = Regex.Matches(searchString, "(?<=\"title\":\")[^\"]+");
  151.  
  152.             for (int i = 0; i < links.Count; i++)
  153.             {
  154.  
  155.                 lstTracks.Items.Add(nameOfTracks[i].ToString());
  156.             }
  157.  
  158.         }
  159.  
  160.         private void btnDownloadSelected_Click(object sender, EventArgs e)
  161.         {
  162.             WebClient wc = new WebClient();
  163.            
  164.            
  165.           for(int i = 0; i < selectedTracks.Count;i++)
  166.             {
  167.                 int selectedTrackIndex;
  168.                 selectedTrackIndex = selectedTracks[i];
  169.                 string url;
  170.                 url = links[selectedTrackIndex].ToString();
  171.  
  172.  
  173.  
  174.                 if (!wc.IsBusy)
  175.                 {
  176.  
  177.                     try
  178.                     {
  179.                         wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
  180.                         wc.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
  181.                         wc.DownloadFileAsync(new Uri(url), folderBrowserDialog1.SelectedPath.ToString() + "\\" + nameOfTracks[selectedTrackIndex].ToString() + ".mp3");
  182.                        
  183.                         i++;
  184.                     }
  185.                     catch (Exception ex)
  186.                     {
  187.                         throw ex;
  188.                     }
  189.                 }
  190.             }
  191.          
  192.  
  193.            
  194.         }
  195.  
  196.         private void button1_Click(object sender, EventArgs e)
  197.         {
  198.             Console.WriteLine(lstTracks.CheckedItems.Count);
  199.  
  200.         }
  201.  
  202.         private void btnQuit_Click(object sender, EventArgs e)
  203.         {
  204.             Application.Exit();
  205.         }
  206.  
  207.         private void txtLink_TextChanged(object sender, EventArgs e)
  208.         {
  209.  
  210.         }
  211.  
  212.         void DownloadAllTracks()
  213.         {
  214.  
  215.  
  216.             WebClient wc = new WebClient();
  217.             if (txtDownloadLocation.Text == folderBrowserDialog1.SelectedPath.ToString())
  218.             {
  219.                 for (int i = 0; i < links.Count; )
  220.                 {
  221.                     string url;
  222.                     url = links[i].ToString();
  223.  
  224.  
  225.                     if (!wc.IsBusy)
  226.                     {
  227.  
  228.                         try
  229.                         {
  230.  
  231.  
  232.                             wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
  233.  
  234.                             wc.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
  235.  
  236.  
  237.                             wc.DownloadFileAsync(new Uri(url), folderBrowserDialog1.SelectedPath.ToString() + "\\" + nameOfTracks[i].ToString() + ".mp3");
  238.                          
  239.  
  240.                          
  241.  
  242.                             i++;
  243.                         }
  244.  
  245.                         catch (Exception ex)
  246.                         {
  247.                             throw ex;
  248.                         }
  249.                     }
  250.  
  251.                 }
  252.                
  253.             }
  254.             else
  255.             {
  256.                 MessageBox.Show("No Valid Tracks Found.");
  257.             }
  258.  
  259.         }
  260.  
  261.         private void lstTracks_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
  262.         {
  263.            
  264.            
  265.         }
  266.  
  267.         private void lstTracks_ItemChecked(object sender, ItemCheckedEventArgs e)
  268.         {
  269.             if (e.Item.Checked)
  270.             {
  271.                 selectedTracks.Add(e.Item.Index);
  272.             }
  273.             else
  274.             {
  275.                 selectedTracks.Remove(e.Item.Index);
  276.             }
  277.            
  278.         }
  279.  
  280.         private void btnClear_Click(object sender, EventArgs e)
  281.         {
  282.             lstTracks.Clear();
  283.             links = Regex.Matches("","");
  284.             nameOfTracks = Regex.Matches("", "");
  285.             btnDownloadAll.Enabled = false;
  286.             btnDownloadSelected.Enabled = false;
  287.         }
  288.  
  289.         private void btnAbout_Click(object sender, EventArgs e)
  290.         {
  291.             MessageBox.Show("Soundcloud Downloader V1.0 by Steven Crangle\nEmail: [email protected]");
  292.         }
  293.  
  294.        
  295.  
  296.        
  297.  
  298.  
  299.     }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement