Some time ago, I built a blog promotion website: Chinese Blog Group ( http://www.bokequn.cn ). There is a function in it that can regularly read the blog article information into the database, and then display it for users to query. And processing TAG. Because it is a virtual host, I only thought of using the server-side timer to solve it.
At that time, I was in a hurry and did not have time to optimize, and my level was very limited. If you think something is wrong or there is a better way to solve it, please feel free to enlighten me.
I think there are two key points in the whole process:
how to deal with timer
Modularize reading RSS to make it easier to call
1. Regarding timer, of course it is written in Global.asax. First create the variable:
System.Timers.Timer t=new System.Timers.Timer(1000*Convert.ToInt16(System.Configuration.ConfigurationSettings.AppSettings["do_time" ]));
Among them, do_time is used in web.config to access how often it is executed,
and then processed in Application_Start:
t.AutoReset=true;
t.Enabled=true;
if(common.func.get_key("can_do")=="1")//It is also accessed in web.config whether to perform regular processing, where common.func.get_key is a custom function to obtain settings
t.Elapsed +=new System.Timers.ElapsedEventHandler(fun);
Code of function fun:
private void fun(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
System.DateTime dt=System.DateTime.Now;
common.func.write_log("Start reading remote XML");//Write log
rss2sql rl=new rss2sql();
rl.readall();
System.DateTime dt2=System.DateTime.Now;
common.func.write_log("XML file imported into database successfully"+Convert.ToString(dt2-dt));//Write this execution time to the log,
}
catch(Exception ex)
{
common.func.write_log("Error reading remote xml file:"+ex.Message);//Write log
}
}
The code in Global.asax is completed
2. The next step is to obtain the rss list from the database in the rss2sql class, read the rss file based on the last read rss time and the modification time of the rss file and write it to the database, because too many are used Customized classes and methods, so all codes are not listed. Interested friends can download them at the end. Among them, the more important functions:
/**//// <summary>
/// Read the RSS content of the specified link to the database
/// </summary>
/// <param name="id">The id of the corresponding blog</param>
/// <param name="url">rss address</param>
/// <param name="dt">Last read time</param>
public void read_from_url(string id,string url,System.DateTime dt)
{
op_db.db_class db1=new op_db.db_class();
try
{
rssFeed feed = new rssFeed(url,dt);//Create an rss reading class instance
feed.read();//Start reading
if(feed.Channel.Items.Count>0)//If the number of articles is greater than 0, start reading into the database
{
for(int i=0;i<feed.Channel.Items.Count;i++)
{
write_artical(id,feed.Channel.Items[i].title,feed.Channel.Items[i].link,feed.Channel.Items[i].description,feed.Channel.Items[i].pubDate);
}
db1.sql="update bokequn set last_rss_date='"+feed.lastModified.ToString()+"' where id="+id;
db1.executesql();
}
}
catch(Exception ex)
{
common.func.write_log(id+":url:"+url+"Error"+ex.Message+ex.Source+ex.StackTrace);
}
finally
{
db1.db_close();
}
}
Among them, rssFeed is a class written by myself to process RSS. For this class, I have used the open source rss.net for a period of time before. However, in the actual process, there are various RSS addresses, and even many of them do not meet the standards, so you will encounter There were a lot of problems, and after looking at the source code, there were too many things he wanted to deal with, and the amount of code was also very large. At my level, I estimated that it would not be possible to correct it in a while, so I made a control myself, which is not a big deal. The controls are just a few categories. Of course, the content I deal with is relatively simple. I only deal with the title and introduction of RSS, the title, introduction, time, and link of the item.
There are too many codes posted. Friends who are interested can download it at the end.
The usage is quite simple. Just reference the compiled dll and use it as follows:
rssFeed feed = new rssFeed(url,dt);//Create an rss reading class instance
feed.read();//Start reading
feed.Channel.title
feed.Channel.description
feed.Channel.Items.Count
feed.Channel.Items[i].title
feed.Channel.Items[i].description
feed.Channel.Items[i].link
The dt in the
feed.Channel.Items[i].pubDate
constructor represents the last reading time, because the modification time of the RSS file will be obtained in the class. If it is greater than the last reading time, it will be read, otherwise it will not be processed.Okay, I believe everyone can understand at a glance.
There is still a problem in RSS reading. That is, if there are hexadecimal characters in some xml files (of course, this is unlikely), it cannot be read. Which If anyone knows how to solve it, please let me know.
It's a bit confusing, but I hope it will be helpful to friends who need this function.
Related downloads: http://guanvee.cnblogs.com/archive/2006/06/17/428329.html