想想就好:这些年,博客园没有变,变的只是我们

Posted 路过秋天

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了想想就好:这些年,博客园没有变,变的只是我们相关的知识,希望对你有一定的参考价值。

今天想着对关注自己的粉丝群发条消息,想到博客园没提供这功能,那就自己来吧:

于是自己写了段代码,采集昵称,并模拟发送:

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 

 

就当我内心开始对博客园又产生一丝抱怨的情感时,就在那一瞬间,我好像明白了什么。

博客园还是当年那个博客园,只是,我们变了,变的不那么的纯理想主义了。

 

以上是关于想想就好:这些年,博客园没有变,变的只是我们的主要内容,如果未能解决你的问题,请参考以下文章

开博随笔

一个老程序员的2020年总结回顾,2021年如何变的更牛逼

《Go 程序员面试笔试宝典》上市半年多了~

《Go 程序员面试笔试宝典》上市半年多了~

python网络爬虫(14)博客园用户信息爬取

用了自定义Banner后,SpringBoot瞬间变的高大上了...