Advertisement
grinyam

Untitled

Sep 10th, 2016
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.67 KB | None | 0 0
  1. <?php
  2.   header("Content-Type: text/html; charset=utf-8");
  3.  
  4.   function parse2ch($link) {
  5.  
  6.     $soc = file_get_contents($link);
  7.     $soc = json_decode($soc, true);
  8.  
  9.     $cities = file_get_contents("cities.json");
  10.     $cities = json_decode($cities, true);
  11.  
  12.     $gender = array(
  13.       'кун' => 'male',
  14.       'юноша' => 'male',
  15.       'М ' => 'male',
  16.       ' М,' => 'male',
  17.       'парень' => 'male',
  18.       'парня' => 'male',
  19.       'кунчика' => 'male',
  20.       'тян' => 'female',
  21.       'девочк' => 'female',
  22.       'девушк' => 'female',
  23.       'тню' => 'female',
  24.       'Ж ' => 'female',
  25.       ' Ж,' => 'female'
  26.     );
  27.  
  28.     $post = $soc["threads"][0]["posts"];
  29.  
  30.     $human = array();
  31.  
  32.     foreach ($post as $key => $value) {
  33.       $comment = explode("<br>", $value["comment"]);
  34.       $form = array();
  35.  
  36.       foreach ($comment as $key => $valueComment) {
  37.         $valueComment = htmlspecialchars_decode(strip_tags($valueComment));
  38.         $twoSymbols = substr($valueComment, 0, 2);
  39.         if ($twoSymbols == "1 " || $twoSymbols == "1." || $twoSymbols == "1)") {
  40.           $form["from"] = $valueComment;
  41.           $form["city"] = cityParse($valueComment, $cities);
  42.           $form["gender"] = genderParse($valueComment, $gender);
  43.         } else if ($twoSymbols == "2 " || $twoSymbols == "2." || $twoSymbols == "2)") {
  44.           $form["description"] = $valueComment;
  45.  
  46.         } else if ($twoSymbols == "3 " || $twoSymbols == "3." || $twoSymbols == "3)") {
  47.           $form["target"] = $valueComment;
  48.           $form["targetGender"] = genderParse($valueComment, $gender);
  49.         } else if ($twoSymbols == "4 " || $twoSymbols == "4." || $twoSymbols == "4)") {
  50.           $form["contacts"] = $valueComment;
  51.         }
  52.  
  53.       }
  54.  
  55.       if ($form) {
  56.  
  57.         $form["timestamp"] = $value["timestamp"];
  58.         $form["id"] = $value["num"];
  59.  
  60.         if (is_array($value["files"])) {
  61.           foreach ($value["files"] as $key => $valueValue) {
  62.               $form["files"][$key]["path"] = $valueValue["path"];
  63.               $form["files"][$key]["thumbnail"] = $valueValue["thumbnail"];
  64.           }
  65.         }
  66.         $human[] = $form;
  67.       }
  68.     }
  69. //    print_r($human);
  70.     return $human;
  71.   }
  72.  
  73.   function genderParse($info, $gender) {
  74.     foreach ($gender as $key => $value) {
  75.       $pos = mb_stripos($info, $key, 0, 'UTF-8');
  76.       if ($pos) {
  77.         return $value;
  78.       }
  79.     }
  80.   }
  81.  
  82.   function cityParse($info, $cities)
  83.   {
  84.     foreach ($cities as $key => $value) {
  85.       if (is_array($cities[$key])) {
  86.         foreach ($cities[$key] as $key1 => $value1) {
  87.           $pos = mb_stripos($info, $key1, 0, 'UTF-8');
  88.           if ($pos) {
  89.             return $key;
  90.           }
  91.         }
  92.       }
  93.       $pos = mb_stripos($info, $key, 0, 'UTF-8');
  94.       if ($pos) {
  95.         return $key;
  96.       }
  97.     }
  98.   }
  99.  
  100.   require_once('config.php');
  101.  
  102.   $mysqli = new mysqli(
  103.     $host,
  104.     $user,
  105.     $password,
  106.     $db
  107.   );
  108.   if ($mysqli->connect_errno) {
  109.       echo "Не удалось подключиться к MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
  110.   }
  111.  
  112.   if (!$mysqli->set_charset("utf8")) {
  113.       printf("Ошибка при загрузке набора символов utf8: %s\n", $mysqli->error);
  114.   } else {
  115.   }
  116.  
  117.   $humans = parse2ch("https://2ch.hk/soc/res/3161305.json");
  118.  
  119.   inserSQL($humans, $db);
  120.  
  121.   function inserSQL ($file, $db) {
  122.     $count = 0;
  123.     $countInsert = 0;
  124.     global $mysqli;
  125.     foreach ($file as $key => $value) {
  126.       $from = $value['from'];
  127.       $city = $value['city'];
  128.       $gender = $value['gender'];
  129.       $description = $value['description'];
  130.       $target = $value['target'];
  131.       $targetGender = $value['targetGender'];
  132.       $contacts = $value['contacts'];
  133.       $timestamp = $value['timestamp'];
  134.       $id_soc = $value['id'];
  135.       $images = '';
  136.  
  137.       $queryId = "SELECT * FROM `soc` WHERE `id_soc`='$id_soc'";
  138.  
  139.  
  140.       if ($resultId = $mysqli->query($queryId)) {
  141.         if (!$resultId->num_rows) {
  142.           if (is_array($value['files'])) {
  143.             $images = array();
  144.             foreach ($value['files'] as $key1 => $value1) {
  145.               $images[] = $value1[thumbnail];
  146.             }
  147.             $images = implode(",", $images);
  148.           }
  149.  
  150.           $queryInsert =   "INSERT INTO `$db`.`soc` (`from`, `city`, `gender`,
  151.          `description`, `target`, `targetGender`, `contacts`, `timestamp`, `id_soc`, `files`)
  152.          VALUES ('$from', '$city', '$gender', '$description', '$target', '$targetGender', '$contacts',
  153.          '$timestamp', '$id_soc', '$images')";
  154.  
  155.           if (!$mysqli->query($queryInsert)) {
  156.             echo "Не удалось создать таблицу: (" . $mysqli->errno . ") " . $mysqli->error;
  157.           }
  158.           $countInsert++;
  159.         } else {
  160.           $count++;
  161.         }
  162.       }
  163.     }
  164.     echo "Всего записей обработано ".($countInsert + $count)."<br>";
  165.     echo "Всего записей добавлено ".$countInsert."<br>";
  166.   }
  167.  
  168.   imgDownload();
  169.  
  170.   function imgDownload() {
  171.     global $mysqli;
  172.     $countRecord = 0;
  173.     $countExist = 0;
  174.     $countNotExist = 0;
  175.     $query = "SELECT `files` FROM `soc` WHERE `files`!=''";
  176.  
  177.     if ($result = $mysqli->query($query)) {
  178.         while ($row = $result->fetch_assoc()) {
  179.           $files = explode(",", $row['files']);
  180.           foreach ($files as $key => $value) {
  181.             $url = 'http://2ch.hk/soc/'.$value;
  182.  
  183.             $count = substr_count($value, '/');
  184.             for ($i=0; $i < $count; $i++) {
  185.               $num = stripos($value, '/');
  186.               $value = substr($value, $num + 1);
  187.             }
  188.  
  189.   //            echo "<br>".$url;
  190.             $path = "./img/".$value;
  191.   //            echo "<br>".$value;
  192.             if (!file_exists($path)) {
  193.               if (fopen($url, "r")) {
  194.                 $countRecord++;
  195. //                echo "<br>Файл существует и записан";
  196.  
  197.                 $img = file_get_contents($url);
  198.                 file_put_contents($path, $img);
  199.               } else {
  200.                 $countNotExist++;
  201. //                echo "<br>Файл не найден";
  202.               }
  203.             } else {
  204.               $countExist++;
  205. //              echo "<br>Файл уже существует на сервере";
  206.             }
  207.           }
  208.         }
  209.         echo "<br>Картинок записано ".$countRecord."<br>";
  210.         echo "Картинок не найдено ".$countNotExist."<br>";
  211.         echo "Картинок обработано ".($countRecord + $countExist + $countNotExist)."<br>";
  212.     }
  213.   }
  214.  
  215.   $mysqli->close();
  216.  
  217. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement