Пример #1
0
        public void PostTwitterMentionsToRest()
        {
            string result = string.Empty;

            //http://localhost/BIDataAPI/api/twitter/mentions
            requestPost                       = (HttpWebRequest)WebRequest.Create("http://localhost/BIdataApi/api/twitter/mentions");
            requestPost.Method                = "POST";
            requestPost.ContentType           = "application/json";
            requestPost.UseDefaultCredentials = true;

            TweetManager  tm = new TweetManager();
            KeywordStatus ks = new KeywordStatus();

            Twitter.Components.Twitter t = new Twitter.Components.Twitter();
            ks.KeywordId  = 7;
            ks.StatusList = tm.GetTweetsByQuery(20, Uri.EscapeDataString("\"go daddy\" OR godaddy OR go-daddy"), Twitter.Utility.GetOAuthToken(t.Xml));

            string requestData = new JavaScriptSerializer().Serialize(ks);

            byte[] data = Encoding.UTF8.GetBytes(requestData);

            using (Stream dataStream = requestPost.GetRequestStream())
                dataStream.Write(data, 0, data.Length);

            HttpWebResponse response = requestPost.GetResponse() as HttpWebResponse;

            result = new StreamReader(response.GetResponseStream()).ReadToEnd();

            Assert.AreEqual(response.StatusCode, HttpStatusCode.Created);
        }
Пример #2
0
        /// <summary>
        /// Public exposed method for calling from a webservice.
        /// Querystring will be "q=[paramstring]"
        /// </summary>
        /// <param name="countPerPage"></param>
        /// <param name="keywordID">keyword id from rptTwitterKeywordLookup (1 through 6)</param>
        /// <param name="pagesToReturn"></param>
        /// <param name="paramString">any valid parameters except for count and pageCount.</param>
        public static Dictionary <int, List <Status> > GetAllMentionsByQuery(int keywordID, int countPerPage, int pagesToReturn, string paramString)
        {
            Dictionary <int, List <Status> > allTwitterMentions = new Dictionary <int, List <Status> >();
            TweetManager tm = new TweetManager();
            Twitter      t  = new Twitter();

            allTwitterMentions.Add(keywordID, tm.GetTweetsByQuery(countPerPage, paramString, Utility.GetOAuthToken(t.Xml)));

            return(allTwitterMentions);
        }
Пример #3
0
        /// <summary>
        /// Get all tweets with "GoDaddy" in them.
        /// 100 per page, up to 15 pages if we loop and make seperate calls for each page
        /// </summary>
        internal Dictionary <int, List <Status> > GetAllMentionsByQuery(TwitterContext cont)
        {
            Dictionary <int, List <Status> > allTwitterMentions = new Dictionary <int, List <Status> >();

            if (XMLUtility.IsComponentEnabled(Xml, ProcessorName))
            {
                int countPerPage; int pageCount;
                TwitterDataSource data = new TwitterDataSource();
                TweetManager      tm   = new TweetManager();

                XMLUtility.GetPageResultCounts(Xml, ProcessorName, out countPerPage, out pageCount, 100, 3);
                List <Keyword> queries    = data.GetTweetQueries();
                bool           useSinceId = XMLUtility.UseSinceId(Xml, ProcessorName);

                //create backup of current keys in case of failure at db level
                lock (cont) cont.prevRunLatestTweetIDs = cont.LatestTweetIDs.ToDictionary(entry => entry.Key, entry => entry.Value);

                foreach (Keyword item in queries)
                {
                    try
                    {
                        long sinceId = 0;
                        if (useSinceId)
                        {
                            //get the last recorded id for this query and use it
                            if (!Object.Equals(cont, null))
                            {
                                lock (cont)
                                {
                                    if (cont.LatestTweetIDs.ContainsKey(item.KeywordId))
                                    {
                                        cont.LatestTweetIDs.TryGetValue(item.KeywordId, out sinceId);
                                    }
                                }
                            }
                        }

                        //call the mention object in the API wrapper
                        TwitterDataComponent _dataComponent = DroneDataComponent as TwitterDataComponent;
                        KeywordStatus        ks             = new KeywordStatus();
                        ks.KeywordId = item.KeywordId;

                        ks.StatusList = tm.GetTweetsByQuery(countPerPage, item.KeywordValue + (sinceId > 0 ? "&since_id=" + sinceId : string.Empty), Utility.GetOAuthToken(Xml));
                        allTwitterMentions.Add(ks.KeywordId, ks.StatusList);

                        _dataComponent.KeywordStatus = ks;
                        DroneDataSource.Process(_dataComponent);

                        //if there was a failure saving to the db, reset the since id to gather and try again
                        if (_dataComponent.SaveFailure)
                        {
                            lock (cont) cont.LatestTweetIDs = cont.prevRunLatestTweetIDs.ToDictionary(entry => entry.Key, entry => entry.Value);
                        }

                        //get the last id for this query and store it
                        if (!Object.Equals(cont, null) && allTwitterMentions.ContainsKey(item.KeywordId) && allTwitterMentions[item.KeywordId].Count > 0)
                        {
                            long latestID;
                            long.TryParse(allTwitterMentions[item.KeywordId][0].id.ToString(), out latestID);

                            lock (cont)
                            {
                                if (cont.LatestTweetIDs.ContainsKey(item.KeywordId))
                                {
                                    cont.LatestTweetIDs[item.KeywordId] = latestID;
                                }
                                else
                                {
                                    cont.LatestTweetIDs.Add(item.KeywordId, latestID);
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        ExceptionExtensions.LogError(e, "Twitter.GetAllMentionsByQuery()", "Keyword name: " + item.KeywordName);
                    }
                }
            }
            return(allTwitterMentions);
        }