diff --git a/.idea/misc.xml b/.idea/misc.xml index 668048d..de0c428 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,8 @@ - + + + + \ No newline at end of file diff --git a/Report-Template.md b/Report-Template.md index 1db99da..906dd77 100644 --- a/Report-Template.md +++ b/Report-Template.md @@ -1,62 +1,45 @@ -# Project Title +# Movies and Actors 🎬🌟 -Simple overview of use/purpose. +This program shows the detail of a movie or an actor. -## Description +## Description πŸ“– -An in-depth paragraph about your project and overview of use. +The program collect the information from resources and shows them to you. +It has two option as well and you have select one. +"Movies and Actors" the title says it all, you just have to search them up by their names. -## Getting Started - -### Dependencies -* Describe any prerequisites, libraries, OS version, etc., needed before installing program. -* ex. Windows 10 +## Getting Started -### Installing +### Dependencies πŸ” -* How/where to download your program -* Any modifications needed to be made to files/folders +* First of all you have to install gradle. +* the resources come from https://omdbapi.com (Movies) and https://api-ninjas.com/api/celebrity (Actors). +* the imported classes are in program as well. -### Executing program +### Installing βœ… -* How to run the program -* Step-by-step bullets -``` -code blocks for commands -``` +* To install gradle go to https://gradle.org/install/ address, and it shows you the way to install it(I downloaded v8.6). +* Get the API key from two websites which was the resources after you made your account. -## Help +### Executing program β–Ά -Any advise for common problems or issues. +* Make sure your gradle works properly. +* Run the "Main" program in terminal. +* Then enter the number of the options. ``` -command to run if program contains helper info +1. Movies +2. Actors +3. Exit ``` -## Authors - -Contributors names and contact info - -ex. Dominique Pizzie -ex. [@DomPizzie](https://twitter.com/dompizzie) - -## Version History - -* 0.2 - * Various bug fixes and optimizations - * See [commit change]() or See [release history]() -* 0.1 - * Initial Release - -## License +## Authors πŸ“ -This project is licensed under the [NAME HERE] License - see the LICENSE.md file for details +**GitHub:** Soroushsbr +**Email:** soroush.13830o@gmail.com -## Acknowledgments +## Resources🧱 -Inspiration, code snippets, etc. -* [awesome-readme](https://github.com/matiassingers/awesome-readme) -* [PurpleBooth](https://gist.github.com/PurpleBooth/109311bb0361f32d87a2) -* [dbader](https://github.com/dbader/readme-template) -* [zenorocha](https://gist.github.com/zenorocha/4526327) -* [fvcproductions](https://gist.github.com/fvcproductions/1bfc2d4aecb01a834b46) \ No newline at end of file +* [πŸ”— Getting Json Data From A RESTFUL API Using Java](https://medium.com/swlh/getting-json-data-from-a-restful-api-using-java-b327aafb3751) +* [πŸ”— Uploading a File and JSON Data in Postman](https://www.baeldung.com/postman-upload-file-json) +* [πŸ”— Send API requests and get response data in Postman](https://learning.postman.com/docs/sending-requests/requests/) \ No newline at end of file diff --git a/src/main/java/Actors.java b/src/main/java/Actors.java index ebf8e2d..2097aad 100644 --- a/src/main/java/Actors.java +++ b/src/main/java/Actors.java @@ -3,13 +3,16 @@ import java.io.InputStreamReader; import java.net.URL; import java.net.HttpURLConnection; +import org.json.JSONObject; public class Actors { - public static final String API_KEY = "Your API_KEY"; // TODO --> add your api key about Actors here - String netWorth; - Boolean isAlive; + public static final String API_KEY = "DxPDkxjUL6bpYDurOQZwiw==1IjnRq8i7CSBVRPQ"; + String name; + String birthday; + double netWorth; + boolean isAlive; + + public Actors(String name, boolean isAlive){ - public Actors(String netWorth, boolean isAlive){ - //TODO --> (Write a proper constructor using the get_from_api functions) } @SuppressWarnings({"deprecation"}) /** @@ -19,22 +22,20 @@ public Actors(String netWorth, boolean isAlive){ */ public String getActorData(String name) { try { - URL url = new URL("https://api.api-ninjas.com/v1/celebrity?name="+ - name.replace(" ", "+")+"&apikey="+API_KEY); + URL url = new URL("https://api.api-ninjas.com/v1/celebrity?name="+ name.replace(" ", "+")); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("X-Api-Key", API_KEY); - System.out.println(connection); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); - while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); - return response.toString(); + String result = response.toString(); + return result.substring(1 ,result.length() - 1); } else { return "Error: " + connection.getResponseCode() + " " + connection.getResponseMessage(); } @@ -44,21 +45,25 @@ public String getActorData(String name) { } } public double getNetWorthViaApi(String actorsInfoJson){ - //TODO --> (This function must return the "NetWorth") + JSONObject jsonObject = new JSONObject(actorsInfoJson); double result = 0.0; + result = jsonObject.getDouble("net_worth"); return result; } - public boolean isAlive(String actorsInfoJson){ - //TODO --> (If your chosen actor is alive it must return true otherwise it must return false) + public static boolean isAlive(String actorsInfoJson){ boolean statues = false; + JSONObject jsonObject = new JSONObject(actorsInfoJson); + statues = jsonObject.getBoolean("is_alive"); return statues; } public String getDateOfDeathViaApi(String actorsInfoJson){ - //TODO --> (If your chosen actor is deceased it must return the date of death) --> String date = ""; + JSONObject jsonObject = new JSONObject(actorsInfoJson); + date = jsonObject.getString("death"); return date; } + } \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 92f3d9c..4ae592b 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,9 +1,35 @@ +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.Scanner; public class Main { public static void main(String[] args) { - // TODO --> complete main function runMenu(); } public static void runMenu() { - // TODO + Scanner in = new Scanner(System.in); + System.out.print("1. Movie\n2. Actor\n3. Exit\nPlease Select an Option: "); + String option = in.nextLine(); + if(option.equals("1")){ + System.out.print("Enter the Name of Movie: "); + String title = in.nextLine(); + Movie movie = new Movie(new ArrayList<>(),"",0); + String movieInfoJson = movie.getMovieData(title); + JSONObject json = new JSONObject(movieInfoJson); + System.out.println(json.toString(4).replace("\"" , "") + "\n**********************************************************"); + runMenu(); + } + else if(option.equals("2")){ + System.out.print("Enter the Name of Actor: "); + String name = in.nextLine(); + Actors actors = new Actors("", false); + String actorsInfoJson = actors.getActorData(name); + JSONObject json = new JSONObject(actorsInfoJson); + System.out.println(json.toString(4).replace("\"" , "") + "\n**********************************************************"); + runMenu(); + } + else{ + System.out.println("Bye^-^"); + } } } \ No newline at end of file diff --git a/src/main/java/Movie.java b/src/main/java/Movie.java index 34b5d4c..c4b0131 100644 --- a/src/main/java/Movie.java +++ b/src/main/java/Movie.java @@ -1,11 +1,14 @@ +import org.json.JSONObject; + import java.io.IOException; import java.io.InputStreamReader; +import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.io.BufferedReader; import java.util.ArrayList; public class Movie { - public static final String API_KEY = "Your API_KEY"; // TODO --> add your api key about Movie here + public static final String API_KEY = "1ee38a46"; // TODO --> add your api key about Movie here int ImdbVotes; ArrayList actorsList; String rating; @@ -22,24 +25,36 @@ public Movie(ArrayList actorsList, String rating, int ImdbVotes){ * @return a string representation of the MovieData, or null if an error occurred */ - public String getMovieData(String title) throws IOException { - URL url = new URL("https://www.omdbapi.com/?t="+title+"&apikey="+API_KEY); - URLConnection Url = url.openConnection(); - Url.setRequestProperty("Authorization", "Key" + API_KEY); - BufferedReader reader = new BufferedReader(new InputStreamReader(Url.getInputStream())); - String line; - StringBuilder stringBuilder = new StringBuilder(); - while ((line = reader.readLine())!=null) { - stringBuilder.append(line); + public String getMovieData(String title) { + try { + URL url = new URL("https://www.omdbapi.com/?t=" + title.replace(" ", "+") + "&apikey=" + API_KEY); + URLConnection Url = url.openConnection(); + Url.setRequestProperty("Authorization", "Key" + API_KEY); + if(((HttpURLConnection) Url).getResponseCode() == HttpURLConnection.HTTP_OK) { + BufferedReader reader = new BufferedReader(new InputStreamReader(Url.getInputStream())); + String line; + StringBuilder stringBuilder = new StringBuilder(); + while ((line = reader.readLine()) != null) { + stringBuilder.append(line); + } + reader.close(); + return stringBuilder.toString(); + } + else { + return "Error: " + ((HttpURLConnection) Url).getResponseCode() + " " + ((HttpURLConnection) Url).getResponseMessage(); + } + }catch (IOException e) { + e.printStackTrace(); + return null; } - reader.close(); - //handle an error if the chosen movie is not found - return stringBuilder.toString(); + } public int getImdbVotesViaApi(String moviesInfoJson){ //TODO --> (This function must change and return the "ImdbVotes" as an Integer) // NOTICE :: you are not permitted to convert this function to return a String instead of an int !!! int ImdbVotes = 0; + JSONObject jsonObject = new JSONObject(moviesInfoJson); + ImdbVotes = Integer.parseInt((jsonObject.getString("imdbVotes").replace("," ,""))); return ImdbVotes; } @@ -47,10 +62,13 @@ public String getRatingViaApi(String moviesInfoJson){ //TODO --> (This function must return the rating in the "Ratings" part // where the source is "Internet Movie Database") --> String rating = ""; - return rating; + JSONObject jsonObject = new JSONObject(moviesInfoJson); + rating = jsonObject.getString("imdbRating"); + return rating + "/10"; } public void getActorListViaApi(String movieInfoJson){ //TODO --> (This function must return the "Actors" in actorsList) + } } \ No newline at end of file