Home >Backend Development >XML/RSS Tutorial >Detailed explanation of examples of calling API to generate RSS resource files

Detailed explanation of examples of calling API to generate RSS resource files

Y2J
Y2JOriginal
2017-05-05 09:49:293775browse

C# Call Sina Weibo API to generate RSS resource files (source file Demo)

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.

<?xml version="1.0" encoding="UTF-8"?><opml version="1.0">
	<head>
		<title>subscriptions</title>
		<dateCreated>2010-05-16 15:45:03</dateCreated>
		<ownerName></ownerName>
	</head>
	<body>
		<outline text="微博客" title="微博客">			
			<outline text="冷笑话(1567852087)" 
				title="冷笑话(1567852087)" type="rss"
				htmlUrl="http://t.sina.com.cn/1567852087" 
				xmlUrl="http://medcl.net/SinaRss.aspx?uid=1567852087" />
			<outline text="后宫优雅(1665289110)" 
				title="后宫优雅(1665289110)" type="rss"
				htmlUrl="http://t.sina.com.cn/1665289110" 
				xmlUrl="http://medcl.net/SinaRss.aspx?uid=1665289110" />
			<outline text="围脖经典语录" 
				title="围脖经典语录" type="rss"
				htmlUrl="http://t.sina.com.cn/1646465281" 
				xmlUrl="http://medcl.net/SinaRss.aspx?uid=1646465281" />			
			<outline text="破阵子(1644022141)" 
				title="破阵子(1644022141)" type="rss"
				htmlUrl="http://t.sina.com.cn/1644022141" 
				xmlUrl="http://medcl.net/SinaRss.aspx?uid=1644022141" />			
		</outline>
		<outline text="珞珈山水" title="珞珈山水">			
			<outline text="今日十大热门话题" 
				title="今日十大热门话题" type="rss"
				htmlUrl="http://bbs.whu.edu.cn/frames.html" 
				xmlUrl="http://bbs.whu.edu.cn/rssi.php?h=1" />			
			<outline text="贴图版" 
				title="贴图版" type="rss"
				htmlUrl="http://bbs.whu.edu.cn/wForum/board.php?name=Picture" 
				xmlUrl="http://bbs.whu.edu.cn/wForum/rss.php?board=Picture&ic=1" />
		</outline>
	</body></opml>
##Analyze the

structure

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-level node under the node. This node corresponds to the information related to the classified folder of RSS resources in the RSS reader (obviously text represents the folder name), and then the first-level Below is the secondary tag pair, which contains the relevant data content of the RSS resource. Some important attributes of the nodes in the second-level : text represents the title of the resource, htmlUrl represents the Web page address of the information, and xmlUrl represents the RSS subscription address of the information. 3.

Sina 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:

private void getFriends()
        {            int previous_cursor=-1;            int next_cursor = -1;            while (next_cursor != 0)
            {                string cursor = Convert.ToString(previous_cursor);                string url = " http://api.t.sina.com.cn/statuses/friends.xml?source=AppKey&cursor=" + cursor;                string username = "dreamzsm@gmail.com";                string password = name; //这里输入你自己微博登录的的密码
                //注意这里的格式哦,为 "username:password"

                System.Net.WebRequest webRequest = System.Net.WebRequest.Create(url);
                System.Net.HttpWebRequest myReq = webRequest as System.Net.HttpWebRequest;                //身份验证
                string usernamePassword = username + ":" + password;
                CredentialCache mycache = new CredentialCache();
                mycache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
                myReq.Credentials = mycache;
                myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));

                WebResponse wr = myReq.GetResponse();
                Stream receiveStream = wr.GetResponseStream();
                StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);                string content = reader.ReadToEnd();


                XmlDocument xmlDoc = new XmlDocument();

                xmlDoc.LoadXml(content);                // xmlDoc.Load("data1.xml");

                XmlNodeList nodeList = xmlDoc.SelectSingleNode("users").ChildNodes;//获取根节点的所有子节点

                next_cursor = Convert.ToInt16(nodeList.Item(nodeList.Count - 2).InnerText);
                previous_cursor = Convert.ToInt16(nodeList.Item(nodeList.Count-1).InnerText);                string xmlName = "friends_" + nodeList.Item(nodeList.Count - 1).InnerText + "_" + Convert.ToInt16(nodeList.Item(nodeList.Count - 2).InnerText) + ".xml";
                previous_cursor = next_cursor;

                xmlDoc.Save(xmlName);

            }
        }


Detailed explanation of examples of calling API to generate RSS resource files为了程序设计简单一点,笔者就有点偷懒了,没有仔细研究如何将所有的数据写到一个XML文件中,而是每次请求得到的20条数据写成一个XML文件,最后我159个好友,按照指定的命名方法生成了8个XML文件。

如此,就得到了所有的你的好友(就是你跟随的人)的信息了,以单人为例,其主要信息如下:

<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,这方便用户分多次到服务器上请求数据时可以此作为定位依据。

20

0</previous_cursor></p> </td></tr></tbody></table> <p><strong>4. </strong><strong>将XML</strong><strong>文件存储到ACCESS</strong><strong>数据库中进行备份</strong></p> <p>如果不想备份的可以直接从第3步中到第5步,但是笔者,觉得将数据转换成此构架后,更加方便后来的程序操作以及浏览数据。</p> <p>关于XML的详细方法参考:小气的鬼</p> <p>《在C#.net中如何操作XML》</p> <p>www.cnblogs.com/weekzero/archive/2005/06/21/178140.html</p> <p>下面开始读取刚才从新浪微博服务器上请求得到的XML文件了。然后转换成ACCESS数据库内容。(当然你要先用ACCESS在指定目录下建立一个*.mdb文件用来存储数据)</p> <p>下面是对单个XML文件进行读取,并插入到数据库中(这段代码是在<a href="http://www.php.cn/wiki/1494.html" target="_blank">ASP.NET</a>中写的)</p> <table style="width: 400px;" border="0"><tbody><tr class="firstRow"><td valign="top" width="400" style="word-break: break-all;"><pre class="brush:c#;toolbar:false;"> public void readTsinaFriends(string fileName) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(Server.MapPath(fileName)); XmlNodeList nodeList = xmlDoc.SelectSingleNode("users").ChildNodes;//获取 根节点的所有子节点 ; //删除不用的一级节点,比如提示人数的所在位置的标记 XmlNode root = xmlDoc.SelectSingleNode("users"); // XmlNodeList xnl = xmlDoc.SelectSingleNode("Employees").ChildNodes; for (int k = 0; k < nodeList.Count; k++) { XmlElement xe = (XmlElement)nodeList.Item(k); if(xe.Name=="user") {//去掉XML文件中不需要的节点:next_cursor,previous_coursor以及user节点中的status,方便XML直接转换成DataTable XmlNodeList nodeList1 = xmlDoc.SelectNodes("users/user");//得到所有的标签user一级节点 foreach (XmlNode xmlNodeTemp in nodeList1) { if (xmlNodeTemp.LastChild.Name == "status")//移除每个user节点中的"status"子节点--(一般情况下此节点都放在最后一个,所以就不遍历了,直接地址定位) { xmlNodeTemp.RemoveChild(xmlNodeTemp.LastChild); } } } else if (xe.Name == "next_cursor" || xe.Name == "previous_cursor") { root.RemoveChild(xe); if (k < nodeList.Count) k = k - 1; } } string tbxml = xmlDoc.OuterXml; DataTable dt = new DataTable(); DataSet ds = new DataSet(); System.IO.StringReader reader = new System.IO.StringReader(tbxml); ds.ReadXml(reader); dt = ds.Tables[0];//如果XML文本中有同名的父子节点,那么此语句就会多读出一条数据,这可能是此API函数的局限性吧 DataTable dtCopy = dt.Copy(); //dtCopy.Columns.Remove("url"); //dtCopy.Columns.Remove("profile_image_url"); dtCopy.Columns.Remove("description"); //这个字段里面字符编码不太规则,在插入ACCESS的时候总有问题,而且用处不大,所以就去除了。(又偷懒了呃) DataRow drTemp = dtCopy.NewRow(); string strInsert = string.Empty; OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\coursware\\网络软文\\API_微波\\weibo.mdb"); aConnection.Open(); for (int i = 0; i < dtCopy.Rows.Count - 1; i++) { drTemp = dtCopy.Rows[i]; strInsert = "&#39;"+drTemp[0].ToString()+"&#39;,&#39;"; for (int j = 1; j < dtCopy.Columns.Count - 1; j++) { strInsert += (drTemp[j].ToString() + "&#39;,&#39;"); } strInsert += drTemp[dtCopy.Columns.Count - 1].ToString() + "&#39;"; string strCmd = "INSERT INTO Friends VALUES(" + strInsert + ")"; OleDbCommand command = new OleDbCommand(strCmd, aConnection); command.ExecuteNonQuery(); } aConnection.Close(); }

 

对多个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中了。如下图所示:

Detailed explanation of examples of calling API to generate RSS resource files

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的文件了。大功告成了!

Detailed explanation of examples of calling API to generate RSS resource files

然后将此文件就可以导入到任何一个RSS阅读器中了,用户就能够通过RSS阅读器来获取微博信息了,而且现在的RSS阅读器都有个一键转贴到微博的功能,很方便的,不想转到自己微博的,也可以通过RSS阅读器直接收藏到阅读器中。辛苦了两天,今天能有这么一点小成果,还是觉得很不错的,呵呵,也祝大家也能好运。本次代码比较还需要各种完善,比如,如何将所以的数据写成一个XML文件,这个笔者就暂时不做了,留给大家去做吧。

Rss阅读器效果图如下:

Detailed explanation of examples of calling API to generate RSS resource files

【相关推荐】

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn