Пример #1
0
        public async static Task <string> Lookup(string link)
        {
            try
            {
                string longUrl;
                if (ShortToLongUrl.TryGetValue(link, out longUrl))
                {
                    return(longUrl);
                }

                var url     = "http://api.longurl.org/v2/expand?format=json&url=" + OAuth.UrlEncode(link);
                var request = WebRequestWrapper.Create(new Uri(url));
                request.UserAgent = "tweetz/5.0";
                request.Timeout   = 1500;
                using (var response = await request.GetResponseAsync())
                {
                    var serializer      = new DataContractJsonSerializer(typeof(LongUrlResponse));
                    var longUrlResponse = (LongUrlResponse)serializer.ReadObject(response.GetResponseStream());
                    if (string.IsNullOrWhiteSpace(longUrlResponse.LongUrl) == false)
                    {
                        if (ShortToLongUrl.Count > 1000)
                        {
                            ShortToLongUrl.Clear();
                        }
                        ShortToLongUrl.TryAdd(link, longUrlResponse.LongUrl);
                        return(longUrlResponse.LongUrl);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return(link);
        }
Пример #2
0
        public static async Task <string> Lookup(string link)
        {
            try
            {
                string longUrl;
                if (ShortToLongUrl.TryGetValue(link, out longUrl))
                {
                    return(longUrl);
                }

                var request = WebRequestWrapper.Create(new Uri(link));
                request.Timeout   = 1500;
                request.Method    = "HEAD";
                request.UserAgent = "tweetz/5.0";
                using (var response = await request.GetResponseAsync())
                {
                    var uri = response.ResponseUri.AbsoluteUri;
                    if (string.IsNullOrWhiteSpace(uri) == false)
                    {
                        if (ShortToLongUrl.Count > 1000)
                        {
                            ShortToLongUrl.Clear();
                        }
                        ShortToLongUrl.TryAdd(link, uri);
                        return(uri);
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
            return(link);
        }
Пример #3
0
        private async static Task <string> WebRequest(string url, IEnumerable <string[]> parameters, bool post = false)
        {
            var oauth            = new OAuth();
            var nonce            = OAuth.Nonce();
            var timestamp        = OAuth.TimeStamp();
            var signature        = OAuth.Signature(post ? "POST" : "GET", url, nonce, timestamp, oauth.AccessToken, oauth.AccessTokenSecret, parameters);
            var authorizeHeader  = OAuth.AuthorizationHeader(nonce, timestamp, oauth.AccessToken, signature);
            var parameterStrings = parameters.Select(p => string.Format("{0}={1}", OAuth.UrlEncode(p[0]), OAuth.UrlEncode(p[1]))).ToList();

            if (!post)
            {
                url += "?" + string.Join("&", parameterStrings);
            }

            var request = WebRequestWrapper.Create(new Uri(url));

            request.Headers.Add("Authorization", authorizeHeader);
            request.Method = post ? "POST" : "GET";

            if (post)
            {
                Trace.TraceInformation(string.Join("&", parameterStrings));
                request.ContentType = "application/x-www-form-urlencoded";
                if (parameters != null)
                {
                    using (var requestStream = request.GetRequestStream())
                    {
                        WriteStream(requestStream, string.Join("&", parameterStrings));
                    }
                }
            }

            using (var response = await request.GetResponseAsync())
            {
                using (var stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    var result = stream.ReadToEnd();
                    return(result);
                }
            }
        }
Пример #4
0
        public static void User(CancellationToken cancelationToken)
        {
            Task.Run(() =>
            {
                while (cancelationToken.IsCancellationRequested == false)
                {
                    var delay = Task.Delay(30 * 1000, cancelationToken);
                    delay.Wait(cancelationToken);
                    if (delay.IsCanceled || delay.IsFaulted)
                    {
                        break;
                    }

                    Trace.TraceInformation("{ Start Twitter User Stream }");
                    const string url    = "https://userstream.twitter.com/1.1/user.json";
                    var oauth           = new OAuth();
                    var nonce           = OAuth.Nonce();
                    var timestamp       = OAuth.TimeStamp();
                    var signature       = OAuth.Signature("GET", url, nonce, timestamp, oauth.AccessToken, oauth.AccessTokenSecret, null);
                    var authorizeHeader = OAuth.AuthorizationHeader(nonce, timestamp, oauth.AccessToken, signature);

                    var request = WebRequestWrapper.Create(new Uri(url));
                    request.Headers.Add("Authorization", authorizeHeader);

                    try
                    {
                        using (var response = request.GetResponse())
                            using (var stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                            {
                                stream.BaseStream.ReadTimeout = 60 * 1000;
                                while (true)
                                {
                                    var json = stream.ReadLine();
                                    if (json == null)
                                    {
                                        Trace.TraceInformation("{ null }");
                                        break;
                                    }
                                    if (cancelationToken.IsCancellationRequested)
                                    {
                                        break;
                                    }
                                    Trace.TraceInformation(string.IsNullOrWhiteSpace(json) ? "{ Blankline }" : json);

                                    var serializer = new JavaScriptSerializer();
                                    var reply      = serializer.Deserialize <Dictionary <string, object> >(json);
                                    if (reply != null && reply.ContainsKey("user"))
                                    {
                                        Trace.TraceInformation("{ tweet identified }");
                                        var statuses = Status.ParseJson("[" + json + "]");
                                        Application.Current.Dispatcher.InvokeAsync
                                            (() => UpdateStatusHomeTimelineCommand.Command.Execute(statuses, Application.Current.MainWindow));
                                    }
                                }
                            }
                    }

                    catch (WebException ex)
                    {
                        Trace.TraceError(ex.ToString());
                    }

                    catch (ArgumentNullException ex)
                    {
                        Trace.TraceError(ex.ToString());
                    }

                    catch (ArgumentException ex)
                    {
                        Trace.TraceError(ex.ToString());
                    }

                    catch (InvalidOperationException ex)
                    {
                        Trace.TraceError(ex.ToString());
                    }

                    catch (IOException ex)
                    {
                        Trace.TraceError(ex.ToString());
                    }
                }

                Trace.TraceInformation("{ Stream task ends }");
            }, cancelationToken);
        }