solofield.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. <?php
  2. class solofield{
  3. public function __construct(){
  4. include "lib/backend.php";
  5. $this->backend = new backend("solofield");
  6. include "lib/fuckhtml.php";
  7. $this->fuckhtml = new fuckhtml();
  8. }
  9. public function getfilters($page){
  10. return [
  11. "nsfw" => [
  12. "display" => "NSFW",
  13. "option" => [
  14. "yes" => "Yes",
  15. "no" => "No",
  16. ]
  17. ]
  18. ];
  19. }
  20. private function get($proxy, $url, $get = []){
  21. $curlproc = curl_init();
  22. if($get !== []){
  23. $get = http_build_query($get);
  24. $url .= "?" . $get;
  25. }
  26. curl_setopt($curlproc, CURLOPT_URL, $url);
  27. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  28. curl_setopt($curlproc, CURLOPT_HTTPHEADER,
  29. ["User-Agent: " . config::USER_AGENT,
  30. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  31. "Accept-Language: en-US,en;q=0.5",
  32. "Accept-Encoding: gzip",
  33. "Referer: https://solofield.net",
  34. "DNT: 1",
  35. "Connection: keep-alive",
  36. "Cookie: cross-site-cookie=name; lno=35842050",
  37. "Upgrade-Insecure-Requests: 1",
  38. "Sec-Fetch-Dest: document",
  39. "Sec-Fetch-Mode: navigate",
  40. "Sec-Fetch-Site: same-origin",
  41. "Sec-Fetch-User: ?1"]
  42. );
  43. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  44. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  45. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  46. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  47. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  48. $this->backend->assign_proxy($curlproc, $proxy);
  49. $data = curl_exec($curlproc);
  50. if(curl_errno($curlproc)){
  51. throw new Exception(curl_error($curlproc));
  52. }
  53. curl_close($curlproc);
  54. return $data;
  55. }
  56. public function web($get){
  57. if($get["npt"]){
  58. [$query, $proxy] = $this->backend->get($get["npt"], "web");
  59. try{
  60. $html =
  61. $this->get(
  62. $proxy,
  63. "https://solofield.net/search?" . $query,
  64. []
  65. );
  66. }catch(Exception $error){
  67. throw new Exception("Failed to fetch search page");
  68. }
  69. }else{
  70. $proxy = $this->backend->get_ip();
  71. try{
  72. $html =
  73. $this->get(
  74. $proxy,
  75. "https://solofield.net/search",
  76. [
  77. "q" => $get["s"],
  78. "ie" => "UTF-8",
  79. "oe" => "UTF-8",
  80. "hl" => "ja", // changing this doesnt do anything
  81. "lr" => "lang_ja", // same here
  82. //"ls" => "", // ??
  83. "f" => ($get["nsfw"] == "yes" ? "off" : "on")
  84. ]
  85. );
  86. }catch(Exception $error){
  87. throw new Exception("Failed to fetch search page");
  88. }
  89. }
  90. $out = [
  91. "status" => "ok",
  92. "spelling" => [
  93. "type" => "no_correction",
  94. "using" => null,
  95. "correction" => null
  96. ],
  97. "npt" => null,
  98. "answer" => [],
  99. "web" => [],
  100. "image" => [],
  101. "video" => [],
  102. "news" => [],
  103. "related" => []
  104. ];
  105. // check for errors and load the result div
  106. $this->error_and_load($html, $out);
  107. $items =
  108. $this->fuckhtml
  109. ->getElementsByClassName(
  110. "g0",
  111. "li"
  112. );
  113. foreach($items as $item){
  114. $this->fuckhtml->load($item);
  115. $title_tag =
  116. $this->fuckhtml
  117. ->getElementsByClassName(
  118. "r",
  119. "h3"
  120. );
  121. if(count($title_tag) === 0){
  122. continue;
  123. }
  124. $this->fuckhtml->load($title_tag[0]);
  125. $link =
  126. $this->fuckhtml
  127. ->getTextContent(
  128. $this->fuckhtml
  129. ->getElementsByTagName(
  130. "a"
  131. )[0]
  132. ["attributes"]
  133. ["href"]
  134. );
  135. $this->fuckhtml->load($item);
  136. $thumb =
  137. $this->fuckhtml
  138. ->getElementsByClassName(
  139. "webshot",
  140. "img"
  141. );
  142. if(count($thumb) !== 0){
  143. $uri =
  144. $this->fuckhtml
  145. ->getTextContent(
  146. $thumb[0]
  147. ["attributes"]
  148. ["src"]
  149. );
  150. if(stripos($uri, "now_printing") === false){
  151. $thumb = [
  152. "ratio" => "1:1",
  153. "url" =>
  154. "https://solofield.net" .
  155. $this->fuckhtml
  156. ->getTextContent(
  157. $thumb[0]
  158. ["attributes"]
  159. ["src"]
  160. )
  161. ];
  162. }else{
  163. $thumb = [
  164. "ratio" => null,
  165. "url" => null
  166. ];
  167. }
  168. }else{
  169. $thumb = [
  170. "ratio" => null,
  171. "url" => null
  172. ];
  173. }
  174. $out["web"][] = [
  175. "title" =>
  176. $this->fuckhtml
  177. ->getTextContent(
  178. $title_tag[0]
  179. ),
  180. "description" =>
  181. $this->fuckhtml
  182. ->getTextContent(
  183. $this->fuckhtml
  184. ->getElementsByClassName(
  185. "s",
  186. "div"
  187. )[0]
  188. ),
  189. "url" => $link,
  190. "date" => null,
  191. "type" => "web",
  192. "thumb" => $thumb,
  193. "sublink" => [],
  194. "table" => []
  195. ];
  196. }
  197. // get next page
  198. $this->get_npt($html, $proxy, $out, "web");
  199. return $out;
  200. }
  201. public function image($get){
  202. // no pagination
  203. $html =
  204. $this->get(
  205. $this->backend->get_ip(),
  206. "https://solofield.net/isearch",
  207. [
  208. "q" => $get["s"],
  209. "ie" => "UTF-8",
  210. "oe" => "UTF-8",
  211. "hl" => "ja", // changing this doesnt do anything
  212. //"lr" => "lang_ja", // same here
  213. "ls" => "", // ??
  214. "f" => ($get["nsfw"] == "yes" ? "off" : "on")
  215. ]
  216. );
  217. $out = [
  218. "status" => "ok",
  219. "npt" => null,
  220. "image" => []
  221. ];
  222. // check for errors and load the result div
  223. $this->error_and_load($html, $out);
  224. $images =
  225. $this->fuckhtml
  226. ->getElementsByTagName(
  227. "li"
  228. );
  229. foreach($images as $image){
  230. $this->fuckhtml->load($image);
  231. $img =
  232. $this->fuckhtml
  233. ->getElementsByTagName(
  234. "img"
  235. );
  236. if(count($img) === 0){
  237. // ?? invalid
  238. continue;
  239. }
  240. $img = $img[0];
  241. $size =
  242. explode(
  243. "x",
  244. $this->fuckhtml
  245. ->getTextContent(
  246. $image
  247. ),
  248. 2
  249. );
  250. $size = [
  251. (int)trim($size[0]), // width
  252. (int)trim($size[1]) // height
  253. ];
  254. $out["image"][] = [
  255. "title" => null,
  256. "source" => [
  257. [
  258. "url" =>
  259. "https://solofield.net/" .
  260. $this->fuckhtml
  261. ->getTextContent(
  262. $img["attributes"]["src"]
  263. ),
  264. "width" => $size[0],
  265. "height" => $size[1]
  266. ]
  267. ],
  268. "url" =>
  269. $this->fuckhtml
  270. ->getTextContent(
  271. $this->fuckhtml
  272. ->getElementsByTagName(
  273. "a"
  274. )[0]
  275. ["attributes"]
  276. ["href"]
  277. )
  278. ];
  279. }
  280. return $out;
  281. }
  282. public function video($get){
  283. if($get["npt"]){
  284. [$query, $proxy] = $this->backend->get($get["npt"], "videos");
  285. try{
  286. $html =
  287. $this->get(
  288. $proxy,
  289. "https://solofield.net/vsearch?" . $query,
  290. []
  291. );
  292. }catch(Exception $error){
  293. throw new Exception("Failed to fetch search page");
  294. }
  295. }else{
  296. $proxy = $this->backend->get_ip();
  297. try{
  298. $html =
  299. $this->get(
  300. $proxy,
  301. "https://solofield.net/vsearch",
  302. [
  303. "q" => $get["s"],
  304. "ie" => "UTF-8",
  305. "oe" => "UTF-8",
  306. "hl" => "ja", // changing this doesnt do anything
  307. //"lr" => "lang_ja", // same here
  308. "ls" => "", // ??
  309. "f" => ($get["nsfw"] == "yes" ? "off" : "on")
  310. ]
  311. );
  312. }catch(Exception $error){
  313. throw new Exception("Failed to fetch search page");
  314. }
  315. }
  316. $out = [
  317. "status" => "ok",
  318. "npt" => null,
  319. "video" => [],
  320. "author" => [],
  321. "livestream" => [],
  322. "playlist" => [],
  323. "reel" => []
  324. ];
  325. // check for errors and load the result div
  326. $this->error_and_load($html, $out);
  327. $items =
  328. $this->fuckhtml
  329. ->getElementsByTagName(
  330. "li"
  331. );
  332. foreach($items as $item){
  333. $this->fuckhtml->load($item);
  334. $as =
  335. $this->fuckhtml
  336. ->getElementsByTagName(
  337. "a"
  338. );
  339. if(count($as) === 0){
  340. continue;
  341. }
  342. $thumb =
  343. $this->fuckhtml
  344. ->getElementsByTagName(
  345. "img"
  346. );
  347. if(count($thumb) !== 0){
  348. $thumb = [
  349. "ratio" => "16:9",
  350. "url" =>
  351. "https://solofield.net/" .
  352. $thumb[0]
  353. ["attributes"]
  354. ["src"]
  355. ];
  356. }else{
  357. $thumb = [
  358. "ratio" => null,
  359. "url" => null
  360. ];
  361. }
  362. $date =
  363. $this->fuckhtml
  364. ->getElementsByAttributeValue(
  365. "style",
  366. "font-size: 10px;",
  367. "span"
  368. );
  369. if(count($date) !== 0){
  370. $date =
  371. $this->unfuckdate(
  372. $this->fuckhtml
  373. ->getTextContent(
  374. $date[0]
  375. )
  376. );
  377. }else{
  378. $date = null;
  379. }
  380. $center_td =
  381. $this->fuckhtml
  382. ->getElementsByAttributeValue(
  383. "align",
  384. "center",
  385. "td"
  386. );
  387. if(count($center_td) === 2){
  388. $duration =
  389. $this->fuckhtml
  390. ->getTextContent(
  391. $this->hms2int(
  392. $center_td[0]
  393. )
  394. );
  395. }else{
  396. $duration = null;
  397. }
  398. $out["video"][] = [
  399. "title" =>
  400. $this->fuckhtml
  401. ->getTextContent(
  402. $as[1]
  403. ),
  404. "description" => null,
  405. "author" => [
  406. "name" => null,
  407. "url" => null,
  408. "avatar" => null
  409. ],
  410. "date" => $date,
  411. "duration" => $duration,
  412. "views" => null,
  413. "thumb" => $thumb,
  414. "url" =>
  415. $this->fuckhtml
  416. ->getTextContent(
  417. $as[0]
  418. ["attributes"]
  419. ["href"]
  420. )
  421. ];
  422. }
  423. // get next page
  424. $this->get_npt($html, $proxy, $out, "videos");
  425. return $out;
  426. }
  427. private function get_npt($html, $proxy, &$out, $type){
  428. // get next page
  429. $this->fuckhtml->load($html);
  430. $pjs =
  431. $this->fuckhtml
  432. ->getElementById(
  433. "pjs"
  434. );
  435. if($pjs){
  436. $alnk =
  437. $this->fuckhtml
  438. ->getElementsByClassName(
  439. "alnk",
  440. "span"
  441. );
  442. foreach($alnk as $lnk){
  443. if(
  444. stripos(
  445. $this->fuckhtml
  446. ->getTextContent(
  447. $lnk
  448. ),
  449. "Next"
  450. ) !== false
  451. ){
  452. $this->fuckhtml->load($lnk);
  453. $out["npt"] =
  454. $this->backend->store(
  455. parse_url(
  456. $this->fuckhtml
  457. ->getElementsByTagName(
  458. "a"
  459. )[0]
  460. ["attributes"]
  461. ["href"],
  462. PHP_URL_QUERY
  463. ),
  464. $type,
  465. $proxy
  466. );
  467. }
  468. }
  469. }
  470. }
  471. private function error_and_load($html, $out){
  472. if(strlen($html) === 0){
  473. throw new Exception("Solofield blocked the request IP");
  474. }
  475. $this->fuckhtml->load($html);
  476. $list =
  477. $this->fuckhtml
  478. ->getElementById(
  479. "list",
  480. "div"
  481. );
  482. if($list === false){
  483. $nosearch =
  484. $this->fuckhtml
  485. ->getElementById(
  486. "nosearch",
  487. "div"
  488. );
  489. if($nosearch){
  490. return $out;
  491. }
  492. throw new Exception("Failed to grep search list");
  493. }
  494. $this->fuckhtml->load($list);
  495. }
  496. private function unfuckdate($date){
  497. return
  498. strtotime(
  499. rtrim(
  500. preg_replace(
  501. '/[^0-9]+/',
  502. "-",
  503. explode(
  504. ":",
  505. $date,
  506. 2
  507. )[1]
  508. ),
  509. "-"
  510. )
  511. );
  512. }
  513. private function hms2int($time){
  514. $parts = explode(":", $time, 3);
  515. $time = 0;
  516. if(count($parts) === 3){
  517. // hours
  518. $time = $time + ((int)$parts[0] * 3600);
  519. array_shift($parts);
  520. }
  521. if(count($parts) === 2){
  522. // minutes
  523. $time = $time + ((int)$parts[0] * 60);
  524. array_shift($parts);
  525. }
  526. // seconds
  527. $time = $time + (int)$parts[0];
  528. return $time;
  529. }
  530. }