增加下载微信公众号文章功能
This commit is contained in:
87
20220330_Vote/Vote.Services/ApiController/WxService.cs
Normal file
87
20220330_Vote/Vote.Services/ApiController/WxService.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using Aspose.Words;
|
||||
using Furion.DynamicApiController;
|
||||
using HtmlAgilityPack;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.ViewFeatures;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Vote.Services.Entities;
|
||||
using Vote.Services.Tools;
|
||||
|
||||
namespace Vote.Services.ApiController
|
||||
{
|
||||
/// <summary>
|
||||
/// 微信
|
||||
/// </summary>
|
||||
[ApiDescriptionSettings("Vote", Order = 0)]
|
||||
[Route("/wx")]
|
||||
public class WxService : IDynamicApiController
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<Article> GetArticle(string url)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(url))
|
||||
{
|
||||
var html = Tools.HtmlHelper.GetHtmlSource2(url);// "https://mp.weixin.qq.com/s/9O8RYvm3nCZfc06yXggfPQ");
|
||||
var htmlDoc = new HtmlDocument();
|
||||
htmlDoc.LoadHtml(html);
|
||||
var article = new Article();
|
||||
var allContent = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='rich_media_wrp']");
|
||||
if (allContent != null)
|
||||
{
|
||||
//var postitemsNodes = allContent.SelectNodes("//div");
|
||||
article.Title = allContent.SelectSingleNode("//h1[@id='activity-name']").InnerText.Replace(" ", "").Replace("\n", "");
|
||||
//ViewBag.Detail = article.Detail = Vote.Services.Tools.HtmlHelper.CleanHtml(allContent.SelectSingleNode("//div[@id='js_content']").InnerHtml);
|
||||
var contents = allContent.SelectSingleNode("//div[@id='js_content']");
|
||||
var sections = contents.SelectNodes("//section");
|
||||
if (sections != null)
|
||||
{
|
||||
foreach (HtmlNode item in sections)
|
||||
{
|
||||
item.Name = "p";//data-src
|
||||
}
|
||||
}
|
||||
var imgs = contents.SelectNodes("//img");
|
||||
foreach (HtmlNode img in imgs)
|
||||
{
|
||||
var src = img.Attributes["src"];
|
||||
var datasrc = img.Attributes["data-src"];
|
||||
if (src == null || string.IsNullOrWhiteSpace(src.Value))
|
||||
{
|
||||
if (datasrc != null && !string.IsNullOrWhiteSpace(datasrc.Value))
|
||||
{
|
||||
img.SetAttributeValue("src", "data:image/jpeg;base64," + Tools.HtmlHelper.HttpRequestGetImageBase64(datasrc.Value));
|
||||
//src.Value = datasrc.Value;
|
||||
datasrc.Remove();
|
||||
//img.SetAttributeValue("style", "width:100%;");
|
||||
}
|
||||
}
|
||||
}
|
||||
article.Detail = allContent.SelectSingleNode("//div[@id='js_content']").InnerHtml;
|
||||
}
|
||||
else
|
||||
article.Detail = htmlDoc.DocumentNode.InnerHtml;
|
||||
return article;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public async Task<string> SaveDoc(string title, string html)
|
||||
{
|
||||
Aspose.Words.Document doc = new Aspose.Words.Document();
|
||||
DocumentBuilder build = new(doc);
|
||||
Aspose.Words.Font font = build.Font;
|
||||
font.Name = "宋体";
|
||||
build.InsertHtml(html);
|
||||
var path = Path.Combine(Environment.CurrentDirectory, title + ".docx");
|
||||
doc.Save(path, SaveFormat.Docx);
|
||||
return path;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user