Issue with filtered stream

Nov 25, 2014 at 8:06 PM
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using Tweetinvi;
using Tweetinvi.Core.Enum;
using Tweetinvi.Core.Extensions;
using Tweetinvi.Core.Interfaces;
using Tweetinvi.Core.Interfaces.Controllers;
using Tweetinvi.Core.Interfaces.DTO;
using Tweetinvi.Core.Interfaces.DTO.QueryDTO;
using Tweetinvi.Core.Interfaces.Models;
using Tweetinvi.Core.Interfaces.Models.Parameters;
using Tweetinvi.Core.Interfaces.oAuth;
using Tweetinvi.Core.Interfaces.Streaminvi;
using Tweetinvi.Json;
using Stream = Tweetinvi.Stream;
using System.Configuration;
using System.Runtime.InteropServices;
using System.Net.NetworkInformation;

namespace Twitter_Scanner
{
    class Program
    {
        static void Main(string[] args)
        {
            TwitterCredentials.SetCredentials(ConfigurationManager.AppSettings["token_AccessToken"], ConfigurationManager.AppSettings["token_AccessTokenSecret"], ConfigurationManager.AppSettings["token_ConsumerKey"], ConfigurationManager.AppSettings["token_ConsumerSecret"]);
            var loggedUser = User.GetLoggedUser();
            var settings = loggedUser.GetAccountSettings();

            Console.WriteLine(settings.ScreenName + ": You Are Successfully Connected to Twitter API");
            GetCredentialsRateLimits();
            Console.WriteLine("Twitter Newswire Scanner");
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("News Sources: ");
            foreach (var NewsSource in File.ReadLines(ConfigurationManager.AppSettings["Path_To_NewsSource_Text_File"]))
            {
                Console.Write(NewsSource.ToString() + "  ");
            }
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Keywords: ");
            foreach (var KeyWords in File.ReadLines(ConfigurationManager.AppSettings["Path_To_KeyWords_Text_File"]))
            {
                Console.Write(KeyWords.ToString() + "  ");
            }
            Console.WriteLine();
            Console.WriteLine();
            streamThread();
            Console.Read();
        }
        // >>>>>>>>>>>>>>>>>>>>>CUSTOM SEARCH FILTERING<<<<<<<<<<<<<<<<<<<//
        private static void Stream_FilteredStreamExample()
        {
            try
            {
                var stream = Stream.CreateFilteredStream();
                foreach (var KeyWords in File.ReadLines(ConfigurationManager.AppSettings["Path_To_KeyWords_Text_File"]))// Gets Key Words From Text File
                {
                    stream.AddTrack(KeyWords);
                }
                stream.MatchingTweetAndLocationReceived += (sender, args) =>
                    {
                        var tweet = args.Tweet;
                        foreach (var NewsSource in File.ReadLines(ConfigurationManager.AppSettings["Path_To_NewsSource_Text_File"]))// Filter by News Sources in Text File
                        {
                            if (tweet.Creator.ScreenName == NewsSource)
                            {
                                Console.WriteLine(tweet.Creator.ScreenName + " :-->> " + tweet.Text);
                                System.Media.SoundPlayer player = new System.Media.SoundPlayer(ConfigurationManager.AppSettings["Path_To_Sound_Alert"]);
                                player.Play();
                            }
                            else
                            {
                                //do nothing
                            }
                        }
                    };
                stream.StartStreamMatchingAllConditions();
            }
            catch
            {
                Console.WriteLine("Stream Terminated");
            }

        }

        private static void GetCredentialsRateLimits()
        {
            var credentials = TwitterCredentials.CreateCredentials(ConfigurationManager.AppSettings["token_AccessToken"], ConfigurationManager.AppSettings["token_AccessTokenSecret"], ConfigurationManager.AppSettings["token_ConsumerKey"], ConfigurationManager.AppSettings["token_ConsumerSecret"]);
            var tokenRateLimits = RateLimit.GetCredentialsRateLimits(credentials);

            Console.WriteLine("Remaning Requests for GetRate : {0}", tokenRateLimits.ApplicationRateLimitStatusLimit.Remaining);
            Console.WriteLine("Total Requests Allowed for GetRate : {0}", tokenRateLimits.ApplicationRateLimitStatusLimit.Limit);
            Console.WriteLine("GetRate limits will reset at : {0} local time", tokenRateLimits.ApplicationRateLimitStatusLimit.ResetDateTime.ToLongTimeString());
        }
        private static void Exceptions_GetExceptionsInfo()
        {
            TwitterCredentials.Credentials = null;

            var user = User.GetLoggedUser();
            if (user == null)
            {
                var lastException = ExceptionHandler.GetLastException();
                Console.WriteLine(lastException.TwitterDescription);
            }
        }
        private static void streamThread()
        {
            var t = new Thread(() =>
                {
                    Console.WriteLine("Stream Thread Started...");
                    Stream_FilteredStreamExample();
                });
            t.Start();
            Console.WriteLine(t.ThreadState.ToString()+" Running");
        }
    }
}
Having an issue getting tweets via the filtered stream, contacted you, don't know if you got a chance to look at it.

Everything works fine until I try to filter by screen name, if I comment out that code, tweets come streaming in based on keywords detected in those tweets.
There are no errors thrown, all the text files and config are in the correct place. Everything connects fine, and returns my username once it connects to the api.
But once I filter the tweets by screen name, nothing comes through, which is strange because prior to last week everything was working fine.

Thanks for any help you can give me.
Nov 26, 2014 at 2:17 PM
Hi,

I have not been able to check any code for a week. I am very very busy.
From what you described, are you sure that Twitter is never returning any result or is it your filter on the ScreenName that goes wrong?

Could you please explain what NewSources is supposed to represent?

Sorry for the delay.
Linvi
Nov 26, 2014 at 7:33 PM
Edited Nov 27, 2014 at 2:16 AM
Yes that's what I believe is the problem, which is strange because it worked ok before.

edit clarification: when the foreach and if statement for NewSources is commented out, tweets come streaming in based on keywords with no problem, but when it filters those tweets additionally by news sources it doesn't receive the tweet or it receives one then the stream stops.
Also tried hard coding the screen names to filter by and that didn't work either.

NewSources is a text file and just contains twitter screen names with 1 per line, and keywords are also stored the same way.
Example:
cnnbrk
BBCBreaking
FoxNews
CNN
WSJ
Reuters
AP
BloombergNews
nytimes
BreakingNews
FinancialTimes
nprnews
washingtonpost
WSJbreakingnews
ReutersLive
AJELive
ATSFutures
zerohedge
Dec 2, 2014 at 8:02 PM
Hi,

I have tested your code with your credentials and it is working perfectly fine. The only difference that I have is that I am just passing through the keywords from a hardcoded list.

Would you please let me know if you continue to experience this problem.

Regards,
Linvi
Dec 3, 2014 at 1:34 AM
Thanks, the issue was with the keyword list. Don't know exactly why but it didn't like the last few words I added to the list.
Marked as answer by linvi on 12/3/2014 at 1:22 AM