Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using AngleSharp;
- using AngleSharp.Dom;
- using MySql.Data.MySqlClient;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace kazanhouse
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- label2.Text = "";
- string curr_version = "1,03";
- String id = "";
- String flag = "";
- String version = "";
- String date = "";
- String message = "";
- String error = "";
- MySql.Data.MySqlClient.MySqlConnection conn;
- string myConnectionString;
- myConnectionString = "server=***;uid=***;" +
- "pwd=!***;database=***;";
- try
- {
- conn = new MySql.Data.MySqlClient.MySqlConnection();
- conn.ConnectionString = myConnectionString;
- conn.Open();
- Console.WriteLine("Подключение к БД");
- MySqlCommand cmd = conn.CreateCommand();
- cmd.CommandText = "SELECT * FROM app ORDER BY id DESC Limit 1;";
- MySqlDataReader reader = cmd.ExecuteReader();
- while (reader.Read())
- {
- id = reader.GetString(0);
- flag = reader.GetString(1);
- version = reader.GetString(2);
- date = reader.GetString(3);
- message = reader.GetString(4);
- error = reader.GetString(5);
- Console.WriteLine("Last version: " + version);
- }
- reader.Close();
- conn.Close();
- Console.WriteLine(version);
- Console.WriteLine(curr_version);
- Console.WriteLine(String.Compare(version, curr_version));
- if (version != curr_version)
- {
- Console.WriteLine("Вышла новая версия приложения.");
- Process.Start("http://***/avitophoto.rar");
- MessageBox.Show(error);
- this.Close();
- Application.Exit();
- Environment.Exit(0);
- }
- }
- catch (MySql.Data.MySqlClient.MySqlException ex)
- {
- MessageBox.Show("Не удалось подключиться к удаленному серверу");
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- button1.Visible = false;
- textBox1.Visible = true;
- textBox1.Text = "";
- button2.Visible = true;
- }
- private void button2_Click(object sender, EventArgs e)
- {
- textBox1.ReadOnly = true;
- if (textBox1.Text.Contains("irr.ru/") == true)
- {
- label9.Text = "irr";
- DownloadImagesFromLinkIrr(textBox1.Text);
- }
- else
- {
- label9.Text = "avito";
- DownloadImagesFromLinkAvito(textBox1.Text);
- }
- label2.Text = "Количество скачанных фотографий: 0";
- button3.Visible = true;
- label3.Visible = true;
- label4.Visible = true;
- label5.Visible = true;
- label6.Visible = true;
- label7.Visible = true;
- label8.Visible = true;
- textBox2.Visible = true;
- button4.Visible = true;
- }
- public async void DownloadImagesFromLinkAvito(String url)
- {
- DateTime thisDay = DateTime.Now;
- Random rand = new Random();
- int temp;
- temp = rand.Next(11,77);
- String filename = thisDay.ToString("MM_dd_yyyy_HH_mm") + temp;
- DirectoryInfo di = Directory.CreateDirectory("C:/avitophoto/" + filename);
- label3.Text = "C:\\avitophoto\\" + filename;
- // Setup the configuration to support document loading
- var config = Configuration.Default.WithDefaultLoader();
- // Load the names of all The Big Bang Theory episodes from Wikipedia
- var address = url;
- // Asynchronously get the document in a new context using the configuration
- var document = await BrowsingContext.New(config).OpenAsync(address);
- String img = "";
- String full_filename = "";
- int i = 0;
- int b = 0;
- foreach (IElement element in document.QuerySelectorAll("meta"))
- {
- img = element.GetAttribute("content");
- if (b == 0)
- {
- b++; continue;
- }
- if (img.Contains("/640x480/") == true)
- {
- if (b == 1)
- {
- b++; continue;
- }
- //MessageBox.Show(img);
- //Console.WriteLine(img);
- WebClient client = new WebClient();
- //Uri uri = new Uri("http:" + img.Replace("/80x60/", "/640x480/"))
- Uri uri = new Uri(img);
- full_filename = "C:/avitophoto/" + filename + "/" + (++i).ToString() + ".jpg";
- client.DownloadFileAsync(uri, full_filename);
- Console.WriteLine("Картинка скачана");
- label2.Text = "Количество скачанных фотографий: " + i;
- }
- }
- }
- public async void DownloadImagesFromLinkIrr(String url)
- {
- DateTime thisDay = DateTime.Now;
- Random rand = new Random();
- int temp;
- temp = rand.Next(11, 77);
- String filename = thisDay.ToString("MM_dd_yyyy_HH_mm") + temp;
- DirectoryInfo di = Directory.CreateDirectory("C:/avitophoto/" + filename);
- label3.Text = "C:\\avitophoto\\" + filename;
- // Setup the configuration to support document loading
- var config = Configuration.Default.WithDefaultLoader().WithCookies();
- // Load the names of all The Big Bang Theory episodes from Wikipedia
- var address = url;
- // Asynchronously get the document in a new context using the configuration
- var document = await BrowsingContext.New(config).OpenAsync(address);
- String img = "";
- String full_filename = "";
- int i = 0;
- int b = 0;
- foreach (IElement element in document.QuerySelectorAll("meta"))
- {
- if(element.GetAttribute("itemprop") == "image")
- {
- img = element.GetAttribute("content");
- //MessageBox.Show(img);
- if (img.Length > 0)
- {
- if (img.Contains("-orig.jpg") == true)
- {
- if (b == 1)
- {
- b++; continue;
- }
- //MessageBox.Show(img);
- //Console.WriteLine(img);
- WebClient client = new WebClient();
- //Uri uri = new Uri("http:" + img.Replace("/80x60/", "/640x480/"))
- Uri uri = new Uri(img);
- full_filename = "C:/avitophoto/" + filename + "/" + (++i).ToString() + ".jpg";
- client.DownloadFileAsync(uri, full_filename);
- Console.WriteLine("Картинка скачана");
- label2.Text = "Количество скачанных фотографий: " + i;
- }
- }
- }
- }
- }
- private void button3_Click(object sender, EventArgs e)
- {
- if (Directory.Exists(label3.Text))
- {
- Process.Start(label3.Text);
- }
- }
- private void textBox2_TextChanged(object sender, EventArgs e)
- {
- if (System.Text.RegularExpressions.Regex.IsMatch(textBox2.Text, "[^0-9]"))
- {
- MessageBox.Show("ID объекта может состоять только из цифр");
- textBox2.Text = textBox2.Text.Remove(textBox2.Text.Length - 1);
- }
- }
- private void button4_Click(object sender, EventArgs e)
- {
- if(textBox2.TextLength > 0)
- {
- int i = 0;
- string[] dirs = Directory.GetFiles(label3.Text, "*.jpg");
- if(dirs.Length == 0)
- {
- MessageBox.Show("В папке нет фотографий");
- }
- else
- {
- button4.Visible = false;
- foreach (string dir in dirs)
- {
- UploadImageByFTP(++i, dir, textBox2.Text, label9.Text);
- }
- MessageBox.Show("Все фотографии были успешно загружены и обработаны");
- newObject();
- }
- }
- else
- {
- MessageBox.Show("Необходимо указать ID объекта");
- }
- }
- static void UploadImageByFTP(int i, String filename, String flat_id, String type)
- {
- String newfilename = "C:/avitophoto/" + flat_id + "-" + (i).ToString() + ".jpg";
- //System.IO.File.Move(filename, newfilename);
- using (WebClient client = new WebClient())
- {
- try
- {
- client.Credentials = new NetworkCredential("***", "***");
- //client.UploadFile("ftp://***/www/***/images/kazan/" + Path.GetFileName(newfilename), WebRequestMethods.Ftp.UploadFile, @filename);
- client.UploadFile("ftp://***/www/***/images/kazan/" + flat_id + "-" + (i).ToString() + ".jpg", WebRequestMethods.Ftp.UploadFile, @filename);
- Console.WriteLine("Картинка закачана");
- }
- catch (WebException ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- System.Threading.Thread.Sleep(1000);
- if(type == "irr")
- {
- WebRequest request = WebRequest.Create("http://***/index-elphotoirr-" + flat_id + "-" + (i).ToString());
- WebResponse response = request.GetResponse();
- response.Close();
- }
- else
- {
- WebRequest request = WebRequest.Create("http://***/index-elphoto-" + flat_id + "-" + (i).ToString());
- WebResponse response = request.GetResponse();
- response.Close();
- }
- }
- public void newObject()
- {
- button1.Visible = true;
- textBox1.ReadOnly = false;
- textBox1.Text = "";
- textBox1.Visible = false;
- label2.Text = "Количество скачанных фотографий: 0";
- textBox2.Text = "";
- button3.Visible = false;
- label2.Visible = false;
- label3.Visible = false;
- label4.Visible = false;
- label5.Visible = false;
- label6.Visible = false;
- label7.Visible = false;
- label8.Visible = false;
- textBox2.Visible = false;
- button4.Visible = false;
- button2.Visible = false;
- button3.Visible = false;
- }
- private void label1_Click(object sender, EventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement