public string Get() { Dictionary <string, object> resultDic = new Dictionary <string, object>(); bool result = false; string error = string.Empty; try { int count = CnblogDal.GetCount(); int forCount = (count / 100) + (count % 100 == 0 ? 0 : 1); for (int i = 1; i < forCount + 1; i++) { List <Cnblog> cnBlogList = CnblogDal.GetPage(i); DownloadHtml(cnBlogList); } result = true; } catch (Exception e) { error = e.Message; } resultDic.Add("result", result); if (!result) { resultDic.Add("error", error); } return(JsonConvert.SerializeObject(resultDic)); }
public bool DownloadHtml(List <Cnblog> list) { try { List <CnblogHtml> cnblogHtmlList = new List <CnblogHtml>(); string path = _hostingEnvironment.WebRootPath; string downloadPath = path + "/download"; if (!Directory.Exists(downloadPath)) { Directory.CreateDirectory(downloadPath); } foreach (var item in list) { try { WebClient webClient = new WebClient(); Uri uri = new Uri(item.Href); //string downloadStr = webClient.DownloadString(uri); //RedisCommon.GetData().HashSet("cnblog:html", item.Id, downloadStr); //cnblogHtmlList.Add(new CnblogHtml() { Id = item.Id, Html = downloadStr }); webClient.DownloadFile(uri, downloadPath + "/" + item.Id + ".html"); } catch (Exception e) { log.ErrorFormat("下载HTML发生异常:{0}", e.Message); } } CnblogDal.InsertCnblogHtml(cnblogHtmlList); } catch (Exception e) { throw e; } return(true); }
//主页 public IActionResult Home() { var cnblogs = CnblogDal.GetPage(1, 10); return(View(cnblogs)); }