Skip to content
This repository has been archived by the owner on Mar 21, 2023. It is now read-only.

Commit

Permalink
Added the function getJSONFromTitleContains()
Browse files Browse the repository at this point in the history
Edited the README.md
Edited the Test.java
  • Loading branch information
Minemobs committed Dec 26, 2020
1 parent e0981c2 commit 65459e6
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 4 deletions.
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,48 @@ And add in your dependencies this :
One example of a possible use.

```java
/**
* @author minemobs
* @version v1.1
*/

public class Main {

static Main main = new Main();

public static void main(String[] args) {
System.out.println("Choose what anime you want to search.");
Scanner scanner = new Scanner(System.in);
//Instantiate the class AnimeSearcherAPI
AnimeSearcherAPI animes = new AnimeSearcherAPI();
//Set the name of the anime
String animeTitle = scanner.nextLine();
System.out.println("Equals or contains this string ? \n" +
"[YES] or [No]");
scanner = new Scanner(System.in);
switch (scanner.nextLine().toLowerCase()) {
case "yes":
/**
* Call the function animeSearchEquals()
* @param animes the variable AnimeSearcherAPI
* @param animeTitle the name of the anime
*/
main.animeSearchEquals(animes, animeTitle);
break;
case "no":
/**
* Call the function animeSearchEquals()
* @param animes the variable AnimeSearcherAPI
* @param animeTitle the name of the anime
*/
main.animeSearchContains(animes, animeTitle);
break;
default:
System.out.println("Please relaunch the code because i don't want to relaunch the function myself.");
break;
}
}
private void animeSearchEquals(AnimeSearcherAPI animes, String animeTitle) {
//Create the variable anime which get the result of the function getJSONFromTitle()
if (animes.getJSONFromTitle(animeTitle) == null) {
throw new NullPointerException("This anime is not on NekoSama");
Expand All @@ -54,5 +87,45 @@ public class Main {
}
}

private void animeSearchContains(AnimeSearcherAPI animes, String animeTitle) {
//Create the variable anime which get the result of the function getJSONFromTitle()
if (animes.getJSONFromTitleContains(animeTitle) == null) {
throw new NullPointerException("This anime is not on NekoSama");
}
List<Anime> animeList = animes.getJSONFromTitleContains(animeTitle);
AnimeHtml animeHtml = null;
for (Anime anime : animeList) {
try {
//Create the variable animeHtml and
//Specify the episode searched
int episodeSearched;
if (anime.getType().equalsIgnoreCase("tv")) {
System.out.println("Please specify the episode wanted \n" +
"if you don't want to search an episode please write 1 (it will search the episode 1 anyways)");
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextInt()) {
episodeSearched = scanner.nextInt();
animeHtml = animes.getHtmlPageOfTheAnime(anime, episodeSearched);
} else {
System.out.println("I want a number!");
}
} else {
animeHtml = animes.getHtmlPageOfTheAnime(anime, 1);
}
//Print everything
System.out.println(
"-------------------------------------------------------------" + "\n" +
"Title: " + anime.getTitle() + "\n" +
"Url:" + anime.getUrl() + "\n" +
"Synopsis: " + animeHtml.getSynop() + "\n" +
"Url of episode: " + animeHtml.getLinkOfTheEpisode() + "\n" +
"-------------------------------------------------------------");
} catch (Exception e) {
//If there is an error, print the error
e.printStackTrace();
}
}
}
}

```
10 changes: 9 additions & 1 deletion src/main/java/fr/minemobs/animes/Anime.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ public class Anime {
private String[] genres;
private String url;
private String nbrOfEps;
private String type;

public Anime(String title, String title_english, String title_romanji, int id, String other, String[] genres, String url, String nbrOfEps) {
public Anime(String title, String title_english, String title_romanji, int id, String other, String[] genres, String url, String nbrOfEps,
String type) {
this.title = title;
this.title_english = title_english;
this.title_romanji = title_romanji;
Expand All @@ -22,6 +24,11 @@ public Anime(String title, String title_english, String title_romanji, int id, S
this.genres = genres;
this.url = url;
this.nbrOfEps = nbrOfEps;
this.type = type;
}

public String getType() {
return type;
}

public String getNbrOfEps() {
Expand Down Expand Up @@ -73,6 +80,7 @@ public String toString() {
", genres=" + Arrays.toString(genres) +
", url='" + url + '\'' +
", nbrOfEps='" + nbrOfEps + '\'' +
", type=" + type +
'}';
}
}
30 changes: 27 additions & 3 deletions src/main/java/fr/minemobs/animes/AnimeSearcherAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class AnimeSearcherAPI {

String urlOfNekoSama = "https://neko-sama.fr";
String jsonUrl = urlOfNekoSama + "/animes-search.json";

public Anime getJSONFromTitle(String animeTitle) {
try{
String jsonUrl = urlOfNekoSama + "/animes-search.json";
InputStream is = new URL(jsonUrl).openStream();
Reader json = new InputStreamReader(is,"UTF-8");

Expand All @@ -43,6 +45,28 @@ public Anime getJSONFromTitle(String animeTitle) {
return null;
}

public List<Anime> getJSONFromTitleContains(String animeTitle) {
try{
InputStream is = new URL(jsonUrl).openStream();
Reader json = new InputStreamReader(is,"UTF-8");

Gson gson = new Gson();
Type animeListType = new TypeToken<List<Anime>>(){}.getType();
List<Anime> animes = gson.fromJson(json, animeListType);
List<Anime> animes1 = animes.stream().filter(anime1 -> anime1.getTitle().toLowerCase().contains(animeTitle.toLowerCase())).collect(Collectors.toList());
return animes1;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

public AnimeHtml getHtmlPageOfTheAnime(Anime anime, int episodeSearched) throws Exception {
Document doc = Jsoup.connect(anime.getUrl()).get();
Elements titles = doc.getElementsByClass("episode");
Expand All @@ -53,9 +77,9 @@ public AnimeHtml getHtmlPageOfTheAnime(Anime anime, int episodeSearched) throws
String synopsis = doc.getElementsByClass("synopsis").text();

if(episodeSearched > nbrOfEps.get()){
throw new Exception("L'episode cherché est supérieur au nombre d'épisode existant");
throw new ArrayIndexOutOfBoundsException("L'episode cherché est supérieur au nombre d'épisode existant");
}else if(episodeSearched <= 0){
throw new Exception("L'episode cherché est inférieur ou égal à zero");
throw new ArrayIndexOutOfBoundsException("L'episode cherché est inférieur ou égal à zero");
}

String url;
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/fr/minemobs/animes/Test.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,54 @@
package fr.minemobs.animes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Scanner;

/**
* @author minemobs
* @version v1.1
*/

public class Test {

static Test test = new Test();

public static void main(String[] args) {
System.out.println("Choose what anime you want to search.");
Scanner scanner = new Scanner(System.in);
//Instantiate the class AnimeSearcherAPI
AnimeSearcherAPI animes = new AnimeSearcherAPI();
//Set the name of the anime
String animeTitle = scanner.nextLine();
System.out.println("Equals or contains this string ? \n" +
"[YES] or [No]");
scanner = new Scanner(System.in);
switch (scanner.nextLine().toLowerCase()){
case "yes":
/**
* Call the function animeSearchEquals()
* @param animes the variable AnimeSearcherAPI
* @param animeTitle the name of the anime
*/
test.animeSearchEquals(animes, animeTitle);
break;
case "no":
/**
* Call the function animeSearchEquals()
* @param animes the variable AnimeSearcherAPI
* @param animeTitle the name of the anime
*/
test.animeSearchContains(animes, animeTitle);
break;
default:
System.out.println("Please relaunch the code because i don't want to relaunch the function myself.");
break;
}
}

private void animeSearchEquals(AnimeSearcherAPI animes, String animeTitle) {
//Create the variable anime which get the result of the function getJSONFromTitle()
if (animes.getJSONFromTitle(animeTitle) == null) {
throw new NullPointerException("This anime is not on NekoSama");
Expand All @@ -32,4 +68,44 @@ public static void main(String[] args) {
}
}

private void animeSearchContains(AnimeSearcherAPI animes, String animeTitle){
//Create the variable anime which get the result of the function getJSONFromTitle()
if (animes.getJSONFromTitleContains(animeTitle) == null) {
throw new NullPointerException("This anime is not on NekoSama");
}
List<Anime> animeList = animes.getJSONFromTitleContains(animeTitle);
AnimeHtml animeHtml = null;
for (Anime anime : animeList) {
try {
//Create the variable animeHtml and
//Specify the episode searched
int episodeSearched;
if(anime.getType().equalsIgnoreCase("tv")){
System.out.println("Please specify the episode wanted \n" +
"if you don't want to search an episode please write 1 (it will search the episode 1 anyways)");
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextInt()){
episodeSearched = scanner.nextInt();
animeHtml = animes.getHtmlPageOfTheAnime(anime, episodeSearched);
}else{
System.out.println("I want a number!");
}
}else{
animeHtml = animes.getHtmlPageOfTheAnime(anime, 1);
}
//Print everything
System.out.println(
"-------------------------------------------------------------" + "\n" +
"Title: " + anime.getTitle() + "\n" +
"Url:" + anime.getUrl() + "\n" +
"Synopsis: " + animeHtml.getSynop() + "\n" +
"Url of episode: " + animeHtml.getLinkOfTheEpisode() + "\n" +
"-------------------------------------------------------------");
} catch (Exception e) {
//If there is an error, print the error
e.printStackTrace();
}
}
}

}

0 comments on commit 65459e6

Please sign in to comment.