Advertisement
Amorf

Untitled

Jun 3rd, 2022
892
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Text;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Interop;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using Image = System.Windows.Controls.Image;
  16.  
  17. namespace Cursach_1._3
  18. {
  19.     /// <summary>
  20.     /// Логика взаимодействия для MainWindow.xaml
  21.     /// </summary>
  22.     public partial class MainWindow : Window
  23.     {
  24.         string[,] rssData = null;
  25.         string urlfinder;
  26.         Dictionary<string, string> Channals = new Diction-ary<string, string>();
  27.         Dictionary<int, string> chanalstack = new Diction-ary<int, string>();
  28.         public string ProgramPath = Directo-ry.GetCurrentDirectory();
  29.         public MainWindow()
  30.         {
  31.             InitializeComponent();
  32.  
  33.             var lines = File.ReadAllLines(ProgramPath + "\\" +"Chanals.txt"); // читаем построчно
  34.             foreach (var line in lines)
  35.             {
  36.                 var tokens = line.Split('=');
  37.                 var key = tokens[0].Trim();
  38.                 var value = string.Join("", to-kens.Skip(1)).Trim();
  39.                 Channals[key] = value;
  40.             }
  41.  
  42.             int i = 0;
  43.  
  44.             foreach (string finder in Channals.Keys)
  45.             {
  46.                 StackPanel stackPanel = new StackPanel { Width = 100, Height = 50 };
  47.  
  48.                 Label label = new Label { Width = 100, Height = 50 };
  49.                 label.Content = finder;
  50.                 label.VerticalContentAlignment = VerticalA-lignment.Top;
  51.                 label.HorizontalAlignment = HorizontalAlign-ment.Left;
  52.                 label.FontWeight = FontWeights.Bold;
  53.                 stackPanel.Children.Add(label);
  54.  
  55.                 chanalstack[i] = finder;
  56.  
  57.                 channalsListBox.Items.Add(stackPanel);
  58.                 i++;
  59.             }
  60.  
  61.         }
  62.  
  63.         public static BitmapImage ToBitmapImage(Bitmap bit-map)
  64.         {
  65.             using (var memory = new MemoryStream())
  66.             {
  67.                 bitmap.Save(memory, ImageFormat.Png);
  68.                 memory.Position = 0;
  69.  
  70.                 var bitmapImage = new BitmapImage();
  71.                 bitmapImage.BeginInit();
  72.                 bitmapImage.StreamSource = memory;
  73.                 bitmapImage.CacheOption = BitmapCacheOp-tion.OnLoad;
  74.                 bitmapImage.EndInit();
  75.                 bitmapImage.Freeze();
  76.  
  77.                 return bitmapImage;
  78.             }
  79.         }
  80.  
  81.         private static string[,] getRssData(string channel)
  82.         {
  83.             System.Net.WebRequest myRequest = Sys-tem.Net.WebRequest.Create(channel);
  84.             System.Net.WebResponse myResponse = myRe-quest.GetResponse();
  85.             System.IO.Stream rssStream = myRe-sponse.GetResponseStream();
  86.             System.Xml.XmlDocument rssDoc = new Sys-tem.Xml.XmlDocument();
  87.             rssDoc.Load(rssStream);
  88.             System.Xml.XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");
  89.             String[,] tempRssData = new String[rssItems.Count, 4];
  90.             for (int i = 0; i < rssItems.Count; i++)
  91.             {
  92.                 System.Xml.XmlNode rssNode;
  93.                 rssNode = rssItems.Item(i).SelectSingleNode("title");
  94.                 if (rssNode != null)
  95.                 {
  96.                     tempRssData[i, 0] = rssNode.InnerText;
  97.                 }
  98.                 else
  99.                 {
  100.                     tempRssData[i, 0] = "";
  101.                 }
  102.  
  103.                 rssNode = rssItems.Item(i).SelectSingleNode("description");
  104.  
  105.                 if (rssNode != null)
  106.                 {
  107.                     tempRssData[i, 1] = rssNode.InnerText;
  108.                 }
  109.                 else
  110.                 {
  111.                     tempRssData[i, 1] = "";
  112.                 }
  113.  
  114.                 rssNode = rssItems.Item(i).SelectSingleNode("link");
  115.                 if (rssNode != null)
  116.                 {
  117.                     tempRssData[i, 2] = rssNode.InnerText;
  118.                 }
  119.                 else
  120.                 {
  121.                     tempRssData[i, 2] = "";
  122.                 }
  123.  
  124.                 rssNode = rssItems.Item(i).SelectSingleNode("enclosure");
  125.                 if (rssNode != null)
  126.                 {
  127.                     string rssNodel = rssNode.OuterXml.Substring(16);
  128.                     tempRssData[i, 3] = rssNodel.Substring(0, rssNodel.IndexOf('"'));
  129.                 }
  130.                 else
  131.                 {
  132.                     tempRssData[i, 3] = "";
  133.                 }
  134.  
  135.             }
  136.             return tempRssData;
  137.         }
  138.  
  139.         private void refreshButton_Click(object sender, RoutedEventArgs e)
  140.         {
  141.  
  142.             //
  143.  
  144.             //
  145.  
  146.             rssData = getRssData(channelTextBox.Text);
  147.  
  148.             contentListBox.Items.Clear();
  149.  
  150.             for (int i = 0; i < rssData.GetLength(0); i++)
  151.             {
  152.                 if (rssData[i, 0] != null)
  153.                 {
  154.                     StackPanel stackPanel = new StackPanel { Width = 520, Height = 200 };
  155.  
  156.                     Label label = new Label { Width = 520, Height = 50 };
  157.                     label.Content = rssData[i, 0];
  158.                     label.VerticalContentAlignment = Verti-calAlignment.Top;
  159.                     label.HorizontalAlignment = Horizonta-lAlignment.Left;
  160.                     label.FontWeight = FontWeights.Bold;
  161.                     stackPanel.Children.Add(label);
  162.  
  163.                     try
  164.                     {
  165.                         Bitmap Picture = new Bitmap(new WebClient().OpenRead(rssData[i, 3].ToString()));
  166.                         BitmapImage WorkImg = ToBit-mapImage(Picture);
  167.  
  168.  
  169.                         stackPanel.Children.Add(new Image
  170.                         {
  171.                             Source = WorkImg,
  172.                             Stretch = Stretch.Fill,
  173.                             Width = 120,
  174.                             Height = 80
  175.                         });
  176.                     }
  177.                     catch
  178.                     {
  179.                         //MessageBox.Show("Error loading the image");
  180.                     }
  181.  
  182.                     TextBlock TB = new TextBlock { Width = 520, Height = 100 };
  183.                     TB.Text = rssData[i, 1];
  184.                     TB.TextWrapping = TextWrapping.Wrap;
  185.                     TB.TextAlignment = TextAlignment.Left;
  186.                    
  187.                     stackPanel.Children.Add(TB);
  188.  
  189.                     contentListBox.Items.Add(stackPanel);
  190.                 }
  191.             }
  192.         }
  193.  
  194.         private void contentListBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  195.         {
  196.             Pro-cess.Start(rssData[contentListBox.SelectedIndex, 2]);
  197.         }
  198.  
  199.         private void addchannalsButton_Click(object sender, RoutedEventArgs e)
  200.         {
  201.             AddWindow newWindow = new AddWindow(this, Chan-nals, chanalstack);
  202.             newWindow.Show();
  203.         }
  204.  
  205.         private void channalsListBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  206.         {
  207.             string keyfinder = cha-nalstack[channalsListBox.SelectedIndex];
  208.             urlfinder = Channals[keyfinder];
  209.  
  210.             rssData = getRssData(urlfinder);
  211.  
  212.             contentListBox.Items.Clear();
  213.  
  214.             for (int i = 0; i < rssData.GetLength(0); i++)
  215.             {
  216.                 if (rssData[i, 0] != null)
  217.                 {
  218.                     StackPanel stackPanel = new StackPanel { Width = 520, Height = 200 };
  219.  
  220.                     Label label = new Label { Width = 520, Height = 50 };
  221.                     label.Content = rssData[i, 0];
  222.                     label.VerticalContentAlignment = Verti-calAlignment.Top;
  223.                     label.HorizontalAlignment = Horizonta-lAlignment.Left;
  224.                     label.FontWeight = FontWeights.Bold;
  225.                     stackPanel.Children.Add(label);
  226.  
  227.                     try
  228.                     {
  229.                         Bitmap Picture = new Bitmap(new WebClient().OpenRead(rssData[i, 3].ToString()));
  230.                         BitmapImage WorkImg = ToBit-mapImage(Picture);
  231.  
  232.  
  233.                         stackPanel.Children.Add(new Image
  234.                         {
  235.                             Source = WorkImg,
  236.                             Stretch = Stretch.Fill,
  237.                             Width = 120,
  238.                             Height = 80
  239.                         });
  240.                     }
  241.                     catch
  242.                     {
  243.                         //MessageBox.Show("Error loading the image");
  244.                     }
  245.  
  246.                     TextBlock TB = new TextBlock { Width = 520, Height = 100 };
  247.                     TB.Text = rssData[i, 1];
  248.                     TB.TextWrapping = TextWrapping.Wrap;
  249.                     TB.TextAlignment = TextAlignment.Left;
  250.  
  251.                     stackPanel.Children.Add(TB);
  252.  
  253.                     contentListBox.Items.Add(stackPanel);
  254.                 }
  255.             }
  256.         }
  257.  
  258.         private void deletechannalButton_Click(object send-er, RoutedEventArgs e)
  259.         {
  260.             string keyfinder = cha-nalstack[channalsListBox.SelectedIndex];
  261.  
  262.             if (Channals[keyfinder] == urlfinder)
  263.             {
  264.                 contentListBox.Items.Clear();
  265.             }
  266.             Channals.Remove(keyfinder);
  267.             channalsListBox.Items.Clear();
  268.  
  269.             int i = 0;
  270.  
  271.             foreach (string finder in Channals.Keys)
  272.             {
  273.                 StackPanel stackPanel = new StackPanel { Width = 100, Height = 50 };
  274.  
  275.                 Label label = new Label { Width = 100, Height = 50 };
  276.                 label.Content = finder;
  277.                 label.VerticalContentAlignment = VerticalA-lignment.Top;
  278.                 label.HorizontalAlignment = HorizontalAlign-ment.Left;
  279.                 label.FontWeight = FontWeights.Bold;
  280.                 stackPanel.Children.Add(label);
  281.  
  282.                 chanalstack[i] = finder;
  283.  
  284.                 channalsListBox.Items.Add(stackPanel);
  285.                 i++;
  286.             }
  287.  
  288.  
  289.         }
  290.  
  291.         private void Window_Closing(object sender, Sys-tem.ComponentModel.CancelEventArgs e)
  292.         {
  293.             var stringbuilder = new StringBuilder();
  294.             foreach (var kv in Channals)
  295.             {
  296.                 stringbuild-er.AppendLine($"{kv.Key}={kv.Value}");
  297.             }
  298.             var text = stringbuilder.ToString();
  299.  
  300.             if (text != null ^ text != "" ^ text != " ")
  301.             {
  302.                 using (StreamWriter writer = new StreamWrit-er(ProgramPath + "\\" + "Chanals.txt", false, Sys-tem.Text.Encoding.Default))
  303.                 {
  304.                     writer.WriteLine(text);
  305.                 }
  306.             }
  307.         }
  308.     }
  309. }
  310.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement