From 392857a15fcd650784c92b6ee42b863f1bfb555b Mon Sep 17 00:00:00 2001 From: moon Date: Fri, 8 Mar 2024 20:16:19 +0330 Subject: [PATCH 1/3] first commit --- .idea/misc.xml | 5 +- src/.idea/.gitignore | 0 src/.idea/misc.xml | 6 ++ src/.idea/modules.xml | 10 ++++ src/.idea/vcs.xml | 6 ++ src/.idea/workspace.xml | 45 +++++++++++++++ src/main/java/Actors.java | 58 ++++++++++++++----- src/main/java/Main.java | 114 +++++++++++++++++++++++++++++++++++++- src/main/java/Movie.java | 80 ++++++++++++++++++-------- 9 files changed, 283 insertions(+), 41 deletions(-) create mode 100644 src/.idea/.gitignore create mode 100644 src/.idea/misc.xml create mode 100644 src/.idea/modules.xml create mode 100644 src/.idea/vcs.xml create mode 100644 src/.idea/workspace.xml 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/src/.idea/.gitignore b/src/.idea/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/src/.idea/misc.xml b/src/.idea/misc.xml new file mode 100644 index 0000000..639900d --- /dev/null +++ b/src/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/.idea/modules.xml b/src/.idea/modules.xml new file mode 100644 index 0000000..31eaf94 --- /dev/null +++ b/src/.idea/modules.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/src/.idea/vcs.xml b/src/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/src/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/.idea/workspace.xml b/src/.idea/workspace.xml new file mode 100644 index 0000000..a81b977 --- /dev/null +++ b/src/.idea/workspace.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + 1709747545962 + + + + + + \ No newline at end of file diff --git a/src/main/java/Actors.java b/src/main/java/Actors.java index ebf8e2d..a7602b9 100644 --- a/src/main/java/Actors.java +++ b/src/main/java/Actors.java @@ -3,14 +3,20 @@ import java.io.InputStreamReader; import java.net.URL; import java.net.HttpURLConnection; +import java.util.Scanner; +import org.json.JSONObject; + public class Actors { - public static final String API_KEY = "Your API_KEY"; // TODO --> add your api key about Actors here + public static final String API_KEY = "W+vh5nItJvK8jXhbKH2x0g==JJe0UK88tZZBGS1n"; // TODO --> add your api key about Actors here String netWorth; Boolean isAlive; - public Actors(String netWorth, boolean isAlive){ + public Actors(String netWorth, boolean isAlive) { //TODO --> (Write a proper constructor using the get_from_api functions) + this.netWorth = netWorth; + this.isAlive = isAlive; } + @SuppressWarnings({"deprecation"}) /** * Retrieves data for the specified actor. @@ -19,8 +25,8 @@ 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(" ", "+") + "&apikey=" + API_KEY); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("X-Api-Key", API_KEY); System.out.println(connection); @@ -34,7 +40,9 @@ public String getActorData(String name) { } in.close(); - return response.toString(); + String res = response.toString(); + res = res.substring(1, res.length() - 1); + return res; } else { return "Error: " + connection.getResponseCode() + " " + connection.getResponseMessage(); } @@ -43,22 +51,42 @@ public String getActorData(String name) { return null; } } - public double getNetWorthViaApi(String actorsInfoJson){ - //TODO --> (This function must return the "NetWorth") - double result = 0.0; - return result; + + + public String getNameViaApi(String actorsInfoJson) { + JSONObject jason = new JSONObject(actorsInfoJson); + return jason.getString("name"); + } + public double getNetWorthViaApi(String actorsInfoJson) { + JSONObject jason = new JSONObject(actorsInfoJson); + return jason.getDouble("net_worth"); + } + public String getGenderViaApi(String actorsInfoJson) { + JSONObject jason = new JSONObject(actorsInfoJson); + return jason.getString("gender"); + }public float getHeightViaApi(String actorsInfoJson) { + JSONObject jason = new JSONObject(actorsInfoJson); + return jason.getFloat("height"); + }public String getDateOfBirthViaApi(String actorsInfoJson) { + JSONObject jason = new JSONObject(actorsInfoJson); + return jason.getString("birthday"); + }public int getAgeViaApi(String actorsInfoJson) { + JSONObject jason = new JSONObject(actorsInfoJson); + return jason.getInt("age"); } - public boolean isAlive(String actorsInfoJson){ + + + public boolean isAlive(String actorsInfoJson) { //TODO --> (If your chosen actor is alive it must return true otherwise it must return false) - boolean statues = false; - return statues; + JSONObject jason = new JSONObject(actorsInfoJson); + return jason.getBoolean("is_alive"); } - public String getDateOfDeathViaApi(String actorsInfoJson){ + public String getDateOfDeathViaApi(String actorsInfoJson) { //TODO --> (If your chosen actor is deceased it must return the date of death) --> - String date = ""; - return date; + JSONObject jason = new JSONObject(actorsInfoJson); + return jason.getString("death"); } } \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 92f3d9c..b5291b6 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,9 +1,119 @@ +import java.util.ArrayList; +import java.util.Scanner; + public class Main { + + public static void main(String[] args) { // TODO --> complete main function - runMenu(); + Scanner input = new Scanner(System.in); + Movie myMovie = new Movie(new ArrayList<>(), "", 0); + boolean secondWhileBreak = false; + System.out.println("Give me a name of a movie!"); + while (true) { + String movieName = input.nextLine(); + String movieJson = myMovie.getMovieData(movieName); + if (movieJson == "{\"Response\":\"False\",\"Error\":\"Movie not found!\"}") { + System.out.println("invalid name... Movie not found"); + continue; + } + while (true) { + System.out.println("1. selected movie\n2. year\n3. imdb votes\n4. imdb rating\n5. actors\n6. select another movie\n7. EXIT"); + int movieMenuOrder = Integer.valueOf(input.nextLine()); + if (movieMenuOrder == 6) { + break; + } else if (movieMenuOrder == 7) { + secondWhileBreak = true; + break; + } + runMovieMenu(movieName, movieMenuOrder); + } + if (secondWhileBreak) { + break; + } + } + } - public static void runMenu() { + + + public static void runMovieMenu(String name, int order) { + Movie myMovie = new Movie(new ArrayList<>(), "", 0); + String movieJson = myMovie.getMovieData(name); + + switch (order) { + case 1: + System.out.println(myMovie.getTitleViaApi(movieJson)); + break; + case 2: + System.out.println(myMovie.getYearViaApi(movieJson)); + break; + case 3: + System.out.println(myMovie.getImdbVotesViaApi(movieJson)); + break; + case 4: + System.out.println(myMovie.getRatingViaApi(movieJson)); + break; + case 5: + System.out.println(myMovie.getActorListViaApi(movieJson)); + System.out.println("Type the actor name to see actor info"); + Scanner input = new Scanner(System.in); + while (true) { + String actorName = input.nextLine(); + Actors myActor = new Actors(name, true); + String actorJson = myActor.getActorData(name); + if (actorJson == null) { + System.out.println("ERROR: Invalid name... Actor not found"); + continue; + } + System.out.println("1. selected actor\n2. net worth\n3. gender\n4. height\n5. birth\n6. age or date of death\n7. BACK"); + while (true) { + int actorMenuOrder = Integer.valueOf(input.nextLine()); + runActorMenu(actorName, actorMenuOrder); + + } + + } + } + return; + } + + + public static void runActorMenu(String name, int order) { // TODO + Actors myActor = new Actors(name, true); + String json = myActor.getActorData(name); + Actors estefade = new Actors(name, true); + + switch (order) { + case 1: + System.out.println(myActor.getNameViaApi(json)); + break; + case 2: + System.out.println(myActor.getNetWorthViaApi(json)); + break; + case 3: + System.out.println(myActor.getGenderViaApi(json)); + break; + case 4: + System.out.println(myActor.getHeightViaApi(json)); + break; + case 5: + System.out.println(myActor.getDateOfBirthViaApi(json)); + break; + case 6: + if (myActor.isAlive(json)) + System.out.println(myActor.getAgeViaApi(json)); + else if (!myActor.isAlive(json)) + System.out.println(myActor.getDateOfDeathViaApi(json)); + break; + default: + System.out.println("invalid choice"); + break; + } + +// String inputName = "ana de armas"; +// String result = myActor.getActorData(inputName); +// System.out.println(result); +// System.out.println(myActor.getNetWorthViaApi(result)); } } \ No newline at end of file diff --git a/src/main/java/Movie.java b/src/main/java/Movie.java index 34b5d4c..725fc34 100644 --- a/src/main/java/Movie.java +++ b/src/main/java/Movie.java @@ -1,16 +1,20 @@ +import org.json.JSONArray; +import org.json.JSONObject; import java.io.IOException; import java.io.InputStreamReader; +import java.net.MalformedURLException; 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 = "e5520821"; // TODO --> add your api key about Movie here int ImdbVotes; ArrayList actorsList; String rating; - public Movie(ArrayList actorsList, String rating, int ImdbVotes){ + public Movie(ArrayList actorsList, String rating, int ImdbVotes) { //TODO --> (Write a proper constructor using the get_from_api functions) } @@ -22,35 +26,65 @@ 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 + "&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); + } + reader.close(); + String movieData = stringBuilder.toString(); + // Check if the movie data contains an error message + if (movieData.contains("{\"Response\":\"False\",\"Error\":\"Movie not found!\"}")) { + throw new IOException("Movie not found"); + } + return movieData; + } catch (MalformedURLException e) { + System.out.println("The URL is not in a valid form"); + } catch (IOException e) { + System.out.println("ERROR: Invalid name... Movie not found\nTry again"); } - reader.close(); - //handle an error if the chosen movie is not found - return stringBuilder.toString(); + return null; + } + + + public String getTitleViaApi(String moviesInfoJson) { + JSONObject jason = new JSONObject(moviesInfoJson); + return jason.getString("Title"); + } + + public int getYearViaApi(String moviesInfoJson) { + JSONObject jason = new JSONObject(moviesInfoJson); + return jason.getInt("Year"); } - 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; - return ImdbVotes; + + public int getImdbVotesViaApi(String moviesInfoJson) { + JSONObject json = new JSONObject(moviesInfoJson); + String imdbVotesString = json.getString("imdbVotes"); + // Remove any non-digit characters + imdbVotesString = imdbVotesString.replaceAll("\\D+",""); + return Integer.parseInt(imdbVotesString); } - public String getRatingViaApi(String moviesInfoJson){ + + 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 jason = new JSONObject(moviesInfoJson); + JSONArray imdbRating = jason.getJSONArray("Ratings"); + return imdbRating.getJSONObject(0).getString("Value"); + + } - public void getActorListViaApi(String movieInfoJson){ + public String getActorListViaApi(String movieInfoJson) { //TODO --> (This function must return the "Actors" in actorsList) + JSONObject jason = new JSONObject(movieInfoJson); + return jason.getString("Actors"); } } \ No newline at end of file From 255c0d2570f1dbc8570fa35991d6ad322fe52a27 Mon Sep 17 00:00:00 2001 From: moon Date: Fri, 8 Mar 2024 21:16:08 +0330 Subject: [PATCH 2/3] final --- Report-Template.md | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/Report-Template.md b/Report-Template.md index 1db99da..90cb3e0 100644 --- a/Report-Template.md +++ b/Report-Template.md @@ -1,22 +1,23 @@ -# Project Title +# Movie Info -Simple overview of use/purpose. +This a simple app that can be us used for generating movie or actor informations. ## Description - -An in-depth paragraph about your project and overview of use. +This application has two menu. One for movie and another is for actors. +In movie menu you can take the imformations such as "release year", "imdb votes", "imdb rating" and "actors". +In actor menu you can see details of "net worth", "actor gender", "actor height", "actor birth" and "actor age or if she/he died, ypou can see the date of death". ## Getting Started +To start the program you should enter a valid name of a movie. ### Dependencies - -* Describe any prerequisites, libraries, OS version, etc., needed before installing program. -* ex. Windows 10 +First of all you should have java and nessary IDEs on your path. +To run this program you need to install Gradle. It let the app to use some special libraries. ### Installing + YOu can fork this files in git hub. + The URL code of this repository is "https://github.com/mahan-thm/Second-Assignment-CineScribe"; -* How/where to download your program -* Any modifications needed to be made to files/folders ### Executing program @@ -25,20 +26,17 @@ An in-depth paragraph about your project and overview of use. ``` code blocks for commands ``` +At first you run the program, you see that it needs a name. You must enter a valid name of a movie to continue. +NOTE: if you enter any unvalid input to the program, an error will be accure and you should try again. +At the first menu (movie menu) there is a couple of option that gives you actor infoes. +In "actors", by entering the actor name, you can see the information of that actor. -## Help -Any advise for common problems or issues. -``` -command to run if program contains helper info -``` +## Help +This application have to connect to internet to export the datas. So dont forget to connect to a proper network. ## Authors - -Contributors names and contact info - -ex. Dominique Pizzie -ex. [@DomPizzie](https://twitter.com/dompizzie) +Email: m.tahmasbi1383@gmail.com ## Version History From f48e3125856d4dd6310461e307bd2e0803fe2a4e Mon Sep 17 00:00:00 2001 From: moon Date: Fri, 8 Mar 2024 21:35:54 +0330 Subject: [PATCH 3/3] last of the last --- Report-Template.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Report-Template.md b/Report-Template.md index 90cb3e0..7925445 100644 --- a/Report-Template.md +++ b/Report-Template.md @@ -51,7 +51,7 @@ Email: m.tahmasbi1383@gmail.com This project is licensed under the [NAME HERE] License - see the LICENSE.md file for details ## Acknowledgments - +. Inspiration, code snippets, etc. * [awesome-readme](https://github.com/matiassingers/awesome-readme) * [PurpleBooth](https://gist.github.com/PurpleBooth/109311bb0361f32d87a2)