public static function getSrcSeedURL($btih) { global $USER_AGENT; /// 1. 从数据库中查询原始页面链接 $res = get_by_btih($btih); if (!$res) { LOGW("BTIH 为 {$btih} 的资源在数据库中不存在"); return FALSE; } /// 2. 获取 link 页面内容 LOGI("正在获取动漫花园的资源页面内容: {$res['link']}"); $content = NULL; $ch = curl_init($res['link']); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_ENCODING, ''); curl_setopt($ch, CURLOPT_USERAGENT, $USER_AGENT); $content = curl_exec($ch); if (!$content) { LOGE("无法抓取动漫花园的资源页面: {$res['link']}'"); return FALSE; } /// 3. 解析 BT 地址 $matches = []; $pattern = '/\\/\\/.+[a-f0-9]{40}\\.torrent/'; $ret = preg_match($pattern, $content, $matches); if ($ret >= 1) { return 'http:' . $matches[0]; } else { return FALSE; } }
/** * 下载资源列表,并返回资源数组 */ public function _fetch() { $rawrss = $this->_fetchRss($this->FEED_URL); if (!$rawrss) { LOGW("无法获取 RSS 列表"); return array(); } $rs = $this->rawrss2arr($rawrss); if (empty($rs)) { LOGN("没有从 RSS 列表中获取到资源"); return array(); } return $rs; }
public function fetch() { $rs = $this->_fetch(); $ret = array(); foreach ($rs as $r) { $match = NULL; $btih = ''; preg_match('([0-9a-f]{40})', $r['guid'], $match); if ($match) { $btih = $match[0]; } else { LOGW("无法解析资源的 BTIH, r = " . var_export($r, TRUE)); continue; } $ret[] = array('btih' => $btih, 'title' => $r['title'], 'guid' => $r['guid'], 'link' => $r['link'], 'description' => $r['description'], 'pubDate' => strtotime($r['pubDate']), 'magnet' => ''); } return $ret; }
$result = $mysqli->query($sql); if (!$result) { LOGW("数据库查询出错,跳过这个资源:" . $mysqli->error); continue; } $row = $result->fetch_assoc(); if ($row['cnt'] > 0) { LOGI("数据库中已经存在相同 btih 的资源了,跳过这个资源"); continue; } /// 3. 将这个资源添加到数据库中 LOGI("将“{$title}”保存到数据库中"); $guid = $mysqli->real_escape_string($res['guid']); $link = $mysqli->real_escape_string($res['link']); $pubDate = (int) $res['pubDate']; $description = ''; $ctime = time(); $sql = "INSERT INTO b_resource(title, guid, link, description, btih, pubDate, ctime)\n VALUES('{$title}', '{$guid}', '{$link}', '{$description}', '{$btih}', {$pubDate}, {$ctime})"; $ret = $mysqli->query($sql); if ($ret === FALSE) { LOGW("数据库查询出错:" . $mysqli->error); } else { $cnt_new++; } } } if ($DRY_RUN) { LOGI("目前运行在测试模式,将回滚数据库"); $mysqli->rollback(); } LOGI("资源索引完成,分析得到 {$cnt_count} 个资源,共新添加了 {$cnt_new} 个资源");
die('<h1>404 Not Found</h1> <h2>BTIH not exists</h2>'); } switch ($res['src']) { case 'popgo': $url = Indexer_Popgo::getSrcSeedURL($btih); break; case 'dmhy': $url = Indexer_DMHY::getSrcSeedURL($btih); break; default: LOGE("代码不应执行到此处"); $url = FALSE; break; } if (!$url) { LOGW("无法获得 BTIH 为 {$btih} 的资源原始种子地址"); header('HTTP/1.1 404 Not Found'); die('<h1>404 Not Found</h1> <h2>Could not get source torrent URL</h2>'); } /// 2.2 开始下载 $content = NULL; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_ENCODING, ''); curl_setopt($ch, CURLOPT_USERAGENT, $USER_AGENT); curl_setopt($ch, CURLOPT_REFERER, ''); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_MAXREDIRS, 5); $content = curl_exec($ch); if (!$content || curl_error($ch)) { LOGD("无法从漫游下载种子({$url}):" . curl_error($ch));
/** * 向对端发送数据 * @param array $msg 要发送的数据 * @param array $address 对端链接信息 * @return void */ function send_response($msg, $address) { global $serv; if (filter_var($address[0], FILTER_VALIDATE_IP) === FALSE) { $ip = gethostbyname($address[0]); if (strcmp($ip, $address[0]) !== 0) { LOGW("{$address[0]} 不是一个有效的 IP 地址,将其作为域名解析得到 IP {$ip}"); $address[0] = $ip; } else { LOGW("{$address[0]} 不是一个有效的 IP 地址,且将其当作域名解析失败"); } } $serv->sendto($address[0], $address[1], Base::encode($msg)); }
/** * 进行一次 SQL 查询,如果连接断开,则自动重新尝试 */ function db_query($sql) { global $mysqli; global $DB_HOST; global $DB_USER; global $DB_PASSWORD; global $DB_DATABASE; $result = $mysqli->query($sql); if (!$result) { LOGN("第一次查询 MySQL 失败,重试后准备继续: " . $mysqli->error . ". SQL: {$sql}"); /// 重连 MySQL /// FIXME: 连接 MySQL 应该独立成一个函数 $ret = $mysqli->real_connect($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_DATABASE); if (!$ret) { LOGE('无法创建 MySQL 连接: ' . $mysqli->error); return FALSE; } $mysqli->query("set NAMES 'utf8'"); $result = $mysqli->query($sql); if (!$result) { LOGW("第二次查询 MySQL 失败: " . $mysqli->error . ". SQL: {$sql}"); return FALSE; } } return $result; }