Hashtag and Url collections of a Tweet

Sep 6, 2013 at 11:41 PM
Edited Sep 6, 2013 at 11:49 PM
Greetings,

This project is great and I'm really interested in the FilteredStream functionality.

I've got it working with the test console application largely without issue.

As an fyi, I have downloaded the Visual Studio project and am working with the example console application.

From the Main() method, I am using the StreamFilterExample(token) function. From there I am trying to customize the console output with more information about the tweet using the example ProcessTweet(ITweet tweet) function in the Examplinvi project. All of this is pretty straight forward and I'm simply attempting to output more attributes about a tweet.

Where I am encountering an unexpected behavior when I attempt to access the tweet.HashTags or tweet.Urls collections of a tweet in the ProcessTweet function - these collections are always null even when there are hash tags or urls in the tweet text. Additionally, the tweet.Entities.HashTags and tweet.Entities.Urls collections are also null.

As an example, here is a snippet of code that I've added to the ProcessTweet function and during execution it never gets past the first if statement:
                if (tweet.Urls != null)
                {
                    if (tweet.Urls.Count > 0)
                    {
                        Console.WriteLine("Urls:");
                        foreach (IUrlEntity url in tweet.Urls)
                        {
                            Console.WriteLine(url.Url.ToString());
                        }
                    }
                }
Am I missing something or doing something wrong? If you could point me in the right direction, that would be greatly appreciated.

Thanks for your project! I look forward to your reply and thank-you for your time.

Kind regards,

Craig
Coordinator
Sep 7, 2013 at 1:56 AM
Edited Sep 7, 2013 at 1:59 AM
Hi there,

Your code seems 100 percent correct.

I will investigate your problem and will give you news as soon as this is done.
If there is any bug related with this behavior it will be corrected in the next update.
I created an issue and will keep you up to date as soon as I have more news (https://tweetinvi.codeplex.com/workitem/1855).

Thank you for informing us about this issue.

Regards,
Linvi
Coordinator
Sep 7, 2013 at 2:43 AM
The issue is coming from the fact that the Javascript serializer either define Entities as being an ArrayList or object[] depending on the number of objects contained in the Json sent by Twitter.

I had a plan to migrate from the JavascriptSerializer to another serializer and I will review this as part of this issue.

While I am fixing this issue, you can fix the TweetEntities.Ctor() and try to cast on either type and then set the value.
I believe this would be a temporary fix.

I will keep you up to date as soon as I can.

Linvi
Coordinator
Sep 7, 2013 at 1:33 PM
Hi there,

Just to let you know that I solved the issue in the latest source control solution : https://tweetinvi.codeplex.com/SourceControl/changeset/31480.
I will update the 0.8.2.0 direct download in the day as well as the nuget packages.

You will not need to check for null anymore. The list will be empty if it is not present in the json sent from Twitter.
// This code is now valid
if (tweet.Urls.Count > 0)
{
    Console.WriteLine("Urls:");
    foreach (IUrlEntity url in tweet.Urls)
    {
        Console.WriteLine(url.Url.ToString());
    }
}
Thank you for reporting the issue.
Linvi

PS : In later versions I will update the code to use json.Net or the DataContractSerializer if they can improve the performances of the solution.
Marked as answer by linvi on 10/7/2013 at 8:07 AM
Sep 7, 2013 at 2:52 PM
Linvi,

Thanks for your assistance and for the quick response! Much appreciated :)

I've loaded up the source control solution and the changes you've made are working great! Thank you.

I'd like to report a very small issue that can be easily addressed. If the application is launched on a machine without an Internet connection (of course it needs one to operate correctly), I am getting the following unhandled exception:

System.Net.WebException was unhandled
HResult=-2146233079
Message=The remote name could not be resolved: 'stream.twitter.com'
Source=System
...

The culprit is the following function which is found within the StreamResultGenerator.cs file:

private StreamReader InitWebRequest(WebRequest webRequest)

The line "HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();" could be executed within in a try/catch block so the exception can be handled elegantly to throw a meaningful exception.

I have to say, great work on this project! It is very useful for a few things I have in mind. If I find anything else while working with this project, I'll let you know.

Cheers,

Craig
Coordinator
Sep 7, 2013 at 6:50 PM
Edited Sep 7, 2013 at 6:55 PM
Thank you :)

Using Tweetinvi you will notice that most of the Exception are not managed by the API.
This was an "expected" behavior of the API because I wanted to let users manage the errors by themselves.

2 months ago I changed my mind concerning Exception Management as it may be complicated for developers to try/catch all the exceptions that the Twitter API can cause. So I create a work item to let the users easily manage their exceptions (https://tweetinvi.codeplex.com/workitem/1846).

I am currently working on this Exception system for the 0.8.3.0 : https://tweetinvi.codeplex.com/releases/view/111423.
  • Tweetinvi Exception will be managed thanks to a TwitterContext system that will let you try any action from the API and let you know if it succeed. In the case the action failed it will store a meaningful exception
  • Streaming Exception will probably be managed thanks to an event or a delegate that will let you know that an event has occurred and let you decide if you want the stream to be resumed.
I will try to release this work as fast as I can in the meantime feel free to update Tweetinvi so that it matches your requirements :)

Linvi