{"size": 2221, "ext": "js", "max_stars_repo_path": "数据库/mongodb/代码/spider.js", "max_stars_repo_name": "SuYxh/Web-Study-Record", "max_stars_repo_stars_event_min_datetime": "2021-04-12T16:52:10.000Z", "max_stars_repo_stars_event_max_datetime": "2021-04-12T16:52:10.000Z", "max_issues_repo_path": "数据库/mongodb/代码/spider.js", "max_issues_repo_name": "SuYxh/Web-Study-Record", "max_issues_repo_issues_event_min_datetime": "2021-09-02T16:58:05.000Z", "max_issues_repo_issues_event_max_datetime": "2021-10-06T22:53:36.000Z", "max_forks_repo_path": "数据库/mongodb/代码/spider.js", "max_forks_repo_name": "SuYxh/Web-Study-Record", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 4, "max_forks_count": null, "avg_line_length": 28.8441558442, "max_line_length": 104, "alphanum_fraction": 0.5929761369}

let axios = require('axios')


//设置insertManyPromise函数
function insertMany(collection,arr){
    return new Promise((resolve,reject)=>{
        var MongoClient = require('mongodb').MongoClient;
        var url = "mongodb://118.126.96.227:27017/";
        MongoClient.connect(url, { useNewUrlParser: true,useUnifiedTopology: true }, function(err, db) {
            if (err) throw err;
            var dbo = db.db("loldb");
            dbo.collection(collection).insertMany(arr, function(err, res) {
                if (err) reject(err);
                console.log("插入的文档数量为: " + res.insertedCount);
                db.close();
                resolve()
            });
        });
    })
    
}



//设置insertManyPromise函数
function insertOne(collection,obj){
    return new Promise((resolve,reject)=>{
        var MongoClient = require('mongodb').MongoClient;
        var url = "mongodb://118.126.96.227:27017/";
        MongoClient.connect(url, { useNewUrlParser: true,useUnifiedTopology: true }, function(err, db) {
            if (err) throw err;
            var dbo = db.db("loldb");
            dbo.collection(collection).insertOne(obj, function(err, res) {
                if (err) reject(err);
                console.log("文档插入成功");
                db.close();
                resolve()
            });
        });
    })    
}


//获取英雄列表
async function getHeroList(){
    let httpUrl = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
    let result = await axios.get(httpUrl);
    console.log(result.data.hero)
    await insertMany('herolist',result.data.hero)
    return result.data.hero;
}
// getHeroList()


// 获取英雄信息内容，并存入数据库
async function getHero(heroid){
    let httpUrl = `https://ossweb-img.qq.com/images/lol/act/img/js/hero/${heroid}.js`
    let result = await axios.get(httpUrl)
    await insertOne('heroinfo',result.data.hero)
    return result.data;
}



//定义主函数，先获取所有英雄列表，并循环英雄列表将所有英雄详情内容载入。
async function run(){
    let herolist = await getHeroList();
    await herolist.reduce(async (prev,item,i)=>{
        await prev
        return new Promise(async (resolve,reject)=>{
            await getHero(item.heroId) 
            resolve()
        })
    },Promise.resolve())
}
run()