me.uni.sushilkumar.geodine.util.YoutubeSearch.java Source code

Java tutorial

Introduction

Here is the source code for me.uni.sushilkumar.geodine.util.YoutubeSearch.java

Source

/*
* Copyright Sushil Kumar <0120sushil@gmail.com>.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
    
*     http://www.apache.org/licenses/LICENSE-2.0
    
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* under the License.
*/
package me.uni.sushilkumar.geodine.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import me.uni.sushilkumar.geodine.db.DBConnection;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 *
 * @author Sushil Kumar <0120sushil@gmail.com>
 */
public class YoutubeSearch extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ParseException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String type = request.getParameter("type");
            String query = null;
            type += "";

            if (type.equals("random")) {
                DBConnection con = new DBConnection();
                ArrayList<String> cuisineList = con.getCuisineList();
                Random generator = new Random();
                query = cuisineList.get(generator.nextInt(cuisineList.size()));

            } else {
                query = request.getParameter("q");
            }
            query += " recipe";
            URL u = new URL("http://gdata.youtube.com/feeds/api/videos?q=" + URLEncoder.encode(query, "UTF-8")
                    + "&max-results=1&v=2&alt=jsonc");
            BufferedReader br = new BufferedReader(new InputStreamReader(u.openStream()));
            JSONParser parser = new JSONParser();
            Object obj = parser.parse(br);
            JSONObject json = (JSONObject) obj;
            JSONObject data = (JSONObject) json.get("data");
            JSONArray items = (JSONArray) data.get("items");
            Iterator<JSONObject> it = items.iterator();
            if (it != null) {
                JSONObject item = it.next();
                String id = (String) item.get("id");
                String title = (String) item.get("title");
                out.println(id);
            } else {
                out.println("Unable to fetch any video");
            }

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            try {
                processRequest(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(YoutubeSearch.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (ParseException ex) {
            Logger.getLogger(YoutubeSearch.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            try {
                processRequest(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(YoutubeSearch.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (ParseException ex) {
            Logger.getLogger(YoutubeSearch.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}