Giter Site home page Giter Site logo

wallhaven4j's Introduction

wallhaven4j

Release Build Status codecov

wallhaven4j is a Java library that allows you to search for wallpapers on Wallhaven and access its resources - wallpapers, collections, tags, and users.

Requirements

  • JRE 7 or higher at runtime
  • JDK 8 or higher to compile the library from source

Installation

Gradle

Step 1. Add the JitPack repository to your root build.gradle at the end of repositories:

allprojects {
    repositories {
      ...
      maven { url "https://jitpack.io" }
    }
}

Step 2. Add the dependency:

dependencies {
    compile 'com.ivkos:wallhaven4j:1.3.0'
}

Maven

Step 1. Add the JitPack repository to your pom.xml file:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

Step 2. Add the dependency:

<dependency>
    <groupId>com.ivkos</groupId>
    <artifactId>wallhaven4j</artifactId>
    <version>1.3.0</version>
</dependency>

Documentation

Javadocs can be found here.

Quick Start

Important note: This library works by parsing the HTML of Wallhaven. Site design can change unexpectedly and potentially break the library, so until a fix is pushed you are advised to handle ParseExceptions gracefully at any Wallhaven operation. See Exception Handling below for more information.

Creating a Wallhaven session

// anonymous session
Wallhaven wh = new Wallhaven();

// log in with your wallhaven account
Wallhaven wh = new Wallhaven("USERNAME", "PASSWORD");

// log in with account and save session cookies to reuse later
Wallhaven wh = new Wallhaven("USERNAME", "PASSWORD", new File("/path/to/cookies.json"));

Searching

To search for wallpapers you need to first build a SearchQuery like so, and then use the search(...) methods of the Wallhaven object:

SearchQuery query = new SearchQueryBuilder()
            .keywords("cars", "bmw")
            .categories(Category.GENERAL)
            .purity(Purity.SFW)
            .sorting(Sorting.VIEWS)
            .order(Order.DESC)
            .resolutions(new Resolution(1920, 1080), new Resolution(1280, 720))
            .ratios(new Ratio(16, 9))
            .pages(3)
            .build();
    
List<Wallpaper> carWallpapers = wh.search(query);

If a filter is omitted, its default value will be used. Default values are defined in SearchQueryDefaults.

SearchQuery query = new SearchQueryBuilder()
            .keywords("minimal")
            .categories(Category.GENERAL)
            .ratios(new Ratio(9, 16))
            .pages(3)
            .build();
            
List<Wallpaper> minimalWallpapers = wh.search(query);

If your application needs to fetch individual pages on demand, for example when doing lazy loading, you can do this like so:

SearchQuery query = new SearchQueryBuilder()
            .keywords("face")
            .categories(Category.PEOPLE)
            .sorting(Sorting.FAVORITES)
            .build();

// fetch individual pages
List<Wallpaper> page1 = wh.search(query, 1);
List<Wallpaper> page3 = wh.search(query, 3);

Getting Resources by ID

Getting resources by their IDs is easy with the methods of the Wallhaven object:

Tag nature = wh.getTag(37);
User gandalf = wh.getUser("Gandalf");
Wallpaper doggo = wh.getWallpaper(254637);
WallpaperCollection wc = wh.getWallpaperCollection("Gandalf", 2);

Exception Handling

wallhaven4j parses Wallhaven's HTML. As a consequence, parsing may break if Wallhaven changes its design. It is recommended to be prepared to handle gracefully such cases. wallhaven4j has the following exception hierarchy:

  • WallhavenException
    • ConnectionException - problem with the HTTP connection to Wallhaven
    • LoginException - logging in to Wallhaven was unsuccessful due to incorrect credentials
    • ParseException - problem parsing Wallhaven's HTML
    • ResourceNotAccessibleException - the requested resource cannot be accessed due to privacy or purity restrictions
    • ResourceNotFoundException - the requested resource cannot be found

wallhaven4j's People

Contributors

ivkos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

live0717

wallhaven4j's Issues

Add Wallpaper#getLink()

Hello

First of all, thanks for this amazing API ! It's really helpful.

It's not a issue but a request, it would be great to have a Wallpaper#getLink() method that returns the wallpaper link :
joystick _ atari _ video_games _ minimalism _ ret_2017-09-24_22-27-05

Thanks :)

sometimes could not parse favorites count in thumbnail of Wallpaper

Hi, I'm using your api, so above all, thank you for what you did :)
Here is my problem: some time it work normal, but sometimes it goes wrong, and throw exception below:

Exception in thread "Thread-6" com.ivkos.wallhaven4j.util.exceptions.ParseException: Could not parse favorites count in thumbnail of Wallpaper
	at com.ivkos.wallhaven4j.util.exceptions.DescriptiveParseExceptionSupplier.lambda$forResource$1(DescriptiveParseExceptionSupplier.java:26)
	at com.ivkos.wallhaven4j.util.exceptions.DescriptiveParseExceptionSupplier$$Lambda$2.get(Unknown Source)
	at com.ivkos.wallhaven4j.util.exceptions.DescriptiveParseExceptionSupplier.get(DescriptiveParseExceptionSupplier.java:34)
	at com.ivkos.wallhaven4j.util.exceptions.DescriptiveParseExceptionSupplier.get(DescriptiveParseExceptionSupplier.java:8)
	at com.ivkos.wallhaven4j.util.htmlparser.OptionalSelector.orElseThrow(OptionalSelector.java:45)
	at com.ivkos.wallhaven4j.util.pagecrawler.thumbnailpage.ThumbnailTransformer.transform(ThumbnailTransformer.java:72)
	at com.ivkos.wallhaven4j.util.pagecrawler.thumbnailpage.ThumbnailPageCrawler$$Lambda$1.apply(Unknown Source)
	at com.google.common.collect.Lists$TransformingRandomAccessList$1.transform(Lists.java:640)
	at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:48)
	at java.util.AbstractCollection.toArray(AbstractCollection.java:141)
	at java.util.Collections$UnmodifiableCollection.toArray(Collections.java:1033)
	at java.util.ArrayList.addAll(ArrayList.java:577)
	at com.ivkos.wallhaven4j.util.pagecrawler.thumbnailpage.ThumbnailPageCrawler.getPageSequence(ThumbnailPageCrawler.java:94)
	at com.ivkos.wallhaven4j.Wallhaven.search(Wallhaven.java:171)
	at cn.dalin.WallhavenFetcherController.lambda$startDownloadOnClick$7(WallhavenFetcherController.java:114)
	at java.lang.Thread.run(Thread.java:745)
Caused by: com.ivkos.wallhaven4j.util.exceptions.ParseException: No such element: a.wall-favs
	at com.ivkos.wallhaven4j.util.htmlparser.OptionalSelector.orElseThrow(OptionalSelector.java:48)

Actually, I know what dose that means, I'm just wondering if it's due to I am in China and my connection to Wallhaven is not fast enough? Is there any other possible reason?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.