How do technical geeks read serials_PHP tutorial

WBOY
Release: 2016-07-13 10:27:18
Original
781 people have browsed it

Problem background

By the way, I am a Naruto fan, so every Wednesday, I always refresh the web page to wait for Naruto updates. Later, I also became obsessed with a serialized novel, which was updated irregularly every day, so I read and read there every day. F5 was almost broken. It doesn't matter if F5 is bad. Programmers are so busy, how can they waste their precious time in a place like this? >_< Isn't it enough to wait until Thursday to go see Naruto, and isn't it enough to read a novel every one or two days, but , I can’t do it T.T

Infatuation plan

In order to avoid suffering day and night due to several serials, as a technical nerd who can save the world, of course I have to do something >_< That’s right, just let the machine automatically detect updates for me. Therefore, I only need to write a program to read the contents of the serial directory regularly. If an update is detected, an email can be automatically sent to my QQ mailbox to remind me to read the serial Y^o^Y

A brief description of implementation

First of all, let’s simplify our problem. The solution program mentioned above can be divided into the following parts:

  • Scheduled execution
  • Read the contents of the serial directory
  • Check if there is an update
  • Send email

By the way, I have been doing a lot of PHP recently, so the development language is mainly PHP ╮(╯_╰)╭

The following is only a brief explanation of the technical solution. After all, what the program does is very simple and there is no magic code

Scheduled execution - crontab

I just rented a very thin toy-grade Linux server some time ago, so I can let the program run on this server 24 hours a day. In Linux systems, scheduled execution can usually be done by adding a scheduled task through the crontab command. For example, if I want to set it to be executed every 10 minutes, then the format of the new crontab item is as follows:

*/10 * * * *  /path/task.sh
Copy after login

Read the contents of the serial directory

The serial directory is on a web page. To read the contents of the directory, you need to break down the problem:

  • Download page
  • Analyze web content

Download web page - curl

The content of web pages is generally obtained from the server through the HTTP protocol by the browser as a client. In our solution, the program we want to write needs to act as a client for HTTP protocol communication to download web page data. If you are familiar with Linux, you should think of the curl command line tool. Here you can use the exec function provided by PHP to execute the curl command. , or use the curl library that comes with PHP.

I am using PHP’s curl library. If you are interested, you can learn about it yourself.

Analyze the content of web pages - regular expressions

The downloaded web page document is html code, taking the Naruto I watched as an example, http://www.mangapanda.com/93/naruto.html, (PS: It means that I can’t understand Japanese and the English version is updated The speed is faster than the Chinese version, so...)

For us, the serial directory is actually the element in the html document whose href attribute value is "/naruto/xxx".

As long as you use simple pattern matching to perform text filtering on the downloaded html code, you can get the contents of the serial directory. I think you should also imagine that using regular expressions is perfect for doing this. Taking the web page just now as an example, you can use the following regular expression to roughly filter out the serial directory:

|href="/naruto/[^"]*"|
Copy after login

Because different regular expressions to be used will be different, this regular expression should be configured by the user.

In PHP, you can use the pregmatchall function in PHP PCRE Functions.

Check if there is an update

This is relatively simple. As long as there are updates, the serialized content will definitely change. So just compare the content of the serial directory read each time with the content of the serial directory read last time. As long as there is a difference, it is deemed that there is an update.

As for the storage of historical data, just use a file. I used a MySQL database to do this with a bit of fuss.

Send email - PHPMailer

As someone who is not familiar with computer networks, I have a poor understanding of email protocols. After searching on Google, I found the PHPMailer library. Just refer to the SMTP example provided by this library.

I use a QQ email as the sender. What needs to be noted is that this QQ email needs to activate the SMTP service.

Effect demonstration and code download

Considering user-friendliness and versatility, we made a simple configuration page, as shown below:

The content of the reminder email received is also very simple. The content can be empty, but it is best to attach a link to the serial directory.

The code can be checked out using svn:

 svn checkout "https://svn.code.sf.net/p/roxma-proj/code/php_learn"
Copy after login

This is mixed with some codes that have nothing to do with the topic accumulated in the personal learning process. I believe no one will want to read it seriously. If you really want to read it, you can learn/apps/series from php learn/apps/seriesupdate_remindder/check.php to follow. It is really not recommended to read the code in detail. I think the most important content is actually the ideas mentioned in the "Technical Overview" section. (Okay, the code is too ugly, just tell me secretly and don't make it public)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/818579.htmlTechArticleProblem background: I am a Naruto fan, so every Wednesday, I always wait for Naruto updates. Refresh the web page. Later, I also became obsessed with a serial novel, which was updated from time to time every day...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!