Пример #1
0
        public int Run(string[] args)
        {
            logger.Info("Arguments: {0}", String.Join(" ", args));

            var options = new Options();

            if (CommandLine.Parser.Default.ParseArguments(args, options))
            {
                if (!ApplyOptions(options))
                {
                    return(0);
                }
            }
            else
            {
                return(0);
            }

            CheckDownloadPath();

            string consumerKey       = Settings.Current.ConsumerKey;
            string consumerSecret    = Settings.Current.ConsumerSecret;
            string accessToken       = Settings.Current.AccessToken;
            string accessTokenSecret = Settings.Current.AccessTokenSecret;

            try
            {
                if (!String.IsNullOrEmpty(accessToken))
                {
                    accessToken = RijndaelEncryption.DecryptRijndael(accessToken);
                }

                if (!String.IsNullOrEmpty(accessTokenSecret))
                {
                    accessTokenSecret = RijndaelEncryption.DecryptRijndael(accessTokenSecret);
                }
            }
            catch (Exception ex)
            {
                logger.ErrorException(Strings.CannotReadOAuthToken, ex);
                Console.WriteLine("{0}", Strings.CannotReadOAuthToken);
                Console.ReadLine();
                return(1);
            }

            Tokens       tokens = null;
            UserResponse myInfo = null;

            try
            {
                tokens = GetTwitterToken(consumerKey, consumerSecret, accessToken, accessTokenSecret);
                myInfo = tokens.Account.VerifyCredentials();

                if (String.IsNullOrEmpty(options.ScreenName))
                {
                    options.ScreenName = myInfo.ScreenName;
                }
            }
            catch (Exception ex)
            {
                logger.ErrorException(ex.Message, ex);
                ConsoleHelper.WriteException(ex);
                Console.ReadLine();
                return(1);
            }

            if (String.IsNullOrEmpty(options.DownloadPath))
            {
                options.DownloadPath = Settings.Current.DownloadPath;
            }

            if (!Directory.Exists(options.DownloadPath))
            {
                try
                {
                    Directory.CreateDirectory(options.DownloadPath);
                    if (!Directory.Exists(options.DownloadPath))
                    {
                        Console.WriteLine("{0}", Strings.CannotCreateDownloadFolder);
                        return(1);
                    }
                }
                catch (Exception ex)
                {
                    logger.ErrorException(ex.Message, ex);
                    ConsoleHelper.WriteException(ex);
                    return(1);
                }
            }


            const int TWEET_COUNT_PER_API = 200;

            long maxId = 0;

            // 200 x 16 = 3200
            int left = 16;

            bool bRunning = true;

            while (bRunning)
            {
                Dictionary <string, object> arguments = new Dictionary <string, object>();
                arguments.Add("count", TWEET_COUNT_PER_API);
                if (maxId != 0)
                {
                    arguments.Add("max_id", maxId - 1);
                }

                CoreTweetResponse response = null;

                try
                {
                    response = GetResponse(tokens, options, arguments);
                    Console.WriteLine(" [] {0}: {1}", Strings.NumberOfFetchedTweets, response.Tweets.Count);
                    logger.Info("{0}: {1}", Strings.NumberOfFetchedTweets, response.Tweets.Count);
                }

                catch (WebException ex)
                {
                    ConsoleHelper.WriteColoredLine(ConsoleColor.Yellow, " [] {0}. {1}", ex.Message, Strings.TryAgain);
                    continue;
                }
                catch (TwitterException ex)
                {
                    // rate limit exceeded
                    if (ex.Status == (HttpStatusCode)429)
                    {
                        ConsoleHelper.WriteColoredLine(ConsoleColor.Yellow, " [] {0}", Strings.APIRateLimitExceeded);
                        Thread.Sleep(60 * 1000);
                        continue;
                    }
                    else
                    {
                        logger.ErrorException(ex.Message, ex);
                        ConsoleHelper.WriteException(ex);
                        Console.ReadLine();
                        return(1);
                    }
                }
                catch (Exception ex)
                {
                    logger.ErrorException(ex.Message, ex);
                    ConsoleHelper.WriteException(ex);
                    Console.ReadLine();
                    return(1);
                }

                foreach (var twt in response.Tweets)
                {
                    DownloadFilesFromTweet(options, twt);

                    if (maxId == 0)
                    {
                        maxId = twt.Id;
                    }
                    else
                    {
                        maxId = Math.Min(maxId, twt.Id);
                    }

                    Statistics.Current.TweetCount += 1;
                }

                ConsoleHelper.WriteColoredLine(ConsoleColor.Yellow,
                                               " [] API: {0}/{1}, Reset: {2}\n",
                                               response.RateLimit.Remaining, response.RateLimit.Limit, response.RateLimit.Reset.LocalDateTime);

                --left;

                if (left == 0 || response.Tweets.Count == 0)
                {
                    bRunning = false;
                }
            }

            Console.WriteLine("{0}", Strings.WorkComplete);
            Console.WriteLine();

            Console.WriteLine(" - Tweet(s): {0}", Statistics.Current.TweetCount);
            Console.WriteLine(" - Media Url(s): {0}", Statistics.Current.DownloadCount);
            Console.WriteLine(" - Downloaded file(s): {0}", Statistics.Current.DownloadedCount);

            Settings.Current.Save();
            return(0);
        }