C# 下载C站和Libu资源实现逻辑

下载Libu资源

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using HtmlAgilityPack;
using System.Net;
using System.IO;
using System.Threading;

namespace LoraTool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //读取解析文件1里面的内容
            textBox1.Text = System.IO.File.ReadAllText(Environment.CurrentDirectory + "\\解析文件1.txt");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //下载对象
            WebClient client = new WebClient();
            client.Encoding = Encoding.UTF8;

            this.button1.Text = "处理中...";
            this.button1.Enabled = false;

            string input = textBox1.Text;
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(input); //加载html
            HtmlNode node = doc.DocumentNode;
            // 代码中的.//img[@src]  是 XPath 路径代码,意思就是返回所有超链接
            HtmlNodeCollection aTags = node.SelectNodes(".//a[@href]"); //获取所有超链接
            int que = 0;
            foreach (var item in aTags)
            {
                try
                {
                    que++;

                    string name = item.SelectSingleNode(".//span[@class='modelList_subtitle__kCBID']").InnerText;//获取名字
                    string imgUrl = item.SelectSingleNode(".//img[@src]").Attributes["src"].Value.Split('?')[0];//图片地址
                    string infoUrl = "https://www.liblib.ai" + item.Attributes["href"].Value; //获得地址,用来获取下载地址


                    //获取下载地址
                    WebClient webClient = new WebClient();
                    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                    Stream myStream = webClient.OpenRead(infoUrl);
                    StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding("utf-8"));
                    string strHTML = sr.ReadToEnd();
                    myStream.Close();


                    Match match = Regex.Match(strHTML, "https://liblibai-online.vibrou.com/[0-9|a-z|A-Z|/]*.safetensors");
                    string downUrl = match.Value;

                    //去掉文件名中的特殊字符
                    foreach (var c in Path.GetInvalidFileNameChars())
                    {
                        name = name.Replace(c.ToString(), string.Empty);
                    }
                    //命名
                    name = que + "_" + name;

                    //新建保存地址
                    string folder = Environment.CurrentDirectory + "\\Libu";
                    if (!Directory.Exists(folder))
                    {
                        Directory.CreateDirectory(folder);
                    }

                    //输出
                    textBox2.AppendText("序号:" + que + "/" + aTags.Count);
                    textBox2.AppendText("\r\n名称:" + name);
                    textBox2.AppendText("\r\n图片地址:" + imgUrl);
                    textBox2.AppendText("\r\n详情地址:" + infoUrl);
                    textBox2.AppendText("\r\n下载地址:" + downUrl);

                    if (string.IsNullOrEmpty(imgUrl) || string.IsNullOrEmpty(infoUrl) || string.IsNullOrEmpty(downUrl))
                    {
                        que--;
                        textBox2.AppendText("\r\n执行异常:" + "ImgUrl或InfoUrl或DownUrl解析为空");
                        continue;
                    }

                    //下载图片
                    client.DownloadFile(imgUrl, folder + "\\" + name + Path.GetExtension(imgUrl));
                    //下载Lora模型
                    client.DownloadFile(downUrl, folder + "\\" + name + Path.GetExtension(downUrl));

                    textBox2.AppendText("\r\n执行成功");
                }
                catch (Exception ex)
                {
                    que--;
                    textBox2.AppendText("\r\n执行异常:" + ex.Message);
                }
                textBox2.AppendText("\r\n------------------------\r\n");
            }

            System.IO.File.WriteAllText(Environment.CurrentDirectory + "\\Libu执行结果.txt", textBox2.Text);

            this.button1.Text = "解析内容";
            this.button1.Enabled = true;

        }

       
    }
}

下载C站实现逻辑

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;
using Newtonsoft.Json;
using HtmlAgilityPack;

namespace LoraTool
{
    public class CSite
    {
        public int id { get; set; }
        public string name { get; set; }
    }
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            //读取解析文件1里面的内容
            string json = System.IO.File.ReadAllText(Environment.CurrentDirectory + "\\解析文件2.txt");

            // 将JSON字符串转换为C#对象
            List person = JsonConvert.DeserializeObject>(json);

            textBox1.Text = JsonConvert.SerializeObject(person);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //下载对象
            WebClient client = new WebClient();
            client.Encoding = Encoding.UTF8;

            this.button1.Text = "处理中...";
            this.button1.Enabled = false;

            string input = textBox1.Text;

            // 将JSON字符串转换为C#对象
            List items = JsonConvert.DeserializeObject>(input);

            int que = 0;
            foreach (var item in items)
            {
                try
                {
                    que++;

                    //if (item.id != 25537) continue;

                    string name = item.name;//获取名字
                    string infoUrl = "https://civitai.com/models/" + item.id; //获得地址,用来获取下载地址


                    //获取下载地址
                    WebClient webClient = new WebClient();
                    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                    Stream myStream = webClient.OpenRead(infoUrl);
                    StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding("utf-8"));
                    string strHTML = sr.ReadToEnd();
                    myStream.Close();


                    Match match = Regex.Match(strHTML, "/api/download/models/[0-9]*");
                    //下载地址
                    string downUrl = "https://civitai.com" + match.Value;


                    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                    doc.LoadHtml(strHTML); //加载html
                    HtmlNode node = doc.DocumentNode;
                    string imgUrl = node.SelectSingleNode(".//img[@class='mantine-7aj0so']").Attributes["src"].Value;//图片地址

                

                    //去掉文件名中的特殊字符
                    foreach (var c in Path.GetInvalidFileNameChars())
                    {
                        name = name.Replace(c.ToString(), string.Empty);
                    }
                    //命名
                    name = que + "_" + name;

                    //新建保存地址
                    string folder = Environment.CurrentDirectory + "\\CSite";
                    if (!Directory.Exists(folder))
                    {
                        Directory.CreateDirectory(folder);
                    }

                    //输出
                    textBox2.AppendText("序号:" + que + "/" + items.Count);
                    textBox2.AppendText("\r\n名称:" + name);
                    textBox2.AppendText("\r\n图片地址:" + imgUrl);
                    textBox2.AppendText("\r\n详情地址:" + infoUrl);
                    textBox2.AppendText("\r\n下载地址:" + downUrl);

                    if (string.IsNullOrEmpty(imgUrl) || string.IsNullOrEmpty(infoUrl) || string.IsNullOrEmpty(downUrl))
                    {
                        que--;
                        textBox2.AppendText("\r\n执行异常:" + "ImgUrl或InfoUrl或DownUrl解析为空");
                        continue;
                    }

                    //下载图片
                    client.DownloadFile(imgUrl, folder + "\\" + name + Path.GetExtension(imgUrl));
                    //下载Lora模型
                    client.DownloadFile(downUrl, folder + "\\" + name + ".safetensors");

                    textBox2.AppendText("\r\n执行成功");
                }
                catch (Exception ex)
                {
                    que--;
                    textBox2.AppendText("\r\n执行异常:" + ex.Message);
                }
                textBox2.AppendText("\r\n------------------------\r\n");
            }

            System.IO.File.WriteAllText(Environment.CurrentDirectory + "\\C站执行结果.txt", textBox2.Text);

            this.button1.Text = "解析内容";
            this.button1.Enabled = true;

        }


    }
}

你可能感兴趣的:(C#,c#,c语言,数据库)