Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Windows;
- using System.Windows.Media;
- using Path = System.IO.Path;
- namespace InstaParser
- {
- /// <summary>
- /// Логика взаимодействия для MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- public string Config { get; } = "config.txt";
- public string Settings { get; } = "settings.txt";
- public string HtmlGenerated { get; } = "file.html";
- public string HtmlPreset { get; } = "sample.html";
- public bool IsSettingsOpened { get; set; } = false;
- public MainWindow()
- {
- InitializeComponent();
- Loaded += Window_Loaded;
- }
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- if (File.Exists(Config))
- {
- var lines = File.ReadLines(Config).ToList();
- for (var i = 1; i < lines.Count; i++)
- {
- if (i + 1 != lines.Count)
- {
- HrefTextBox.Text += "https://www.instagram.com/p/" + lines[i] + Environment.NewLine;
- }
- else
- {
- HrefTextBox.Text += "https://www.instagram.com/p/" + lines[i];
- }
- }
- }
- if (File.Exists(Settings))
- {
- try
- {
- var lines = File.ReadLines(Settings).ToList();
- HeightTextBox.Text = lines[0];
- WidthTextBox.Text = lines[1];
- TopTextBox.Text = lines[2];
- RightTextBox.Text = lines[3];
- LeftTextBox.Text = lines[4];
- }
- catch (Exception exception)
- {
- MessageBox.Show($"Ошибка чтения файла с настройками: {exception}", "Ошибка", MessageBoxButton.OK,
- MessageBoxImage.Error);
- }
- }
- }
- private void HrefBtn_Click(object sender, RoutedEventArgs e)
- {
- if (HrefTextBox.Text.Length > 0)
- {
- var lines = HrefTextBox.Text.Split(Environment.NewLine.ToCharArray());
- string text = "";
- for (int i = 0; i < lines.Length; i++)
- {
- Uri uri = new Uri(lines[i]);
- text += uri.Segments[2].Replace("/", "");
- if (i + 1 != lines.Length)
- {
- text += Environment.NewLine;
- }
- }
- File.WriteAllText(Config, HtmlPreset + Environment.NewLine + text);
- var process = Process.Start("parser.exe");
- process?.WaitForExit();
- if (File.Exists(HtmlGenerated))
- {
- HrefTextBox.Foreground = new SolidColorBrush(Color.FromRgb(242, 140, 164));
- HrefTextBox.BorderBrush = new SolidColorBrush(Color.FromRgb(242, 140, 164));
- HrefBtn.Background = new SolidColorBrush(Color.FromRgb(242, 140, 164));
- HrefBtn.BorderBrush = new SolidColorBrush(Color.FromRgb(242, 140, 164));
- HrefText.Foreground = new SolidColorBrush(Color.FromRgb(242, 140, 164));
- PreviewBtn.Opacity = 1.0;
- SettingsBtn.Opacity = 1.0;
- GeneratePdfBtn.Opacity = 1.0;
- Win.Height = 190;
- }
- else
- {
- MessageBox.Show("Ошибка в работе скрипта, файл шаблона не был сгенерирован");
- }
- }
- }
- private void PreviewBtn_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- Process.Start("chrome.exe", "file.html");
- }
- catch (Exception exception)
- {
- MessageBox.Show($"Ошибка: {exception}");
- }
- }
- private void GeneratePdfBtn_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- 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");
- genPdf?.WaitForExit();
- var result = MessageBox.Show("PDF файл успешно создан. Открыть?", "Успешно", MessageBoxButton.YesNo, MessageBoxImage.Information);
- if (result == MessageBoxResult.Yes)
- {
- Process.Start("out.pdf");
- }
- }
- catch (Exception exception)
- {
- MessageBox.Show($"Ошибка: {exception}");
- }
- }
- private void SettingsBtn_Click(object sender, RoutedEventArgs e)
- {
- if (!IsSettingsOpened)
- {
- IsSettingsOpened = true;
- SettingsGrid.Opacity = 1.0;
- Win.Height = 380;
- SettingsBtn.Foreground = new Solid)ColorBrush(Color.FromRgb(242, 140, 164));
- SettingsBtn.BorderBrush = new SolidColorBrush(Color.FromRgb(242, 140, 164));
- }
- else
- {
- IsSettingsOpened = false;
- SettingsGrid.Opacity = 0.0;
- Win.Height = 190;
- SettingsBtn.Foreground = new SolidColorBrush(Color.FromRgb(154, 187, 218));
- SettingsBtn.BorderBrush = new SolidColorBrush(Color.FromRgb(154, 187, 218));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement