想一想就好:這些年,博客園沒有變,變的只是咱們

今天想着對關注本身的粉絲羣發條消息,想到博客園沒提供這功能,那就本身來吧:html

因而本身寫了段代碼,採集暱稱,並模擬發送:web

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace Cnblogs_SendMsg
{
    class Program
    {

        static void Main(string[] args)
        {
            // GatherNickNames();
            SendMsg();
        }

        #region 向粉絲髮送消息
        static void SendMsg()
        {
            string[] nicks = File.ReadAllLines(path, Encoding.Default);
            Random rnd = new Random();
            for (int i = 0; i < nicks.Length; i++)
            {
                Send(nicks[i], nicks[i] + ",感謝關注個人博客!", @"感謝您關注個人博客,同時也歡迎關注個人微信公衆號
![](https://images2018.cnblogs.com/blog/17408/201805/17408-20180523041027505-1002652922.jpg)", i);
                Thread.Sleep(60*1000 + rnd.Next(1500, 2000));
            }

        }
        private static void Send(string nickName, string title, string text, int index)
        {
            try
            {
                string json = "{content:\"" + text + "\",name:\"" + nickName + "\",title:\"" + title + "\"}";
                byte[] data = Encoding.UTF8.GetBytes(json);
                //WebClient wc = new WebClient();
                //wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36");
                //wc.Headers.Add("cookie", cookie);
                //wc.Headers.Add("content-type", "application/json; charset=UTF-8");
                //wc.Headers.Add("referer", "https://msg.cnblogs.com/send?recipient=" + System.Web.HttpUtility.UrlEncode(nickName, Encoding.UTF8));
                //wc.Encoding = Encoding.UTF8;
                
                //byte[] html = wc.UploadData("https://msg.cnblogs.com/api/message", data);
               


                //string cookieStr = "51fd9f14fa7561b5";
                //string postData = string.Format("userid={0}&password={1}", "guest", "123456");
                //byte[] data = Encoding.UTF8.GetBytes(postData);

                // Prepare web request...  
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://msg.cnblogs.com/api/message");
                
                request.Method = "POST";
                request.Referer = "https://msg.cnblogs.com/send?recipient=" + System.Web.HttpUtility.UrlEncode(nickName, Encoding.UTF8);
                request.ContentType = "application/json; charset=UTF-8";
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537."+DateTime.Now.Second;
                request.Headers.Add("Cookie", cookie);
                request.ContentLength = data.Length;
                Stream newStream = request.GetRequestStream();

                // Send the data.  
                newStream.Write(data, 0, data.Length);
                newStream.Close();

                // Get response  
                HttpWebResponse myResponse = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                string content = reader.ReadToEnd();
                string err = myResponse.Headers["X-Error"];
                if (!string.IsNullOrEmpty(err))
                {
                    Console.WriteLine(index + " : err " + err);
                    Thread.Sleep(60 * 1000 * 5);
                    Send(nickName, title, text, index);
                }
                else
                {
                    Console.WriteLine(index + " : ok " + nickName);
                }
            }
            catch (Exception err)
            {
                Console.WriteLine(index + " : err " + nickName + " : " + err.Message);
            }
        }
        #endregion
        #region 採集粉絲的暱稱


        static void GatherNickNames()
        {
            try
            {
                WebClient wc = new WebClient();
                wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36");
                wc.Headers.Add("cookie", cookie);
                wc.Headers.Add("referer", "https://home.cnblogs.com/followers/");
                wc.Encoding = Encoding.UTF8;
                for (int i = 1; i <= 89; i++)
                {
                    string html = wc.DownloadString("https://home.cnblogs.com/followers/?page=" + i);
                    GetNickName(html);
                    Console.WriteLine(i);
                    Thread.Sleep(500);
                }
            }
            catch (Exception err)
            {
                Console.WriteLine(err.Message);
            }
        }
        private static void GetNickName(string html)
        {
            int startIndex = html.IndexOf("class=\"avatar_list\">");
            if (startIndex == -1) { return; }
            int endIndex = html.IndexOf("</ul>", startIndex);
            if (startIndex > endIndex) { return; }

            string text = html.Substring(startIndex, endIndex - startIndex);

            MatchCollection mc = Regex.Matches(text, "title=\"(.+)\"");
            if (mc != null && mc.Count > 0)
            {
                string[] names = new string[mc.Count];
                for (int i = 0; i < mc.Count; i++)
                {
                    names[i] = mc[i].Groups[1].Value;
                }
                File.AppendAllLines(path, names, Encoding.Default);

            }

        }
        #endregion

        static string path = AppDomain.CurrentDomain.BaseDirectory + "nicks.txt";
        static string cookie = @"";
    }
}

然而發現博客園對發送的次數有嚴格的限制,多數狀況下你會獲得一個:SendTooManyMessages json

 

就當我心裏開始對博客園又產生一絲抱怨的情感時,就在那一瞬間,我好像明白了什麼。api

博客園仍是當年那個博客園,只是,咱們變了,變的不那麼的純理想主義了。微信

相關文章
相關標籤/搜索