监控阮1峰老师的blog
监控阮一峰老师的blog
引言
阮一峰大家基本都认识,很厉害的一个人,经济学博士,文章写得很棒,知识面很广泛,计算机、算法、英语、文采,这是能想到的他的一些标签,他的博客应该算是最受欢迎的博客之一了。
我经常回去看他的博客,但有时候时间长了,再次去看,发现他已经有好几篇新文章了,我就在想,能不能自己写个程序,监控的他博客,当他有新文章的时候,第一时间推送给我。
思路
他的博客中有一个feed,是一个返回xml格式文档的接口,这个接口是最新的文章列表,总共15篇,可以通过监控这个接口中前几篇文章列表的变化来间接的检测他的文章更新,如果有新的文章马上给我的邮箱发送邮件,这样我就可以尽可能早的get到阮老师的最新文章了。
这是设计思路图:
首先就是rss解析了,这个使用php的原生函数simplexml_load_string得到一个解析之后的SimpleXMLElement对象,通过该对象可以很轻松的获取到阮老师更新的前几篇文章。然后和redis中的已发送文章列表集合作对比,如果有新的更新,将更新的信息发送给UDPserver,让UDPserver去发送邮件给用户列表。检测程序循环执行,每10分钟跑一次。
开始编码
有了思路编码就很快了(这里其实优化了好几次,刚开始的时候思路也没有这么明了,边做边改,当然了,还是要慢慢锻炼,开始之前尽可能多的完善思路)。
使用PHP依赖管理利器——Composer,经常使用PHP的开发者对这个工具应该很熟悉,不熟悉的同学可以点击前面的链接进行脑补,文档是中文的,很好懂。这个小系统将会使用到三个类库:phpmailer
,predis
,workerman
。第一个是一个发送邮件的类库,可以点击这里查看他的相关信息,第二个类库是php对redis接口的封装,这里是他的源码地址,第三个是一个创建UDPserver的类库,这里是他的官方网站。
安装依赖的类库
首先新建一个项目目录,然后进入项目目录安装依赖
<code>mkdir blog-observercd blog-observercomposer require phpmailer/phpmailercomposer require predis/prediscomposer require workerman/workerman</code>
执行完上面的命令之后,会在项目目录blog-observer目录下面看到下面几个文件composer.json
,composer.lock
和文件夹vender
,composer.json中的内容如下,至此依赖的类库安装好了。
<code class="sourceCode json"><span class="fu">{</span> <span class="dt">"require"</span><span class="fu">:</span> <span class="fu">{</span> <span class="dt">"phpmailer/phpmailer"</span><span class="fu">:</span> <span class="st">"^5.2"</span><span class="fu">,</span> <span class="dt">"predis/predis"</span><span class="fu">:</span> <span class="st">"^1.0"</span><span class="fu">,</span> <span class="dt">"workerman/workerman"</span><span class="fu">:</span> <span class="st">"^3.3"</span> <span class="fu">}</span><span class="fu">}</span></code>
监控博客更新&推送更新消息给UDP
下面是主要代码,由于是服务端程序,所以这里设置为daemon进程,我这里UDPserver为udp://127.0.0.1:1234
<code class="sourceCode php">daemonize<span class="ot">();</span><span class="kw">while</span><span class="ot">(</span><span class="dv">1</span><span class="ot">)</span>{ <span class="co">//获取最新的几篇文章,看看是否需要推送</span> <span class="kw">$c</span> = <span class="fu">file_get_contents</span><span class="ot">(</span><span class="kw">XML_URL</span><span class="ot">);</span> <span class="kw">$parse</span> = <span class="er">@</span><span class="fu">simplexml_load_string</span><span class="ot">(</span><span class="kw">$c</span><span class="ot">);</span> <span class="kw">if</span><span class="ot">(</span><span class="kw">$parse</span><span class="ot">)</span> { <span class="kw">$count</span> = <span class="fu">count</span><span class="ot">(</span><span class="kw">$parse</span>->entry<span class="ot">);</span> <span class="kw">$count</span> = <span class="kw">$count</span> > <span class="kw">RECENT_NUM</span> <span class="ot">?</span> <span class="kw">RECENT_NUM</span> <span class="ot">:</span> <span class="kw">$count</span><span class="ot">;</span> <span class="kw">$maynew</span> = <span class="ot">[];</span> <span class="kw">for</span><span class="ot">(</span><span class="kw">$i</span> = <span class="dv">0</span><span class="ot">;</span> <span class="kw">$i</span> < <span class="kw">$count</span><span class="ot">;</span> <span class="kw">$i</span>++<span class="ot">)</span> { <span class="kw">$maynew</span><span class="ot">[</span><span class="kw">$parse</span>->entry<span class="ot">[</span><span class="kw">$i</span><span class="ot">]</span>-><span class="fu">link</span>->attributes<span class="ot">()</span>->href-><span class="fu">__toString</span><span class="ot">()]</span> = <span class="kw">$parse</span>->entry<span class="ot">[</span><span class="kw">$i</span><span class="ot">]</span>->title-><span class="fu">__toString</span><span class="ot">();</span> } <span class="kw">$body</span> = <span class="st">""</span><span class="ot">;</span> <span class="co">//是否推送</span> <span class="kw">foreach</span><span class="ot">(</span><span class="kw">$maynew</span> <span class="kw">as</span> <span class="kw">$url</span> => <span class="kw">$title</span><span class="ot">)</span> { <span class="kw">if</span><span class="ot">(</span><span class="kw">$client</span>->sadd<span class="ot">(</span><span class="kw">SENDED_SET_KEY</span><span class="ot">,</span> <span class="kw">$url</span><span class="ot">))</span> { <span class="co">//send EMAIL</span> <span class="kw">$body</span> .= <span class="st">"<a href='"</span>.<span class="kw">$url</span>.<span class="st">"'>"</span>.<span class="kw">$title</span>.<span class="st">"</a><br>"</span><span class="ot">;</span> } } <span class="kw">if</span><span class="ot">(</span><span class="kw">$body</span><span class="ot">)</span> { <span class="kw">$msg</span> = <span class="ot">[];</span> <span class="kw">$msg</span><span class="ot">[</span><span class="st">'type'</span><span class="ot">]</span> = <span class="dv">1</span><span class="ot">;</span> <span class="kw">$msg</span><span class="ot">[</span><span class="st">'mailbody'</span><span class="ot">]</span> = <span class="kw">$body</span><span class="ot">;</span> <span class="kw">$start</span> = <span class="dv">0</span><span class="ot">;</span> <span class="kw">while</span><span class="ot">(</span><span class="kw">$mailaddrs</span> = <span class="kw">$client</span>->lrange<span class="ot">(</span><span class="kw">EMAIL_LIST_KEY</span> <span class="ot">,</span><span class="kw">$start</span><span class="ot">,</span> <span class="ot">(</span><span class="kw">$start</span> + <span class="kw">EVERY_SEND_NUM</span> -<span class="dv">1</span> <span class="ot">)))</span> { <span class="kw">$msg</span><span class="ot">[</span><span class="st">'mailaddrs'</span><span class="ot">]</span> = <span class="kw">$mailaddrs</span><span class="ot">;</span> <span class="kw">$send_msg</span> = <span class="fu">json_encode</span><span class="ot">(</span><span class="kw">$msg</span><span class="ot">);</span> <span class="fu">socket_sendto</span><span class="ot">(</span><span class="kw">$sock</span><span class="ot">,</span> <span class="kw">$send_msg</span><span class="ot">,</span> <span class="fu">strlen</span><span class="ot">(</span><span class="kw">$send_msg</span><span class="ot">),</span> <span class="dv">0</span><span class="ot">,</span> <span class="st">'127.0.0.1'</span><span class="ot">,</span> <span class="dv">1234</span><span class="ot">);</span> <span class="kw">$start</span> += <span class="kw">EVERY_SEND_NUM</span><span class="ot">;</span> } } } <span class="fu">sleep</span><span class="ot">(</span><span class="kw">GAP_SECONDS</span><span class="ot">);</span>}</code>
UDPserver程序
有了workerman,可以很方便的实现UDPserver,比自己写来的快得多。
<code class="sourceCode php"><span class="kw">$udp_worker</span> = <span class="kw">new</span> Workerman\Worker<span class="ot">(</span><span class="st">"udp://0.0.0.0:"</span>.<span class="kw">MAIL_UDP_PORT</span><span class="ot">);</span><span class="kw">$udp_worker</span>-><span class="fu">count</span> = <span class="dv">2</span><span class="ot">;</span><span class="kw">$udp_worker</span>->onMessage = <span class="kw">function</span><span class="ot">(</span><span class="kw">$connection</span><span class="ot">,</span> <span class="kw">$data</span><span class="ot">)</span> <span class="kw">use</span> <span class="ot">(</span><span class="kw">$mail</span><span class="ot">)</span>{ <span class="kw">$arr</span> = <span class="fu">json_decode</span><span class="ot">(</span><span class="kw">$data</span><span class="ot">,</span> <span class="kw">true</span><span class="ot">);</span> <span class="kw">switch</span><span class="ot">(</span><span class="kw">$arr</span><span class="ot">[</span><span class="st">'type'</span><span class="ot">])</span> { <span class="co">//发送邮件</span> <span class="kw">case </span><span class="st">'1'</span><span class="ot">:</span> { <span class="kw">$mailaddrs</span> = <span class="kw">$arr</span><span class="ot">[</span><span class="st">'mailaddrs'</span><span class="ot">];</span> <span class="kw">if</span><span class="ot">(</span>!<span class="fu">empty</span><span class="ot">(</span><span class="kw">$mailaddrs</span><span class="ot">)</span> && <span class="kw">$arr</span><span class="ot">[</span><span class="st">'mailbody'</span><span class="ot">])</span> { <span class="kw">foreach</span><span class="ot">(</span><span class="kw">$mailaddrs</span> <span class="kw">as</span> <span class="kw">$to</span><span class="ot">)</span> { <span class="kw">$mail</span>->clearAddresses<span class="ot">();</span> <span class="kw">$mail</span>->AddAddress<span class="ot">(</span><span class="kw">$to</span><span class="ot">);</span> <span class="kw">$mail</span>->Body = <span class="kw">$arr</span><span class="ot">[</span><span class="st">'mailbody'</span><span class="ot">];</span> <span class="kw">if</span><span class="ot">(</span>!<span class="kw">$mail</span>->Send<span class="ot">())</span> { <span class="fu">echo</span> <span class="st">"发送邮件失败:</span><span class="kw">\n</span><span class="st">"</span>.<span class="st">"address:"</span>.<span class="kw">$to</span>.<span class="st">"</span><span class="kw">\n</span><span class="st">"</span><span class="ot">;</span> } } } <span class="kw">break</span><span class="ot">;</span> } <span class="kw">default:</span> <span class="kw">break</span><span class="ot">;</span> }}<span class="ot">;</span>Workerman\Worker::runAll<span class="ot">();</span></code>
启动监控程序
好了,至此所有的设计编码工作就完成了,现在启动程序,进程启动之后会议daemon的形式运行,不会随着终端的关闭而停止。
<code>php xmldup.php startphp xmlmail.php</code>
总结
这是一个小系统,当然了还有很多不规范的地方,比如daemon进程一般都会以字母d
结尾,还有就是启动很不方便,要启动两次脚本,哈哈,当然了,这只是自己先来无事玩玩了,要真设计一个完成的系统估计会考虑很多很多的东西,加油吧,继续前进。
这里是github地址:blog-observer,自己试用的时候记得修改邮箱名称和密码。
如果谁也想第一时间获取到最新的阮老师的文章可以给我发邮件,我把你们的邮件地添加到邮件list中,但不保证会发送到,有时候关了电脑程序就停止了~
- 7楼飞凡123
- 时间间隔设定的是多少
- Re: 奔跑的Man
- @飞凡123,测试的时候是600s,这个时间不能太短,太短了没多大意义,还浪费阮老师的服务器资源
- 6楼Liez
- 马克
- 5楼白丸
- 真是好学生
- 4楼旭宝爱吃鱼
- 可以,赞
- Re: 奔跑的Man
- @旭宝爱吃鱼,THX
- 3楼wzx_xle
- 我用的foxmail的rss订阅功能
- Re: 奔跑的Man
- @wzx_xle,@星夜落尘,RSS订阅?行,你们可以啊,我又没说不可以,你要用RSS订阅我啥也不说了,我就喜欢DIV,得劲了吧~~
- 2楼永远的麦子
- 写得很好,也很喜欢阮老师的博客,上次了解到阮老师的博客是学习git的时候了解到阮老师的。
- Re: 奔跑的Man
- @永远的麦子,谢谢了。多像阮老师学习,哈哈,多涉猎技术之外的知识充实自己
- 1楼星夜落尘
- 不是有rss订阅吗。。。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

To implement file upload and download in Workerman documents, specific code examples are required. Introduction: Workerman is a high-performance PHP asynchronous network communication framework that is simple, efficient, and easy to use. In actual development, file uploading and downloading are common functional requirements. This article will introduce how to use the Workerman framework to implement file uploading and downloading, and give specific code examples. 1. File upload: File upload refers to the operation of transferring files on the local computer to the server. The following is used

Introduction to how to implement the basic usage of Workerman documents: Workerman is a high-performance PHP development framework that can help developers easily build high-concurrency network applications. This article will introduce the basic usage of Workerman, including installation and configuration, creating services and listening ports, handling client requests, etc. And give corresponding code examples. 1. Install and configure Workerman. Enter the following command on the command line to install Workerman: c

Swoole and Workerman are both high-performance PHP server frameworks. Known for its asynchronous processing, excellent performance, and scalability, Swoole is suitable for projects that need to handle a large number of concurrent requests and high throughput. Workerman offers the flexibility of both asynchronous and synchronous modes, with an intuitive API that is better suited for ease of use and projects that handle lower concurrency volumes.

Workerman development: real-time video call based on UDP protocol Summary: This article will introduce how to use the Workerman framework to implement real-time video call function based on UDP protocol. We will have an in-depth understanding of the characteristics of the UDP protocol and show how to build a simple but complete real-time video call application through code examples. Introduction: In network communication, real-time video calling is a very important function. The traditional TCP protocol may have problems such as transmission delays when implementing high-real-time video calls. And UDP

How to implement the timer function in the Workerman document Workerman is a powerful PHP asynchronous network communication framework that provides a wealth of functions, including the timer function. Use timers to execute code within specified time intervals, which is very suitable for application scenarios such as scheduled tasks and polling. Next, I will introduce in detail how to implement the timer function in Workerman and provide specific code examples. Step 1: Install Workerman First, we need to install Worker

How to use Workerman to build a high-availability load balancing system requires specific code examples. In the field of modern technology, with the rapid development of the Internet, more and more websites and applications need to handle a large number of concurrent requests. In order to achieve high availability and high performance, the load balancing system has become one of the essential components. This article will introduce how to use the PHP open source framework Workerman to build a high-availability load balancing system and provide specific code examples. 1. Introduction to Workerman Worke

How to implement the reverse proxy function in the Workerman document requires specific code examples. Introduction: Workerman is a high-performance PHP multi-process network communication framework that provides rich functions and powerful performance and is widely used in Web real-time communication and long connections. Service scenarios. Among them, Workerman also supports the reverse proxy function, which can realize load balancing and static resource caching when the server provides external services. This article will introduce how to use Workerman to implement the reverse proxy function.
