示例#1
0
        /// <summary>
        /// 有则更新,无则添加
        /// </summary>
        /// <param name="songId">songId</param>
        /// <param name="songName">songName</param>
        /// <param name="artist">artist</param>
        /// <param name="count">count</param>
        public void SolveSongs(int songId, string songName, string artist, int count)
        {
            StringBuilder updateCmd = new StringBuilder();

            //判断songId是否已经存在
            DAL.GetSongList getsonglist = new DAL.GetSongList();
            //List<int> list = getsonglist.getAllSongs();
            //有则更新,无则添加
            //if (list.Contains(songId))
            //{
            //    getsonglist.UpdateCount(songId.ToString(), count.ToString());
            //}
            //else
            //{
            MySqlConnection conn = null;

            try
            {
                using (conn = new MySqlConnection(mysqlAddress))
                {
                    //歌名可能会有',导致插入失败,转为双引号
                    updateCmd.AppendFormat("insert into music_v2 (songId, songName, artist, count) values ({0},'{1}','{2}',{3}) on duplicate key update count = {4}", songId, songName.Replace("'", "''"), artist, count, count);
                    string updateCmdstr = updateCmd.ToString();
                    updateCmd.Clear();
                    //private string updateCmd = "update music_v2 set count ";
                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    Console.WriteLine("命令:" + updateCmdstr + "\n");

                    using (MySqlCommand cmd = new MySqlCommand(updateCmdstr, conn))
                    {
                        //执行sql
                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception e)
            {
                //出现主键冲突代表已存在  则更新
                if (e.ToString().ToUpper().Contains("Duplicate entry".ToUpper()))
                {
                    UpdateCount(songId, count);
                }
                else
                //else if (e.ToString().ToUpper().Contains("syntax".ToUpper()))
                {
                    Console.WriteLine(e.ToString() + "\n" + updateCmd);
                }
            }
        }
示例#2
0
        /// <summary>
        /// 抓取
        /// </summary>
        /// <param name="startId">起始ID</param>
        /// <param name="endId">终止ID</param>
        /// <param name="threadNumber">线程数</param>
        private void GetSongs(int startId, int endId, int threadNumber)
        {
            DAL.Send        post        = new DAL.Send();
            DAL.GetSongList getsonglist = new DAL.GetSongList();
            StringBuilder   onceGet     = new StringBuilder();
            StringBuilder   artists     = new StringBuilder();

            for (int songId = startId; songId <= endId;)
            {
                ////List<int> list = getsonglist.getAllSongs();
                try
                {
                    for (int index = 0; index < this.get_info_once; index++)
                    {
                        onceGet.Append((songId + index * threadNumber <= endId) ? (songId + index * threadNumber).ToString() : string.Empty).Append((songId + index * threadNumber <= endId) ? "," : string.Empty);
                    }

                    songId += this.get_info_once * this.threadNumber;
                    if (songId > endId)
                    {
                        songId = endId;
                    }
                    string onceGetstr = onceGet.ToString().Substring(0, onceGet.ToString().Length - 1);
                    ////txtlog.writeLog(Thread.CurrentThread.ManagedThreadId.ToString()+" 获取这个songId: "+onceGetstr);
                    onceGet.Clear();
                    string toGet = info_url.Replace("#songIds#", onceGetstr);

                    ////post 获取歌曲信息
                    string songInfoJson = post.getdata(toGet);

                    ////获取到的json转为对象
                    JObject jobj = JObject.Parse(songInfoJson);

                    for (int i = 0; i < jobj["songs"].Count(); i++)
                    {
                        ////不用全局  用局部变量 用完后需要初始化
                        string comment_url = "http://music.163.com/weapi/v1/resource/comments/R_SO_4_#songId#/?csrf_token=";
                        int    exsitSongId = Convert.ToInt32(jobj["songs"][i]["id"].ToString());

                        ////从对象中获取songName
                        string songName = jobj["songs"][i]["name"].ToString();

                        ////从对象中获取artist

                        for (int j = 0; j < jobj["songs"][i]["artists"].Count(); j++)
                        {
                            artists.AppendFormat("{0},", jobj["songs"][i]["artists"][j]["name"].ToString());
                        }
                        string artist = artists.ToString().Substring(0, artists.Length - 1);
                        artists.Clear();
                        ////好像对这些字段做了处理 先不用了
                        //////获取bMusic
                        //string bMusic = jobj["songs"][i]["bMusic"].ToString();
                        //string hMusic = jobj["songs"][i]["hMusic"].ToString();
                        //string lMusic = jobj["songs"][i]["lMusic"].ToString();
                        //string mMusic = jobj["songs"][i]["mMusic"].ToString();

                        //if (string.IsNullOrEmpty(bMusic) && string.IsNullOrEmpty(hMusic) && string.IsNullOrEmpty(lMusic) && string.IsNullOrEmpty(mMusic))
                        //{ return; }
                        //else if (!(string.IsNullOrEmpty(bMusic) || string.IsNullOrEmpty(hMusic) || string.IsNullOrEmpty(lMusic) || string.IsNullOrEmpty(mMusic)))
                        //{
                        //    if (jobj["songs"][i]["bMusic"]["dfsId"].ToString() == "0" &&
                        //        jobj["songs"][i]["hMusic"]["dfsId"].ToString() == "0" &&
                        //        jobj["songs"][i]["lMusic"]["dfsId"].ToString() == "0" &&
                        //        jobj["songs"][i]["mMusic"]["dfsId"].ToString() == "0")
                        //    { return; }
                        //}
                        //else
                        //{
                        //    if (!string.IsNullOrEmpty(bMusic))
                        //    {
                        //        if (jobj["songs"][i]["bMusic"]["dfsId"].ToString() == "0")
                        //        { return; }
                        //    }

                        //    if (!string.IsNullOrEmpty(hMusic))
                        //    {
                        //        if (jobj["songs"][i]["hMusic"]["dfsId"].ToString() == "0")
                        //        { return; }
                        //    }

                        //    if (!string.IsNullOrEmpty(lMusic))
                        //    {
                        //        if (jobj["songs"][i]["lMusic"]["dfsId"].ToString() == "0")
                        //        { return; }
                        //    }

                        //    if (!string.IsNullOrEmpty(mMusic))
                        //    {
                        //        if (jobj["songs"][i]["mMusic"]["dfsId"].ToString() == "0")
                        //        { return; }
                        //    }
                        //}
                        ////post 获取评论数
                        comment_url = comment_url.Replace("#songId#", exsitSongId.ToString());
                        string songCommentJson = post.postdata(comment_url, exsitSongId.ToString());

                        JObject jobj_comment = JObject.Parse(songCommentJson);
                        ////从对象中获取total的值
                        string needStr = jobj_comment["total"].Value <string>();

                        int count = Convert.ToInt32(needStr);
                        ////获取的评论数大于等于最小值,就进行处理

                        if (count >= this.MinCommentCount)
                        {
                            getsonglist.SolveSongs(exsitSongId, songName, artist, count);
                        }
                        this.txtlog.log(DateTime.Now.ToString() + " 这个点我没偷懒哦!", 1);
                        Thread.Sleep(200);
                    }
                    Thread.Sleep(1000);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString() + "\n");
                    ////common.txtLog txtlog = new common.txtLog();
                    this.txtlog.log(e.ToString() + "\n", 0);
                }
            }
        }