This project has moved and is read-only. For the latest updates, please go here.

Query/Hashtag Example for VB.NET

May 7, 2014 at 8:06 AM
I got some code to work perfectly after searching forever on the
net for something similar. I notice it is limited to searching for
feeds of particular usernames. On top of that, what i'd like to do is
be able to return results from a general search among all users (all of twitter) and/or
those containing certain hashtags.

I looked at the Linq2Twitter documentation and examples, but I can't
seem to get them to work. I either get errors, or I get no results
displayed in my textbox as I do when using your code as is with
usernames. I see that the API allows what i'm trying to do, I just
don't know how to code it up. Any advice or examples someone may be able to
provide? I'm building an IRC bot that posts twitter feeds based on
search parameters on a routine basis.

Thanks in advance.

Here is the code I found to search for particular user feeds, i'd like to know how and where to modify it to do twitter searches basically.
'*** Type in your own consumer key and consumer secret here. ***
Dim oCredential As New InMemoryCredentials
oCredential.ConsumerKey = "Enter your consumer key here"
oCredential.ConsumerSecret = "Enter your consumer secret here"

'*** Use application-only authentication to connect to Twitter. ***
Dim oAuthorizer As New ApplicationOnlyAuthorizer
oAuthorizer.Credentials = oCredential

'*** Define a list of screen names to check for public tweets. ***
Dim screenNames As String = "LambtonAlerts,LambtonFireInfo,SarniaPolice"

'*** Define a cutoff date, e.g., 1-day old. ***
Dim cutoffDate As Date = Now.Subtract(New TimeSpan(1, 0, 0, 0))

'*** This is to remove the time from the date. ***
cutoffDate = cutoffDate.Date

Using oTwitterContext As New TwitterContext(oAuthorizer)

  '*** Instantiate a master list of tweets. ***
  Dim allTweets As New List(Of LinqToTwitter.Status)

  '*** Need to check one screen name at a time. ***
  For Each s As String In screenNames.Split(",")

    Dim screenName As String = s.Trim

    If String.IsNullOrEmpty(screenName) = False Then

      '*** Get a list of 1 day old public tweets for a given user. ***
      '*** Exclude all replies. ***
      Dim tweets = (From tweet In oTwitterContext.Status _
                    Where tweet.ScreenName = screenName _
                          And tweet.Type = StatusType.User _
                          And tweet.CreatedAt.ToLocalTime >= cutoffDate _
                          And tweet.ExcludeReplies = True _
                    Select tweet).ToList

      '*** Add tweets to the master list. ***
      If tweets.Count > 0 Then allTweets.AddRange(tweets)

    End If


  '*** Display all the tweets from newest to oldest. ***
  For Each tweet In allTweets.OrderByDescending(Function(a) a.CreatedAt)
    Page.Response.Write(String.Format("{0}<br />[{1}] {2}<br/><br/>", _
                                      tweet.CreatedAt.ToLocalTime.ToString, _
                                      tweet.ScreenName, _

  '*** To display current rate limit info. ***
  Dim resetWhen As New DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
  resetWhen = resetWhen.AddSeconds(oTwitterContext.RateLimitReset).ToLocalTime()
  Page.Response.Write(String.Format("Remaining = {0}; Limit = {1}; Next Reset = {2}", _
                                    oTwitterContext.RateLimitRemaining, _
                                    oTwitterContext.RateLimitCurrent, _

End Using
May 8, 2014 at 3:26 AM

When doing a LINQ to Twitter query, only use the parameters that are listed in the documentation. e.g. A Status/User query would be changed as follows:
      Dim tweets = (From tweet In oTwitterContext.Status _
                    Where tweet.ScreenName = screenName _
                          And tweet.Type = StatusType.User _
                          And tweet.ExcludeReplies = True _
                    Select tweet).ToList
This is because the data source is a REST interface, as opposed to a SQL database, and those are the only parameters that the Twitter API accepts. You can always make your query with LINQ to Twitter and then refine the results with a LINQ to Objects query afterwards.

There's a LinqToTwitterDemoVB project in the downloadable source code with more VB examples for v2.1.12. I don't have v3.0.2 samples in VB yet.