Sign in to follow this  
Blue Eyes

Code source bot travian

3 posts in this topic

Salut, a tout le monde,

j'avais commencé un bot pour travian, mais manque de temps (enfin d'envie) j'ai pas fini le bot

mais je donne tout la partie des connexion et récupération des donné (une veille sauvegarde que j'avais sur le pc)

 

webses.vb :

>Public Class webses
   Private _isPost As Boolean
   Private _htmlDoc As HtmlDocument
   Public Property Cookies() As CookieCollection
       Get
           Return m_Cookies
       End Get
       Set(value As CookieCollection)
           m_Cookies = Value
       End Set
   End Property
   Private m_Cookies As CookieCollection
   Public Property FormElements() As FormElementCollection
       Get
           Return m_FormElements
       End Get
       Set(value As FormElementCollection)
           m_FormElements = Value
       End Set
   End Property
   Private m_FormElements As FormElementCollection
   Public Function [Get](url As String) As String
       _isPost = False
       CreateWebRequestObject().Load(url)
       Return _htmlDoc.DocumentNode.InnerHtml
   End Function

   Public Function Post(url As String) As String
       _isPost = True
       CreateWebRequestObject().Load(url, "POST")
       Return _htmlDoc.DocumentNode.InnerHtml
   End Function
   Private Function CreateWebRequestObject() As HtmlWeb
       Dim web As New HtmlWeb()
       web.UseCookies = True
       web.PreRequest = New HtmlWeb.PreRequestHandler(AddressOf OnPreRequest)
       web.PostResponse = New HtmlWeb.PostResponseHandler(AddressOf OnAfterResponse)
       web.PreHandleDocument = New HtmlWeb.PreHandleDocumentHandler(AddressOf OnPreHandleDocument)
       Return web
   End Function
   Protected Function OnPreRequest(request As HttpWebRequest) As Boolean
       AddCookiesTo(request)
       If _isPost Then
           AddPostDataTo(request)
       End If
       Return True
   End Function
   Protected Sub OnAfterResponse(request As HttpWebRequest, response As HttpWebResponse)
       SaveCookiesFrom(response)
   End Sub
   Protected Sub OnPreHandleDocument(document As HtmlDocument)
       SaveHtmlDocument(document)
   End Sub
   Private Sub AddPostDataTo(request As HttpWebRequest)
       Dim payload As String = FormElements.AssemblePostPayload()
       Dim buff As Byte() = Encoding.UTF8.GetBytes(payload.ToCharArray())
       request.ContentLength = buff.Length
       request.ContentType = "application/x-www-form-urlencoded"
       Dim reqStream As System.IO.Stream = request.GetRequestStream()
       reqStream.Write(buff, 0, buff.Length)
   End Sub
   Private Sub AddCookiesTo(request As HttpWebRequest)
       If Cookies IsNot Nothing AndAlso Cookies.Count > 0 Then
           request.CookieContainer.Add(Cookies)
       End If
   End Sub
   Private Sub SaveCookiesFrom(response As HttpWebResponse)
       If response.Cookies.Count > 0 Then
           If Cookies Is Nothing Then
               Cookies = New CookieCollection()
           End If
           Cookies.Add(response.Cookies)
       End If
   End Sub
   Private Sub SaveHtmlDocument(document As HtmlDocument)
       _htmlDoc = document
       FormElements = New FormElementCollection(_htmlDoc)
   End Sub
End Class

 

Form1.vb

 

>
Imports HtmlAgilityPack
Public Class Form1
   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       Dim b As New webses()
       b.[Get]("http://" & ListBox1.SelectedItem & ".travian.fr/login.php")
       b.FormElements("") = textbox1.text
       b.FormElements("") = textbox2.text
       Dim response As String = b.Post("http://" & ListBox1.SelectedItem & ".travian.fr/login.php")
       If response = True Then
           msgbox("Connecté")
           Dim url As String = "http://" & ListBox1.SelectedItem & ".travian.fr"
           Dim web As New HtmlWeb()
           Dim doc As HtmlDocument = web.Load(url)
           Dim bois = doc.GetElementbyId("stockBarResource1").InnerText.Trim
           Dim argile = doc.GetElementbyId("stockBarResource2").InnerText.Trim
           Dim fer = doc.GetElementbyId("stockBarResource3").InnerText.Trim
           Dim depot = doc.GetElementbyId("stockBarWarehouse").InnerText.Trim
           Label5.Text = depot
           Label6.Text = bois
           Label7.Text = argile
           Label8.Text = fer
       Else
           msgbox("Impossible de se connecté")
       End If
   End Sub
   Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
       web.load("http://" & ListBox1.SelectedItem & ".travian.fr/logout.php")
       Label5.Text = "0"
       Label6.Text = "0"
       Label7.Text = "0"
       Label8.Text = "0"
   End Sub
End Class

 

Il me semble que le code marche mais pas testé de puis des mois (désolé sa fait un temps que j'ai stop la programmation).

 

pour htmlagilitypack

faut prendre la version 1.4.6 ici

http://htmlagilitypack.codeplex.com/

 

 

Je vais faire une conversion avec une webbrowser pour les débutent dans la journée je pense, que ça peut les aidez a début le dev en vb.net (même si c'est mieux de passé par du c#)

Edited by Blue Eyes

Share this post


Link to post
Share on other sites

Voilà la conversion pour utilisé uniquement une webbrowser

 

>Public Class Form1

   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       WebBrowser1.Navigate("http://" & ListBox1.SelectedItem & ".travian.fr/login.php")
       Dim serveur = ListBox1.SelectedItem.ToString
       serveur = serveur.Replace("t", "")
       WebBrowser1.Document.GetElementById("name").SetAttribute("value", (TextBox1.Text))
       WebBrowser1.Document.GetElementById("password").SetAttribute("value", (TextBox2.Text))
       WebBrowser1.Document.GetElementById(serveur).InvokeMember("click")
       Dim bois = WebBrowser1.Document.GetElementById("stockBarResource1").InnerText
       Dim argile = WebBrowser1.Document.GetElementById("stockBarResource2").InnerText
       Dim fer = WebBrowser1.Document.GetElementById("stockBarResource3").InnerText
       Dim depot = WebBrowser1.Document.GetElementById("stockBarWarehouse").InnerText
       Label5.Text = depot
       Label6.Text = bois.Trim
       Label7.Text = argile.Trim
       Label8.Text = fer.Trim
   End Sub

   Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
       WebBrowser1.Navigate("http://" & ListBox1.SelectedItem & ".travian.fr/logout.php")
       Label5.Text = "0"
       Label6.Text = "0"
       Label7.Text = "0"
       Label8.Text = "0"
   End Sub
End Class

 

voilà j’espère que ça peut vous aidez

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this