0

Im trying to build an app for myself to use to automate the downloading of mp3 from youtube. I'm using a web browser to navigate to the site, I was following a tutorial on codeproject, but the project was in visual basic, and I don't know if I converted it properly or what. I understand how to click on buttons within the site with getelementbyID and invokemember. Anyways, the first half of the code works, and it puts the url where its supposed to go, and then presses the button, but when the blue link pops up, the html is different for the link than that of a button, and i'm not sure how to click on the hyperlink through code. I'm decently confident with c# but when it comes to html and network stuff in code, I have no idea where to begin learning. Sorry for being overly complicated, here is my code.

namespace YoutubeMP3Tool
{
 public partial class Form1 : Form
 {
    public Form1()
    {
        InitializeComponent();
    }



    private void button1_Click(object sender, EventArgs e)
    {
        if (textBox1.Text.StartsWith("https://www.youtube.com/watch?v="))
        {
            button2.Enabled = true;
        }
        else
        {
            button2.Enabled = false;
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {

        //following two lines work as intended
        webBrowser1.Document.GetElementById("youtube-url").SetAttribute("value", textBox1.Text);
        webBrowser1.Document.GetElementById("submit").InvokeMember("click");



        //trying to click the download link
        webBrowser1.Document.GetElementById("download").InvokeMember("click");

        button3.Enabled = true;

    }

    private void button3_Click(object sender, EventArgs e)
    {
        //this code crashes the project
        webBrowser1.Document.GetElementById("dl_link").InvokeMember("click");

        HtmlElement download_link = webBrowser1.Document.GetElementById("dl_link");
        HtmlElementCollection links = download_link.GetElementsByTagName("a");
        string link = links[0].GetAttribute("href");
        System.Diagnostics.Process.Start(link);
    }

    private void Form1_Load_1(object sender, EventArgs e)
    {
        webBrowser1.Navigate("http://www.youtube-mp3.org/");

    }


 }
}
cooldudsk
  • 65
  • 1
  • 4
  • 13
  • (1) Please format the wall of text you've written on the top. (2) Can you show us a **snippet** of the html you intend to run this program on? – gideon Jul 29 '15 at 10:37
  • @gideon [Here is a picture Link](http://i.imgur.com/mgdPo5S.png) i screenshotted it because It wouldnt let me copy the whole thing from inspect element, and that does not show up in view page source. I assume this is because the url changes whenever you supply a new link. – cooldudsk Jul 29 '15 at 10:50

2 Answers2

0

WebClient will do this for you. Based on How to download a file from a website in C#

    private void button3_Click(object sender, EventArgs e)
    {

        if (saveFileDialog1.ShowDialog() == DialogResult.OK) { 
            webBrowser1.Document.GetElementById("dl_link").InvokeMember("click");

            HtmlElement download_link = webBrowser1.Document.GetElementById("dl_link");
            HtmlElementCollection links = download_link.GetElementsByTagName("a");
            Uri link = new Uri(links[0].GetAttribute("href"));
            WebClient Client = new WebClient();
            Client.DownloadFileAsync(link, saveFileDialog1.FileName);
        }
    }

Should work for you. You may want to add a SaveFileDialog to allow the user to choose where to save the MP3 to (assuming the other code in you method works, I didn't test it). Using DownloadFileAsync will stop your UI from hanging.

Community
  • 1
  • 1
Phil
  • 3,568
  • 3
  • 34
  • 40
  • Ok, I tried it with this code, and no crashing thankfully. Although with the save file dialog it only generates an empty mp3 file – cooldudsk Jul 29 '15 at 11:25
  • Try it with `Client.DownloadFile()` instead of `Client.DownloadFileAsync()`. It does take a short while to download the file, and you may want to add a watch to `link` to make sure it's populated and populated with the correct address. – Phil Jul 29 '15 at 11:54
  • without the async It crashes giving 403 forbidden. So the issue is with the link itself then? – cooldudsk Jul 29 '15 at 21:20
  • 403 is on the server side, it might be that they've blocked you because you're scraping their site. Can you catch the error and post it? – Phil Jul 30 '15 at 07:58
0

From your screenshot update, the page has no element with the id you're looking for. Your code is looking for an element with an id='download'

Try

webBrowser1.Document.GetElementById("dl_link").InvokeMember("click");

This should, I believe, click the <div> and this click the hyperlink.

gideon
  • 19,329
  • 11
  • 72
  • 113