Android Open Source - wally Parser






From Project

Back to project page wally.

License

The source code is released under:

Apache License

If you think the Android project wally listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2014 Freddie (Musenkishi) Lust-Hed
 *//from w  w w  .j  a  v a 2  s.co m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.musenkishi.wally.dataprovider.util;

import android.net.Uri;
import android.text.TextUtils;

import com.musenkishi.wally.models.Author;
import com.musenkishi.wally.models.ExceptionReporter;
import com.musenkishi.wally.models.Image;
import com.musenkishi.wally.models.ImagePage;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.util.ArrayList;
import java.util.Iterator;

import static com.musenkishi.wally.dataprovider.NetworkDataProvider.WALLHAVEN_BASE_URL;

/**
 * A class for parsing data from Wallhaven to usable objects.
 * Uses SharedPreferencesProvider for knowing whether to send Crashlytics logs or do nothing.
 *
 * Created by Freddie (Musenkishi) Lust-Hed on 2014-10-16.
 */
public class Parser {

    private ExceptionReporter.OnReportListener onReportListener;

    public Parser(ExceptionReporter.OnReportListener onReportListener) {
        this.onReportListener = onReportListener;
    }

    public ArrayList<Image> parseImages(String data) {
        ArrayList<Image> images = new ArrayList<Image>();
        Document document = Jsoup.parse(data, WALLHAVEN_BASE_URL);
        Iterator thumbIterator = document.select("section.thumb-listing-page figure").iterator();

        while (thumbIterator.hasNext()) {
            Element wrapperElement = (Element) thumbIterator.next();

            //Get the resolution
            Element resElement = wrapperElement.getElementsByClass("wall-info").first();
            String resolution = resElement.select("span.wall-res").text();

            //Get the image with link
            Element linkElement = wrapperElement.select("a.preview").first();
            String imagePageURL = linkElement.absUrl("href");
            Element localElement2 = wrapperElement.getElementsByTag("img").first();
            if (localElement2 != null) {
                String thumbURL = localElement2.attr("data-src");
                if ((!TextUtils.isEmpty(imagePageURL)) && (!TextUtils.isEmpty(thumbURL))) {
                    Uri pageUri = Uri.parse(imagePageURL);
                    String imageId = pageUri.getLastPathSegment();
                    Image localThumbnail = Image.create(imageId, thumbURL, imagePageURL, resolution);
                    images.add(localThumbnail);
                }
            }
        }
        return images;
    }

    public ImagePage parseImagePage(String data, String url) {
        Document document = Jsoup.parse(data, WALLHAVEN_BASE_URL);

        String title = document.head().select("title").text();

        //All titles from wallhaven ends with 'wallpaper (#123456) / Wallhaven.cc
        // which takes up unnecessary space.
        String[] strings = title.split("Wallpaper");
        title = strings[0].trim();

        String idUri = Uri.parse(url).getLastPathSegment();

        String categoryMatch = "div.sidebar-section dl dd";
        String category = "Unknown";
        try {
            Element categoryElement = document.select(categoryMatch).get(2);
            category = categoryElement.text();
        } catch (IndexOutOfBoundsException e) {
            reportCrash(categoryMatch, e);
        }

        Element sfwElement = document.select("fieldset.framed input").first();
        String rating = "Unknown";
        if (sfwElement != null) {
            if (sfwElement.toString().contains("checked")){
                rating = "SFW";
            } else {
                rating = "Sketchy";
            }
        }

        String uploaderMatch = "div.sidebar-section dl dd";
        String uploader = "Unknown";
        try {
            Element uploaderElement = document.select(uploaderMatch).get(5);
            uploader = uploaderElement.text();
            if (uploader != null) {
                uploader = uploader.trim();
            }
        } catch (IndexOutOfBoundsException e) {
            reportCrash(uploaderMatch, e);
        }

        String uploadDateMatch = "div.sidebar-section dl dd";
        String uploadDate = "Unknown";
        try {
            Element uploadDateElement = document.select(uploadDateMatch).get(6);
            if (uploadDateElement.text() != null) {
                uploadDate = uploadDateElement.text();
            }
        } catch (IndexOutOfBoundsException e) {
            reportCrash(uploadDateMatch, e);
        }

        String sourceMatch = "div.sidebar-section dl dd";
        Author author = Author.create("Unknown", Uri.EMPTY);
        try {
            Element sourceElement = document.select(sourceMatch).get(7);

            String linkText = sourceElement.text();
            String linkHref = sourceElement.select("a").attr("href");
            if (!TextUtils.isEmpty(linkText)) {
                Uri uri = Uri.EMPTY;
                if (!TextUtils.isEmpty(linkHref)){
                    uri = Uri.parse(linkHref);
                }
                author = Author.create(linkText, uri);
            }
        } catch (IndexOutOfBoundsException e) {
            reportCrash(sourceMatch, e);
        }

        Element imageUrlElement = document.select("div#showcase img#wallpaper").first();
        String resolution = "0 x 0";
        Uri imagePath = Uri.EMPTY;
        if (imageUrlElement != null) {
            String width = imageUrlElement.attr("data-wallpaper-width");
            String height = imageUrlElement.attr("data-wallpaper-height");
            resolution = width + " x " + height;
            String imageUrl = imageUrlElement.attr("src");
            imagePath = Uri.parse(imageUrl);

            if (imagePath.getScheme() == null || !imagePath.getScheme().equalsIgnoreCase("http")
                    || !imagePath.getScheme().equalsIgnoreCase("https")) {
                imagePath = imagePath.buildUpon().scheme("http").build();
            }
        }
        return ImagePage.create(title, idUri, imagePath, resolution, category, rating, uploader, uploadDate, author);
    }

    private void reportCrash(String match, Exception e) {
        onReportListener.report(getClass(), match, e.getMessage());
    }

}




Java Source Code List

com.musenkishi.wally.activities.ImageDetailsActivity.java
com.musenkishi.wally.activities.MainActivity.java
com.musenkishi.wally.adapters.RecyclerImagesAdapter.java
com.musenkishi.wally.adapters.RecyclerSavedImagesAdapter.java
com.musenkishi.wally.adapters.SmartFragmentPagerAdapter.java
com.musenkishi.wally.adapters.SmartFragmentStatePagerAdapter.java
com.musenkishi.wally.anim.BaseItemAnimator.java
com.musenkishi.wally.anim.ScaleInOutItemAnimator.java
com.musenkishi.wally.anim.interpolator.EaseInOutBezierInterpolator.java
com.musenkishi.wally.anim.interpolator.FastOutLinearInInterpolator.java
com.musenkishi.wally.anim.interpolator.FastOutSlowInInterpolator.java
com.musenkishi.wally.anim.interpolator.LinearOutSlowInInterpolator.java
com.musenkishi.wally.base.BaseActivity.java
com.musenkishi.wally.base.BaseFragment.java
com.musenkishi.wally.base.GridFragment.java
com.musenkishi.wally.base.WallyApplication.java
com.musenkishi.wally.dataprovider.DataProvider.java
com.musenkishi.wally.dataprovider.FileManager.java
com.musenkishi.wally.dataprovider.NetworkDataProvider.java
com.musenkishi.wally.dataprovider.SharedPreferencesDataProvider.java
com.musenkishi.wally.dataprovider.models.DataProviderError.java
com.musenkishi.wally.dataprovider.models.SaveImageRequest.java
com.musenkishi.wally.dataprovider.okhttp.OkHttpStreamFetcher.java
com.musenkishi.wally.dataprovider.okhttp.OkHttpUrlLoader.java
com.musenkishi.wally.dataprovider.util.Parser.java
com.musenkishi.wally.fragments.CustomResolutionDialogFragment.java
com.musenkishi.wally.fragments.FilterDialogFragment.java
com.musenkishi.wally.fragments.ImageZoomFragment.java
com.musenkishi.wally.fragments.LatestFragment.java
com.musenkishi.wally.fragments.MaterialDialogFragment.java
com.musenkishi.wally.fragments.RandomImagesFragment.java
com.musenkishi.wally.fragments.SavedImagesFragment.java
com.musenkishi.wally.fragments.SearchFragment.java
com.musenkishi.wally.fragments.ToplistFragment.java
com.musenkishi.wally.models.Author.java
com.musenkishi.wally.models.ExceptionReporter.java
com.musenkishi.wally.models.Filter.java
com.musenkishi.wally.models.ImagePage.java
com.musenkishi.wally.models.Image.java
com.musenkishi.wally.models.ListFilterGroup.java
com.musenkishi.wally.models.Rating.java
com.musenkishi.wally.models.SavedImageData.java
com.musenkishi.wally.models.Size.java
com.musenkishi.wally.models.Tag.java
com.musenkishi.wally.models.filters.FilterAspectRatioKeys.java
com.musenkishi.wally.models.filters.FilterBoardsKeys.java
com.musenkishi.wally.models.filters.FilterBoards.java
com.musenkishi.wally.models.filters.FilterGroup.java
com.musenkishi.wally.models.filters.FilterGroupsStructure.java
com.musenkishi.wally.models.filters.FilterPurityKeys.java
com.musenkishi.wally.models.filters.FilterPurity.java
com.musenkishi.wally.models.filters.FilterResOptKeys.java
com.musenkishi.wally.models.filters.FilterResOpt.java
com.musenkishi.wally.models.filters.FilterResolutionKeys.java
com.musenkishi.wally.models.filters.FilterTimeSpanKeys.java
com.musenkishi.wally.muzei.WallyArtSource.java
com.musenkishi.wally.notification.NotificationProvider.java
com.musenkishi.wally.observers.FileChangeReceiver.java
com.musenkishi.wally.observers.FiltersChangeReceiver.java
com.musenkishi.wally.util.Blur.java
com.musenkishi.wally.util.PaletteLoader.java
com.musenkishi.wally.util.PaletteRequest.java
com.musenkishi.wally.util.SparseBooleanArrayParcelable.java
com.musenkishi.wally.util.TextClickableSpan.java
com.musenkishi.wally.util.TextLinkBuilder.java
com.musenkishi.wally.util.TypefaceSpan.java
com.musenkishi.wally.views.AutoGridView.java
com.musenkishi.wally.views.GridRecyclerView.java
com.musenkishi.wally.views.ObservableScrollView.java
com.musenkishi.wally.views.TabBarView.java
com.musenkishi.wally.views.TabView.java
com.musenkishi.wally.views.swipeclearlayout.SwipeClearLayout.java
net.margaritov.preference.colorpicker.dialog.ColorPickerDialogFragment.java
net.margaritov.preference.colorpicker.drawable.AlphaPatternDrawable.java
net.margaritov.preference.colorpicker.preference.ColorPickerPreference.java
net.margaritov.preference.colorpicker.view.ColorPanelView.java
net.margaritov.preference.colorpicker.view.ColorPickerView.java
nl.codesoup.cubicbezier.CubicBezierInterpolator.java