这几天车子跟别人撞了,水箱撞稀烂了,要坐一个星期的公车
实在太无聊,就抽时间搞了这么个APP出来,打发公车上的时光
(开发者头条里的新闻专业性太强,公车上实在无法静心研读)
我经常看的也就博客园、infoq、36kr、开源中国的新闻
现在只做了博客园和infoq,接下来打算做36kr和开源中国的新闻,如果大家有什么好建议也可以在评论里提
数据都是准实时的取来的,以后争取做到你去刷就有新闻给你看
以后可能会做:新闻的分享、收藏、标签、分类等功能,当然这要看大家的热情程度啦!
废话少说,先来上图
接着上APK的下载连接(大家热情的话,苹果版也会有的)
https://github.com/xland/DeveloperNews/blob/master/DeveloperNews.apk?raw=true
接着公布源码
https://github.com/xland/DeveloperNews
关键源码讲解:
采集程序的源码
static void cnblogs() { CQ doc; try { var client = new RestClient("http://www.cnblogs.com/news/"); var resq = new RestRequest(Method.GET); var resp = client.Execute(resq); doc = resp.Content; } catch (Exception ex) { Thread.Sleep(GetWaitTime()); cnblogs(); return; } var arr = doc[".post_item_body"].ToList(); var dataList = new List<allen_news>(); var db = dbFactory.Open(); foreach (var item in arr) { var str = item.InnerText; var strArr = str.Split(Environment.NewLine.ToCharArray(),StringSplitOptions.RemoveEmptyEntries); var data = new allen_news(); data.news_title = strArr[0]; if (checkTitle(data.news_title)) { break; } data.news_summary = strArr[1].Trim(); data.author = strArr[2].Split("发布于".ToCharArray(),StringSplitOptions.RemoveEmptyEntries)[0]; data.add_time = DateTime.Now; data.from_site_flag = 0; data.news_url = ((CQ)item.InnerHTML)["h3 a"].Attr("href"); dataList.Insert(0, data); } if(dataList.Count >0) { db.InsertAll<allen_news>(dataList); } db.Dispose(); Console.WriteLine("增加了{0}条文章0", dataList.Count); Thread.Sleep(GetWaitTime()); cnblogs(); }
采集程序用到了CsQuery和RestSharp和ServiceStack.OrmLite三个开源程序
GetWaitTime()随机等1分钟到10分钟之间的一个时间长度,不要频繁的采,不要有规律的采,避免目标主机封锁采集程序所在的IP
再来看WEB服务的关键代码
protected void Page_Load(object sender, EventArgs e) { dbFactory = new OrmLiteConnectionFactory(ConfigurationManager.AppSettings["dbConnStr"], MySqlDialect.Provider); var action = Request["Action"]; var id = Request["Id"]; List<allen_news> result = null; if (action == "PullDown") { var db = dbFactory.Open(); result = db.SelectFmt<allen_news>("select * from allen_news where news_id > {0} order by news_id desc limit 0,30",id); db.Dispose(); } else if(action == "PullUp") { var db = dbFactory.Open(); result = db.SelectFmt<allen_news>("select * from allen_news where news_id < {0} order by news_id desc limit 0,30", id); db.Dispose(); } else if(action == "CheckVersion") { Response.Write("1.0.0"); Response.End(); return; } else { var db = dbFactory.Open(); result = db.Select<allen_news>("select * from allen_news order by news_id desc limit 0,30"); db.Dispose(); } Response.Write(JsonConvert.SerializeObject(result)); Response.End(); }
APK客户端的代码,没有什么特殊的,大家可以自己去看吧
OK,就这些啦,喜欢的请给我点个赞,或者到github上给我颗星
多谢各位啦
时间: 2024-09-29 10:32:52