Advertisement
Guest User

Untitled

a guest
Nov 10th, 2017
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using WordPressSharp.Models;
  11. using WordPressSharp;
  12. using WordPressSharp.Constants;
  13. using System.Net;
  14. using KBCsv;
  15.  
  16.  
  17. namespace wp_sharp
  18. {
  19.     public partial class Form1 : Form
  20.     {
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.         }
  25.  
  26.  
  27.         private void button1_Click(object sender, EventArgs e)
  28.         {
  29.             /*
  30.             var post = new Post
  31.             {
  32.                 PostType = "post",
  33.                 Title = "My Awesome Post",
  34.                 Content = "This is the content",
  35.                 PublishDateTime = DateTime.Now
  36.             };
  37.  
  38.             using (var client =  new WordPressClient(new WordPressSiteConfig
  39.             {
  40.                 BaseUrl = "http://wps.ru",
  41.                 Username = "admin",
  42.                 Password = "123",
  43.                 BlogId = 1
  44.             }))
  45. {
  46.  
  47.     //var id = Convert.ToInt32(client.NewPost(post));
  48. }
  49.        */
  50.  
  51.             foreach (string item in GetTitle())
  52.             {
  53.  
  54.                 if (isPost(item.ToString()) == false)
  55.                 {
  56.                     NewPost(item);
  57.                     MessageBox.Show(item+" +");
  58.                 }
  59.                 else
  60.                 {
  61.                     MessageBox.Show("Уже есть в базе");
  62.                 }
  63.             }
  64.         }
  65.         public static void NewPost(string title)
  66.         {
  67.            
  68.         var post = new Post
  69.         {
  70.             PostType = "post",
  71.             Title = title,
  72.             Content = "This is the content",
  73.             Status = "publish",
  74.             PublishDateTime = DateTime.Now
  75.         };
  76.  
  77.         using (var client =  new WordPressClient(new WordPressSiteConfig
  78.         {
  79.             BaseUrl = "http://wps.ru",
  80.             Username = "admin",
  81.             Password = "123",
  82.             BlogId = 1
  83.         }))
  84.         {
  85.  
  86.         var id = Convert.ToInt32(client.NewPost(post));
  87.         }
  88.  
  89.         }
  90.         public static bool isPost(string title)
  91.         {
  92.             using (var client = new WordPressClient(new WordPressSiteConfig
  93.             {
  94.                 BaseUrl = "http://wps.ru",
  95.                 Username = "admin",
  96.                 Password = "123",
  97.                 BlogId = 1
  98.             }))
  99.             {
  100.                 PostFilter r = new PostFilter();
  101.                 r.PostType = "post";
  102.  
  103.                 Post[] ten = client.GetPosts(r);
  104.  
  105.                 foreach (Post ipost in ten)
  106.                 {
  107.                     if (ipost.Title == title)
  108.                     {
  109.                         return true;
  110.                     }
  111.                 }
  112.      
  113.             }
  114.  
  115.             return false;
  116.         }
  117.         public static List<string> GetTitle()
  118.         {
  119.             List<string> list = new List<string>();
  120.             WebClient wb = new WebClient();
  121.             string html = wb.DownloadString("https://hit.ua/site_search/99424?csv=1");
  122.  
  123.             var csv = html;
  124.  
  125.             using (var reader = CsvReader.FromCsvString(csv))
  126.             {
  127.                 reader.ValueSeparator = ';';
  128.                 while (reader.HasMoreRecords)
  129.                 {
  130.                     var dataRecord = reader.ReadDataRecord();
  131.                     list.Add(CapitalizeFirstLetter(dataRecord[1]));
  132.                 }
  133.             }
  134.             return list;
  135.         }
  136.         public static string CapitalizeFirstLetter(string s)
  137.         {
  138.             if (String.IsNullOrEmpty(s))
  139.                 return s;
  140.             if (s.Length == 1)
  141.                 return s.ToUpper();
  142.             return s.Remove(1).ToUpper() + s.Substring(1);
  143.         }
  144.  
  145.  
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement