Пример #1
0
        public TweetCount fetchCount(string topic, string city_name)
        {
            LatLng l = new LatLng();
            l.getLatLng(city_name);
            string geocode = l.lat + "," + l.lng + ",10km";
            string param_string = topic; // +"&geocode" + geocode;
            string url = "https://api.twitter.com/1.1/search/tweets.json?q=" + param_string;

            // Create OAuth parameters for twitter API
            string oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
            TimeSpan time_span = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            string oauthtimestamp = Convert.ToInt64(time_span.TotalSeconds).ToString();
            SortedDictionary<string, string> basestringParameters = new SortedDictionary<string, string>();
            basestringParameters.Add("q", topic);
            //basestringParameters.Add("geocode", geocode);
            basestringParameters.Add("oauth_version", oauth_version);
            basestringParameters.Add("oauth_consumer_key", oauth_consumer_key);
            basestringParameters.Add("oauth_nonce", oauth_nonce);
            basestringParameters.Add("oauth_signature_method", oauth_signature_method);
            basestringParameters.Add("oauth_timestamp", oauthtimestamp);
            basestringParameters.Add("oauth_token", oauth_token);
            //Build the signature string
            string baseString = String.Empty;
            baseString += "GET" + "&";
            baseString += Uri.EscapeDataString(url.Split('?')[0]) + "&";
            foreach (KeyValuePair<string, string> entry in basestringParameters)
            {
                baseString += Uri.EscapeDataString(entry.Key + "=" + entry.Value + "&");
            }

            //Remove the trailing ambersand char last 3 chars - %26
            baseString = baseString.Substring(0, baseString.Length - 3);

            //Build the signing key
            string signingKey = Uri.EscapeDataString(oauth_consumer_secret) + "&" + Uri.EscapeDataString(oauth_token_secret);

            //Sign the request
            HMACSHA1 hasher = new HMACSHA1(new ASCIIEncoding().GetBytes(signingKey));
            string oauthsignature = Convert.ToBase64String(hasher.ComputeHash(new ASCIIEncoding().GetBytes(baseString)));

            //Tell Twitter we don't do the 100 continue thing
            ServicePointManager.Expect100Continue = false;

            //authorization header
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@url);
            string authorizationHeaderParams = String.Empty;
            authorizationHeaderParams += "OAuth ";
            authorizationHeaderParams += "oauth_nonce=\"" + Uri.EscapeDataString(oauth_nonce) + "\",";
            authorizationHeaderParams += "oauth_signature_method=\"" + Uri.EscapeDataString(oauth_signature_method) + "\",";
            authorizationHeaderParams += "oauth_timestamp=\"" + Uri.EscapeDataString(oauthtimestamp) + "\",";
            authorizationHeaderParams += "oauth_consumer_key=\"" + Uri.EscapeDataString(oauth_consumer_key) + "\",";
            authorizationHeaderParams += "oauth_token=\"" + Uri.EscapeDataString(oauth_token) + "\",";
            authorizationHeaderParams += "oauth_signature=\"" + Uri.EscapeDataString(oauthsignature) + "\",";
            authorizationHeaderParams += "oauth_version=\"" + Uri.EscapeDataString(oauth_version) + "\"";
            webRequest.Headers.Add("Authorization", authorizationHeaderParams);

            webRequest.Method = "GET";
            webRequest.ContentType = "application/x-www-form-urlencoded";

            TweetCount tc = new TweetCount();
            //Allow us a reasonable timeout in case Twitter's busy
            webRequest.Timeout = 3 * 60 * 1000;

            try
            {
                //Proxy settings
                HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
                Stream dataStream = webResponse.GetResponseStream();

                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);
                string responseFromServer = reader.ReadToEnd();

                XmlDocument doc = ConvertJsonToXml(responseFromServer);
                XmlNodeList items = doc.GetElementsByTagName("item");
                tc.Count = Convert.ToString(items.Count);
                tc.Tweets = new List<Tweets>();
                XmlNodeList tweets = doc.GetElementsByTagName("text");
                foreach (XmlNode t in tweets)
                {
                    Tweets tt = new Tweets();
                    tt.Text = t.InnerText;
                    tc.Tweets.Add(tt);
                }
            }
            catch (Exception ex)
            {
                tc.Error = ex.Message;
            }
            return tc;
        }
Пример #2
0
        public TweetCount fetchCount(string topic, string city_name)
        {
            LatLng l = new LatLng();

            l.getLatLng(city_name);
            string geocode      = l.lat + "," + l.lng + ",10km";
            string param_string = topic; // +"&geocode" + geocode;
            string url          = "https://api.twitter.com/1.1/search/tweets.json?q=" + param_string;

            // Create OAuth parameters for twitter API
            string   oauth_nonce    = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
            TimeSpan time_span      = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            string   oauthtimestamp = Convert.ToInt64(time_span.TotalSeconds).ToString();
            SortedDictionary <string, string> basestringParameters = new SortedDictionary <string, string>();

            basestringParameters.Add("q", topic);
            //basestringParameters.Add("geocode", geocode);
            basestringParameters.Add("oauth_version", oauth_version);
            basestringParameters.Add("oauth_consumer_key", oauth_consumer_key);
            basestringParameters.Add("oauth_nonce", oauth_nonce);
            basestringParameters.Add("oauth_signature_method", oauth_signature_method);
            basestringParameters.Add("oauth_timestamp", oauthtimestamp);
            basestringParameters.Add("oauth_token", oauth_token);
            //Build the signature string
            string baseString = String.Empty;

            baseString += "GET" + "&";
            baseString += Uri.EscapeDataString(url.Split('?')[0]) + "&";
            foreach (KeyValuePair <string, string> entry in basestringParameters)
            {
                baseString += Uri.EscapeDataString(entry.Key + "=" + entry.Value + "&");
            }

            //Remove the trailing ambersand char last 3 chars - %26
            baseString = baseString.Substring(0, baseString.Length - 3);

            //Build the signing key
            string signingKey = Uri.EscapeDataString(oauth_consumer_secret) + "&" + Uri.EscapeDataString(oauth_token_secret);

            //Sign the request
            HMACSHA1 hasher         = new HMACSHA1(new ASCIIEncoding().GetBytes(signingKey));
            string   oauthsignature = Convert.ToBase64String(hasher.ComputeHash(new ASCIIEncoding().GetBytes(baseString)));

            //Tell Twitter we don't do the 100 continue thing
            ServicePointManager.Expect100Continue = false;

            //authorization header
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@url);
            string         authorizationHeaderParams = String.Empty;

            authorizationHeaderParams += "OAuth ";
            authorizationHeaderParams += "oauth_nonce=\"" + Uri.EscapeDataString(oauth_nonce) + "\",";
            authorizationHeaderParams += "oauth_signature_method=\"" + Uri.EscapeDataString(oauth_signature_method) + "\",";
            authorizationHeaderParams += "oauth_timestamp=\"" + Uri.EscapeDataString(oauthtimestamp) + "\",";
            authorizationHeaderParams += "oauth_consumer_key=\"" + Uri.EscapeDataString(oauth_consumer_key) + "\",";
            authorizationHeaderParams += "oauth_token=\"" + Uri.EscapeDataString(oauth_token) + "\",";
            authorizationHeaderParams += "oauth_signature=\"" + Uri.EscapeDataString(oauthsignature) + "\",";
            authorizationHeaderParams += "oauth_version=\"" + Uri.EscapeDataString(oauth_version) + "\"";
            webRequest.Headers.Add("Authorization", authorizationHeaderParams);

            webRequest.Method      = "GET";
            webRequest.ContentType = "application/x-www-form-urlencoded";

            TweetCount tc = new TweetCount();

            //Allow us a reasonable timeout in case Twitter's busy
            webRequest.Timeout = 3 * 60 * 1000;

            try
            {
                //Proxy settings
                HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
                Stream          dataStream  = webResponse.GetResponseStream();

                // Open the stream using a StreamReader for easy access.
                StreamReader reader             = new StreamReader(dataStream);
                string       responseFromServer = reader.ReadToEnd();

                XmlDocument doc   = ConvertJsonToXml(responseFromServer);
                XmlNodeList items = doc.GetElementsByTagName("item");
                tc.Count  = Convert.ToString(items.Count);
                tc.Tweets = new List <Tweets>();
                XmlNodeList tweets = doc.GetElementsByTagName("text");
                foreach (XmlNode t in tweets)
                {
                    Tweets tt = new Tweets();
                    tt.Text = t.InnerText;
                    tc.Tweets.Add(tt);
                }
            }
            catch (Exception ex)
            {
                tc.Error = ex.Message;
            }
            return(tc);
        }