app.js 9.96 KB
// 네이버 검색 API예제는 블로그를 비롯 전문자료까지 호출방법이 동일하므로 blog검색만 대표로 예제를 올렸습니다.
// 네이버 검색 Open API 예제 - 블로그 검색
// -> 이걸 영화로 바꿀라고 blog를 다 movie로 바꿈
var express = require('express');
var app = express();
app.set('view engine','ejs');
var fs = require('fs');
var client_id = 'Lqxd8LWDGMYTe9YWJPBb';
var client_secret = '9HrBpH8oy1';

app.use('/', express.static(__dirname + '/../public'));
app.get('/errata', function (req, res) {
   var api_url = 'https://openapi.naver.com/v1/search/errata.json?query=' + encodeURI(req.query.query); // json 결과
   var request = require('request');
   console.log(req.query.query);
   console.log(req.query);
   var options = {
       url: api_url,
       headers: {'X-Naver-Client-Id':'3VmmpKXfOwySxf5ONdKM', 'X-Naver-Client-Secret': 'q0xaSGu8Vl'}
    };
   request.get(options, function (error, response, body) {

     if (!error && response.statusCode == 200) {
       res.writeHead(200, {'Content-Type': 'text/json;charset=utf-8'});
       console.log(body);
       res.end(body);
     } else {
       res.status(response.statusCode).end();
       console.log('error = ' + response.statusCode);
     }
   });
 });


 app.get('/', function(req, res){
     fs.readFile('fb_login.ejs', function(error, data){
     res.writeHead(200, {'Content-Type' : 'text/html '});
     res.end(data);
   });
 });
 app.get('/css/style.css', function(req, res){
     fs.readFile('css/style.css', function(error, data){
     res.writeHead(200, {'Content-Type' : 'text/css '});
     res.end(data);
   });
 });
 app.get('/css/bootstrap.min.css', function(req, res){
     fs.readFile('/css/bootstrap.min.css', function(error, data){
     res.writeHead(200, {'Content-Type' : 'text/css '});
     res.end(data);
   });
 });
 app.get('/css/bootstrap.css', function(req, res){
     fs.readFile('/css/bootstrap.css', function(error, data){
     res.writeHead(200, {'Content-Type' : 'text/css '});
     res.end(data);
   });
 });
 app.get('/css/bootstrap-theme.css', function(req, res){
     fs.readFile('/css/bootstrap-theme.css', function(error, data){
     res.writeHead(200, {'Content-Type' : 'text/css '});
     res.end(data);
   });
 });
 app.get('/css/bootstrap-theme.min.css', function(req, res){
     fs.readFile('/css/bootstrap-theme.min.css', function(error, data){
     res.writeHead(200, {'Content-Type' : 'text/css '});
     res.end(data);
   });
 });
 app.get('/css/bootstrap-theme.css.map', function(req, res){
     fs.readFile('/css/bootstrap-theme.css.map', function(error, data){
     res.writeHead(200, {'Content-Type' : 'text/css '});
     res.end(data);
   });
 });
 app.get('/js/bootstrap.js', function(req, res){
     fs.readFile('/js/bootstrap.js', function(error, data){
     res.writeHead(200, {'Content-Type' : 'Application/javascript'});
     res.end(data);
   });
 });
 app.get('/js/bootstrap.min.js', function(req, res){
     fs.readFile('/js/bootstrap.min.js', function(error, data){
     res.writeHead(200, {'Content-Type' : 'Application/javascript'});
     res.end(data);
   });
 });
 app.get('/js/npm.js', function(req, res){
     fs.readFile('/js/npm.js', function(error, data){
     res.writeHead(200, {'Content-Type' : 'Application/javascript'});
     res.end(data);
   });
 });

app.get('/imgs/megabox',function (req,res) {
    fs.readFile('mega.png', function (error, data) {
      res.writeHead(200, {'Content-Type' : 'text/html'});
      res.end(data);
    });
})

app.get('/main',function (req,res) {
  fs.readFile('main.ejs', function(error, data){
  res.writeHead(200, {'Content-Type' : 'text/html '});
  res.end(data);
});
})

app.get('/serach',function (req,res) {
  var query=  req.query;
  var genre = query['genre'];
  var year = query['year'];
  var country = query['country'];
  var search = query['search'];
  var searchAll = query['searchAll'];

  if(genre != null){
    var code = get_genre_code(genre);
    get_info_using_genre(code,search, res);
  }
  else if(year != null){
    var year_num = get_year_num(year);
    get_info_using_year(year_num,search, res);
  }
  else if(country != null){
    var country_real = makeETC(country);
    get_info_using_country(country_real,search, res);
  }
  else if(searchAll != null){
    get_info_all(searchAll, res);
  }
})

function makeETC(country_input){
  var country_input;
  if(country_input == "ET"){
    country_input = "ETC"
  }
  return country_input;
}

function get_genre_code(genre_str) {
 var genre_num;
  switch (genre_str) {
    case "Action":
    genre_num = 19;
    break;
    case "Romance":
    genre_num = 5;
    break;
    case "Horror":
    genre_num = 4;
    break;
    case "Drama":
    genre_num = 1;
    break;
    case "Fantasy":
    genre_num = 2;
    break;
    case "Adventure":
    genre_num = 6;
    break;
    case "Thriller":
    genre_num = 7;
    break;
    case "Comedy":
    genre_num = 11;
    break;
    case "War":
    genre_num = 14;
    break;

    default:
  }
  return genre_num;
}
function get_year_num(year){
  var return_year;
  switch (year) {
    case "1980년대":
    return_year = 1980;
    break;
    case "1990년대":
    return_year = 1990;
    break;
    case "2000년대":
    return_year = 2000;
    break;
    case "2010년대":
    return_year = 2010;
    break;
  }
  return return_year;
}
//장르 검색
function get_info_using_genre(code,search, res) {
  var en = encodeURI(search);
  var api_url = 'https://openapi.naver.com/v1/search/movie.json?query='+en+'&display=100'+'&genre=' + code; // json 결과
  var request = require('request');
  var options = {
      url: api_url,
      //headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret}
      headers: {'X-Naver-Client-Id':'Lqxd8LWDGMYTe9YWJPBb', 'X-Naver-Client-Secret': '9HrBpH8oy1'}
   };
  request.get(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      var data = JSON.parse(body);
      var total = data['total'];
      var items = data['items'];

      console.log(data);
      res.render('movie_info',{item:items});
    }
  });
}
//연도 검색
function get_info_using_year(year_num,search, res) {
  var en = encodeURI(search);
  var yearto = year_num+10;
  var api_url = 'https://openapi.naver.com/v1/search/movie.json?query='+en+ '&display=100' + '&yearfrom=' + year_num + '&yearto=' + yearto // json 결과
  //var api_url = 'https://openapi.naver.com/v1/search/movie.json?query='+en+'&display=100'; // json 결과
  var request = require('request');
  var options = {
      url: api_url,
      //headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret}
      headers: {'X-Naver-Client-Id':'Lqxd8LWDGMYTe9YWJPBb', 'X-Naver-Client-Secret': '9HrBpH8oy1'}
   };
  request.get(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      var data = JSON.parse(body);
      var total = data['total'];
      var items = data['items'];
      // var temps = items;
      // var j = 0;
      // for(var i = 0 ;  i < total; i++){
      //   if(items[i]['pubDate'] >= year && items[i]['pubDate'] <= year+10 ){
      //     temps[j] = items[i];
      //   }
      // }

      console.log(data);
      res.render('movie_info',{item:items});
    }
  });
}
//국가 검색
function get_info_using_country(country,search, res) {
  var en = encodeURI(search);
  var api_url = 'https://openapi.naver.com/v1/search/movie.json?query='+en+'&display=100'+'&country=' + country; // json 결과
  var request = require('request');
  var options = {
      url: api_url,
      //headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret}
      headers: {'X-Naver-Client-Id':'Lqxd8LWDGMYTe9YWJPBb', 'X-Naver-Client-Secret': '9HrBpH8oy1'}
   };
  request.get(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      var data = JSON.parse(body);
      var total = data['total'];
      var items = data['items'];

      console.log(data);
      res.render('movie_info',{item:items});
    }
  });
}
//전체 검색
// 미정 /미확인 이딴거 두개만 뜸
function get_info_all(searchAll, res) {
  var en = encodeURI(searchAll);
  var api_url = 'https://openapi.naver.com/v1/search/movie.json?query='+en+'&display=100'; // json 결과
  var request = require('request');
  var options = {
      url: api_url,
      //headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret}
      headers: {'X-Naver-Client-Id':'Lqxd8LWDGMYTe9YWJPBb', 'X-Naver-Client-Secret': '9HrBpH8oy1'}
   };
  request.get(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      var data = JSON.parse(body);
      var total = data['total'];
      var items = data['items'];

      console.log(data);
      res.render('movie_info',{item:items});
    }
  });
}

// app.get('/search/movie', function (req, res) {
//    var api_url = 'https://openapi.naver.com/v1/search/movie.json?query=%EC%A3%BC%EC%8B%9D&display=10&start=1&genre=1' + encodeURI(req.query.query); // json 결과
//    var request = require('request');
//    var options = {
//        url: api_url,
//        //headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret}
//        headers: {'X-Naver-Client-Id':'Lqxd8LWDGMYTe9YWJPBb', 'X-Naver-Client-Secret': '9HrBpH8oy1'}
//     };
//    request.get(options, function (error, response, body) {
//      if (!error && response.statusCode == 200) {
//        var data = JSON.parse(body);
//        var total = data['total'];
//        var items = data['items'];
//        res.writeHead(200, {'Content-Type': 'text/json;charset=utf-8'});
//       // res.end(JSON.stringify(items));
//        res.end(body);
//      } else {
//        res.status(response.statusCode).end();
//        console.log('error = ' + response.statusCode);
//      }
//    });
//  });


 app.listen(3000, function () {
   //console.log('http://127.0.0.1:3000/search/blog?query=검색어 app listening on port 3000!');
   console.log('http://127.0.0.1:3000/search/movie?query=검색어 app listening on port 3000!');
 });