Advertisement
ShadowTzu

Search on twitter

Nov 12th, 2014
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.67 KB | None | 0 0
  1. Imports System.Xml
  2. Imports System.Web
  3.  
  4. Public Class Form1
  5.     Private Structure struct_Tweet
  6.         Public guid As String
  7.         Public Title As String
  8.         Public link As String
  9.         Public description As String
  10.         Public pubDate As String
  11.         Public enclosure As String
  12.         Public category As String
  13.     End Structure
  14.  
  15.     Private Tweets As List(Of struct_Tweet)
  16.  
  17.     Private mUrl_Search As String = "http://www.queryfeed.net/twitter?q="
  18.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  19.         Tweets = New List(Of struct_Tweet)
  20.  
  21.         'Process("shadowtzu")
  22.         Process("#gamedev")
  23.         '100 is maximum
  24.         If Tweets.Count > 0 Then
  25.             MsgBox("Result Count: " & Tweets.Count & vbCrLf _
  26.                 & "Last Tweet: " & vbCrLf _
  27.                & "User: " & Tweets(0).Title & vbCrLf _
  28.                 & "Date: " & Tweets(0).pubDate & vbCrLf _
  29.                 & "Text: " & Tweets(0).description)
  30.         End If
  31.        
  32.  
  33.     End Sub
  34.  
  35.     Private Sub Process(Search As String)    
  36.         'Dim SDate As DateTime = Date.Now.AddDays(-1)
  37.       '  Dim Since As String = "since:" & SDate.Year & "-" & SDate.Month & "-" & SDate.Day
  38.         Dim doc As New XmlDocument()
  39.         Dim HasError As Boolean = False
  40.         Do
  41.             Try
  42.                 doc.Load(Url_Search(Search)) ' & " " & Since
  43.                 HasError = False
  44.             Catch ex As Exception
  45.                 HasError = True
  46.             End Try
  47.         Loop While HasError = True
  48.  
  49.  
  50.         For Each MainNode As XmlNode In doc.ChildNodes
  51.             For Each rss As XmlNode In MainNode
  52.                 If rss.Name = "channel" AndAlso rss.HasChildNodes Then
  53.                     For Each Child As XmlNode In rss.ChildNodes
  54.                         If Child.Name = "item" AndAlso Child.HasChildNodes Then
  55.                             Dim NewTweet As struct_Tweet = Nothing
  56.                             With NewTweet
  57.                                 If Not Child("guid") Is Nothing Then .guid = Child("guid").InnerText
  58.                                 If Not Child("title") Is Nothing Then .Title = Child("title").InnerText
  59.                                 If Not Child("link") Is Nothing Then .link = Child("link").InnerText
  60.                                 If Not Child("description") Is Nothing Then .description = Child("description").InnerText
  61.                                 If Not Child("pubDate") Is Nothing Then .pubDate = Child("pubDate").InnerText
  62.                                 If Not Child("enclosure") Is Nothing Then .enclosure = ReadTag(Child("enclosure"), "url")                                
  63.                                 If Not Child("category") Is Nothing Then .category = Child("category").InnerText
  64.                             End With
  65.                             Tweets.Add(NewTweet)
  66.                         End If
  67.                     Next
  68.                 End If
  69.             Next
  70.         Next
  71.  
  72.     End Sub
  73.  
  74.     Private Function Url_Search(Search As String) As String
  75.         Return mUrl_Search & URLEncode(Search) & "&geocode="
  76.     End Function
  77.  
  78.     Private Function ReadTag(Node As XmlNode, Tag As String, Optional DefaultValue As String = "") As String
  79.         If Not Node.Attributes Is Nothing Then
  80.  
  81.             If Not Node.Attributes(Tag) Is Nothing Then
  82.                 Return Node.Attributes(Tag).Value
  83.             Else
  84.                 Return DefaultValue
  85.             End If
  86.         Else
  87.             Return DefaultValue
  88.         End If
  89.     End Function
  90.  
  91.     Public Function URLEncode(StringToEncode As String) As String
  92.         Return HttpUtility.UrlEncode(StringToEncode)
  93.     End Function
  94.  
  95. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement