Initial commit
This commit is contained in:
25
jsoup/pom.xml
Normal file
25
jsoup/pom.xml
Normal file
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>ch.gtache.fro</groupId>
|
||||
<artifactId>fro</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>fro-jsoup</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>ch.gtache.fro</groupId>
|
||||
<artifactId>fro-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jsoup</groupId>
|
||||
<artifactId>jsoup</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,109 @@
|
||||
package ch.gtache.fro.jsoup;
|
||||
|
||||
import ch.gtache.fro.Bird;
|
||||
import ch.gtache.fro.BirdProvider;
|
||||
import ch.gtache.fro.Configuration;
|
||||
import ch.gtache.fro.Fetcher;
|
||||
import ch.gtache.fro.PictureType;
|
||||
import ch.gtache.fro.SoundType;
|
||||
import ch.gtache.fro.impl.AbstractFetcher;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Implementation of {@link Fetcher} using JSoup
|
||||
*/
|
||||
public abstract class AbstractJSoupFetcher extends AbstractFetcher {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(AbstractJSoupFetcher.class);
|
||||
|
||||
/**
|
||||
* Instantiates the fetcher
|
||||
*
|
||||
* @param birdProvider The bird provider
|
||||
* @param configuration The configuration
|
||||
* @throws NullPointerException If any parameter is null
|
||||
*/
|
||||
protected AbstractJSoupFetcher(final BirdProvider birdProvider, final Configuration configuration) {
|
||||
super(birdProvider, configuration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a document from an URL
|
||||
*
|
||||
* @param url The URL
|
||||
* @return The document
|
||||
* @throws IOException If an error occurs
|
||||
*/
|
||||
protected Document getDocument(final String url) throws IOException {
|
||||
return Jsoup.connect(url).get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a file from an URL
|
||||
*
|
||||
* @param url The URL
|
||||
* @return The bytes
|
||||
* @throws IOException If an error occurs
|
||||
*/
|
||||
protected static byte[] downloadFile(final String url) throws IOException {
|
||||
final var response = Jsoup.connect(url).ignoreContentType(true).execute();
|
||||
return response.bodyAsBytes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads an image
|
||||
*
|
||||
* @param bird The bird
|
||||
* @param url The image location
|
||||
* @param pictureType The picture type
|
||||
* @throws IOException If an error occurs
|
||||
*/
|
||||
protected void downloadImage(final Bird bird, final String url,
|
||||
final PictureType pictureType) throws IOException {
|
||||
logger.info("Trying to download {}", url);
|
||||
final var bytes = downloadFile(url);
|
||||
final var name = getImageFilename(url);
|
||||
savePicture(bird, bytes, name, pictureType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads a sound
|
||||
*
|
||||
* @param bird The bird
|
||||
* @param url The sound location
|
||||
* @param soundType The sound type
|
||||
* @throws IOException If an error occurs
|
||||
*/
|
||||
protected void downloadSound(final Bird bird, final String url, final SoundType soundType) throws IOException {
|
||||
final var bytes = downloadFile(url);
|
||||
final var name = getSoundFilename(url);
|
||||
saveSound(bird, bytes, name, soundType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the filename from an URL
|
||||
*
|
||||
* @param url The URL
|
||||
* @return The filename
|
||||
*/
|
||||
protected String getSoundFilename(final String url) {
|
||||
final var lastSlash = url.lastIndexOf('/');
|
||||
return url.substring(lastSlash + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the filename from an URL
|
||||
*
|
||||
* @param url The URL
|
||||
* @return The filename
|
||||
*/
|
||||
protected String getImageFilename(final String url) {
|
||||
final var lastSlash = url.lastIndexOf('/');
|
||||
return url.substring(lastSlash + 1);
|
||||
}
|
||||
}
|
||||
11
jsoup/src/main/java/module-info.java
Normal file
11
jsoup/src/main/java/module-info.java
Normal file
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* JSoup fetcher module for the FRO application
|
||||
*/
|
||||
module ch.gtache.fro.jsoup {
|
||||
requires transitive ch.gtache.fro.core;
|
||||
requires org.jsoup;
|
||||
requires ch.gtache.fro.api;
|
||||
requires org.apache.logging.log4j;
|
||||
|
||||
exports ch.gtache.fro.jsoup;
|
||||
}
|
||||
Reference in New Issue
Block a user