Advertisement
Guest User

privet ilya

a guest
Dec 5th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.24 KB | None | 0 0
  1. using AngleSharp;
  2. using AngleSharp.Dom;
  3. using MySql.Data.MySqlClient;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Diagnostics;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Net;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16.  
  17. namespace kazanhouse
  18. {
  19.     public partial class Form1 : Form
  20.     {
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.             label2.Text = "";
  25.             string curr_version = "1,03";
  26.             String id = "";
  27.             String flag = "";
  28.             String version = "";
  29.             String date = "";
  30.             String message = "";
  31.             String error = "";
  32.             MySql.Data.MySqlClient.MySqlConnection conn;
  33.             string myConnectionString;
  34.  
  35.             myConnectionString = "server=***;uid=***;" +
  36.                 "pwd=!***;database=***;";
  37.  
  38.             try
  39.             {
  40.                 conn = new MySql.Data.MySqlClient.MySqlConnection();
  41.                 conn.ConnectionString = myConnectionString;
  42.                 conn.Open();
  43.                 Console.WriteLine("Подключение к БД");
  44.                 MySqlCommand cmd = conn.CreateCommand();
  45.                 cmd.CommandText = "SELECT * FROM app ORDER BY id DESC Limit 1;";
  46.                 MySqlDataReader reader = cmd.ExecuteReader();
  47.                 while (reader.Read())
  48.                 {
  49.                     id = reader.GetString(0);
  50.                     flag = reader.GetString(1);
  51.                     version = reader.GetString(2);
  52.                     date = reader.GetString(3);
  53.                     message = reader.GetString(4);
  54.                     error = reader.GetString(5);
  55.                     Console.WriteLine("Last version: " + version);
  56.                 }
  57.                 reader.Close();
  58.                 conn.Close();
  59.                 Console.WriteLine(version);
  60.                 Console.WriteLine(curr_version);
  61.                 Console.WriteLine(String.Compare(version, curr_version));
  62.                 if (version != curr_version)
  63.                 {
  64.                     Console.WriteLine("Вышла новая версия приложения.");
  65.                     Process.Start("http://***/avitophoto.rar");
  66.                     MessageBox.Show(error);
  67.                     this.Close();
  68.                     Application.Exit();
  69.                     Environment.Exit(0);
  70.                 }
  71.             }
  72.             catch (MySql.Data.MySqlClient.MySqlException ex)
  73.             {
  74.                 MessageBox.Show("Не удалось подключиться к удаленному серверу");
  75.             }
  76.         }
  77.  
  78.         private void button1_Click(object sender, EventArgs e)
  79.         {
  80.             button1.Visible = false;
  81.             textBox1.Visible = true;
  82.             textBox1.Text = "";
  83.             button2.Visible = true;
  84.         }
  85.  
  86.         private void button2_Click(object sender, EventArgs e)
  87.         {
  88.             textBox1.ReadOnly = true;
  89.             if (textBox1.Text.Contains("irr.ru/") == true)
  90.             {
  91.                 label9.Text = "irr";
  92.                 DownloadImagesFromLinkIrr(textBox1.Text);
  93.             }
  94.             else
  95.             {
  96.                 label9.Text = "avito";
  97.                 DownloadImagesFromLinkAvito(textBox1.Text);
  98.             }
  99.             label2.Text = "Количество скачанных фотографий: 0";
  100.             button3.Visible = true;
  101.             label3.Visible = true;
  102.             label4.Visible = true;
  103.             label5.Visible = true;
  104.             label6.Visible = true;
  105.             label7.Visible = true;
  106.             label8.Visible = true;
  107.             textBox2.Visible = true;
  108.             button4.Visible = true;
  109.         }
  110.  
  111.         public async void DownloadImagesFromLinkAvito(String url)
  112.         {
  113.             DateTime thisDay = DateTime.Now;
  114.             Random rand = new Random();
  115.             int temp;
  116.             temp = rand.Next(11,77);
  117.             String filename = thisDay.ToString("MM_dd_yyyy_HH_mm") + temp;
  118.             DirectoryInfo di = Directory.CreateDirectory("C:/avitophoto/" + filename);
  119.             label3.Text = "C:\\avitophoto\\" + filename;
  120.             // Setup the configuration to support document loading
  121.             var config = Configuration.Default.WithDefaultLoader();
  122.             // Load the names of all The Big Bang Theory episodes from Wikipedia
  123.             var address = url;
  124.             // Asynchronously get the document in a new context using the configuration
  125.             var document = await BrowsingContext.New(config).OpenAsync(address);
  126.            
  127.             String img = "";
  128.             String full_filename = "";
  129.  
  130.             int i = 0;
  131.             int b = 0;
  132.             foreach (IElement element in document.QuerySelectorAll("meta"))
  133.             {
  134.                 img = element.GetAttribute("content");
  135.                 if (b == 0)
  136.                 {
  137.                     b++; continue;
  138.                 }              
  139.                 if (img.Contains("/640x480/") == true)
  140.                 {
  141.                     if (b == 1)
  142.                     {
  143.                         b++; continue;
  144.                     }
  145.                     //MessageBox.Show(img);
  146.                     //Console.WriteLine(img);
  147.                     WebClient client = new WebClient();
  148.                     //Uri uri = new Uri("http:" + img.Replace("/80x60/", "/640x480/"))
  149.                     Uri uri = new Uri(img);
  150.                     full_filename = "C:/avitophoto/" + filename + "/" + (++i).ToString() + ".jpg";
  151.                     client.DownloadFileAsync(uri, full_filename);
  152.                     Console.WriteLine("Картинка скачана");
  153.                     label2.Text = "Количество скачанных фотографий: " + i;
  154.                 }
  155.             }
  156.         }
  157.  
  158.         public async void DownloadImagesFromLinkIrr(String url)
  159.         {
  160.             DateTime thisDay = DateTime.Now;
  161.             Random rand = new Random();
  162.             int temp;
  163.             temp = rand.Next(11, 77);
  164.             String filename = thisDay.ToString("MM_dd_yyyy_HH_mm") + temp;
  165.             DirectoryInfo di = Directory.CreateDirectory("C:/avitophoto/" + filename);
  166.             label3.Text = "C:\\avitophoto\\" + filename;
  167.             // Setup the configuration to support document loading
  168.             var config = Configuration.Default.WithDefaultLoader().WithCookies();
  169.             // Load the names of all The Big Bang Theory episodes from Wikipedia
  170.             var address = url;
  171.             // Asynchronously get the document in a new context using the configuration
  172.             var document = await BrowsingContext.New(config).OpenAsync(address);
  173.  
  174.             String img = "";
  175.             String full_filename = "";
  176.  
  177.             int i = 0;
  178.             int b = 0;
  179.             foreach (IElement element in document.QuerySelectorAll("meta"))
  180.             {
  181.                 if(element.GetAttribute("itemprop") == "image")
  182.                 {
  183.                     img = element.GetAttribute("content");
  184.                     //MessageBox.Show(img);
  185.                     if (img.Length > 0)
  186.                     {
  187.                         if (img.Contains("-orig.jpg") == true)
  188.                         {
  189.                             if (b == 1)
  190.                             {
  191.                                 b++; continue;
  192.                             }
  193.                             //MessageBox.Show(img);
  194.                             //Console.WriteLine(img);
  195.                             WebClient client = new WebClient();
  196.                             //Uri uri = new Uri("http:" + img.Replace("/80x60/", "/640x480/"))
  197.                             Uri uri = new Uri(img);
  198.                             full_filename = "C:/avitophoto/" + filename + "/" + (++i).ToString() + ".jpg";
  199.                             client.DownloadFileAsync(uri, full_filename);
  200.                             Console.WriteLine("Картинка скачана");
  201.                             label2.Text = "Количество скачанных фотографий: " + i;
  202.                         }
  203.                     }
  204.                 }
  205.             }
  206.         }
  207.  
  208.         private void button3_Click(object sender, EventArgs e)
  209.         {
  210.             if (Directory.Exists(label3.Text))
  211.             {
  212.                 Process.Start(label3.Text);
  213.             }
  214.         }
  215.  
  216.         private void textBox2_TextChanged(object sender, EventArgs e)
  217.         {
  218.             if (System.Text.RegularExpressions.Regex.IsMatch(textBox2.Text, "[^0-9]"))
  219.             {
  220.                 MessageBox.Show("ID объекта может состоять только из цифр");
  221.                 textBox2.Text = textBox2.Text.Remove(textBox2.Text.Length - 1);
  222.             }
  223.         }
  224.  
  225.         private void button4_Click(object sender, EventArgs e)
  226.         {
  227.             if(textBox2.TextLength > 0)
  228.             {
  229.                 int i = 0;
  230.                 string[] dirs = Directory.GetFiles(label3.Text, "*.jpg");
  231.                 if(dirs.Length == 0)
  232.                 {
  233.                     MessageBox.Show("В папке нет фотографий");
  234.                 }
  235.                 else
  236.                 {
  237.                     button4.Visible = false;
  238.                     foreach (string dir in dirs)
  239.                     {
  240.                         UploadImageByFTP(++i, dir, textBox2.Text, label9.Text);
  241.                     }
  242.                     MessageBox.Show("Все фотографии были успешно загружены и обработаны");
  243.                     newObject();
  244.                 }
  245.             }
  246.             else
  247.             {
  248.                 MessageBox.Show("Необходимо указать ID объекта");
  249.             }
  250.         }
  251.  
  252.         static void UploadImageByFTP(int i, String filename, String flat_id, String type)
  253.         {
  254.             String newfilename = "C:/avitophoto/" + flat_id + "-" + (i).ToString() + ".jpg";
  255.             //System.IO.File.Move(filename, newfilename);
  256.             using (WebClient client = new WebClient())
  257.             {
  258.                 try
  259.                 {
  260.                     client.Credentials = new NetworkCredential("***", "***");
  261.                     //client.UploadFile("ftp://***/www/***/images/kazan/" + Path.GetFileName(newfilename), WebRequestMethods.Ftp.UploadFile, @filename);
  262.                     client.UploadFile("ftp://***/www/***/images/kazan/" + flat_id + "-" + (i).ToString() + ".jpg", WebRequestMethods.Ftp.UploadFile, @filename);
  263.                     Console.WriteLine("Картинка закачана");
  264.                 }
  265.                 catch (WebException ex)
  266.                 {
  267.                     Console.WriteLine(ex.Message);
  268.                 }
  269.             }
  270.             System.Threading.Thread.Sleep(1000);
  271.             if(type == "irr")
  272.             {
  273.                 WebRequest request = WebRequest.Create("http://***/index-elphotoirr-" + flat_id + "-" + (i).ToString());
  274.                 WebResponse response = request.GetResponse();
  275.                 response.Close();
  276.             }
  277.             else
  278.             {
  279.                 WebRequest request = WebRequest.Create("http://***/index-elphoto-" + flat_id + "-" + (i).ToString());
  280.                 WebResponse response = request.GetResponse();
  281.                 response.Close();
  282.             }
  283.         }
  284.  
  285.         public void newObject()
  286.         {
  287.             button1.Visible = true;
  288.             textBox1.ReadOnly = false;
  289.             textBox1.Text = "";
  290.             textBox1.Visible = false;
  291.             label2.Text = "Количество скачанных фотографий: 0";
  292.             textBox2.Text = "";
  293.             button3.Visible = false;
  294.             label2.Visible = false;
  295.             label3.Visible = false;
  296.             label4.Visible = false;
  297.             label5.Visible = false;
  298.             label6.Visible = false;
  299.             label7.Visible = false;
  300.             label8.Visible = false;
  301.             textBox2.Visible = false;
  302.             button4.Visible = false;
  303.             button2.Visible = false;
  304.             button3.Visible = false;
  305.         }
  306.  
  307.         private void label1_Click(object sender, EventArgs e)
  308.         {
  309.  
  310.         }
  311.     }
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement