コード例 #1
0
ファイル: TwitterClient.cs プロジェクト: Fennir-k/FavoImgs
        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);
        }
コード例 #2
0
ファイル: TwitterClient.cs プロジェクト: Fennir-k/FavoImgs
        private void DownloadFilesFromTweet(Options options, Status twt)
        {
            string twtxt = ShowTweet(twt);

            Console.WriteLine(twtxt);

            string downloadRootPath = PathHelper.GetSubDirectoryName(options);

            if (!Directory.Exists(downloadRootPath))
            {
                Directory.CreateDirectory(downloadRootPath);
            }

            var downloadItems = new List <DownloadItem>();

            try
            {
                TweetHelper.GetMediaUris(twt, ref downloadItems);
            }
            catch (Exception ex)
            {
                ConsoleHelper.WriteException(ex);
            }

            var tempPath = Path.GetTempPath();

            Statistics.Current.DownloadCount += downloadItems.Count;
            for (int j = 0; j < downloadItems.Count; ++j)
            {
                if (TweetCache.IsImageTaken(downloadItems[j].TweetId, downloadItems[j].Uri.ToString()))
                {
                    ConsoleHelper.WriteColoredLine(ConsoleColor.DarkRed, " - {0} ({1})", downloadItems[j].Uri, Strings.AlreadyDownloaded);
                    continue;
                }

                string tempFilePath   = Path.Combine(tempPath, downloadItems[j].FileName);
                string targetFilePath = String.Empty;
                switch (options.GroupBy)
                {
                default:
                case GroupBy.None:
                    targetFilePath = downloadItems[j].FileName;
                    break;

                case GroupBy.ScreenName:
                    if (options.TweetSource != TweetSource.Tweets)
                    {
                        var upperFilePath = Path.Combine(downloadRootPath, twt.User.ScreenName);
                        if (!Directory.Exists(upperFilePath))
                        {
                            Directory.CreateDirectory(upperFilePath);
                        }

                        targetFilePath = Path.Combine(twt.User.ScreenName, downloadItems[j].FileName);
                    }
                    else
                    {
                        targetFilePath = downloadItems[j].FileName;
                    }
                    break;
                }

                string realFilePath = Path.Combine(downloadRootPath, targetFilePath);
                long   tweetId      = downloadItems[j].TweetId;
                Uri    uri          = downloadItems[j].Uri;

                DownloadFile(tempFilePath, realFilePath, tweetId, uri);
            }

            Console.WriteLine();
        }
コード例 #3
0
ファイル: TwitterClient.cs プロジェクト: Fennir-k/FavoImgs
        private CoreTweetResponse GetResponse(
            Tokens tokens, Options options, Dictionary <string, object> arguments)
        {
            CoreTweetResponse response = new CoreTweetResponse();

            ConsoleHelper.WriteColoredLine(ConsoleColor.Yellow, " [] {0}", Strings.GetTweetsFromTwitter);
            logger.Info(Strings.GetTweetsFromTwitter);

            switch (options.TweetSource)
            {
            case TweetSource.Favorites:
            {
                arguments.Add("screen_name", options.ScreenName);
                var result = tokens.Favorites.List(arguments);
                response.Tweets    = result.ToList();
                response.RateLimit = result.RateLimit;
            }
            break;

            case TweetSource.Tweets:
            {
                if (options.ExcludeRetweets)
                {
                    arguments.Add("include_rts", "false");
                }

                arguments.Add("screen_name", options.ScreenName);
                var result = tokens.Statuses.UserTimeline(arguments);
                response.Tweets    = result.ToList();
                response.RateLimit = result.RateLimit;
            }
            break;

            case TweetSource.Lists:
            {
                if (options.ExcludeRetweets)
                {
                    arguments.Add("include_rts", "false");
                }

                arguments.Add("slug", options.Slug);
                arguments.Add("owner_screen_name", options.ScreenName);
                var result = tokens.Lists.Statuses(arguments);
                response.Tweets    = result.ToList();
                response.RateLimit = result.RateLimit;
            }
            break;


            case TweetSource.Hashtag:
            {
                String query = String.Format("{0} filter:images -filter:retweets", options.Hashtag);
                arguments.Add("q", query);
                var result = tokens.Search.Tweets(arguments);
                response.Tweets    = result.ToList();
                response.RateLimit = result.RateLimit;
            }

                /*
                 * case TweetSource.Search:
                 * {
                 * arguments.Add("q", options.Query);
                 * var result = tokens.Search.Tweets(arguments);
                 * response.Tweets = result.ToList();
                 * response.RateLimit = result.RateLimit;
                 * }
                 * */
                break;
            }

            return(response);
        }