-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRSSWindow.xaml.cs
135 lines (121 loc) · 3.27 KB
/
RSSWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
using System.Net;
using System.IO;
using System.Diagnostics;
using Concursus;
namespace YourNamespace
{
public partial class RSSWindow : Window
{
private string rssFeedLink;
private string game_id;
public RSSWindow(string rssFeedLink)
{
this.rssFeedLink = rssFeedLink;
int end_len = rssFeedLink.IndexOf('&') - (rssFeedLink.IndexOf('=') + 1);
this.game_id = rssFeedLink.Substring(rssFeedLink.IndexOf('=') + 1, end_len);
InitializeComponent();
// Apply the theme
Themes.UpdateForm(Themes.CURRENT_THEME, this);
LoadRSSFeed();
}
private void LoadRSSFeed()
{
try
{
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri(rssFeedLink));
}
catch (Exception ex)
{
MessageBox.Show("Error loading RSS feed: " + ex.Message);
}
}
private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
if (e.Error == null && !e.Cancelled)
{
XDocument xdoc = XDocument.Parse(e.Result);
List<RssItem> items = new List<RssItem>();
foreach (XElement item in xdoc.Descendants("item"))
{
RssItem rssItem = new RssItem
{
Title = item.Element("title").Value,
Link = item.Element("link").Value,
Image = GetImageFromUrl(item.Element("image").Value),
ModId = GetModIdFromLink(item.Element("link").Value)
};
items.Add(rssItem);
}
feedListView.ItemsSource = items;
}
else
{
MessageBox.Show("Error loading RSS feed: " + e.Error?.Message);
}
}
catch (Exception ex)
{
MessageBox.Show("Error parsing RSS feed: " + ex.Message);
}
}
private ImageSource GetImageFromUrl(string imageUrl)
{
try
{
WebClient webClient = new WebClient();
byte[] bytes = webClient.DownloadData(imageUrl);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = new MemoryStream(bytes);
bitmapImage.EndInit();
return bitmapImage;
}
catch (Exception)
{
return null;
}
}
private int GetModIdFromLink(string link)
{
// Fuck you
int startIndex = link.LastIndexOf("/") + 1;
int endIndex = link.Length;
string modIdStr = link.Substring(startIndex, endIndex - startIndex);
return int.Parse(modIdStr);
}
private void DownloadButton_Click(object sender, RoutedEventArgs e)
{
if (sender is Button button)
{
if (button.DataContext is RssItem rssItem)
{
// Apply the theme to the GBModPrompt window
GBModPrompt modPrompt = new GBModPrompt(game_id, rssItem.ModId.ToString());
Themes.UpdateForm(Themes.CURRENT_THEME, modPrompt);
modPrompt.ShowDialog();
}
}
}
public class RssItem
{
public string Title { get; set; }
public string Link { get; set; }
public ImageSource Image { get; set; }
public int ModId { get; set; }
}
}
}