Ejemplo n.º 1
0
        public List <Status> Get(string query, string maxId)
        {
            _auth.Credentials = new InMemoryCredentials
            {
                ConsumerKey    = System.Configuration.ConfigurationManager.AppSettings["ConsumerKey"],
                ConsumerSecret = System.Configuration.ConfigurationManager.AppSettings["ConsumerSecret"]
            };

            _auth.Authorize();

            using (var tweetsDb = new TweetsDBEntities())
            {
                using (var twitterContext = new TwitterContext(_auth))
                {
                    var current = (from search in twitterContext.Search
                                   where
                                   search.Type == SearchType.Search &&
                                   search.Query == query &&
                                   search.Count == 100 &&
                                   search.MaxID == ulong.Parse(maxId)
                                   select search).Single().Statuses;

                    return(current);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Demonstrates how to use ApplicationOnlyAuthorizer
        /// </summary>
        /// <param name="twitterCtx"></param>
        private static void HandleApplicationOnlyAuthentication()
        {
            var auth = new ApplicationOnlyAuthorizer
            {
                Credentials = new InMemoryCredentials
                {
                    ConsumerKey    = ConfigurationManager.AppSettings["twitterConsumerKey"],
                    ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"]
                }
            };

            auth.Authorize();
            //auth.Invalidate();

            var twitterCtx = new TwitterContext(auth);

            var srch =
                (from search in twitterCtx.Search
                 where search.Type == SearchType.Search &&
                 search.Query == "LINQ to Twitter"
                 select search)
                .SingleOrDefault();

            Console.WriteLine("\nQuery: {0}\n", srch.SearchMetaData.Query);
            srch.Statuses.ForEach(entry =>
                                  Console.WriteLine(
                                      "ID: {0, -15}, Source: {1}\nContent: {2}\n",
                                      entry.StatusID, entry.Source, entry.Text));
        }
Ejemplo n.º 3
0
        public List<Status> Get(string query)
        {
            try
            {
                _auth.Credentials = new InMemoryCredentials
                {
                    ConsumerKey = System.Configuration.ConfigurationManager.AppSettings["ConsumerKey"],
                    ConsumerSecret = System.Configuration.ConfigurationManager.AppSettings["ConsumerSecret"]
                };

                _auth.Authorize();

                var result = new List<Status>();

                using (var twitterContext = new TwitterContext(_auth))
                {
                    var gatherMore = true;
                    ulong maxID = 0;
                    while (gatherMore)
                    {
                        var temp = (from search in twitterContext.Search
                                    where search.Type == SearchType.Search && search.Count == 1000
                                              && search.Query == query && search.MaxID == maxID
                                        //Tweets older than date
                                              && search.Until == DateTime.Parse("06-08-2014")
                                    select search).SingleOrDefault().Statuses;
                        result.AddRange(temp);
                        if (temp.Count <= 1)
                            gatherMore = false;
                        else
                        {
                            maxID = ulong.Parse(temp[temp.Count - 1].StatusID);
                            var date = new DateTime(2014, 06, 05, 15, 00, 00);
                            if (temp[temp.Count - 1].CreatedAt <= date)
                                gatherMore = false;
                        }
                    }
                };
                WriteFile(result.GroupBy(m => m.StatusID).Select(n => n.First()).OrderByDescending(s => s.StatusID).ToList(), query);
                return result.GroupBy(m => m.StatusID).Select(n => n.First()).OrderByDescending(s => s.StatusID).ToList();
            }
            catch (Exception e)
            {
                throw new Exception("An error occured. Please check your network connection. " + e.Message);
            }
        }
Ejemplo n.º 4
0
        static ITwitterAuthorizer DoApplicationOnly()
        {
            var auth = new ApplicationOnlyAuthorizer
            {
                Credentials = new InMemoryCredentials
                {
                    ConsumerKey    = ConfigurationManager.AppSettings["twitterConsumerKey"],
                    ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"]
                }
            };

            auth.Authorize();

            return(auth);
        }
Ejemplo n.º 5
0
        public void GetTweet(string query, string maxId = "0")
        {
            try
            {
                _auth.Credentials = new InMemoryCredentials
                {
                    ConsumerKey    = System.Configuration.ConfigurationManager.AppSettings["ConsumerKey"],
                    ConsumerSecret = System.Configuration.ConfigurationManager.AppSettings["ConsumerSecret"]
                };

                _auth.Authorize();

                var checker = true;

                using (var tweetsDb = new TweetsDBEntities())
                {
                    using (var twitterContext = new TwitterContext(_auth))
                    {
                        while (checker)
                        {
                            var current = (from search in twitterContext.Search
                                           where
                                           search.Type == SearchType.Search &&
                                           search.Query == query &&
                                           search.Count == 100 &&
                                           search.MaxID == ulong.Parse(maxId)
                                           select search).Single().Statuses;

                            if (current.Count > 0)
                            {
                                if (current.Count == 1)
                                {
                                    if (current[0].StatusID == maxId)
                                    {
                                        checker = false;
                                    }
                                }

                                if (!checker)
                                {
                                    break;
                                }

                                foreach (var each in current)
                                {
                                    Clients.Group(query).retrieveTweet(each);
                                    maxId = each.StatusID;
                                }
                            }
                            else
                            {
                                checker = false;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
Ejemplo n.º 6
0
        public List <Status> Get(string query)
        {
            try
            {
                _auth.Credentials = new InMemoryCredentials
                {
                    ConsumerKey    = System.Configuration.ConfigurationManager.AppSettings["ConsumerKey"],
                    ConsumerSecret = System.Configuration.ConfigurationManager.AppSettings["ConsumerSecret"]
                };

                _auth.Authorize();

                using (var tweetsDb = new TweetsDBEntities())
                {
                    var maxId        = "0";
                    var checker      = true;
                    var returnResult = new List <Status>();

                    var resultForSinceIdInDB = (from table in tweetsDb.Tweets where table.Query.Equals(query) select table).OrderByDescending(m => m.StatusID).FirstOrDefault();
                    var sinceId = (resultForSinceIdInDB == null) ? "0" : resultForSinceIdInDB.StatusID;

                    var resultForMaxIdInDB = (from table in tweetsDb.Tweets where table.Query.Equals(query) select table).OrderBy(m => m.StatusID).FirstOrDefault();
                    var maxIdInDB          = (resultForMaxIdInDB == null) ? "0" : resultForMaxIdInDB.StatusID;

                    var newSearch = (resultForSinceIdInDB == null) ? true : false;

                    using (var twitterContext = new TwitterContext(_auth))
                    {
                        while (checker)
                        {
                            if (newSearch)
                            {
                                var current = (from search in twitterContext.Search
                                               where
                                               search.Type == SearchType.Search &&
                                               search.Query == query &&
                                               search.Count == 100 &&
                                               search.MaxID == ulong.Parse(maxId)
                                               select search).Single().Statuses;

                                if (current != null)
                                {
                                    if (current.Count > 0)
                                    {
                                        foreach (var each in current)
                                        {
                                            if (ulong.Parse(each.StatusID) < ulong.Parse(sinceId))
                                            {
                                                checker = false;
                                                break;
                                            }
                                            if ((from table in tweetsDb.Tweets where table.Query.Equals(query) && table.StatusID.Equals(each.StatusID) select table).SingleOrDefault() == null)
                                            {
                                                tweetsDb.Tweets.Add(new Tweets
                                                {
                                                    Content          = each.Text,
                                                    CreatedAt        = each.CreatedAt,
                                                    Language         = each.Lang,
                                                    Name             = each.User.Name,
                                                    Query            = query,
                                                    StatusID         = each.StatusID,
                                                    TweetLink        = "https://twitter.com/" + each.User.Identifier.ScreenName + "/statuses/" + each.StatusID,
                                                    UserID           = each.User.Identifier.ID,
                                                    Username         = each.User.Identifier.ScreenName,
                                                    DateInsertedInDB = DateTime.Now
                                                });
                                                tweetsDb.SaveChanges();
                                            }
                                        }
                                        maxId = (ulong.Parse(current[current.Count - 1].StatusID) - 1).ToString(CultureInfo.InvariantCulture);
                                    }
                                    else
                                    {
                                        checker = false;
                                    }
                                    returnResult.AddRange(current);
                                }
                            }
                            else //if this is not a new search
                            {
                                var current = (from search in twitterContext.Search
                                               where
                                               search.Type == SearchType.Search &&
                                               search.Query == query &&
                                               search.Count == 100 &&
                                               search.MaxID == ulong.Parse(maxId)
                                               select search).Single().Statuses;
                                if (current != null)
                                {
                                    var currentResult = new List <Status>();

                                    if (current.Count > 0)
                                    {
                                        foreach (var each in current)
                                        {
                                            if (ulong.Parse(each.StatusID) <= ulong.Parse(sinceId))
                                            {
                                                checker = false;
                                                break;
                                            }
                                            if ((from table in tweetsDb.Tweets where table.Query.Equals(query) && table.StatusID.Equals(each.StatusID) select table).SingleOrDefault() == null)
                                            {
                                                tweetsDb.Tweets.Add(new Tweets
                                                {
                                                    Content          = each.Text,
                                                    CreatedAt        = each.CreatedAt,
                                                    Language         = each.Lang,
                                                    Name             = each.User.Name,
                                                    Query            = query,
                                                    StatusID         = each.StatusID,
                                                    TweetLink        = "https://twitter.com/" + each.User.Identifier.ScreenName + "/statuses/" + each.StatusID,
                                                    UserID           = each.User.Identifier.ID,
                                                    Username         = each.User.Identifier.ScreenName,
                                                    DateInsertedInDB = DateTime.Now
                                                });
                                                tweetsDb.SaveChanges();

                                                currentResult.Add(each);
                                            }
                                        }
                                    }
                                    returnResult.AddRange(currentResult);
                                }
                            }
                        }
                        return(returnResult);
                    };
                };
            }
            catch (Exception e)
            {
                throw new Exception("An error occured. Please check your network connection. " + e.Message);
            }
        }