Skip to content

Instantly share code, notes, and snippets.

@rubyon
Created March 27, 2017 02:12
Show Gist options
  • Save rubyon/5198229b5ea8e23e3bf8a501da0b8d68 to your computer and use it in GitHub Desktop.
Save rubyon/5198229b5ea8e23e3bf8a501da0b8d68 to your computer and use it in GitHub Desktop.
<?php
/*********************************************************************\
| (c)2011-2015 Synoboost http://www.synoboost.com |
|---------------------------------------------------------------------|
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of the GNU General Public License |
| along with this program; If not, see <http://www.gnu.org/licenses/> |
\*********************************************************************/
?>
<?php
class SynoDLMSearchT {
private $wurl = 'https://bezze.me/';
private $wurl_host = 'https://bezze.me/';
private $qurl = '/torrents.php?searchstr=%s';
private $lurl = '/login.php';
private $COOKIE = '/tmp/t_dlm.cookie';
private $debug = false;
private function DebugLog($str) {
if ($this->debug == true) {
file_put_contents('/tmp/t_dlm.log', $str . "\r\n\r\n", FILE_APPEND);
}
}
public function __construct() {
$this->qurl = $this->wurl . $this->qurl;
$this->wurl_host = $this->wurl_host . $this->lurl;
$this->lurl = $this->wurl . $this->lurl;
}
public function prepare($curl, $query, $username, $password) {
$url = $this->qurl;
curl_setopt($curl, CURLOPT_URL, sprintf($url, urlencode($query)));
curl_setopt($curl, CURLOPT_USERAGENT, DOWNLOAD_STATION_USER_AGENT);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
if ($username !== NULL && $password !== NULL) {
$this->VerifyAccount($username, $password);
curl_setopt($curl, CURLOPT_COOKIEFILE, $this->COOKIE);
}
}
public function GetCookie() {
return $this->COOKIE;
}
public function VerifyAccount($username, $password) {
$ret = FALSE;
if (file_exists($this->COOKIE)) {
unlink($this->COOKIE);
}
$PostData = array(
'username' => $username,
'password' => $password,
'keeplogged' => '1',
'login' => 'submit'
);
$PostData = http_build_query($PostData);
$fscurl = curl_init();
$headers = array(
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*;q=0.8',
'Accept-Language: ru,en-us;q=0.7,en;q=0.3',
'Accept-Encoding: deflate',
'Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7'
);
curl_setopt($fscurl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($fscurl, CURLOPT_URL, $this->lurl);
curl_setopt($fscurl, CURLOPT_FAILONERROR, 1);
curl_setopt($fscurl, CURLOPT_REFERER, $this->lurl);
curl_setopt($fscurl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($fscurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($fscurl, CURLOPT_TIMEOUT, 20);
curl_setopt($fscurl, CURLOPT_USERAGENT, DOWNLOAD_STATION_USER_AGENT);
curl_setopt($fscurl, CURLOPT_POST, 1);
curl_setopt($fscurl, CURLOPT_COOKIEJAR, $this->COOKIE);
curl_setopt($fscurl, CURLOPT_COOKIEFILE, $this->COOKIE);
curl_setopt($fscurl, CURLOPT_POSTFIELDS, $PostData);
curl_setopt($fscurl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($fscurl, CURLOPT_SSL_VERIFYPEER, false);
$Result = curl_exec($fscurl);
if (FALSE !== strpos($Result, 'user.php?id=')) {
$ret = TRUE;
// Dirty patch to enable downloading direct torrents both with www and non-www links.
curl_setopt($fscurl, CURLOPT_URL, $this->wurl_host);
curl_setopt($fscurl, CURLOPT_REFERER, $this->wurl_host);
curl_exec($fscurl);
} else {
$this->DebugLog("Login fail: " . $Result);
}
curl_close($fscurl);
return $ret;
}
public function parse($plugin, $response) {
$regexp_get_auth_pass = "user=(.*)&amp;auth=(.*)&amp;passkey=(.*)&amp;authkey=(.*)\"";
preg_match("/$regexp_get_auth_pass/siU", $response, $match_auth_pass);
$t_user = $match_auth_pass[1];
$t_auth = $match_auth_pass[2];
$t_passkey = $match_auth_pass[3];
$t_authkey = $match_auth_pass[4];
$this->DebugLog("User: " . $t_user);
$this->DebugLog("Auth: " . $t_auth);
$this->DebugLog("Passkey: " . $t_passkey);
$this->DebugLog("Authkey: " . $t_authkey);
$cut_start = stripos($response, '<table class="torrent_table grouping" id="torrent_table">');
$response = substr($response, $cut_start);
$response = preg_replace('/(\s+)/is', ' ', $response);
$response = $response . "<tr class=\"group\">";
$this->DebugLog("Response: " . $response);
$regexp2 = "<td class=\"center\">(.*)<tr class=\"group\">";
$regexp_category = "<td class=\"center cats_col\"> <div title=\"(.*)\" class=\"cats_";
$regexp_title = "<td colspan=\"2\" class=\"list_title\">(.*)<\/font>";
if (preg_match_all("/$regexp2/siU", $response, $regexp2_matches, PREG_SET_ORDER)) {
foreach ($regexp2_matches as $regexp2_match) {
$title = "Unknown title";
$category = "Unknown category";
$this->DebugLog("Sub Response: " . $regexp2_match[0]);
if (preg_match_all("/$regexp_category/siU", $regexp2_match[0], $regexp_category_matches, PREG_SET_ORDER)) {
foreach ($regexp_category_matches as $regexp_category_match) {
$category = $regexp_category_match[1];
$this->DebugLog("Category: " . $category);
}
}
if (preg_match_all("/$regexp_title/siU", $regexp2_match[0], $regexp_title_matches, PREG_SET_ORDER)) {
foreach ($regexp_title_matches as $regexp_title_match) {
$title = str_replace("&nbsp;&nbsp;", " ", strip_tags($regexp_title_match[1]));
$this->DebugLog("Title: " . $title);
}
}
if ($title != "Unknown title") {
$regexp3 = "<tr class=\"group_torrent groupid_(.*)\"> <td colspan=\"4\">(.*)<\/tr>";
$regexp_title2 = "torrentid=(.*)\">(.*)<\/a>";
$regexp_download = "<a href=\"(.*)\" title=\"Download\">";
$regexp_size = "<\/span><\/td> <td class=\"nobr\">([0-9\,\.]+) (GB|MB|KB|TB)<\/td> <td>(.*)<\/td> <td>(.*)<\/td> <td>(.*)<\/td>";
$regexp_time = "<span class=\"time\" title=\"(.*), (.*):(.*):(.*)\">";
if (preg_match_all("/$regexp3/siU", $regexp2_match[0], $regexp3_matches, PREG_SET_ORDER)) {
foreach ($regexp3_matches as $regexp3_match) {
$title2 = "Unknown title";
$download = "Unknown download";
$size = 0;
$datetime = "2016-10-29";
$page = "Default page";
$hash = "Hash unknown";
$seeds = 0;
$leechs = 0;
$this->DebugLog("Sub Match: " . $regexp3_match[0]);
if (preg_match_all("/$regexp_download/siU", $regexp3_match[0], $regexp_download_matches, PREG_SET_ORDER)) {
foreach ($regexp_download_matches as $regexp_download_match) {
$download = $this->wurl . $regexp_download_match[1];
$download = str_replace("&amp;", "&", $download);
$regexp_download_id = "torrentid=(.*)\">";
if (preg_match_all("/$regexp_download_id/siU", $regexp3_match[0], $regexp_download_id_matches, PREG_SET_ORDER)) {
foreach ($regexp_download_id_matches as $regexp_download_id_match) {
$page = "https://bezze.me/torrents.php?torrentid=$regexp_download_id_match[1]";
}
}
$this->DebugLog("Page: " . $page);
$this->DebugLog("Download: " . $download . "&force=1");
}
}
if (preg_match_all("/$regexp_title2/siU", $regexp3_match[0], $regexp_title2_matches, PREG_SET_ORDER)) {
foreach ($regexp_title2_matches as $regexp_title2_match) {
$title2 = $title . " : " . $regexp_title2_match[2];
$regexp_icon = "<img src=\"(.*)\" class=\"(.*)\" title=\"(.*)\" \/>";
$icon = "";
if (preg_match_all("/$regexp_icon/siU", $title2, $regexp_icon_matches, PREG_SET_ORDER)) {
foreach ($regexp_icon_matches as $regexp_icon_match) {
$icon = $icon . " " . trim($regexp_icon_match[3]);
$this->DebugLog("Change Title: " . $icon);
}
}
$title2 = $title2 . " " . $icon;
$title2 = strip_tags($title2);
$title2 = str_replace("&nbsp;", "", $title2);
$this->DebugLog("Title: " . $title2);
if ($download == "Unknown download") {
$regexp_download_id = "torrentid=(.*)\">";
if (preg_match_all("/$regexp_download_id/siU", $regexp3_match[0], $regexp_download_id_matches, PREG_SET_ORDER)) {
foreach ($regexp_download_id_matches as $regexp_download_id_match) {
$download = "https://bezze.me/torrents.php?action=download&id=$regexp_download_id_match[1]&authkey=$t_authkey&torrent_pass=$t_passkey&force=1";
$page = "https://bezze.me/torrents.php?torrentid=$regexp_download_id_match[1]";
}
}
$this->DebugLog("Page: " . $page);
$this->DebugLog("Download: " . $download);
}
}
}
if (preg_match_all("/$regexp_time/siU", $regexp3_match[0], $regexp_time_matches, PREG_SET_ORDER)) {
foreach ($regexp_time_matches as $regexp_time_match) {
$datetime = $regexp_time_match[1];
$this->DebugLog("Datetime: " . $datetime);
}
}
if (preg_match_all("/$regexp_size/siU", $regexp3_match[0], $regexp_size_matches, PREG_SET_ORDER)) {
foreach ($regexp_size_matches as $regexp_size_match) {
$size = str_replace(",", ".", $regexp_size_match[1]);
$this->DebugLog("Size: " . $size);
$this->DebugLog("Unit: " . trim($regexp_size_match[2]));
switch (trim($regexp_size_match[2])) {
case 'KB':
$size = $size * 1024;
break;
case 'MB':
$size = $size * 1024 * 1024;
break;
case 'GB':
$size = $size * 1024 * 1024 * 1024;
break;
case 'TB':
$size = $size * 1024 * 1024 * 1024 * 1024;
break;
}
$size = floor($size);
$this->DebugLog("Seeds: " . $regexp_size_match[4]);
$this->DebugLog("Peers: " . $regexp_size_match[5]);
$seeds = $regexp_size_match[4];
$leechs = $regexp_size_match[5] - $regexp_size_match[4];
}
}
if ($title2 != "Unknown title") {
$hash = md5(trim($title2));
$plugin->addResult(trim($title2), $download, $size, $datetime, $page, $hash, $seeds, $leechs, $category);
$res++;
}
}
}
}
}
}
return $res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment