-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.vb
More file actions
241 lines (214 loc) · 10.2 KB
/
reader.vb
File metadata and controls
241 lines (214 loc) · 10.2 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
Imports System
Imports System.IO
Imports System.Net
Imports System.Xml
Imports System.Text.RegularExpressions
Public Class frmRSS
Dim rssDirPath As String = "C:\Users\" & Environment.UserName & "\Documents\"
Dim rssFilePath As String = rssDirPath & "rssFeed.txt"
Private Sub rssForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' Check for default text file on program start, quietly ignore if nonexistent
Try
Dim textIn As New StreamReader(
New FileStream(rssFilePath, FileMode.Open, FileAccess.Read))
buildLinksListFromTxtFile(textIn)
textIn.Close()
Catch ex As FileNotFoundException
Catch ex As DirectoryNotFoundException
Catch ex As IOException
End Try
End Sub
Private Sub buildLinksListFromTxtFile(ByVal file As StreamReader)
' Read contents of RSS links text file, check for valid format
Dim rssLinks As New List(Of String)
Dim lineNumber As Integer = 0
Do While file.Peek <> -1
lineNumber = lineNumber + 1
Dim row As String = file.ReadLine
' Hack to check if rssFeeds file is binary
If row.Contains("\0\0") Or row.Contains("ÿ") Then
MsgBox("This feeds list appears not to be a text file!", MsgBoxStyle.OkOnly)
Exit Do
ElseIf lineNumber = 1 And Not row.Contains("http") Then
' Hack to check that first line contains http; otherwise call it invalid text file
MsgBox("This feeds list is invalid!", MsgBoxStyle.OkOnly)
Exit Do
Else
rssLinks.Add(row)
End If
Loop
' Zero out cmbFeedList
cmbFeedList.Items.Clear()
For Each rssLink In rssLinks
cmbFeedList.Items.Add(rssLink)
Next
file.Close()
End Sub
Private Sub btnFetch_Click(sender As System.Object, e As System.EventArgs) Handles btnFetch.Click
' Check for strings that are zero-length or without http protocol
' Set up some regular expressions to check for valid URLs
Dim pattern As String = "^https?://[a-z0-9-]+(\.[a-z0-9-]+)+([/?].+)?$"
Dim pattern2 As String = "^https?://www.[a-z0-9-]+(\.[a-z0-9-]+)+([/?].+)?$"
Dim validURL As New Regex(pattern)
Dim validURL2 As New Regex(pattern2)
' If URL contains www, it has to contain four parts: the protocol, www,
' the domain suffix, and some trailing characters, i.e., http://www.mysite.com/feed
' If URL doesn't contain www, it has to contain three parts: the protocol, domain
' suffix, and trailing characters, i.e., http://mysite.com/feed
If ((cmbFeedList.Text.Contains("www") And validURL2.IsMatch(cmbFeedList.Text)) Or
(Not cmbFeedList.Text.Contains("www") And validURL.IsMatch(cmbFeedList.Text))) Then
' Don't duplicate entries
If Not cmbFeedList.Items.Contains(cmbFeedList.Text) Then
cmbFeedList.Items.Add(cmbFeedList.Text)
End If
fetchRSS()
Else
MessageBox.Show("Please enter a valid RSS feed!", "Invalid RSS Feed", _
MessageBoxButtons.OK)
End If
End Sub
Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) _
Handles btnDelete.Click
Dim deleteConfirm = MessageBox.Show("Are you sure you want to delete this feed?", _
"Confirm feed deletion", MessageBoxButtons.YesNo)
If deleteConfirm = Windows.Forms.DialogResult.Yes Then
cmbFeedList.Items.Remove(cmbFeedList.Text)
End If
End Sub
Private Sub rssForm_FormClosing(sender As System.Object, e As _
System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
' Write rss list to text file
Dim saveConfirm = MessageBox.Show("Do you want to save your current feed _
list? (If no, your existing rssFeeds.txt will be left untouched.)", "Save feed list", _
MessageBoxButtons.YesNoCancel)
If saveConfirm = Windows.Forms.DialogResult.Yes Then
writeRSSFile()
ElseIf saveConfirm = Windows.Forms.DialogResult.Cancel Then
e.Cancel = True
End If
End Sub
Private Sub fetchRSS()
Dim rssURL = cmbFeedList.Text
Dim rssFeed As Stream = Nothing
Dim errorMsg As String = Nothing
' Set up an HTTP request
Dim request As HttpWebRequest = CType(WebRequest.Create(rssURL), HttpWebRequest)
' Try the download, check for HTTP OK status, grab feed if successful
Try
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
If response.StatusCode = HttpStatusCode.OK Then
rssFeed = response.GetResponseStream()
showRSS(rssFeed)
End If
Catch e As WebException
errorMsg = "Download failed. The response from the server was: " +
CType(e.Response, HttpWebResponse).StatusDescription
MessageBox.Show(errorMsg, "Error", MessageBoxButtons.OK)
Catch e As Exception
errorMsg = "This doesn't look like an RSS feed. The specific error is: " + e.Message
'errorMsg = "Hmm, there was a problem: " + e.Message
MessageBox.Show(errorMsg, "Error", MessageBoxButtons.OK)
End Try
End Sub
Private Overloads Sub writeRSSFile()
' Write rss list to text file - method with fixed path called on program close
Dim textOut As New StreamWriter(
New FileStream(rssFilePath, FileMode.Create, FileAccess.Write))
For Each rssLink In cmbFeedList.Items
textOut.WriteLine(rssLink.ToString)
Next
textOut.Close()
End Sub
Private Overloads Sub writeRSSFile(ByVal file As Integer)
' Write rss list to text file - method called from File menu with user-determined path
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.InitialDirectory = rssFilePath
saveFileDialog1.Filter = "txt files (*.txt)|*.txt"
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
Dim outputFile As StreamWriter = New StreamWriter(saveFileDialog1.OpenFile())
If (outputFile IsNot Nothing) Then
For Each rssLink In cmbFeedList.Items
outputFile.WriteLine(rssLink.ToString)
Next
outputFile.Close()
End If
End If
End Sub
Private Sub showRSS(ByVal rssStream As Stream)
' Process contents of RSS feed (XML)
Dim rssFeed = XDocument.Load(rssStream)
Dim output As String = Nothing
' Use XML literals to pull out the tags I want
For Each post In From element In rssFeed...<item>
output += "<h3>" + post.<title>.Value + "</h3>"
' Fix the date
Dim correctDate = DateTime.Parse(post.<pubDate>.Value)
output += "<strong>Posted on " + correctDate + "</strong>"
'"<a href=""" + post.<link>.Value + " target=""_blank"">"
output += post.<description>.Value
Next
' Rewrite articles to open links in new window; replaced by navigating event override below
' Dim fixedOutput = output.Replace("<a rel=""nofollow""", "<a rel=""nofollow"" target=""_blank""")
wbFeedList.DocumentText = "<html><body><font face=""sans-serif"">" + _
output.ToString() + "</font></body></html>"
End Sub
Private Sub HelpToolStripMenuItemHelp_Click(sender As System.Object, e As _
System.EventArgs) Handles HelpToolStripMenuItemHelp.Click
rssHelpBox.Show()
End Sub
Private Sub AboutToolStripMenuItem_Click(sender As System.Object, e As _
System.EventArgs) Handles AboutToolStripMenuItem.Click
rssAboutBox.Show()
End Sub
Private Sub QuitToolStripMenuItem_Click(sender As System.Object, e As _
System.EventArgs) Handles QuitToolStripMenuItem.Click
writeRSSFile()
Me.Close()
End Sub
Private Sub OpenFeedlistToolStripMenuItem_Click(sender As System.Object, e As _
System.EventArgs) Handles OpenFeedlistToolStripMenuItem.Click
' Open feeds file from file menu
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = rssFilePath
openFileDialog1.Filter = "txt files (*.txt)|*.txt"
openFileDialog1.RestoreDirectory = True
' Offer to save existing feeds list first
Dim saveOnClose = MsgBox("Would you like to save your existing feeds list ?", _
MsgBoxStyle.YesNoCancel)
If saveOnClose = MsgBoxResult.Yes Then
writeRSSFile(1)
ElseIf saveOnClose = MsgBoxResult.Cancel Then
Return
End If
' Open open file dialog
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
Dim fileStream = New StreamReader(openFileDialog1.OpenFile)
If (fileStream IsNot Nothing) Then
' Open dialog
buildLinksListFromTxtFile(fileStream)
End If
Catch Ex As IOException
MessageBox.Show("Cannot read file from disk. The error is: " & Ex.Message)
End Try
End If
End Sub
Private Sub SaveFeedlistToolStripMenuItem_Click(sender As System.Object, e As _
System.EventArgs) Handles SaveFeedlistToolStripMenuItem.Click
' Save current file as txt
If cmbFeedList.Items.Count > 0 Then
writeRSSFile(1)
Else
MessageBox.Show("Add some RSS feeds before you try to save them!", "Error", _
MessageBoxButtons.OK)
End If
End Sub
Private Sub wbFeedList_Navigating(sender As Object, e As _
System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles wbFeedList.Navigating
' Open links in default browser instead of webbrowser control
If Not (e.Url.ToString().Equals("about:blank", StringComparison.InvariantCultureIgnoreCase)) Then
e.Cancel = True
Process.Start(e.Url.ToString())
End If
End Sub
End Class