Home > Article > Backend Development > Detailed explanation of examples of calling API to generate RSS resource files
Sina Weibo seems to be gaining momentum now, and many people are creating their own When you were on Weibo, you would follow many people out of curiosity at first. As time goes by, when the number of your friends exceeds 100, you will find that you have been completely submerged in the torrent of information. You have followed so many people, so many Everyone is someone you may never think of or see in the future. This is obviously contrary to the purpose of "following". More often, you may just want to see news about people you knowUpdate Is it updated, but obviously you can't click on the people you follow one by one and then visit their homepages one by one. This operation is too cumbersome.
Users who have used RSS readers may have realized the superiority of RSS readers in obtaining information. It can track the updates of RSS resources in real time and display the number of updates behind the specified subscription resources. It allows users to view information in a targeted manner and allows users to actively obtain information instead of passively accepting information. This is very important for It is very effective to solve the "information flood" problem of Weibo.
The author spent a few days and finally wrote a program to obtain the friends of a specified user, store and back them up in the ACCESS database, and then generate an RSS reading resource. I don’t dare to keep it to myself, so I open source it and share it with everyone. If I have time in the future, I would like to make it into a desktop software to facilitate the operation of users who do not understand the program. This is a story for another time.
1. Sina Weibo RSSSubscribe to third-party websites
Sina Weibo itself does not provide RSS subscription, but I searched on the Internet and found a third-party website that provides RSS resources for Sina Weibo. Therefore, the RSS subscriptions in this article are all based on this third-party website.
log.medcl.net/item/2010/02/sina-bo-rss-subscribe-feed-generate-micro/2. Online Common OPML fileXML format
The following is the opml file exported from Google Reader, this is network RSS reading It is the standard format of any RSS reader or even all RSS readers. At least the more popular online readers such as "Xianguo" and "Youdao" support the import of files in this format.of the OPML file, and then use the program to write the information it needs into a file of this structure, so that the reader can reference . The OPML file consists of the header tag
(mainly some comments of this file, which does not affect the actual RSS subscription information and is not too important) and (the RSS reader extracts the subscription resource All data sources). There is a first-levelSina Weibo API——Export user friend data from the server to local XML fileFor a detailed introduction to Sina Weibo API, please refer to the official website of Sina Weibo API:
open.t.sina.com.cn/wiki/index.php/首页
For identity authentication and data requests, please refer to cnblogs:
"Code example of .NET calling Sina Weibo open platform
interface"www.cnblogs.com /cmt/archive/2010/05/13/1733904.html
The following is the code I compiled to request the user’s friend information from the server:
<user> <id>1710993410</id> <screen_name>某丫大人</screen_name> <name>某丫大人</name> <province>43</province> <city>1</city> <location>湖南 长沙</location> <description>饭否儿,心朝饭否,春暖花开。 我还是@饿YA 我还真是懒得介绍了。</description> <url>http://1</url> <profile_image_url>http://tp3.sinaimg.cn/1710993410/50/1273755892</profile_image_url> <domain> </domain> <gender>f</gender> <followers_count>168</followers_count> <friends_count>79</friends_count> <statuses_count>846</statuses_count> <favourites_count>0</favourites_count> <created_at>Sun Mar 14 00:00:00 +0800 2010</created_at> <following>false</following> <verified>false</verified> <allow_all_act_msg>false</allow_all_act_msg> <geo_enabled>false</geo_enabled> <status> <created_at>Sun May 16 21:02:44 +0800 2010</created_at> <id>364379114</id> <text>烦死了快、</text> <source> <a href="">新浪微博</a> </source> <favorited>false</favorited> <truncated>false</truncated> <geo /> <in_reply_to_status_id> </in_reply_to_status_id> <in_reply_to_user_id> </in_reply_to_user_id> <in_reply_to_screen_name> </in_reply_to_screen_name> </status> </user> |
|
可以看到这里面的信息量是超级多的,我简单介绍下几个主要的节点吧
id |
用户新浪微博的数字ID,就像你的QQ号一样 |
name |
用户昵称 |
province |
省代号 |
city |
市代号 |
location |
所在省市(好像和上面两个节点重复了) |
description |
自我描述 |
domain |
域名,就是除了数字ID后,用户申请的修改域名 |
gender |
性别。男的是Male,女的是Female. |
followers_count |
粉丝数 |
friends_count |
跟随的人数 |
statuses_count |
发表的状态也就是微博数 |
favourites_count |
收藏微博数目吧?(不知道这个有什么用) |
created_at |
用户创建此微博客的时间 |
verified |
是否经过新浪的VIP认证 |
status |
用户最近的一次状态 |
除了user信息外,还有一些其它信息,比如根节点下的next_cursor和previous_cousor,这方便用户分多次到服务器上请求数据时可以此作为定位依据。
|
|
对多个XML文件进行遍历,一个个导入到ACCESS数据库中:
/// <summary> /// 将所有好友都导出了,然后存储在ACCESS数据库中了。 /// </summary> public void readAllXml() { for (int i = 0; i < 8; i++) { string fileName = "friends_" + Convert.ToString(i * 20) + "_" + Convert.ToString(i*20+20)+".xml";//按照存储XML文件时的命名规则进行读取 readTsinaFriends(fileName); } } |
|
经过上面的操作后,你再打开你的ACCESS数据库文件weibo.mdb文件中对应的表,就可以看到所以的信息都已经导入到ACCESS中了。如下图所示:
5. 对ACCESS数据库查询并写成RSS阅读器的OPML格式
对于制作RSS阅读器的OPML格式,需要的数据只有两条字段:一个是id字段,一个是name字段。
这个过程实际上就是对数据进行XML编码的过程,啥都不说了,一切都在代码中了(也是在ASP.NET工程中写的):
/// <summary> /// 建立新浪微博的RSS文件 /// </summary> public void CreateTsinaRssXmlFile() { OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\coursware\\网络软文\\API_微波\\weibo.mdb"); string strCmd = "select id as idnum,screen_name as name from Friends"; //从ACCESS中获取数据 aConnection.Open(); OleDbDataAdapter da = new OleDbDataAdapter(strCmd, aConnection); DataSet ds = new DataSet(); da.Fill(ds, "TSina"); ds.DataSetName = "RssReader"; DataTable dt = ds.Tables[0];//数据集的第0张表格 XmlDocument xmldoc; XmlElement xmlelem; xmldoc = new XmlDocument(); //加入XML的声明段落 XmlDeclaration xmldecl; xmldecl = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null); xmldoc.AppendChild(xmldecl); //加入一个根元素 xmlelem = xmldoc.CreateElement(" ", "opml", " "); xmldoc.AppendChild(xmlelem); XmlNode root = xmldoc.SelectSingleNode("opml");//查找<opml> 节点 XmlElement xeHead = xmldoc.CreateElement("head");//创建一个节点 //为head节点增加子节点 XmlElement xeHeadsub = xmldoc.CreateElement("title"); xeHeadsub.InnerText = "Rss Reader";//设置节点文本 xeHead.AppendChild(xeHeadsub);//添加到子节点中 root.AppendChild(xeHead);//添加到节点中 //增加body子节点,然后,将所有的RSS订阅信息全部写入到body节点中间 XmlElement xeBody = xmldoc.CreateElement("body"); root.AppendChild(xeBody); //第一层循环是标签(文件夹循环)由于本次只做一个标签,所以就只循环一次了 //RSS的文件夹属性节点 XmlElement xe1 = xmldoc.CreateElement("outline"); xe1.SetAttribute("text", "Tsina");//设置该节点title属性 xe1.SetAttribute("title", "Tsina");//设置该节点title属性 --第一层的outline节点的属性表示的是RSS的标签或者说是文件夹 //下面就要开始为此文件夹节点添加下属子节点,也就是添加一些实质的RSS地址了 string strTitle = string.Empty; string strText = string.Empty; string strXmlUrl = string.Empty; string strHtmlUrl = string.Empty; for (int i = 0; i 节点中 } xeBody.AppendChild(xe1); //保存创建好的XML文档 xmldoc.Save(Server.MapPath("RssReader.xml")); }</opml> |
|
最后在指定的目录下,程序就自动生成了一个RssReader.xml的文件了。大功告成了!
然后将此文件就可以导入到任何一个RSS阅读器中了,用户就能够通过RSS阅读器来获取微博信息了,而且现在的RSS阅读器都有个一键转贴到微博的功能,很方便的,不想转到自己微博的,也可以通过RSS阅读器直接收藏到阅读器中。辛苦了两天,今天能有这么一点小成果,还是觉得很不错的,呵呵,也祝大家也能好运。本次代码比较还需要各种完善,比如,如何将所以的数据写成一个XML文件,这个笔者就暂时不做了,留给大家去做吧。
Rss阅读器效果图如下:
【相关推荐】
1. RSS高校入门教程
The above is the detailed content of Detailed explanation of examples of calling API to generate RSS resource files. For more information, please follow other related articles on the PHP Chinese website!