Advertisement
BangYadi

scraper

Apr 22nd, 2025
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. <?php
  2. $url = 'https://tv.nunadrama.store/';
  3. $cacheFile = __DIR__ . '/data.json';
  4. $logFile = __DIR__ . '/scraper.log';
  5. $cacheTime = 3600; // Cache selama 1 jam
  6.  
  7. if (file_exists($cacheFile) && (time() - filemtime($cacheFile)) < $cacheTime && !isset($_GET['force'])) {
  8. header('Content-Type: application/json');
  9. readfile($cacheFile);
  10. exit;
  11. }
  12.  
  13. $ch = curl_init($url);
  14. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  15. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)');
  16. $response = curl_exec($ch);
  17. $err = curl_error($ch);
  18. curl_close($ch);
  19.  
  20. if (!$response) {
  21. file_put_contents($logFile, "[".date("Y-m-d H:i:s")."] Gagal: $err\n", FILE_APPEND);
  22. http_response_code(500);
  23. echo json_encode(['error' => 'Gagal mengambil data dari sumber']);
  24. exit;
  25. }
  26.  
  27. libxml_use_internal_errors(true);
  28. $dom = new DOMDocument();
  29. $dom->loadHTML($response);
  30. libxml_clear_errors();
  31.  
  32. $xpath = new DOMXPath($dom);
  33. $items = $xpath->query("//div[contains(@class, 'film_list-wrap')]/div");
  34.  
  35. $data = [];
  36. foreach ($items as $item) {
  37. $titleNode = $xpath->query(".//h3/a", $item)[0] ?? null;
  38. $imgNode = $xpath->query(".//img", $item)[0] ?? null;
  39.  
  40. if ($titleNode && $imgNode) {
  41. $data[] = [
  42. 'title' => trim($titleNode->nodeValue),
  43. 'link' => $titleNode->getAttribute('href'),
  44. 'image' => $imgNode->getAttribute('data-src'),
  45. ];
  46. }
  47. }
  48.  
  49. if (!empty($data)) {
  50. file_put_contents($cacheFile, json_encode($data, JSON_PRETTY_PRINT));
  51. file_put_contents($logFile, "[".date("Y-m-d H:i:s")."] Update sukses: ".count($data)." data\n", FILE_APPEND);
  52. file_put_contents(__DIR__ . '/last_update.json', json_encode([
  53. 'time' => date('Y-m-d H:i:s')
  54. ]));
  55. }
  56.  
  57. header('Content-Type: application/json');
  58. echo json_encode($data, JSON_PRETTY_PRINT);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement