Code Monkey home page Code Monkey logo

api_imdb_movies's Introduction

☕ An HTTP request

Prerequisites

Before starting, you will need to have the following tools installed on your machine: JVM. Besides, it's good to have an editor to work with the code like VSCode

# clone this right
$ git clone <https://github.com/Ana204/api_imdb_movies.git>

# run the application
$ In option 'Run Java'

🛠 Technology

The following tool was used in building the project:

# if you don't want to clone
 //connection https -  fetch the top 250 movies
        String url = "https://alura-imdb-api.herokuapp.com/movies";

        URI endereco = URI.create(url);
        var client = HttpClient.newHttpClient();
        var request = HttpRequest.newBuilder(endereco).GET().build();
        HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
        String body = response.body();

        //get only the data we are interested in - Title, image, classification
        var jsonParser = new JsonParser();

        List<Map<String, String>> listMovie = jsonParser.parse(body);

        //extract and manipulate the data
        for (Map<String, String> filmes : listMovie){
            System.out.println("Titulo: " + filmes.get("title"));
            System.out.println("Poster: " + filmes.get("image"));
            System.out.println("\u001b[37m \u001b[44m Classificação: \u001b[m"  + filmes.get("imDbRating"));
            System.out.println("⭐⭐⭐⭐⭐⭐");

        }

    }

# you will also need to create a class called jsonParser

    private static final Pattern REGEX_ITEMS = Pattern.compile(".*\\[(.+)\\].*");
    private static final Pattern REGEX_ATRIBUTOS_JSON = Pattern.compile("\"(.+?)\":\"(.*?)\"");
    
    public List<Map<String, String>> parse(String json){
        
        Matcher matcher = REGEX_ITEMS.matcher(json);
        if (!matcher.find()) {

            throw new IllegalArgumentException("Não encontrou items.");
        }

        String[] items = matcher.group(1).split("\\},\\{");

        List<Map<String, String>> dados = new ArrayList<>();

        for (String item : items) {

            Map<String, String> atributosItem = new HashMap<>();

            Matcher matcherAtributosJson = REGEX_ATRIBUTOS_JSON.matcher(item);
            while (matcherAtributosJson.find()) {
                String atributo = matcherAtributosJson.group(1);
                String valor = matcherAtributosJson.group(2);
                atributosItem.put(atributo, valor);
            }

            dados.add(atributosItem);
        }

        return dados;

    }

api_imdb_movies's People

Contributors

ana204 avatar analgomess avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.