.net几行代码音乐API各排行榜 热搜 入库

对比了几家大厂的音乐API的接口 这家相对规范些

现在开始从零开始 net6敏捷开发对接 入库吧

关键技术工具和思维

  1. 1 json 生成类
  2. 2 分析类 规划表设计
  3. 3 sqlsuger codefirst 生成表
  4. 4 封装get post 连接
  5. 5 类映射automapper
  6. 6 sqlsuger 插入数据

1 json 生成类

宇宙 第 一的编辑器生成 相应的类

cs 复制代码
using miniAPI.Model;

namespace miniAPI.Models
{
    public class QQtoplistDTO
    { 

        public class DataQQtoplistDTO
        {
            public List<Toplist> topList { get; set; }
        }

        public class Toplist
        {
            public DateTime CreateDate { get; set; } 
            public int id { get; set; }
            public int listenCount { get; set; }
            public string picUrl { get; set; }
            public List<TopSonglist> songList { get; set; }
            public string topTitle { get; set; }
            public int type { get; set; }
        }

        //public class Songlist
        //{
        //    public string singername { get; set; }
        //    public string songname { get; set; }
        //}

    }

    public class Outputpara<T>
    {
        private int _code = 500;
        public int code
        {
            get { return _code; }
            set { _code = value; }
        }
        public int subcode { get; set; }
        private string _msg = "";
        public string msg
        {
            get { return _msg; }
            set { _msg = value; }
        }
        public string message { get; set; }
        public int _default { get; set; }
        //public Data data { get; set; }
        public T data { get; set; }
    }

}

2 分析类 规划表设计

排行榜单 维度和songlist的明细

3 codefirst 生成表

sqlsuger 爽得不要不要的

4 封装get post 连接

使用 httpClientFactory

cs 复制代码
 {
 private IHttpClientFactory _httpClientFactory;
        private readonly Cachelper _cachelper;
        private readonly IMapper _mapper;
        public MusicApibyqqController(IMapper mapper, IHttpClientFactory httpClientFactory, Cachelper cachelper)
        {
            _mapper = mapper;
            _httpClientFactory = httpClientFactory;
            _cachelper = cachelper;
        } 

  [HttpGet]
        [Route("createmusicapibyqq")]
        public async Task<string> CreateMusicApibyqqIndex()
        {
 var client = _httpClientFactory.CreateClient();
            var param = new {   };
            string jsonData = System.Text.Json.JsonSerializer.Serialize(param);
            StringContent paramContent = new StringContent(jsonData);

 var logOutResponse = await client.GetAsync(url);
                string resultStr = await logOutResponse.Content.ReadAsStringAsync();
                Outputpara<DataQQtoplistDTO> response = System.Text.Json.JsonSerializer.Deserialize<Outputpara<DataQQtoplistDTO>>(resultStr);
       Console.WriteLine($"url:{url},  成功");
                }
                else
                {
                    Console.WriteLine($"url:{url}, 失败");
                }
            }; 
            return "同步完毕 toplist:" + listToplist.Count+ " TopSonglist:"+ listTopSonglist.Count;
}

5 类映射automapper

cs 复制代码
using AutoMapper;
using miniAPI.Model;
using static miniAPI.Models.QQtoplistDTO;

namespace miniAPI.Config
{
    /// <summary>
    /// Dto的映射配置
    /// </summary>
    public class AutoMapperConfigs : Profile
    {
        public AutoMapperConfigs()
        {
            //从 Toplist=》 TopMusiclist
            CreateMap<Toplist, TopMusiclist>(); 
        }
    }
}

注册

cs 复制代码
//Automapper映射
builder.Services.AddAutoMapper(typeof(AutoMapperConfigs));

映射 使用

_mapper.Map(temptoplist, tempTopMusiclist);

6 插入数据

cs 复制代码
 foreach (var temptoplist in response?.data?.topList)
                    { 
                        TopMusiclist tempTopMusiclist = new TopMusiclist(); 
                        _mapper.Map(temptoplist, tempTopMusiclist);
                        tempTopMusiclist.CreateDate = DateTime.Now;
                        tempTopMusiclist.codeid = temptoplist.id;
                        listToplist.Add(tempTopMusiclist);
                        foreach (var tempsongList in temptoplist.songList)
                        {
                            tempsongList.codeid=temptoplist.id; 
                            listTopSonglist.Add(tempsongList);
                        } 
                    } 
                    SqlSugarHelper.addtoplist(listToplist);
                    SqlSugarHelper.addtopSonglist(listTopSonglist); 
cs 复制代码
    Db.Insertable(listToplist).ExecuteCommand();

开始使用数据吧 据说API不能商用 QAQ 但放出来干啥QAQ

相关推荐
rockey6273 小时前
C#脚本引擎之AScript与DynamicExpresso对比
c#·.net·script·动态脚本
不会代码的小猴18 小时前
7. JSON
开发语言·c++·笔记·qt·算法·json
qq_5486124519 小时前
.NET 平台报表工具汇总(.NET Framework /.NET6-8,中国式复杂报表、Web 嵌入、填报、导出打印)
前端·.net
公子小六19 小时前
基于.NET的Windows窗体编程之WinForms音频控件
windows·microsoft·c#·.net·音视频·winforms
界面开发小八哥1 天前
界面控件DevExpress Blazor v26.1新版亮点 - Navigation(导航)
javascript·.net·界面控件·blazor·devexpress·ui开发
丰锋ff1 天前
7.1Json
json
用户298698530141 天前
C# 实现 PowerPoint 多格式转换:图片、PDF 与 SVG 实战
c#·.net·svg
江畔柳前堤1 天前
LLM + Agent 模型效果评估:从入门到工业级体系构建的完整指南
开发语言·人工智能·自然语言处理·chatgpt·架构·json·batch
Elastic 中国社区官方博客1 天前
跳过 mapping 爆炸:ES|QL 无需动态 mapping 即可查询无 schema JSON key
大数据·人工智能·sql·elasticsearch·搜索引擎·json·全文检索
李高钢1 天前
【WPF】从 Avalon 到 .NET 10:WPF 平台演进史与特性全览
.net·wpf