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:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
|
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Xml;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using MySql.Data.MySqlClient;
// direct news namespace
namespace DirectNews
{
public partial class Form1 : Form
{
// replaces '' with ' and ' with'' so text gets inserted without any sql errors
private string EncodeIt(string TextString)
{
// return the formatted string
return TextString.Replace("'", "''");
}
// form 1
public Form1()
{
// initialize
InitializeComponent();
}
// on load
private void Form1_Load(object sender, EventArgs e)
{
// run the timer
timer1.Enabled = true;
}
// for each timer click
private void timer1_Tick(object sender, EventArgs e)
{
// update all articles
GetArticles();
}
// run the timer
private void GetArticles()
{
// file location and name
FileStream fs = new FileStream(@"c:/temp/directnews/log/" + DateTime.Now.ToLongDateString() + ".txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
// update label
toolStripStatusLabel1.Text = "Working...";
// write the first line of text, logged at etc
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine(" \n");
m_streamWriter.WriteLine("===================================== \n");
m_streamWriter.Write("DirectNews Article Log: ");
m_streamWriter.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
m_streamWriter.WriteLine("===================================== \n");
m_streamWriter.Flush();
// try
try
{
// variables
string articleID;
string Heading;
string LongHeading;
string articledate;
string content;
string summary;
string ImgURL;
string ImgWidth;
string ImgHeight;
string ImgRatio;
string PhotoTag;
// connecting
toolStripStatusLabel1.Text = "Connecting to the database..";
// connection to the database
MySqlConnection dbcon = new MySqlConnection("SQL CON;");
dbcon.Open();
// connected
toolStripStatusLabel1.Text = "Connected";
// link to the reader, load the reader
XmlTextReader xtr = new XmlTextReader("http://feeds.directnews.org.uk/?c21c0905-b9ca-43ee-87be-7e4308406da6");
xtr.WhitespaceHandling = WhitespaceHandling.None;
XmlDocument X = new XmlDocument();
X.Load(xtr);
// reading xml
toolStripStatusLabel1.Text = "Reading XML ..";
// check value found
if (X != null)
{
// select the article node
XmlNodeList ArticleList = X.SelectNodes("InfoStreamResults/Article");
// for each article
foreach (XmlNode Article in ArticleList)
{
// empty variables
Heading = "";
LongHeading = "";
articledate = "";
articleID = "";
content = "";
summary = "";
ImgURL = "";
PhotoTag = "";
ImgWidth = "";
ImgRatio = "";
ImgHeight = "";
// get article value
articleID = Article.SelectSingleNode("@ID").Value;
// check for heading
if (Article.SelectSingleNode("Heading") != null)
{
// encode the header
Heading = EncodeIt(Article.SelectSingleNode("Heading").InnerText);
}
// check for long heading
if (Article.SelectSingleNode("LongHeading") != null)
{
// encode the header
LongHeading = EncodeIt(Article.SelectSingleNode("LongHeading").InnerText);
}
// check for date
if (Article.SelectSingleNode("Date") != null)
{
// check for created date
if (Article.SelectSingleNode("@Created") != null)
{
// build the article date
articledate = Article.SelectSingleNode("Date").InnerText + " " + Article.SelectSingleNode("@Created").Value;
}
// else created date
else
{
// build the article date
articledate = Article.SelectSingleNode("Date").InnerText;
}
}
// check for Contents
if (Article.SelectSingleNode("Contents") != null)
{
// encode the Contents
content = EncodeIt(Article.SelectSingleNode("Contents").InnerText);
}
// check for long Summary
if (Article.SelectSingleNode("Summary") != null)
{
// encode the Summary
summary = EncodeIt(Article.SelectSingleNode("Summary").InnerText);
}
// check for image url
if (Article.SelectSingleNode("Picture/Large/URL") != null)
{
// encode the image url
ImgURL = EncodeIt(Article.SelectSingleNode("Picture/Large/URL").InnerText);
}
// check for image width
if (Article.SelectSingleNode("Picture/Large/@Width") != null)
{
// set the image width
ImgWidth = Article.SelectSingleNode("Picture/Large/@Width").Value;
}
// check for img ratio
if (Article.SelectSingleNode("Picture/@Ratio") != null)
{
// img ratio
ImgRatio = Article.SelectSingleNode("Picture/@Ratio").Value;
}
// check for image height
if (Article.SelectSingleNode("Picture/Large/@Height") != null)
{
// image height
ImgHeight = Article.SelectSingleNode("Picture/Large/@Height").Value;
}
// check for photo tag
if (Article.SelectSingleNode("Picture/@PhotoTag") != null)
{
// encode the photo tag
PhotoTag = EncodeIt(Article.SelectSingleNode("Picture/@PhotoTag").Value);
}
// variables
string sqlquery;
MySqlCommand checkItemExist, deleteCategory, addCategory;
MySqlCommand InsertUpdateCommand;
// sql statement to delete the cats
sqlquery = "Delete from Categories where ArticleID ='" + articleID + "'";
deleteCategory = new MySqlCommand(sqlquery, dbcon);
deleteCategory.ExecuteNonQuery();
// delete old cats
toolStripStatusLabel1.Text = "Deleting old categories";
// the category
XmlNodeList CatNodesList = Article.SelectNodes("Categories/Category");
// for each category
foreach (XmlNode category in CatNodesList)
{
// add new cat query
sqlquery = "Insert INTO Categories (ArticleID, CategoryID, CategoryName) Values ('" + articleID + "' ,'" + category.SelectSingleNode("@ID").Value + "', '" + EncodeIt(category.InnerText) + "')";
addCategory = new MySqlCommand(sqlquery, dbcon);
addCategory.ExecuteNonQuery();
}
// add cats
toolStripStatusLabel1.Text = "add updated categories";
// select query
sqlquery = "Select ArticleID from FeedData where ArticleID='" + articleID + "'";
checkItemExist = new MySqlCommand(sqlquery, dbcon);
checkItemExist.ExecuteNonQuery();
// check if article already exists
toolStripStatusLabel1.Text = "checking for article";
// reader
MySqlDataReader dr = checkItemExist.ExecuteReader();
Boolean ItemAlreadyExists = dr.HasRows;
dr.Close();
// item found
if (ItemAlreadyExists == true)
{
// update the article in the database
sqlquery = "Update FeedData SET Heading ='" + Heading + "', Date ='" + articledate + "', Content = '" + content + "', LongHeading ='" + LongHeading + "', Summary ='" + summary + "', ImgURL ='" + ImgURL + "', ImgWidth ='" + ImgWidth + "', ImgHeight ='" + ImgHeight + "', ImgRatio ='" + ImgRatio + "', PhotoTag ='" + PhotoTag + "' WHERE ArticleID = '" + articleID + "'";
InsertUpdateCommand = new MySqlCommand(sqlquery, dbcon);
m_streamWriter.WriteLine("Article ID updated: " + articleID + " @ " + DateTime.Now.ToLongTimeString());
m_streamWriter.Flush();
// update article
toolStripStatusLabel1.Text = "article found, updating it";
}
// item not found
else
{
// insert new article
sqlquery = "Insert INTO FeedData (ArticleID, Heading, Content, Date, LongHeading, Summary, ImgURL, ImgHeight, ImgWidth, ImgRatio, PhotoTag) Values ('" + articleID + "' ,'" + Heading + "', '" + content + "', '" + articledate + "', '" + LongHeading + "', '" + summary + "', '" + ImgURL + "', '" + ImgHeight + "', '" + ImgWidth + "', '" + ImgRatio + "', '" + PhotoTag + "')";
InsertUpdateCommand = new MySqlCommand(sqlquery, dbcon);
m_streamWriter.WriteLine("Article ID added: " + articleID + " @ " + DateTime.Now.ToLongTimeString());
m_streamWriter.Flush();
toolStripStatusLabel1.Text = "article not found, adding it";
}
// execute
InsertUpdateCommand.ExecuteNonQuery();
// update the label
label3.Text = DateTime.Now.ToString();
}
}
// flush the connection
dbcon.Close();
// update label
toolStripStatusLabel1.Text = "Ready";
timer1.Interval = 1200000;
// close
fs.Close();
m_streamWriter.Close();
}
// catch exception
catch (Exception ex)
{
}
}
// view log
private void viewLogToolStripMenuItem_Click(object sender, EventArgs e)
{
// open with notepad
System.Diagnostics.Process.Start("notepad.exe", "c:/temp/directnews/log/" + DateTime.Now.ToLongDateString() + ".txt");
}
// force sync
private void forceSyncToolStripMenuItem_Click(object sender, EventArgs e)
{
timer1.Interval = 1000;
toolStripStatusLabel1.Text = "Working...";
}
// close the program
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
|