Skip to content

Commit

Permalink
Ajout d'une base pour rechercher sur le web avec une API open-source
Browse files Browse the repository at this point in the history
  • Loading branch information
Dim145 committed Apr 1, 2021
1 parent 5a4f0ad commit e308cca
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 0 deletions.
1 change: 1 addition & 0 deletions Files_renamer.iml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@
<orderEntry type="library" name="Maven: org.jetbrains:annotations:20.1.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.13.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.6" level="project" />
</component>
</module>
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,11 @@
<version>4.13.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
</dependencies>
</project>
9 changes: 9 additions & 0 deletions src/main/java/renameFiles/metier/types/series/Serie.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import renameFiles.ihm.dialogs.DialogAvancement;
import renameFiles.metier.types.ListeInterface;
import renameFiles.metier.web.WebInfoHelper;

import java.io.File;
import java.util.ArrayList;
Expand Down Expand Up @@ -102,6 +103,9 @@ public String traitement(DialogAvancement dialog)

this.setNbMaxSaisonAllSaison();


WebInfoHelper.selectInfosTests(this);

for (Saison s : listSaison)
{
s.setRoundEpisodeAllEpisode();
Expand Down Expand Up @@ -145,4 +149,9 @@ public int hashCode()
{
return Objects.hash(serieName);
}

public String getSerieName()
{
return serieName;
}
}
44 changes: 44 additions & 0 deletions src/main/java/renameFiles/metier/web/Serie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package renameFiles.metier.web;

import java.net.URI;
import java.util.Date;

public class Serie
{
private int id;
private URI url;
private String name;
private String type;
private Date premiered;

public int getId()
{
return id;
}

public URI getUrl()
{
return url;
}

public String getName()
{
return name;
}

public String getType()
{
return type;
}

public Date getPremiered()
{
return premiered;
}

@Override
public String toString()
{
return "Serie{" + "id=" + id + ", url='" + url + '\'' + ", name='" + name + '\'' + ", type='" + type + '\'' + ", premiered='" + premiered + '\'' + '}';
}
}
133 changes: 133 additions & 0 deletions src/main/java/renameFiles/metier/web/WebInfoHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package renameFiles.metier.web;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class WebInfoHelper
{
/*
%% = series ID
%s = season number
%e = episode number
*/
private static final String BASE_SEARCH_URL = "http://api.tvmaze.com/singlesearch/shows?q=";
private static final String EPISODE_URL = "http://api.tvmaze.com/shows/%%/episodes";
private static final String EPISODE_SEARCH = "http://api.tvmaze.com/shows/%%/episodebynumber?season=%s&number=%e";

private static final Gson GSON = new Gson();

private static final int DEFAULT_BUFFER_SIZE = 8192;

public static void selectInfosTests(renameFiles.metier.types.series.Serie serie)
{
String urlComplet = BASE_SEARCH_URL + serie.getSerieName().replaceAll(" ", "%20");
String donnees = getJsonResponseFromURL(urlComplet);
JsonElement webSerie = JsonParser.parseString(donnees);

Serie result = GSON.fromJson(webSerie, Serie.class);

if( result != null )
System.out.println(result.getName() + ": " + result.getId());
else
System.out.println(urlComplet + " => serie inconnue\n" + donnees);
}

public static String getJsonResponseFromURL(String urlString)
{
try
{
URL url = URI.create(urlString).toURL();

URLConnection connect = url.openConnection();

connect.connect();

InputStream stream = connect.getInputStream();

return new String(readAllBytes(stream));
}
catch (IOException e)
{
return "";
}
}

private static byte[] readAllBytes(InputStream stream) throws IOException
{
int len = Integer.MAX_VALUE;

List<byte[]> bufs = null;
byte[] result = null;
int total = 0;
int remaining = len;
int n;
do
{
byte[] buf = new byte[Math.min(remaining, DEFAULT_BUFFER_SIZE)];
int nread = 0;

// read to EOF which may read more or less than buffer size
while ((n = stream.read(buf, nread, Math.min(buf.length - nread, remaining))) > 0)
{
nread += n;
remaining -= n;
}

if (nread > 0)
{
if (Integer.MAX_VALUE - 8 - total < nread)
{
throw new OutOfMemoryError("Required array size too large");
}
total += nread;
if (result == null)
{
result = buf;
}
else
{
if (bufs == null)
{
bufs = new ArrayList<>();
bufs.add(result);
}
bufs.add(buf);
}
}
// if the last call to read returned -1 or the number of bytes
// requested have been read then break
} while (n >= 0 && remaining > 0);

if (bufs == null)
{
if (result == null)
{
return new byte[0];
}
return result.length == total ? result : Arrays.copyOf(result, total);
}

result = new byte[total];
int offset = 0;
remaining = total;
for (byte[] b : bufs)
{
int count = Math.min(b.length, remaining);
System.arraycopy(b, 0, result, offset, count);
offset += count;
remaining -= count;
}

return result;
}
}

0 comments on commit e308cca

Please sign in to comment.