private static IEnumerable <PttArticle> GetArticles(string html) { var titleRegex = new Regex("<div class=\"title\">([\\s\\S]*?)</div>"); var authorRegex = new Regex("<div class=\"author\">(.*)</div>"); var dateRegex = new Regex("<div class=\"date\">(.*)</div>"); var titleMatch = titleRegex.Match(html); var authorMatch = authorRegex.Match(html); var dateMatch = dateRegex.Match(html); var articles = new List <PttArticle>(); while (titleMatch.Success) { var titleDiv = titleMatch.Groups[1].Value; var regex = new Regex("<a href=\"(.*)\">(.*)</a>"); var match = regex.Match(titleDiv); var article = new PttArticle() { Title = match.Groups[2].Value, Link = match.Groups[1].Value, Author = authorMatch.Groups[1].Value, Date = dateMatch.Groups[1].Value }; articles.Add(article); titleMatch = titleMatch.NextMatch(); authorMatch = authorMatch.NextMatch(); dateMatch = dateMatch.NextMatch(); } return(articles); }
public async Task Insert(PttArticle article) { var json = JsonConvert.SerializeObject(article); var filePath = $"{_webHostEnvironment.ContentRootPath}{DirPath}Stock/{article.Title}.json"; if (!File.Exists(filePath)) { await File.WriteAllTextAsync(filePath, json); } }