コード例 #1
0
        public MyTwitterObj GetData(MyTwitterObj myTwitter)
        {
            DateTime dtm = new DateTime();

            dtm = DateTime.Now;

            //first search:
            TwitterHelper helper1 = new TwitterHelper
            {
                search   = myTwitter.search_1,
                max_id   = 0,
                since_id = 0,
                count    = 0
            };

            RootObject root = new RootObject();

            root = GetTweets(helper1);
            while (root.statuses.Count > 0)
            {
                if (root.statuses.Count == Int32.Parse(ConfigurationManager.AppSettings["TwitterMaxResults"]))
                {
                    // there are more tweets than we can process in one batch -- loop
                    foreach (Status value in root.statuses)
                    {
                        helper1.count = helper1.count + value.retweet_count + 1;
                    }
                    helper1.max_id = root.statuses[99].id; // oldest tweet in collection
                    root           = GetTweets(helper1);
                }
                else
                {
                    // there is less than the maximum amount of tweets -- no need to loop
                    foreach (Status value in root.statuses)
                    {
                        helper1.count   = helper1.count + value.retweet_count + 1;
                        helper1.created = value.created_at;
                    }
                    break;
                }
            }

            //second search:
            TwitterHelper helper2 = new TwitterHelper
            {
                search   = myTwitter.search_2,
                max_id   = 0,
                since_id = 0,
                count    = 0
            };

            root = new RootObject();
            root = GetTweets(helper2);
            while (root.statuses.Count > 0)
            {
                if (root.statuses.Count == Int32.Parse(ConfigurationManager.AppSettings["TwitterMaxResults"]))
                {
                    // there are more tweets than we can process in one batch -- loop
                    foreach (Status value in root.statuses)
                    {
                        helper2.count = helper2.count + value.retweet_count + 1;
                    }
                    helper2.max_id = root.statuses[99].id; // oldest tweet in collection
                    root           = GetTweets(helper2);
                }
                else
                {
                    // there is less than the maximum amount of tweets -- no need to loop
                    foreach (Status value in root.statuses)
                    {
                        helper2.count   = helper2.count + value.retweet_count + 1;
                        helper2.created = value.created_at;
                    }
                    break;
                }
            }

            //return data
            MyTwitterObj RetTwitterObj = new MyTwitterObj();

            RetTwitterObj.search_1 = myTwitter.search_1;
            RetTwitterObj.search_2 = myTwitter.search_2;
            RetTwitterObj.tweets_1 = helper1.count;
            RetTwitterObj.tweets_2 = helper2.count;
            if (helper1.created == null)
            {
                RetTwitterObj.seconds_1 = 0;
            }
            else
            {
                DateTime date1 = DateTime.ParseExact(helper1.created, "ddd MMM dd HH:mm:ss zzz yyyy", System.Globalization.CultureInfo.InvariantCulture);
                RetTwitterObj.seconds_1 = Convert.ToInt32((dtm - date1).TotalSeconds);
            }
            if (helper2.created == null)
            {
                RetTwitterObj.seconds_2 = 0;
            }
            else
            {
                DateTime date2 = DateTime.ParseExact(helper2.created, "ddd MMM dd HH:mm:ss zzz yyyy", System.Globalization.CultureInfo.InvariantCulture);
                RetTwitterObj.seconds_2 = Convert.ToInt32((dtm - date2).TotalSeconds);
            }
            return(RetTwitterObj);
        }
コード例 #2
0
        public RootObject GetTweets(TwitterHelper helper)
        {
            // set the keys
            string oAuthConsumerKey    = ConfigurationManager.AppSettings["TwitterKey"];
            var    oAuthConsumerSecret = ConfigurationManager.AppSettings["TwitterSecret"];
            var    oAuthUrl            = ConfigurationManager.AppSettings["TwitterAuthURL"];

            // authenticate
            var authHeaderFormat = "Basic {0}";

            var authHeader = string.Format(authHeaderFormat,
                                           Convert.ToBase64String(Encoding.UTF8.GetBytes(Uri.EscapeDataString(oAuthConsumerKey) + ":" +
                                                                                         Uri.EscapeDataString((oAuthConsumerSecret)))
                                                                  ));

            var postBody = "grant_type=client_credentials";

            //create request
            HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(oAuthUrl);

            authRequest.Headers.Add("Authorization", authHeader);
            authRequest.Method                 = "POST";
            authRequest.ContentType            = "application/x-www-form-urlencoded;charset=UTF-8";
            authRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

            using (Stream stream = authRequest.GetRequestStream())
            {
                byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody);
                stream.Write(content, 0, content.Length);
            }

            authRequest.Headers.Add("Accept-Encoding", "gzip");

            // deserialize
            WebResponse authResponse = authRequest.GetResponse();
            TwitAuthenticateResponse twitAuthResponse;

            using (authResponse)
            {
                using (var reader = new StreamReader(authResponse.GetResponseStream()))
                {
                    var objectText = reader.ReadToEnd();
                    twitAuthResponse = JsonConvert.DeserializeObject <TwitAuthenticateResponse>(objectText);
                }
            }

            // Do the search
            string         maxResults           = ConfigurationManager.AppSettings["TwitterMaxResults"];
            var            searchFormat         = ConfigurationManager.AppSettings["TwitterSearchURL"];
            var            searchURL            = string.Format(searchFormat, helper.search, helper.since_id, helper.max_id, maxResults);
            HttpWebRequest searchRequest        = (HttpWebRequest)WebRequest.Create(searchURL);
            var            timelineHeaderFormat = "{0} {1}";

            searchRequest.Headers.Add("Authorization", string.Format(timelineHeaderFormat, twitAuthResponse.token_type, twitAuthResponse.access_token));
            searchRequest.Method = "Get";
            WebResponse searchResponse = searchRequest.GetResponse();
            var         searchJson     = string.Empty;

            using (searchResponse)
            {
                using (var reader = new StreamReader(searchResponse.GetResponseStream()))
                {
                    searchJson = reader.ReadToEnd();
                    //RootObject root = new RootObject();
                    return(JsonConvert.DeserializeObject <RootObject>(searchJson));
                }
            }
        }