小鸡汤机器人之动弹自动回复
无聊想了一下怎样才可以搞一个机器人出来。分析了一下页面,就得出了以下代码。 功能比较弱,耗时无非就是正则的调试。 要做得比较复杂的话,需要做词性分析和词的统计了。当然,这不是本次讨论的重点。 本次已经同步添加了数据库表结构。PS:别拿来做坏事哦
无聊想了一下怎样才可以搞一个机器人出来。分析了一下页面,就得出了以下代码。
功能比较弱,耗时无非就是正则的调试。
要做得比较复杂的话,需要做词性分析和词的统计了。当然,这不是本次讨论的重点。
本次已经同步添加了数据库表结构。PS:别拿来做坏事哦。
项目的git地址:http://git.oschina.net/fallBirds/oscsend-chicken-soup
<?php require dirname(__FILE__) . '/Core.php'; /** * 发送鸡汤动弹 */ class reply extends Core { private $cookie = 'cookie.txt'; public function index() { $this->login(); $this->getContent(); } /** * 登录帐号 */ private function login() { $url = "https://www.oschina.net/action/user/hash_login"; /** * 填写你的帐号 */ $data = "email=&pwd=&verifyCode=&save_login=1"; $curl = curl_init(); // 启动一个CURL会话 curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在 curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包 curl_setopt($curl, CURLOPT_COOKIEJAR, $this->cookie); // 存放Cookie信息的文件名称 curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookie); // 读取上面所储存的Cookie信息 curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环 curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 curl_exec($curl); // 执行操作 curl_close($curl); // 关闭CURL会话 } /** * 获取内容 */ public function getContent() { $replyType = $this->replyType(); /** * 用户中心的地址。 ft=atme 就是@提醒的消息地址 */ $url = "http://my.oschina.net/u/2251019/?ft=atme"; $curl = curl_init(); // 启动一个CURL会话 curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookie); // 读取上面所储存的Cookie信息 curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环 curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 $tmpInfo = curl_exec($curl); // 执行操作 curl_close($curl); // 关闭CURL会话 /** * 下面开始就是匹配 @提醒的用户和条件 */ preg_match_all('/<td class="TweetContent">([\s\S]+?)<\/td>/', $tmpInfo, $allContent); foreach ($allContent[0] as $key => $value) { /** * 先获取@提醒的唯一ID值 * 获取这个,用来跟数据库匹配是否已经存在了。 */ preg_match("/tweet_reply\((\d+)\)/", $value, $replyId); $recordId = preg_replace('/[^0-9]/', '', $replyId['0']); if ($this->exitReply($recordId)) { continue; } /** * 取名字 */ preg_match('/<a.*class="user">([\s\S]+?)<\/a>/', $value, $userName); $replyName = strip_tags($userName['0']); /** * 获取需要执行的指令 */ preg_match("/<div.*class='post'>([\s\S]+?)<\/div>/", $value, $cmd); foreach ($replyType as $k => $v) { /** * 匹配一下指令 * 组装需要回复的内容 * 从这块代码开始,可以做得搞基点 * 如:加入学习指令呀。当然,学习指令这个需要另外写一个方法。 * 嗯,当你写到这里会发现上面代码重复很多,必须进行精简封装了。 * 目前我只写到这里了,没写更深入的。 */ if (strpos($cmd[0], $v['type_name']) !== false) { $replyConten = ""; $replyConten .= "@{$replyName} "; $type = $this->getReplyType($v['type_name']); if (empty($type)) { $replyConten .= "非常抱歉,小鸡汤目前还没有找到符合的指令,你可以输入#学习#+内容让小鸡汤学习呀。"; } else { $replyConten .= $type['learn_reply']; } $this->doReply($replyConten); $this->recordReplySend($recordId, $replyName, $replyConten); } } } } /** * 回复类型 */ public function replyType() { $sth = $this->db->prepare("SELECT * FROM {$this->prefix}reply_type "); $sth->execute(); return $sth->fetchAll(); } /** * 最后的回复 */ private function exitReply($id) { $sth = $this->db->prepare("SELECT * FROM {$this->prefix}reply_record where record_id = :record_id "); $sth->execute(array('record_id' => $id)); return $sth->fetch(); } /** * 获取对应的回复内容 * @param type $type */ private function getReplyType($type) { $sth = $this->db->prepare("SELECT * FROM {$this->prefix}learn WHERE learn_title = :learn_title limit 1 "); $sth->execute(array('learn_title' => $type)); return $sth->fetch(); } /** * 发送鸡汤 * @todo 你看!这里的代码明显是重复了。 */ private function doReply($content) { sleep(2); $url = "http://my.oschina.net/action/tweet/pub"; $data = "user=&user_code=&attachment=0&code_brush=&code_snippet=&msg={$content}"; $curl = curl_init(); // 启动一个CURL会话 curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包 curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookie); // 读取上面所储存的Cookie信息 curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环 curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 $tmpInfo = curl_exec($curl); // 执行操作 curl_close($curl); // 关闭CURL会话 } /** * 记录已经发送 */ private function recordReplySend($id, $user, $content) { $sql = "INSERT INTO {$this->prefix}reply_record(`record_id`, `user_name`, `content`) VALUES (:record_id, :user_name, :content)"; $sth = $this->db->prepare($sql); $sth->execute(array('record_id' => $id, 'user_name' => $user, 'content' => $content)); } } $mail = new reply(); $mail->index();
-- phpMyAdmin SQL Dump -- version 4.1.8 -- http://www.phpmyadmin.net -- -- Host: 127.0.0.1 -- Generation Time: 2014-10-21 09:35:46 -- 服务器版本: 5.6.15 -- PHP Version: 5.5.9 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; -- -- Database: `chicken` -- -- -------------------------------------------------------- -- -- 表的结构 `ck_learn` -- CREATE TABLE IF NOT EXISTS `ck_learn` ( `learn_id` int(11) NOT NULL AUTO_INCREMENT, `learn_title` varchar(255) NOT NULL, `learn_reply` varchar(255) NOT NULL, `learn_status` tinyint(1) NOT NULL, `learn_time` int(11) NOT NULL, PRIMARY KEY (`learn_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ; -- -------------------------------------------------------- -- -- 表的结构 `ck_post` -- CREATE TABLE IF NOT EXISTS `ck_post` ( `post_id` int(11) NOT NULL AUTO_INCREMENT, `post_page` int(11) NOT NULL COMMENT '采集页数', `post_content` text NOT NULL COMMENT '鸡汤内容', `post_send` tinyint(1) NOT NULL COMMENT '是否发送', `post_time` int(11) NOT NULL COMMENT '添加时间', PRIMARY KEY (`post_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=6971 ; -- -------------------------------------------------------- -- -- 表的结构 `ck_reply_record` -- CREATE TABLE IF NOT EXISTS `ck_reply_record` ( `reply_record_id` int(11) NOT NULL AUTO_INCREMENT, `record_id` bigint(20) NOT NULL, `user_name` varchar(128) NOT NULL, `content` varchar(255) NOT NULL, PRIMARY KEY (`reply_record_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系统回复记录' AUTO_INCREMENT=22 ; -- -------------------------------------------------------- -- -- 表的结构 `ck_reply_type` -- CREATE TABLE IF NOT EXISTS `ck_reply_type` ( `reply_type_id` int(11) NOT NULL AUTO_INCREMENT, `type_name` varchar(128) NOT NULL, PRIMARY KEY (`reply_type_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

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

AI Hentai Generator
Generate AI Hentai for free.

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



Windows Recovery Environment (WinRE) is an environment used to repair Windows operating system errors. After entering WinRE, you can perform system restore, factory reset, uninstall updates, etc. If you are unable to boot into WinRE, this article will guide you through fixes to resolve the issue. Unable to boot into the Windows Recovery Environment If you cannot boot into the Windows Recovery Environment, use the fixes provided below: Check the status of the Windows Recovery Environment Use other methods to enter the Windows Recovery Environment Did you accidentally delete the Windows Recovery Partition? Perform an in-place upgrade or clean installation of Windows below, we have explained all these fixes in detail. 1] Check Wi

DDREASE is a tool for recovering data from file or block devices such as hard drives, SSDs, RAM disks, CDs, DVDs and USB storage devices. It copies data from one block device to another, leaving corrupted data blocks behind and moving only good data blocks. ddreasue is a powerful recovery tool that is fully automated as it does not require any interference during recovery operations. Additionally, thanks to the ddasue map file, it can be stopped and resumed at any time. Other key features of DDREASE are as follows: It does not overwrite recovered data but fills the gaps in case of iterative recovery. However, it can be truncated if the tool is instructed to do so explicitly. Recover data from multiple files or blocks to a single

How to Use Swipe to Reply in iMessages on iPhone Note: The Swipe to Reply feature only works with iMessage conversations in iOS 17, not regular SMS conversations in the Messages app. Open the Messages app on your iPhone. Then, head to the iMessage conversation and simply swipe right on the iMessage you want to reply to. Once this is done, the selected iMessage will be in focus while all other messages will be blurred in the background. You'll see a text box for typing a reply and a "+" icon for accessing iMessage apps like Check-ins, Places, Stickers, Photos, and more. Just enter your message,

Computing is a familiar concept that most of us understand intuitively. Let's take the function f(x)=x+3 as an example. When x is 3, f(3)=3+3. The answer is 6, very simple. Obviously, this function is computable. But some functions are not that simple, and determining whether they can be calculated is not trivial, meaning they may never lead to a final answer. In 1928, German mathematicians David Hilbert and Wilhelm Ackermann proposed a problem called Entscheidungsproblem (ie "decision problem"). Over time, the question they ask will lead to possible

In a world where social media is overflowing with information, people are paying more and more attention to the content they post on the platform and how they interact with others. When we leave a comment under a certain post, if the original author deletes it, whether the comment will continue to exist has become a hotly debated issue. 1. The original comment has been deleted. Is the reply still there? First, it needs to be clear that social media platforms are highly flexible in how they handle user information and interactions. Even though the original comment is deleted, replies often remain below the post, even though they may not appear to be directly connected. This means that even if the original comment has disappeared, subsequent readers can still see the replies and infer some information based on those replies. Therefore, deleting the original comment will not completely eliminate traces of the interaction.

On this platform, in addition to watching interesting short videos, browsing interesting comments has also become an enjoyable experience for many users. Funny Tiktok comment replies not only elicit laughter but also resonate, and sometimes add sparkle to the content. 1. How to write an interesting reply sentence to a Douyin comment? 1. Integrate current affairs hot spots: Current affairs hot spots are the focus of everyone's attention. Integrating them into comments and replies can quickly arouse the interest of others. For example, on a popular dance video on Douyin, you can comment: "Is this the 'social shake' that became popular during the epidemic in my country? It's so energetic!" Such comments are humorous and appropriate, and can make people understand My heart smiles. The use of exaggeration is a common technique in humorous commentary. By moderately exaggerating an object or situation, you can make the review more interesting and make the

If you are using a Linux operating system and want the system to automatically mount the drive on boot, you can do this by adding the device's unique identifier (UID) and mount point path to the fstab configuration file. fstab is a file system table file located in the /etc directory. It contains information about the file systems that need to be mounted when the system starts. By editing the fstab file, you can ensure that the required drives are loaded correctly every time the system starts, thus ensuring stable system operation. Automatically mounting drivers can be conveniently used in a variety of situations. For example, I plan to back up my system to an external storage device. To achieve automation, ensure that the device remains connected to the system, even at startup. Likewise, many applications will directly

With the development of the Internet, pictures have become an indispensable part of web pages. But as the number of images increases, the loading speed of images has become a very important issue. In order to solve this problem, many websites use thumbnails to display images, but in order to generate thumbnails, we need to use professional image processing tools, which is a very troublesome thing for some non-professionals. Then, using JavaScript to achieve automatic thumbnail generation becomes a good choice. How to use JavaS
