Пример #1
0
 public TweetReceivedHandler(ILog log, IEmotionDetector emotionDetector, EmotionPersister emotionPersister, TweetCache tweetCache)
 {
     _log              = log;
     _emotionDetector  = emotionDetector;
     _emotionPersister = emotionPersister;
     _tweetCache       = tweetCache;
 }
Пример #2
0
        private static void DownloadFile(string tempFilePath, string realFilePath, long tweetId, Uri uri)
        {
            try
            {
                WebClient wc = new WebClient();

                ConsoleHelper.WriteColoredLine(ConsoleColor.DarkCyan, " - {0}", uri);
                wc.DownloadFile(uri, tempFilePath);

                // 확장자가 붙지 않았을 경우, Content-Type으로 추론
                if (!Path.HasExtension(tempFilePath))
                {
                    string extension   = MimeHelper.GetFileExtension(wc.ResponseHeaders["Content-Type"]);
                    string newFilePath = String.Format("{0}{1}", tempFilePath, extension);

                    File.Move(tempFilePath, newFilePath);
                    tempFilePath = newFilePath;
                    realFilePath = String.Format("{0}{1}", realFilePath, extension);
                }


                // 탐색기 섬네일 캐시 문제로 인하여 임시 폴더에서 파일을 받은 다음, 해당 폴더로 이동
                File.Move(tempFilePath, realFilePath);

                TweetCache.Add(tweetId, uri.ToString());

                Statistics.Current.DownloadedCount += 1;
            }
            catch (Exception ex)
            {
                ConsoleHelper.WriteException(ex);
                logger.InfoException(String.Format("{0} - {1}", ex.Message, uri.ToString()), ex);

                if (File.Exists(tempFilePath))
                {
                    File.Delete(tempFilePath);
                }
            }
        }
Пример #3
0
        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();
        }