Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Collections.Generic
- Imports System.Linq
- Imports System.Text
- Imports System.Net
- Imports System.IO
- Imports System.Text.RegularExpressions
- Imports System.Windows.Forms
- Namespace Tumblr_Bot
- Class Tumblr
- Private Property s0() As CookieContainer
- Get
- Return m_s0
- End Get
- Set
- m_s0 = Value
- End Set
- End Property
- Private m_s0 As CookieContainer
- Public Property USERNAME() As String
- Get
- Return m_USERNAME
- End Get
- Set
- m_USERNAME = Value
- End Set
- End Property
- Private m_USERNAME As String
- Public Property PASSWORD() As String
- Get
- Return m_PASSWORD
- End Get
- Set
- m_PASSWORD = Value
- End Set
- End Property
- Private m_PASSWORD As String
- Private Property form_key() As String
- Get
- Return m_form_key
- End Get
- Set
- m_form_key = Value
- End Set
- End Property
- Private m_form_key As String
- Public Sub New()
- s0 = New CookieContainer()
- ServicePointManager.Expect100Continue = False
- End Sub
- Private Function GetRegexValue(name As String, src As String) As String
- Try
- Return New Regex((Convert.ToString("name=""") & name) + """ value=""(.*?)""").Match(src).Groups(1).Value
- Catch
- Return Nothing
- End Try
- End Function
- Public Function Login() As Boolean
- Try
- Dim src As String = SimpleGET("https://www.tumblr.com/login")
- form_key = GetRegexValue("form_key", src)
- Dim postD As String = String.Format("user%5Bemail%5D={0}&user%5Bpassword%5D={1}&tumblelog%5Bname%5D=&user%5Bage%5D=&context=home_signup&version=STANDARD&follow=&http_referer=https%3A%2F%2Fwww.tumblr.com%2F&form_key={2}&seen_suggestion=0&used_suggestion=0&used_auto_suggestion=0&about_tumblr_slide=&random_username_suggestions={3}", USERNAME, PASSWORD, form_key, GetRegexValue("random_username_suggestions", src).Replace(""", """"))
- Dim postB As Byte() = Encoding.ASCII.GetBytes(postD)
- Dim req As HttpWebRequest = DirectCast(HttpWebRequest.Create("https://www.tumblr.com/login"), HttpWebRequest)
- req.Method = "POST"
- req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
- req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"
- req.CookieContainer = s0
- req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate")
- req.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
- req.Referer = "https://www.tumblr.com/login"
- req.ContentType = "application/x-www-form-urlencoded"
- req.ContentLength = postB.Length
- req.AllowAutoRedirect = False
- Using ss As Stream = req.GetRequestStream()
- ss.Write(postB, 0, postB.Length)
- End Using
- Using resp As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
- Return (resp.StatusCode = HttpStatusCode.Redirect)
- End Using
- Catch
- MessageBox.Show("Could not login to user, please try again with other credentials", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
- Return False
- End Try
- End Function
- Public Function SimpleGET(link As String) As String
- Try
- Dim req As HttpWebRequest = DirectCast(HttpWebRequest.Create(link), HttpWebRequest)
- req.Method = "GET"
- req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
- req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"
- req.CookieContainer = s0
- req.Referer = link
- req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate")
- req.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
- Using resp As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
- Using sr As New StreamReader(resp.GetResponseStream())
- Return sr.ReadToEnd()
- End Using
- End Using
- Catch
- Return "Empty Request"
- End Try
- End Function
- #Region "Scraping"
- Private before As Integer = 0
- Private Function Query(q As String, page As Integer, num_posts As Integer, key As String, posts As Boolean) As List(Of KeyValuePair(Of String, String))
- Try
- Dim qDictionary As New List(Of KeyValuePair(Of String, String))()
- Dim postB As Byte() = Encoding.ASCII.GetBytes(String.Format("q={0}&sort=top&post_view=masonry&blogs_before=9&num_blogs_shown=8&num_posts_shown={1}&before={2}&blog_page=1&post_page={3}&filter_nsfw=true&filter_post_type=&next_ad_offset=0&ad_placement_id=0&more_posts=true", q, num_posts, before, page))
- Dim req As HttpWebRequest = DirectCast(HttpWebRequest.Create((Convert.ToString("https://www.tumblr.com/search/") & q) + "/post_page/" + page), HttpWebRequest)
- req.Method = "POST"
- req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"
- req.Accept = "application/json, text/javascript, */*; q=0.01"
- req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate")
- req.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
- req.Referer = Convert.ToString("https://www.tumblr.com/search/") & q
- req.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"
- req.ContentLength = postB.Length
- req.Headers.Add("X-Requested-With", "XMLHttpRequest")
- req.Headers.Add("X-tumblr-form-key", key)
- Using ss As Stream = req.GetRequestStream()
- ss.Write(postB, 0, postB.Length)
- End Using
- Using resp As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
- Using sr As New StreamReader(resp.GetResponseStream())
- Dim queryRegex As New Regex("data-post-id=" + Regex.Escape("\") + "u0027(.*?)" + Regex.Escape("\") + "u0027 data-tumblelog-name=" + Regex.Escape("\") + "u0027(.*?)" + Regex.Escape("\") + "u0027 data-reblog-key=" + Regex.Escape("\") + "u0027(.*?)" + Regex.Escape("\") + "u0027")
- For Each m As Match In queryRegex.Matches(sr.ReadToEnd())
- If posts Then
- qDictionary.Add(New KeyValuePair(Of String, String)(m.Groups(1).Value, m.Groups(3).Value))
- Else
- qDictionary.Add(New KeyValuePair(Of String, String)(m.Groups(2).Value, "FOLLOW_SOURCE_DISCOVER_POSTS_GRID"))
- End If
- Next
- End Using
- before = Convert.ToInt32(resp.GetResponseHeader("X-next-offset"))
- End Using
- Return qDictionary
- Catch
- Return Nothing
- End Try
- End Function
- Public Function Follow(p As KeyValuePair(Of String, String)) As Boolean
- Try
- Dim postB As Byte() = Encoding.ASCII.GetBytes(String.Format("data%5Btumblelog%5D={0}&data%5Bsource%5D={1}", p.Key, p.Value))
- Dim req As HttpWebRequest = DirectCast(HttpWebRequest.Create("https://www.tumblr.com/svc/follow"), HttpWebRequest)
- req.Accept = "*/*"
- req.CookieContainer = s0
- req.Method = "POST"
- req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"
- req.Headers.Add("X-Requested-With", "XMLHttpRequest")
- req.ContentLength = postB.Length
- req.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"
- req.Headers.Add("X-tumblr-form-key", form_key)
- req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate")
- req.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
- Using ss As Stream = req.GetRequestStream()
- ss.Write(postB, 0, postB.Length)
- End Using
- Using resp As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
- Using sr As New StreamReader(resp.GetResponseStream())
- Return sr.ReadToEnd().Contains("liked"":true")
- End Using
- End Using
- Catch
- MessageBox.Show("Could not follow a user due to an unknown error.", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
- Return False
- End Try
- End Function
- Public Function [Like](p As KeyValuePair(Of String, String)) As Boolean
- Try
- Dim postB As Byte() = Encoding.ASCII.GetBytes(String.Format("data%5Bid%5D={0}&data%5Broot_id%5D={0}&data%5Bkey%5D={1}&data%5Bmethod%5D=mouse&data%5Bsource%5D=LIKE_SOURCE_SEARCH_RESULTS_PAGE&data%5Bplacement_id%5D=false&data%5Bis_recommended%5D=", p.Key, p.Value))
- Dim req As HttpWebRequest = DirectCast(HttpWebRequest.Create("https://www.tumblr.com/svc/like"), HttpWebRequest)
- req.Accept = "*/*"
- req.CookieContainer = s0
- req.Method = "POST"
- req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"
- req.Headers.Add("X-Requested-With", "XMLHttpRequest")
- req.ContentLength = postB.Length
- req.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"
- req.Headers.Add("X-tumblr-form-key", form_key)
- req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate")
- req.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
- Using ss As Stream = req.GetRequestStream()
- ss.Write(postB, 0, postB.Length)
- End Using
- Using resp As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
- Using sr As New StreamReader(resp.GetResponseStream())
- Return sr.ReadToEnd().Contains("liked"":true")
- End Using
- End Using
- Catch
- MessageBox.Show("Could not Like a user due to an unknown error.", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
- Return False
- End Try
- End Function
- Public Function Scrape(q As String, max As Integer, posts As Boolean) As List(Of KeyValuePair(Of String, String))
- Try
- Dim mDictionary As New List(Of KeyValuePair(Of String, String))()
- Dim mainSearch As New Regex("data-post-id='(.*?)' data-tumblelog-name='(.*?)' data-reblog-key='(.*?)'")
- Dim s1 As String = SimpleGET(Convert.ToString("https://www.tumblr.com/search/") & q)
- For Each m As Match In mainSearch.Matches(s1)
- If posts Then
- mDictionary.Add(New KeyValuePair(Of String, String)(m.Groups(1).Value, m.Groups(3).Value))
- Else
- mDictionary.Add(New KeyValuePair(Of String, String)(m.Groups(2).Value, "FOLLOW_SOURCE_DISCOVER_POSTS_GRID"))
- End If
- Next
- Dim page As Integer = 2
- Dim num_posts As Integer = mDictionary.Count
- ' Console.WriteLine("\nD: " + mDictionary.Count);
- form_key = GetRegexValue("form_key", s1)
- ' Console.WriteLine("D: "+mDictionary.Count+"\nQ:"+Query(q, 1, 0, 0, GetRegexValue("form_key", s1)).Count);
- While max >= mDictionary.Count
- Dim temp As List(Of KeyValuePair(Of String, String)) = Query(q, page, num_posts, form_key, posts).Distinct().ToList()
- mDictionary.AddRange(temp)
- page += 1
- num_posts += temp.Count
- mDictionary = mDictionary.Distinct().ToList()
- End While
- ' return mDictionary.Take(0, max);
- Return mDictionary.GetRange(0, max)
- Catch
- Return Nothing
- End Try
- End Function
- #End Region
- End Class
- End Namespace
- '=======================================================
- 'Service provided by Telerik (www.telerik.com)
- 'Conversion powered by NRefactory.
- 'Twitter: @telerik
- 'Facebook: facebook.com/telerik
- '=======================================================
Add Comment
Please, Sign In to add comment