Guest User

Untitled

a guest
Jun 20th, 2017
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 11.11 KB | None | 0 0
  1.  
  2. Imports System.Collections.Generic
  3. Imports System.Linq
  4. Imports System.Text
  5. Imports System.Net
  6. Imports System.IO
  7. Imports System.Text.RegularExpressions
  8. Imports System.Windows.Forms
  9.  
  10. Namespace Tumblr_Bot
  11.     Class Tumblr
  12.         Private Property s0() As CookieContainer
  13.             Get
  14.                 Return m_s0
  15.             End Get
  16.             Set
  17.                 m_s0 = Value
  18.             End Set
  19.         End Property
  20.         Private m_s0 As CookieContainer
  21.         Public Property USERNAME() As String
  22.             Get
  23.                 Return m_USERNAME
  24.             End Get
  25.             Set
  26.                 m_USERNAME = Value
  27.             End Set
  28.         End Property
  29.         Private m_USERNAME As String
  30.         Public Property PASSWORD() As String
  31.             Get
  32.                 Return m_PASSWORD
  33.             End Get
  34.             Set
  35.                 m_PASSWORD = Value
  36.             End Set
  37.         End Property
  38.         Private m_PASSWORD As String
  39.         Private Property form_key() As String
  40.             Get
  41.                 Return m_form_key
  42.             End Get
  43.             Set
  44.                 m_form_key = Value
  45.             End Set
  46.         End Property
  47.         Private m_form_key As String
  48.         Public Sub New()
  49.             s0 = New CookieContainer()
  50.             ServicePointManager.Expect100Continue = False
  51.         End Sub
  52.         Private Function GetRegexValue(name As String, src As String) As String
  53.             Try
  54.                 Return New Regex((Convert.ToString("name=""") & name) + """ value=""(.*?)""").Match(src).Groups(1).Value
  55.             Catch
  56.                 Return Nothing
  57.             End Try
  58.         End Function
  59.         Public Function Login() As Boolean
  60.             Try
  61.                 Dim src As String = SimpleGET("https://www.tumblr.com/login")
  62.                 form_key = GetRegexValue("form_key", src)
  63.  
  64.                 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(""", """"))
  65.  
  66.                 Dim postB As Byte() = Encoding.ASCII.GetBytes(postD)
  67.                 Dim req As HttpWebRequest = DirectCast(HttpWebRequest.Create("https://www.tumblr.com/login"), HttpWebRequest)
  68.                 req.Method = "POST"
  69.                 req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
  70.                 req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"
  71.                 req.CookieContainer = s0
  72.                 req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate")
  73.                 req.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
  74.  
  75.                 req.Referer = "https://www.tumblr.com/login"
  76.                 req.ContentType = "application/x-www-form-urlencoded"
  77.                 req.ContentLength = postB.Length
  78.                 req.AllowAutoRedirect = False
  79.  
  80.                 Using ss As Stream = req.GetRequestStream()
  81.                     ss.Write(postB, 0, postB.Length)
  82.                 End Using
  83.  
  84.                 Using resp As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
  85.                     Return (resp.StatusCode = HttpStatusCode.Redirect)
  86.                 End Using
  87.             Catch
  88.                 MessageBox.Show("Could not login to user, please try again with other credentials", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
  89.                 Return False
  90.             End Try
  91.         End Function
  92.  
  93.         Public Function SimpleGET(link As String) As String
  94.             Try
  95.                 Dim req As HttpWebRequest = DirectCast(HttpWebRequest.Create(link), HttpWebRequest)
  96.                 req.Method = "GET"
  97.                 req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
  98.                 req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"
  99.                 req.CookieContainer = s0
  100.                 req.Referer = link
  101.                 req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate")
  102.                 req.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
  103.  
  104.  
  105.                 Using resp As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
  106.                     Using sr As New StreamReader(resp.GetResponseStream())
  107.                         Return sr.ReadToEnd()
  108.                     End Using
  109.                 End Using
  110.             Catch
  111.                 Return "Empty Request"
  112.             End Try
  113.         End Function
  114.  
  115.         #Region "Scraping"
  116.         Private before As Integer = 0
  117.         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))
  118.             Try
  119.                 Dim qDictionary As New List(Of KeyValuePair(Of String, String))()
  120.                 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))
  121.                 Dim req As HttpWebRequest = DirectCast(HttpWebRequest.Create((Convert.ToString("https://www.tumblr.com/search/") & q) + "/post_page/" + page), HttpWebRequest)
  122.                 req.Method = "POST"
  123.                 req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"
  124.                 req.Accept = "application/json, text/javascript, */*; q=0.01"
  125.                 req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate")
  126.                 req.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
  127.                 req.Referer = Convert.ToString("https://www.tumblr.com/search/") & q
  128.                 req.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"
  129.                 req.ContentLength = postB.Length
  130.                 req.Headers.Add("X-Requested-With", "XMLHttpRequest")
  131.                 req.Headers.Add("X-tumblr-form-key", key)
  132.  
  133.                 Using ss As Stream = req.GetRequestStream()
  134.                     ss.Write(postB, 0, postB.Length)
  135.                 End Using
  136.  
  137.                 Using resp As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
  138.                     Using sr As New StreamReader(resp.GetResponseStream())
  139.                         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")
  140.                         For Each m As Match In queryRegex.Matches(sr.ReadToEnd())
  141.                             If posts Then
  142.                                 qDictionary.Add(New KeyValuePair(Of String, String)(m.Groups(1).Value, m.Groups(3).Value))
  143.                             Else
  144.                                 qDictionary.Add(New KeyValuePair(Of String, String)(m.Groups(2).Value, "FOLLOW_SOURCE_DISCOVER_POSTS_GRID"))
  145.                             End If
  146.                         Next
  147.                     End Using
  148.  
  149.                     before = Convert.ToInt32(resp.GetResponseHeader("X-next-offset"))
  150.                 End Using
  151.                 Return qDictionary
  152.             Catch
  153.                 Return Nothing
  154.             End Try
  155.         End Function
  156.         Public Function Follow(p As KeyValuePair(Of String, String)) As Boolean
  157.             Try
  158.                 Dim postB As Byte() = Encoding.ASCII.GetBytes(String.Format("data%5Btumblelog%5D={0}&data%5Bsource%5D={1}", p.Key, p.Value))
  159.  
  160.                 Dim req As HttpWebRequest = DirectCast(HttpWebRequest.Create("https://www.tumblr.com/svc/follow"), HttpWebRequest)
  161.                 req.Accept = "*/*"
  162.                 req.CookieContainer = s0
  163.                 req.Method = "POST"
  164.                 req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"
  165.                 req.Headers.Add("X-Requested-With", "XMLHttpRequest")
  166.                 req.ContentLength = postB.Length
  167.                 req.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"
  168.                 req.Headers.Add("X-tumblr-form-key", form_key)
  169.                 req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate")
  170.                 req.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
  171.  
  172.                 Using ss As Stream = req.GetRequestStream()
  173.                     ss.Write(postB, 0, postB.Length)
  174.                 End Using
  175.  
  176.                 Using resp As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
  177.                     Using sr As New StreamReader(resp.GetResponseStream())
  178.  
  179.                         Return sr.ReadToEnd().Contains("liked"":true")
  180.                     End Using
  181.                 End Using
  182.             Catch
  183.                 MessageBox.Show("Could not follow a user due to an unknown error.", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
  184.                 Return False
  185.             End Try
  186.         End Function
  187.         Public Function [Like](p As KeyValuePair(Of String, String)) As Boolean
  188.             Try
  189.                 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))
  190.  
  191.                 Dim req As HttpWebRequest = DirectCast(HttpWebRequest.Create("https://www.tumblr.com/svc/like"), HttpWebRequest)
  192.                 req.Accept = "*/*"
  193.                 req.CookieContainer = s0
  194.                 req.Method = "POST"
  195.                 req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"
  196.                 req.Headers.Add("X-Requested-With", "XMLHttpRequest")
  197.                 req.ContentLength = postB.Length
  198.                 req.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"
  199.                 req.Headers.Add("X-tumblr-form-key", form_key)
  200.                 req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate")
  201.                 req.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
  202.  
  203.                 Using ss As Stream = req.GetRequestStream()
  204.                     ss.Write(postB, 0, postB.Length)
  205.                 End Using
  206.  
  207.                 Using resp As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
  208.                     Using sr As New StreamReader(resp.GetResponseStream())
  209.  
  210.                         Return sr.ReadToEnd().Contains("liked"":true")
  211.                     End Using
  212.                 End Using
  213.             Catch
  214.                 MessageBox.Show("Could not Like a user due to an unknown error.", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
  215.                 Return False
  216.             End Try
  217.         End Function
  218.  
  219.         Public Function Scrape(q As String, max As Integer, posts As Boolean) As List(Of KeyValuePair(Of String, String))
  220.             Try
  221.                 Dim mDictionary As New List(Of KeyValuePair(Of String, String))()
  222.                 Dim mainSearch As New Regex("data-post-id='(.*?)' data-tumblelog-name='(.*?)' data-reblog-key='(.*?)'")
  223.  
  224.                 Dim s1 As String = SimpleGET(Convert.ToString("https://www.tumblr.com/search/") & q)
  225.                 For Each m As Match In mainSearch.Matches(s1)
  226.                     If posts Then
  227.                         mDictionary.Add(New KeyValuePair(Of String, String)(m.Groups(1).Value, m.Groups(3).Value))
  228.                     Else
  229.                         mDictionary.Add(New KeyValuePair(Of String, String)(m.Groups(2).Value, "FOLLOW_SOURCE_DISCOVER_POSTS_GRID"))
  230.                     End If
  231.                 Next
  232.                 Dim page As Integer = 2
  233.                 Dim num_posts As Integer = mDictionary.Count
  234.                 '  Console.WriteLine("\nD: " + mDictionary.Count);
  235.                 form_key = GetRegexValue("form_key", s1)
  236.                 '      Console.WriteLine("D: "+mDictionary.Count+"\nQ:"+Query(q, 1, 0, 0, GetRegexValue("form_key", s1)).Count);
  237.                 While max >= mDictionary.Count
  238.                     Dim temp As List(Of KeyValuePair(Of String, String)) = Query(q, page, num_posts, form_key, posts).Distinct().ToList()
  239.                     mDictionary.AddRange(temp)
  240.                     page += 1
  241.                     num_posts += temp.Count
  242.  
  243.                     mDictionary = mDictionary.Distinct().ToList()
  244.                 End While
  245.                     '   return mDictionary.Take(0, max);
  246.                 Return mDictionary.GetRange(0, max)
  247.             Catch
  248.                 Return Nothing
  249.             End Try
  250.         End Function
  251.         #End Region
  252.     End Class
  253. End Namespace
  254.  
  255. '=======================================================
  256. 'Service provided by Telerik (www.telerik.com)
  257. 'Conversion powered by NRefactory.
  258. 'Twitter: @telerik
  259. 'Facebook: facebook.com/telerik
  260. '=======================================================
Add Comment
Please, Sign In to add comment