Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.03 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Windows;
  7. using System.Windows.Media;
  8. using Path = System.IO.Path;
  9.  
  10. namespace InstaParser
  11. {
  12.     /// <summary>
  13.     /// Логика взаимодействия для MainWindow.xaml
  14.     /// </summary>
  15.     public partial class MainWindow : Window
  16.     {
  17.         public string Config { get; } = "config.txt";
  18.         public string Settings { get; } = "settings.txt";
  19.         public string HtmlGenerated { get; } = "file.html";
  20.         public string HtmlPreset { get; } = "sample.html";
  21.         public bool IsSettingsOpened { get; set; } = false;
  22.  
  23.         public MainWindow()
  24.         {
  25.             InitializeComponent();
  26.             Loaded += Window_Loaded;
  27.         }
  28.  
  29.         private void Window_Loaded(object sender, RoutedEventArgs e)
  30.         {
  31.             if (File.Exists(Config))
  32.             {
  33.                 var lines = File.ReadLines(Config).ToList();
  34.                 for (var i = 1; i < lines.Count; i++)
  35.                 {
  36.                     if (i + 1 != lines.Count)
  37.                     {
  38.                         HrefTextBox.Text += "https://www.instagram.com/p/" + lines[i] + Environment.NewLine;
  39.                     }
  40.                     else
  41.                     {
  42.                         HrefTextBox.Text += "https://www.instagram.com/p/" + lines[i];
  43.                     }
  44.                 }
  45.             }
  46.  
  47.             if (File.Exists(Settings))
  48.             {
  49.                 try
  50.                 {
  51.                     var lines = File.ReadLines(Settings).ToList();
  52.                     HeightTextBox.Text = lines[0];
  53.                     WidthTextBox.Text = lines[1];
  54.                     TopTextBox.Text = lines[2];
  55.                     RightTextBox.Text = lines[3];
  56.                     LeftTextBox.Text = lines[4];
  57.                 }
  58.                 catch (Exception exception)
  59.                 {
  60.                     MessageBox.Show($"Ошибка чтения файла с настройками: {exception}", "Ошибка", MessageBoxButton.OK,
  61.                         MessageBoxImage.Error);
  62.                 }
  63.             }
  64.         }
  65.  
  66.         private void HrefBtn_Click(object sender, RoutedEventArgs e)
  67.         {
  68.             if (HrefTextBox.Text.Length > 0)
  69.             {
  70.                 var lines = HrefTextBox.Text.Split(Environment.NewLine.ToCharArray());
  71.                 string text = "";
  72.                 for (int i = 0; i < lines.Length; i++)
  73.                 {
  74.                     Uri uri = new Uri(lines[i]);
  75.                     text += uri.Segments[2].Replace("/", "");
  76.                     if (i + 1 != lines.Length)
  77.                     {
  78.                         text += Environment.NewLine;
  79.                     }
  80.                 }
  81.                 File.WriteAllText(Config, HtmlPreset + Environment.NewLine + text);
  82.                 var process = Process.Start("parser.exe");
  83.                 process?.WaitForExit();
  84.                 if (File.Exists(HtmlGenerated))
  85.                 {
  86.                     HrefTextBox.Foreground = new SolidColorBrush(Color.FromRgb(242, 140, 164));
  87.                     HrefTextBox.BorderBrush = new SolidColorBrush(Color.FromRgb(242, 140, 164));
  88.                     HrefBtn.Background = new SolidColorBrush(Color.FromRgb(242, 140, 164));
  89.                     HrefBtn.BorderBrush = new SolidColorBrush(Color.FromRgb(242, 140, 164));
  90.                     HrefText.Foreground = new SolidColorBrush(Color.FromRgb(242, 140, 164));
  91.                     PreviewBtn.Opacity = 1.0;
  92.                     SettingsBtn.Opacity = 1.0;
  93.                     GeneratePdfBtn.Opacity = 1.0;
  94.                     Win.Height = 190;
  95.                 }
  96.                 else
  97.                 {
  98.                     MessageBox.Show("Ошибка в работе скрипта, файл шаблона не был сгенерирован");
  99.                 }
  100.             }
  101.         }
  102.  
  103.         private void PreviewBtn_Click(object sender, RoutedEventArgs e)
  104.         {
  105.             try
  106.             {
  107.                 Process.Start("chrome.exe", "file.html");
  108.             }
  109.             catch (Exception exception)
  110.             {
  111.                 MessageBox.Show($"Ошибка: {exception}");
  112.             }
  113.         }
  114.  
  115.         private void GeneratePdfBtn_Click(object sender, RoutedEventArgs e)
  116.         {
  117.             try
  118.             {
  119.                 var genPdf = Process.Start($"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}/wkhtmltopdf/bin/wkhtmltopdf.exe", $"--page-height {HeightTextBox.Text}cm --page-width {WidthTextBox.Text}cm -T {TopTextBox.Text}cm -R {RightTextBox.Text}cm -B {BottomTextBox.Text}cm -L {LeftTextBox.Text}cm file.html out.pdf");
  120.                 genPdf?.WaitForExit();
  121.                 var result = MessageBox.Show("PDF файл успешно создан. Открыть?", "Успешно", MessageBoxButton.YesNo, MessageBoxImage.Information);
  122.                 if (result == MessageBoxResult.Yes)
  123.                 {
  124.                     Process.Start("out.pdf");
  125.                 }
  126.             }
  127.             catch (Exception exception)
  128.             {
  129.                 MessageBox.Show($"Ошибка: {exception}");
  130.             }
  131.         }
  132.  
  133.         private void SettingsBtn_Click(object sender, RoutedEventArgs e)
  134.         {
  135.             if (!IsSettingsOpened)
  136.             {
  137.                 IsSettingsOpened = true;
  138.                 SettingsGrid.Opacity = 1.0;
  139.                 Win.Height = 380;
  140.                 SettingsBtn.Foreground = new Solid)ColorBrush(Color.FromRgb(242, 140, 164));
  141.                 SettingsBtn.BorderBrush = new SolidColorBrush(Color.FromRgb(242, 140, 164));
  142.             }
  143.             else
  144.             {
  145.                 IsSettingsOpened = false;
  146.                 SettingsGrid.Opacity = 0.0;
  147.                 Win.Height = 190;
  148.                 SettingsBtn.Foreground = new SolidColorBrush(Color.FromRgb(154, 187, 218));
  149.                 SettingsBtn.BorderBrush = new SolidColorBrush(Color.FromRgb(154, 187, 218));
  150.             }
  151.         }
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement