Sunday, March 13, 2011

Automatic Wordpress Comment Browser in VB.NET

In this series we'll build software that will automatically comment in the wordpress blogs. I'll explain the ideas and main codes so that you will understand the idea behind automation in the web from client side and explore the unlimited potential behind it!

For development purpose, I don’t comment on real blogs on the internet for obvious reasons. Instead I set up two or three Wordpress blogs on my 'localhost' and play with them.

Lets start

Create a new project in VB.NET

Drag a TabControl in the form and name the TabPages as Input and Browser

In the Input TabPage, drag four text boxes for Name, Email, Website and Comment. Name them txtname, txtemail, txtwebsite, txtcomment respectively. Your Form should look something like this.

Now on the Browser TabPage, drag a WebBrowser Control and and ToolStrip Control. Add items as buttons in ToolStrip Control as shown below. Name them as:

                Fill Name = tsbfillname

                Fill Email = tsbfillemail

                Fill Website = tsbfillwebsite

                Fill Comment = tsbfillcomment

                Textbox = tsburl

                Go = tsbgo

Your application should look like this when running. Oh ya I've also added one textbox and a button 'Go' in the ToolStrip1 Control.

The design part is over. Now let's start coding.

First let's make a simple browser that will work like any other browser. It should be able to display websites that we enter in the address bar.

Double Click the 'Go' button to show its code window and enter
So when we type and address in the textbox and click 'Go', WebBrowser1 will load the page for us. It will work just like IE. But you will notice that when you browse from different pages you will see that the address bar does not update. So let's add one line of code to make it happen.

Double Click WebBrowser1  and in its DocumentCompleted event add the following:
                Private Sub WebBrowser1_DocumentCompleted(…) Handles  WebBrowser1.DocumentCompleted

        tsburl.Text = WebBrowser1.Url.ToString

       End Sub
Now we have a full functional browser. Now let's make it unique.

In your project, add a new Module and name it spinner. We'll add codes related to spinning the texts. Please see my post about article spinning, copy the two functions spin() and extractwords() from there and paste it in this Module. Also don’t forget to import RegularExpressions
                Imports  System.Text.RegularExpressions
Now add another Module and name it 'comment'. We’ll add codes that will automatically comment on Wordpress blogs.

So how are we going to automate form submission process? First of all lets dissect the HTML form. The comment form for Wordpress looks like this
<form action="http://localhost/wp2/wp-comments-post.php" method="post" id="commentform">

<p><input type="text" name="author" id="author" value="sd" size="22" tabindex="1" aria-required='true' />

<label for="author"><small>Name (required)</small></label></p>
<p><input type="text" name="email" id="email" value="" size="22" tabindex="2" aria-required='true' />

<label for="email"><small>Mail (will not be published) (required)</small></label></p>
<p><input type="text" name="url" id="url" value="http://slkf" size="22" tabindex="3" />

<label for="url"><small>Website</small></label></p>
<p><textarea name="comment" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p>
<p><input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" />

<input type="hidden" name="comment_post_ID" value="5" />

Our main concern here is the HTML tag that is used to render the textboxes, buttons etc. We can see here that there are four "input" HTML tag along with their names and one "textarea" HTML tag which also has its name.

Now comes the power of .NET. It provides us some methods that will be handy. Lets look at the code that will automatically fill the Name in the comment form.
Public Sub fillname()

        Dim  elementcoll As HtmlElementCollection

        Dim  element As HtmlElement


            elementcoll =  Form1.WebBrowser1.Document.GetElementsByTagName("input")

            For Each element In elementcoll                 If element.GetAttribute("name").Equals("author") Or _                 element.GetAttribute("name").Equals("name") Then                     element.Focus()                     element.SetAttribute("value", spin(Form1.txtname.Text))                 End If             Next         Catch ex As Exception             MsgBox(ex.Message)         End Try

    End Sub 
What just happened here? First we identify all the elements that have tag "input". This is done by the GetElementByTag method exposed by WebBrowser Control. After identifying all the elements having tag "input" we then check if the element's name attribute is equal to "author" or "name".
<input type="text" name="author" id="author" value="" size="22" tabindex="1" aria-required='true' />
See this element has tag "input" and its name attribute is "author". Now we have identified the place to put our name we set the value of this element by calling SetAttribute(). In the code above we are setting the attribute "value" as spin(Form1.txtname.Text) which returns a name from spinned text given in the input TabPage. If you are confused what is happening here, take a look at my article spinning post and you will understand what spin() function will do. So this is for filling the Name. Filling out Website and Email is also same, you just need to change  the string in Equals method.
For  Each element In  elementcoll

                If  element.GetAttribute("name").Equals("email or url")

For  Comment textarea elementcoll = Form1.WebBrowser1.Document.GetElementsByTagName("textarea")

            For Each element In elementcoll                 
If element.GetAttribute("name").Equals("comment")

For Submit Comment button elementcoll = Form1.WebBrowser1.Document.GetElementsByTagName("input")

        For Each element In elementcoll             
If element.GetAttribute("name").Equals("submit") Then                 element.Focus()                 
End If         
We now have methods that can simulate human input in HTML form. Now it is time to call these methods from ToolStrip Control. I have named these methods as






Call these methods from appropriate buttons in ToolStrip Control. Here is how my application looks like. With these ideas you can make truly awesome and time saving applications.

1 comment:

Anonymous said...

Hi, can you provide the complete source for this? Because im having difficulties compiling it. Thanks

Recent Posts