private static void GetImagesFromImgSrcTag(WPRecord wpr, string shortDate) { List <Uri> imagesFromSrc = FetchImagesFromSource(wpr.Content); foreach (Uri uri in imagesFromSrc) { string savedImage = SaveImage(uri, shortDate); if (!savedImage.Contains("Ошибка")) { Console.WriteLine("Картинка: " + savedImage); wpr.Content = wpr.Content.Replace(uri.ToString(), savedImage); } else { Console.WriteLine("" + savedImage); } } }
private static void AddToWordPress(WPRecord wpr) { var post = new WordPressSharp.Models.Post { PostType = wpr.PostType, Title = wpr.Title, Content = wpr.Content, PublishDateTime = wpr.PublishDateTime, Status = wpr.Status // "draft" or "publish" }; using (var client = new WordPressClient(new WordPressSiteConfig { BaseUrl = "http://dm", Username = "******", Password = "******" })) { var id = Convert.ToInt32(client.NewPost(post)); Console.WriteLine(id); } }
private static void GetImagesFormAHrefLinks(WPRecord wpr, string shortDate) { //Download original photos from hyerlinks found <a href=.... List <Uri> imageLinks = FetchImagesFromLinksSource(wpr.Content); foreach (Uri uri in imageLinks) { string savedImage = SaveImage(uri, shortDate); if (!savedImage.Contains("Ошибка")) { Console.WriteLine("Картинка: " + savedImage); wpr.Content = wpr.Content.Replace(uri.ToString(), savedImage); } else { Console.WriteLine("" + savedImage); } Console.WriteLine(uri); } }
static void Main(string[] args) { string filepath = @"C:\Users\Alexey\documents\visual studio 2017\Projects\ImportWP\ImportWP\data\allPostsSmall.xml"; var xml = XDocument.Load(filepath, LoadOptions.None); var query = from c in xml.Root.Descendants("post") //where Convert.ToDateTime(c.Attribute("date").Value) < DateTime.Parse("2016-01-01") select c; List <WPRecord> wprecords = new List <WPRecord>(); foreach (XElement title in query) { WPRecord wpr = new WPRecord(); try { wpr.PublishDateTime = Convert.ToDateTime(title.Attribute("date").Value); } catch (Exception ex) { Console.WriteLine("Неверный формат даты " + ex.Message); } wpr.Title = title.Element("title").Value; wpr.Content = title.Element("body").Value; wpr.PostType = "post"; wpr.Status = "publish"; wpr.Content = wpr.Content.Replace("\n", ""); string shortDate = wpr.PublishDateTime.ToString("yyyy-MM-dd"); RegexOptions options = RegexOptions.None; Regex regex = new Regex("[ ]{2,}", options); wpr.Content = regex.Replace(wpr.Content, " "); //< !--more-- > //string ljCutRe = @"<lj-cut[^>]*?text\s*=\s*[""']?([^'"" >]+?)[ '""][^>]*?>"; string ljCutPattern = @"<lj-cut\s*?text\s*=\s*[""']?([^'"">]+?)['""][^>]*?>"; Regex ljCutRegex = new Regex(ljCutPattern, RegexOptions.IgnoreCase); wpr.Content = ljCutRegex.Replace(wpr.Content, "<!--more$1-->"); wpr.Content = wpr.Content.Replace("</lj-cut>", ""); //Console.WriteLine(wpr.Content); //MatchCollection ljcuts = Regex.Matches(wpr.Content, ljCutPattern, RegexOptions.IgnoreCase); //foreach (Match match in ljcuts) //{ // Console.WriteLine("0: " + match.Groups[0]); // Console.WriteLine("1: " + match.Groups[1]); // Console.WriteLine("2: " + match.Groups[2]); //} GetImagesFormAHrefLinks(wpr, shortDate); GetImagesFromImgSrcTag(wpr, shortDate); AddToWordPress(wpr); } }