Пример #1
0
        public EntryPage Work(string URI, string rootLocation, IFolderNamingStrategy subFolderGetter, string cookie)
        {
            LiveJournalTarget t          = LiveJournalTarget.FromString(URI);
            ILJClientData     cookieData = _ext.Client.CreateDataObject(cookie);

            // Get fresh version.
            log.InfoFormat("Extracting {0}...", t);
            EntryPage freshSource = _ext.GetFrom(t, cookieData);

            string     innerFolder;
            IEntryBase freshSourceEntry = freshSource.Entry;

            if (!subFolderGetter.TryGetSubfolderByEntry(freshSourceEntry, out innerFolder))
            {
                string error = string.Format(
                    "Cannot extract number from entry {0}, \"{1}\"."
                    , freshSourceEntry.Id
                    , freshSourceEntry.Subject
                    );
                throw new NotSupportedException(error);
            }

            string subFolder = string.Format("{0}\\{1}", freshSource.Entry.Date.Value.Year, innerFolder);

            string workLocation = _fs.Path.Combine(rootLocation, subFolder);

            log.Info("Will work from " + workLocation);

            EntryPage ep       = null;
            string    dumpFile = _fs.Path.Combine(workLocation, DumpFileName);

            if (_fs.File.Exists(dumpFile))
            {
                log.Info("File " + DumpFileName + " exists, will load it...");
                ep = _lp.ParseAsAnEntryPage(_fs.File.ReadAllText(dumpFile));
            }
            else
            {
                log.Info("File " + DumpFileName + " does not exist.");
            }

            bool needsSaving = _ext.AbsorbAllData(freshSource, cookieData, ref ep);

            log.Info("Will save changes: " + needsSaving + ".");
            if (needsSaving)
            {
                // Save the content as is.
                string content = _lp.Serialize(ep);
                _fs.Directory.CreateDirectory(workLocation);

                UTF8Encoding enc = new UTF8Encoding(true);
                _fs.File.WriteAllText(dumpFile, content, enc);

                // Pick usable comments.
                List <Comment[]> comments = _scp.Pick(ep);
                log.Info("Picked threads: " + comments.Count + ".");

                // Everything we want to store.
                var allData = new List <EntryBase>();
                allData.Add(ep.Entry);
                allData.AddRange(comments.SelectMany(a => a));

                log.Info("Making sure everything is saved.");
                _rds.EnsureAllIsSaved(allData, rootLocation, workLocation);
            }

            log.Info("Finished.");
            return(ep);
        }