public function start() { // 다운로드할 경로가 존재하는지 확인한다. Postcodify_Utility::print_message('Postcodify Indexer ' . POSTCODIFY_VERSION); Postcodify_Utility::print_newline(); $download_path = dirname(POSTCODIFY_LIB_DIR) . '/data'; $downloaded_files = 0; if ((!file_exists($download_path) || !is_dir($download_path)) && !@mkdir($download_path, 0755)) { echo '[ERROR] 다운로드 대상 경로(' . $download_path . ')가 존재하지 않습니다.' . PHP_EOL; exit(2); } // 게시물 목록을 다운로드한다. $html = Postcodify_Utility::download(self::RELATIVE_DOMAIN . self::LIST_URL); // 데이터가 존재하는 가장 최근 년월을 찾는다. $address_maxdate = null; $code_maxdate = null; preg_match_all(self::LIST_REGEXP1, $html, $address_links, PREG_SET_ORDER); foreach ($address_links as $address_link) { $address_maxdate = max($address_maxdate, intval(sprintf('%04d%02d', $address_link[1], $address_link[2]))); } preg_match_all(self::LIST_REGEXP2, $html, $code_links, PREG_SET_ORDER); foreach ($code_links as $code_link) { $code_maxdate = max($code_maxdate, intval(sprintf('%04d%02d', $code_link[1], $code_link[2]))); } $data_maxdate = min($address_maxdate, $code_maxdate); if (strlen($data_maxdate) !== 6) { echo '[ERROR] 다운로드할 데이터를 찾을 수 없습니다.' . PHP_EOL; exit(2); } $data_year = intval(substr($data_maxdate, 0, 4), 10); $data_month = intval(substr($data_maxdate, 4, 2), 10); $data_day = intval(date('t', mktime(12, 0, 0, $data_month, 1, $data_year))); // 데이터 기준일을 YYYYMMDD 포맷으로 정리한다. Postcodify_Utility::print_message('데이터 기준일은 ' . $data_year . '년 ' . $data_month . '월 ' . $data_day . '일입니다.'); // 주소 데이터를 다운로드한다. $download_url = self::RELATIVE_DOMAIN . sprintf(self::DOWNLOAD_URL, $data_year, $data_month, 'RDNMADR'); $filename = sprintf('%04d%02d%s.zip', $data_year, $data_month, 'RDNMADR'); $filepath = $download_path . '/' . $filename; Postcodify_Utility::print_message('다운로드: ' . $filename); $result = Postcodify_Utility::download($download_url, $filepath, array(__CLASS__, 'progress')); if (!$result || !file_exists($filepath) || filesize($filepath) < 1024) { Postcodify_Utility::print_error(); exit(2); } else { Postcodify_Utility::print_ok(filesize($filepath)); $downloaded_files++; } // 도로명코드 데이터를 다운로드한다. $download_url = self::RELATIVE_DOMAIN . sprintf(self::DOWNLOAD_URL, $data_year, $data_month, 'RDNMCODE'); $filename = sprintf('%04d%02d%s.zip', $data_year, $data_month, 'RDNMCODE'); $filepath = $download_path . '/' . $filename; Postcodify_Utility::print_message('다운로드: ' . $filename); $result = Postcodify_Utility::download($download_url, $filepath, array(__CLASS__, 'progress')); if (!$result || !file_exists($filepath) || filesize($filepath) < 1024) { Postcodify_Utility::print_error(); exit(2); } else { Postcodify_Utility::print_ok(filesize($filepath)); $downloaded_files++; } // 우체국 사서함 파일을 다운로드한다. Postcodify_Utility::print_message('다운로드: ' . basename(self::POBOX_URL)); $filepath = $download_path . '/' . basename(self::POBOX_URL); $result = Postcodify_Utility::download(self::POBOX_URL, $filepath, array(__CLASS__, 'progress')); if (!$result || !file_exists($filepath) || filesize($filepath) < 1024) { Postcodify_Utility::print_error(); exit(2); } else { Postcodify_Utility::print_ok(filesize($filepath)); $downloaded_files++; } // 우편번호 범위 데이터를 다운로드한다. Postcodify_Utility::print_message('다운로드: ' . basename(self::RANGES_URL)); $filepath = $download_path . '/' . basename(self::RANGES_URL); $result = Postcodify_Utility::download(self::RANGES_URL, $filepath, array(__CLASS__, 'progress')); if (!$result || !file_exists($filepath) || filesize($filepath) < 1024) { Postcodify_Utility::print_error(); exit(2); } else { Postcodify_Utility::print_ok(filesize($filepath)); $downloaded_files++; } // 영문 동 명칭을 다운로드한다. Postcodify_Utility::print_message('다운로드: ' . basename(self::ENGLISH_URL)); $filepath = $download_path . '/' . basename(self::ENGLISH_URL); $result = Postcodify_Utility::download(self::ENGLISH_URL, $filepath, array(__CLASS__, 'progress')); if (!$result || !file_exists($filepath) || filesize($filepath) < 1024) { Postcodify_Utility::print_error(); exit(2); } else { Postcodify_Utility::print_ok(filesize($filepath)); $downloaded_files++; } // 구 우편번호 범위 데이터를 다운로드한다. Postcodify_Utility::print_message('다운로드: ' . basename(self::OLDADDR_ZIPCODE_URL)); $filepath = $download_path . '/' . basename(self::OLDADDR_ZIPCODE_URL); $result = Postcodify_Utility::download(self::OLDADDR_ZIPCODE_URL, $filepath, array(__CLASS__, 'progress')); if (!$result || !file_exists($filepath) || filesize($filepath) < 1024) { Postcodify_Utility::print_error(); exit(2); } else { Postcodify_Utility::print_ok(filesize($filepath)); $downloaded_files++; } Postcodify_Utility::print_message('다운로드: ' . basename(self::OLDADDR_SPECIAL_URL)); $filepath = $download_path . '/' . basename(self::OLDADDR_SPECIAL_URL); $result = Postcodify_Utility::download(self::OLDADDR_SPECIAL_URL, $filepath, array(__CLASS__, 'progress')); if (!$result || !file_exists($filepath) || filesize($filepath) < 1024) { Postcodify_Utility::print_error(); exit(2); } else { Postcodify_Utility::print_ok(filesize($filepath)); $downloaded_files++; } // 파일 수가 맞는지 확인한다. if ($downloaded_files < 7) { echo '[ERROR] 다운로드한 파일 수가 일치하지 않습니다.' . PHP_EOL; exit(2); } }
public function start() { // 다운로드할 경로가 존재하는지 확인한다. Postcodify_Utility::print_message('Postcodify Indexer ' . POSTCODIFY_VERSION); Postcodify_Utility::print_newline(); $download_path = dirname(POSTCODIFY_LIB_DIR) . '/data'; if ((!file_exists($download_path) || !is_dir($download_path)) && !@mkdir($download_path, 0755, true)) { echo '[ERROR] 다운로드 대상 경로(' . $download_path . ')가 존재하지 않습니다.' . PHP_EOL; exit(2); } // 어디까지 업데이트했는지 찾아본다. $db = Postcodify_Utility::get_db(); $updated_query = $db->query('SELECT v FROM postcodify_settings WHERE k = \'updated\''); $updated = $updated_query->fetchColumn(); $updated_query->closeCursor(); if (!preg_match('/^20[0-9]{6}$/', $updated)) { echo '[ERROR] 기존 DB의 데이터 기준일을 찾을 수 없습니다.' . PHP_EOL; exit(3); } $current_time = mktime(12, 0, 0, date('m'), date('d'), date('Y')); $updated_time = mktime(12, 0, 0, substr($updated, 4, 2), substr($updated, 6, 2), substr($updated, 0, 4)); if ($updated_time < $current_time - 86400 * 60) { echo '[ERROR] 마지막 업데이트로부터 60일 이상이 경과하였습니다. DB를 새로 생성하시기 바랍니다.' . PHP_EOL; exit(3); } if ($updated_time >= $current_time) { echo '업데이트가 필요하지 않습니다.' . PHP_EOL; exit(0); } // 다운로드할 업데이트 목록을 생성한다. $updates = array(); for ($time = $updated_time + 86400; $time < $current_time; $time += 86400) { $updates[] = date('Ymd', $time); } // 업데이트를 다운로드한다. foreach ($updates as $date) { $filepath = $download_path . '/' . $date . '_dailynoticedata.zip'; if (file_exists($filepath)) { Postcodify_Utility::print_message('파일이 이미 존재함: ' . $date . '_dailynoticedata.zip'); continue; } Postcodify_Utility::print_message('다운로드: ' . $date . '_dailynoticedata.zip'); $link = self::RELATIVE_DOMAIN . sprintf(self::DOWNLOAD_URL, $date); $result = Postcodify_Utility::download($link, $filepath, array(__CLASS__, 'progress')); if (!$result || !file_exists($filepath)) { Postcodify_Utility::print_error(); @unlink($filepath); continue; } if (filesize($filepath) < 512 && stripos(file_get_contents($filepath), 'not found') !== false) { Postcodify_Utility::print_error(); @unlink($filepath); continue; } $zip = new ZipArchive(); $result = $zip->open($filepath); if (!$result) { Postcodify_Utility::print_error(); @unlink($filepath); continue; } Postcodify_Utility::print_ok(filesize($filepath)); } }