public IHttpActionResult PoetryDetails(long poetryId, int pn, Boolean type = false) { try { var poetry = db.Poetry.AsNoTracking().Where(p => p.poetryId == poetryId).Select(p => new { p.name, p.dynasty, p.poetryContent, p.shangxi, p.about, p.author, p.tags, p.poetryForBaidu }).First(); Dictionary <string, dynamic> result = new Dictionary <string, dynamic>(); if (type) { string poetryContent = ChineseStringUtility.ToTraditional(poetry.poetryContent); string name = ChineseStringUtility.ToTraditional(poetry.name); string dynasty = ChineseStringUtility.ToTraditional(poetry.dynasty); string shangxi = ChineseStringUtility.ToTraditional(poetry.shangxi); string about = ChineseStringUtility.ToTraditional(poetry.about); string tags = ChineseStringUtility.ToTraditional(poetry.tags); string author = ChineseStringUtility.ToTraditional(poetry.author); string baiduPoetry = ChineseStringUtility.ToTraditional(poetry.poetryForBaidu); result.Add("name", name); result.Add("dynasty", dynasty); result.Add("poetryContent", poetryContent); result.Add("shangxi", shangxi); result.Add("about", about); result.Add("tags", tags); result.Add("author", author); result.Add("baiduPoetry", baiduPoetry); } else { result.Add("name", poetry.name); result.Add("dynasty", poetry.dynasty); result.Add("poetryContent", poetry.poetryContent); result.Add("shangxi", poetry.shangxi); result.Add("about", poetry.about); result.Add("tags", poetry.tags); result.Add("author", poetry.author); result.Add("baiduPoetry", poetry.poetryForBaidu); } string image = getPoetryImage(poetry.name, pn); result.Add("image", image); return(Json(result)); } catch { return(Json("error")); } }
public IHttpActionResult SearchRandomSentence(Boolean type) { Random rd = new Random(); long randomNum = rd.Next(1, 10001); var data = db.PoetrySentence.AsNoTracking().Where(p => p.id == randomNum).Select(p => new { p.poetry, p.poetryId }).FirstOrDefault(); if (type) { Dictionary <string, dynamic> result = new Dictionary <string, dynamic>(); string poetry = ChineseStringUtility.ToTraditional(data.poetry); result.Add("poetry", poetry); result.Add("poetryId", data.poetryId); return(Json(result)); } else { return(Json(data)); } }
public IHttpActionResult CollectList(int page, Boolean type) { string username = HttpContext.Current.Request.Cookies["UserInfo"].Value; username = AES.DecryptAes(username, AES.AesKey); var data = (from a in db.UserList.Where(p => p.username == username) join b in db.Collect on a.id equals b.userId orderby b.time descending select new { b.poetryId }).Skip(10 * (page - 1)).Take(10).ToList(); List <object> result = new List <object>(); for (int i = 0; i < data.Count(); i++) { long poetryId = data[i].poetryId; var poetryData = db.Poetry.Where(p => p.poetryId == poetryId).Select(p => new { p.poetryId, p.name, p.dynasty, p.author, p.poetryContent }).First(); Dictionary <string, dynamic> tempResult = new Dictionary <string, dynamic>(); if (type) { string name = ChineseStringUtility.ToTraditional(poetryData.name); string dynasty = ChineseStringUtility.ToTraditional(poetryData.dynasty); string author = ChineseStringUtility.ToTraditional(poetryData.author); string poetryContent = ChineseStringUtility.ToTraditional(poetryData.poetryContent); tempResult.Add("name", name); tempResult.Add("dynasty", dynasty); tempResult.Add("author", author); tempResult.Add("poetryContent", poetryContent); } else { tempResult.Add("name", poetryData.name); tempResult.Add("dynasty", poetryData.dynasty); tempResult.Add("author", poetryData.author); tempResult.Add("poetryContent", poetryData.poetryContent); } tempResult.Add("poetryId", data[i].poetryId); result.Add(tempResult); } return(Json(result)); }
public IHttpActionResult SearchPoet(int page, Boolean type) { var poetList = db.Poet.AsNoTracking().OrderBy(p => Guid.NewGuid()).Skip(10 * (page - 1)).Take(10).Select(p => new { p.name, p.image, p.minIntroduce }).ToList(); if (type) { List <object> list = new List <object>(); for (int i = 0; i < poetList.Count(); i++) { Dictionary <string, dynamic> result = new Dictionary <string, dynamic>(); string name = ChineseStringUtility.ToTraditional(poetList[i].name); string minIntroduce = ChineseStringUtility.ToTraditional(poetList[i].minIntroduce); result.Add("image", poetList[i].image); result.Add("name", name); result.Add("minIntroduce", minIntroduce); list.Add(result); } return(Json(list)); } else { return(Json(poetList)); } }
public IHttpActionResult SearchPoetryFromTags(int page, string tags, string dynasty, string state, Boolean type) { if (dynasty == null) { dynasty = ""; } if (tags == null) { tags = ""; } if (state == null) { state = ""; } tags = ChineseStringUtility.ToSimplified(tags); dynasty = ChineseStringUtility.ToSimplified(dynasty); state = ChineseStringUtility.ToSimplified(state); List <dynamic> poetry = new List <dynamic>(); if (dynasty == "") { var tempResult = db.Poetry.Where(p => p.tags.Contains(tags) && p.tags.Contains(state)).OrderByDescending(p => p.star).Skip(10 * (page - 1)).Take(10).Select(p => new { p.poetryId, name = p.name + "\n" + p.dynasty + "·" + p.author, p.poetryContent, p.dynasty, p.author, onlyname = p.name }).ToList(); poetry.Add(tempResult); } else if (dynasty == "当代" || dynasty == "明末清初" || dynasty == "近现代" || dynasty == "民国末当代初") // 因为这几个没有star 所有每次随机取 { var tempResult = db.Poetry.Where(p => p.tags.Contains(tags) && p.tags.Contains(state) && p.dynasty == dynasty).OrderBy(p => Guid.NewGuid()).Skip(10 * (page - 1)).Take(10).Select(p => new { p.poetryId, name = p.name + "\n" + p.dynasty + "·" + p.author, p.poetryContent, p.dynasty, p.author, onlyname = p.name }).ToList(); poetry.Add(tempResult); } else { var tempResult = db.Poetry.Where(p => p.tags.Contains(tags) && p.tags.Contains(state) && p.dynasty == dynasty).OrderByDescending(p => p.star).Skip(10 * (page - 1)).Take(10).Select(p => new { p.poetryId, name = p.name + "\n" + p.dynasty + "·" + p.author, p.poetryContent, p.dynasty, p.author, onlyname = p.name }).ToList(); poetry.Add(tempResult); } //var tempResult = db.Poetry.Where(p => p.tags.Contains(tags) && p.tags.Contains(state) && p.dynasty == dynasty).OrderByDescending(p => p.star).Skip(10 * (page - 1)).Take(10).Select(p => new { p.poetryId, name = p.name + "\n" + p.dynasty + "·" + p.author, p.poetryContent, p.dynasty, p.author, onlyname = p.name }).ToList(); //List<dynamic> poetry = new List<dynamic>(tempResult); List <object> data = new List <object>(); if (type) { for (int i = 0; i < poetry[0].Count; i++) { Dictionary <string, dynamic> result = new Dictionary <string, dynamic>(); string name = ChineseStringUtility.ToTraditional(poetry[0][i].onlyname) + "\n" + ChineseStringUtility.ToTraditional(poetry[0][i].dynasty) + "·" + ChineseStringUtility.ToTraditional(poetry[0][i].author); string poetryContent = ChineseStringUtility.ToTraditional(poetry[0][i].poetryContent); result.Add("poetryId", poetry[0][i].poetryId); result.Add("name", name); result.Add("poetryContent", poetryContent); data.Add(result); } return(Json(data)); } else { return(Json(poetry)); } }
public IEnumerable <dynamic> PoetryTopStar(int page, Boolean type = false) { //List<object> data = new List<object>(); var tempPoetry = db.Poetry.AsNoTracking().OrderBy(p => Guid.NewGuid()).Skip(10 * (page - 1)).Take(10).Select(p => new { p.poetryId, name = p.name + "\n" + p.dynasty + "·" + p.author, p.poetryContent, p.dynasty, p.author, onlyname = p.name }); List <dynamic> poetry = new List <dynamic>(tempPoetry); List <object> data = new List <object>(); if (type) { for (int i = 0; i < poetry.Count(); i++) { Dictionary <string, dynamic> result = new Dictionary <string, dynamic>(); string name = ChineseStringUtility.ToTraditional(poetry[i].onlyname) + "\n" + ChineseStringUtility.ToTraditional(poetry[i].dynasty) + "·" + ChineseStringUtility.ToTraditional(poetry[i].author); string poetryContent = ChineseStringUtility.ToTraditional(poetry[i].poetryContent); result.Add("poetryId", poetry[i].poetryId); result.Add("name", name); result.Add("poetryContent", poetryContent); data.Add(result); } return(data); } else { return(poetry); } }
public IEnumerable <dynamic> SearchPoetry(string key, int page, Boolean type, Boolean bol) { if (bol) // bol 等于true 的时候根据诗人查古诗 { if (key != null) { key = ChineseStringUtility.ToSimplified(key); } var tempPoetry = db.Poetry.AsNoTracking().Where(p => p.author == key).OrderByDescending(p => p.star).Skip(10 * (page - 1)).Take(20).Select(p => new { p.poetryId, name = p.name + "\n" + p.dynasty + "·" + p.author, p.poetryContent, p.dynasty, p.author, onlyname = p.name }).ToList(); List <dynamic> poetry = new List <dynamic>(tempPoetry); List <object> data = new List <object>(); if (type) { for (int i = 0; i < poetry.Count(); i++) { Dictionary <string, dynamic> result = new Dictionary <string, dynamic>(); string name = ChineseStringUtility.ToTraditional(poetry[i].onlyname) + "\n" + ChineseStringUtility.ToTraditional(poetry[i].dynasty) + "·" + ChineseStringUtility.ToTraditional(poetry[i].author); string poetryContent = ChineseStringUtility.ToTraditional(poetry[i].poetryContent); result.Add("poetryId", poetry[i].poetryId); result.Add("name", name); result.Add("poetryContent", poetryContent); data.Add(result); } return(data); } else { return(poetry); } } else // bol 等于false 的时候根据诗人,诗名,诗词查古诗 { //先根据关键词查诗名 key = ChineseStringUtility.ToSimplified(key); var tempPoetry = db.Poetry.AsNoTracking().Where(p => p.name.Contains(key) || p.author.Contains(key) || p.poetryContent.Contains(key)).OrderByDescending(p => p.star).Skip(10 * (page - 1)).Take(10).Select(p => new { p.poetryId, name = p.name + "\n" + p.dynasty + "·" + p.author, p.poetryContent, p.star, p.dynasty, p.author, onlyname = p.name }).ToList(); if (type) { List <object> list = new List <object>(); for (int i = 0; i < tempPoetry.Count(); i++) { Dictionary <string, dynamic> result = new Dictionary <string, dynamic>(); string name = ChineseStringUtility.ToTraditional(tempPoetry[i].onlyname) + "\n" + ChineseStringUtility.ToTraditional(tempPoetry[i].dynasty) + '·' + ChineseStringUtility.ToTraditional(tempPoetry[i].author); string poetryContent = ChineseStringUtility.ToTraditional(tempPoetry[i].poetryContent); result.Add("name", name); result.Add("poetryContent", poetryContent); result.Add("poetryId", tempPoetry[i].poetryId); list.Add(result); } return(list); } else { return(tempPoetry); } } }
public IHttpActionResult getPoetryBaidu(string word, Boolean type) { string url = "https://baike.baidu.com/item/" + word; HttpWebRequest httpReq; HttpWebResponse httpResp; string strBuff = ""; char[] cbuffer = new char[256]; int byteRead = 0; Uri httpURL = new Uri(url); httpReq = (HttpWebRequest)WebRequest.Create(httpURL); httpResp = (HttpWebResponse)httpReq.GetResponse(); Stream respStream = httpResp.GetResponseStream(); StreamReader respStreamReader = new StreamReader(respStream, Encoding.UTF8); byteRead = respStreamReader.Read(cbuffer, 0, 256); while (byteRead != 0) { string strResp = new string(cbuffer, 0, byteRead); strBuff = strBuff + strResp; byteRead = respStreamReader.Read(cbuffer, 0, 256); } respStream.Close(); Regex reg = new Regex(@"(?m)<div class=""para"" [^>]*>(?<div>(?:\w|\W)*?)[^(]*</div[^>]*>"); List <string> image = new List <string>(); MatchCollection match = reg.Matches(strBuff); foreach (Match m in match) { image.Add(m.Value); // 序号为6的是大图 } string result = null; for (int i = 0; i < image.Count(); i++) { result = result + image[i]; } if (result == null) { return(Json("未找到数据")); } string pppppp = Regex.Replace(result, @"<table\b[^>]*>([\s\S]*?)</table>", ""); string ppppppp = Regex.Replace(pppppp, @"<img\b[^>]*>([\s\S]*?)</img>", ""); string qqqqq = Regex.Replace(ppppppp, @"<dl\b[^>]*>([\s\S]*?)</dl>", ""); string qqqq = Regex.Replace(qqqqq, @"<dd\b[^>]*>([\s\S]*?)</dd>", ""); string qaz = Regex.Replace(qqqq, @"<img\b[^>]*>", ""); string qaz1 = Regex.Replace(qaz, @"<li\b[^>]*>", ""); string cts = Regex.Replace(qaz1, @"(?m)<div class=""anchor-list""[^>]*>(?<div>(?:\w|\W)*?)</div[^>]*>", ""); string cts1 = Regex.Replace(cts, @"(?m)<div class=""hotspotminingContent_rel_tit""[^>]*>(?<div>(?:\w|\W)*?)</div[^>]*>", ""); string cts2 = Regex.Replace(cts1, @"(?m)<div class=""hotspotminingContent_web""[^>]*>(?<div>(?:\w|\W)*?)</div[^>]*>", ""); string ctsqq = Regex.Replace(cts2, @"(?m)<a class=""edit-icon j-edit-link""[^>]*>(?<div>(?:\w|\W)*?)</a[^>]*>", ""); //一定要在过滤a标签之前 string xx = Regex.Replace(ctsqq, "<a (.*?)>", "", RegexOptions.Compiled); //过滤a标签 string ct = Regex.Replace(xx, @"(?m)<div class=""lemmaWgt-lemmaCatalog""[^>]*>(?<div>(?:\w|\W)*?)</div[^>]*>", ""); string ct1 = Regex.Replace(ct, @"(?m)<div class=""album-wrap""[^>]*>(?<div>(?:\w|\W)*?)</div[^>]*>", ""); string ct2 = Regex.Replace(ct1, @"(?m)<div id=""hotspotmining_s""[^>]*>(?<div>(?:\w|\W)*?)</div[^>]*>", ""); string a = Regex.Replace(ct2, @"(?m)<div class=""tashuo-bottom""[^>]*>(?<div>(?:\w|\W)*?)</div[^>]*>", ""); string p = Regex.Replace(a, @"(?m)<div class=""tashuo-multiple""[^>]*>(?<div>(?:\w|\W)*?)</div[^>]*>", ""); string pq = Regex.Replace(p, @"(?m)<div class=""side-content""[^>]*>(?<div>(?:\w|\W)*?)</div[^>]*>", ""); string p1 = Regex.Replace(pq, @"(?m)<div class=""lemma-picture text-pic layout-right""[^>]*>(?<div>(?:\w|\W)*?)</div[^>]*>", ""); string pa = Regex.Replace(p1, @"(?m)<div class=""lemmaWgt-promotion-vbaike""[^>]*>(?<div>(?:\w|\W)*?)</div[^>]*>", ""); string paa = Regex.Replace(pa, @"(?m)<div class=""tashuo-operator""[^>]*>(?<div>(?:\w|\W)*?)</div[^>]*>", ""); string paaa = Regex.Replace(paa, @"(?m)<div class=""album-list""[^>]*>(?<div>(?:\w|\W)*?)</div[^>]*>", ""); string b = Regex.Replace(paaa, @"(?m)<sup class=""normal""[^>]*>(?<div>(?:\w|\W)*?)</sup[^>]*>", ""); string aa = Regex.Replace(b, @"(?m)<sup class=""sup--normal""[^>]*>(?<div>(?:\w|\W)*?)</sup[^>]*>", ""); string aaa = Regex.Replace(aa, @"(?m)<div id=""open-tag""[^>]*>(?<div>(?:\w|\W)*?)</div[^>]*>", ""); string aaaa = Regex.Replace(aaa, @"(?m)<div class=""wgt-footer-main""[^>]*>(?<div>(?:\w|\W)*?)</div[^>]*>", ""); if (type) { aaa = ChineseStringUtility.ToTraditional(aaa); } return(Json(aaaa)); }