示例#1
0
        public static IEnumerable <Status> GetStreamingTimeline(StreammingFilterOptions options)
        {
            if (options.User == null)
            {
                options.User = AuthenticatedUser.LoadCredentials();
            }

            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);

                List <KeyValuePair <string, string> > p = new List <KeyValuePair <string, string> >();

                foreach (var item in options.GetParameters())
                {
                    p.Add(item);
                }

                var formUrlEncodedContent = new FormUrlEncodedContent(p);
                formUrlEncodedContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");

                options.Track     = string.IsNullOrEmpty(options.Track) ? options.Track : Util.EncodeString(WebUtility.HtmlDecode(options.Track));
                options.Follow    = string.IsNullOrEmpty(options.Follow) ? options.Follow : Util.EncodeString(WebUtility.HtmlDecode(options.Follow));
                options.Locations = string.IsNullOrEmpty(options.Locations) ? options.Locations : Util.EncodeString(WebUtility.HtmlDecode(options.Locations));

                HttpRequestMessage request = OAuthHelper.GetRequest(HttpMethod.Post, STATUS_FILTER, options);
                request.Content = formUrlEncodedContent;

                var response = httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).Result;
                if (!response.IsSuccessStatusCode)
                {
                    throw new Exception($"{response.StatusCode}:{response.ReasonPhrase}");
                }

                using (var reader = new StreamReader(response.Content.ReadAsStreamAsync().Result))
                {
                    while (!reader.EndOfStream)
                    {
                        string json = reader.ReadLine();
                        if (!string.IsNullOrWhiteSpace(json))
                        {
                            Status status = Util.Deserialize <Status>(json);
                            if (status != null && !string.IsNullOrEmpty(status.text))
                            {
                                yield return(status);
                            }
                        }
                    }
                }
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            try
            {
                //Debug.Assert(false, "Attach VS here!");

                Console.OutputEncoding = new UTF8Encoding();

                PrintHeader();

                BaseAPI.SetMessageAction(message => Console.WriteLine(message));

                if (args.Length == 0)
                {
                    PrintTwits(Timeline.GetTimeline().Result);
                    return;
                }


                if (args[0].StartsWith("/"))
                {
                    string flag = args[0].ToLower().Trim();
                    switch (flag)
                    {
                    case CLEAR:
                        AuthenticatedUser.ClearCredentials();
                        Console.WriteLine("User credentials cleared!");
                        return;

                    case HELP:
                        ShowUsage();
                        return;

                    case TIME_LINE:
                        PrintTwits(Timeline.GetTimeline().Result);
                        return;

                    case MENTIONS:
                        PrintTwits(Mentions.GetMentions().Result);
                        return;

                    case SEARCH:
                        SearchOptions options = new SearchOptions {
                            Query = string.Join(" ", args).Substring(2), User = AuthenticatedUser.LoadCredentials()
                        };
                        PrintTwits(Search.SearchTweets(options).Result);
                        return;

                    case LIKES:
                        PrintTwits(Likes.GetUserLikes(new LikesOptions()).Result);
                        return;

                    case USER:
                        if (args.Length != 2)
                        {
                            throw new ArgumentNullException("screenname", "The user' screen name must be provided");
                        }
                        UserTimelineOptions usrOptions = new UserTimelineOptions {
                            ScreenName = args[1]
                        };
                        PrintTwits(UserTimeline.GetUserTimeline(usrOptions).Result);
                        return;

                    case STREAMING:
                        if (args.Length != 2)
                        {
                            throw new ArgumentNullException("streaming", "The track must be provided");
                        }
                        StreammingFilterOptions streamingOptions = new StreammingFilterOptions {
                            Track = args[1]
                        };
                        Console.WriteLine("Starting live streaming, press ctrl+c to quit");
                        foreach (Status s in StreamingFilter.GetStreamingTimeline(streamingOptions))
                        {
                            PrintTwit(s);
                        }
                        return;

                    default:
                        Console.WriteLine($"Invalid flag: {flag}");
                        ShowUsage();
                        return;
                    }
                }

                if (args[0].StartsWith("\\") || args[0].Length == 1)
                {
                    Console.WriteLine("Really? do you really wanna twit that?. [T]wit, or [N]o sorry, I messed up...");
                    ConsoleKeyInfo input = Console.ReadKey();
                    while (input.Key != ConsoleKey.T && input.Key != ConsoleKey.N)
                    {
                        Console.WriteLine();
                        Console.WriteLine("[T]wit, or [N]o sorry, I messed up...");
                        input = Console.ReadKey();
                    }
                    Console.WriteLine();

                    if (input.Key == ConsoleKey.N)
                    {
                        Console.WriteLine("That's what I thought! ;)");
                        return;
                    }
                }

                string response = Update.UpdateStatus(string.Join(" ", args)).Result;

                if (response != "OK")
                {
                    Console.WriteLine($"Response was not OK: {response}");
                }
            }
            catch (WebException wex)
            {
                Console.WriteLine(wex.Message);

                HttpWebResponse res = (HttpWebResponse)wex.Response;
                if (res != null)
                {
                    UpdateError errors = ShelltwitLib.Helpers.Util.Deserialize <UpdateError>(res.GetResponseStream());
                    errors.errors.ForEach(e => Console.WriteLine(e.ToString()));
                }
            }
            catch (Exception ex)
            {
                PrintException(ex);
            }
            finally
            {
#if DEBUG
                if (Debugger.IsAttached)
                {
                    Console.WriteLine("Press <enter> to exit...");
                    Console.ReadLine();
                }
#endif
            }

            Environment.Exit(0);
        }