Search in sources :

Example 1 with GalleryTagGroup

use of com.hippo.ehviewer.client.data.GalleryTagGroup in project EhViewer by seven332.

the class GalleryDetailScene method bindTags.

@SuppressWarnings("deprecation")
private void bindTags(GalleryTagGroup[] tagGroups) {
    Context context = getContext2();
    LayoutInflater inflater = getLayoutInflater2();
    Resources resources = getResources2();
    if (null == context || null == inflater || null == resources || null == mTags || null == mNoTags) {
        return;
    }
    mTags.removeViews(1, mTags.getChildCount() - 1);
    if (tagGroups == null || tagGroups.length == 0) {
        mNoTags.setVisibility(View.VISIBLE);
        return;
    } else {
        mNoTags.setVisibility(View.GONE);
    }
    EhTagDatabase ehTags = Settings.getShowTagTranslations() ? EhTagDatabase.getInstance(context) : null;
    int colorTag = AttrResources.getAttrColor(context, R.attr.tagBackgroundColor);
    int colorName = AttrResources.getAttrColor(context, R.attr.tagGroupBackgroundColor);
    for (GalleryTagGroup tg : tagGroups) {
        LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.gallery_tag_group, mTags, false);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        mTags.addView(ll);
        String readableTagName = null;
        if (ehTags != null) {
            readableTagName = ehTags.getTranslation("n:" + tg.groupName);
        }
        TextView tgName = (TextView) inflater.inflate(R.layout.item_gallery_tag, ll, false);
        ll.addView(tgName);
        tgName.setText(readableTagName != null ? readableTagName : tg.groupName);
        tgName.setBackgroundDrawable(new RoundSideRectDrawable(colorName));
        String prefix = EhTagDatabase.namespaceToPrefix(tg.groupName);
        if (prefix == null) {
            prefix = "";
        }
        AutoWrapLayout awl = new AutoWrapLayout(context);
        ll.addView(awl, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        for (int j = 0, z = tg.size(); j < z; j++) {
            TextView tag = (TextView) inflater.inflate(R.layout.item_gallery_tag, awl, false);
            awl.addView(tag);
            String tagStr = tg.getTagAt(j);
            String readableTag = null;
            if (ehTags != null) {
                readableTag = ehTags.getTranslation(prefix + tagStr);
            }
            tag.setText(readableTag != null ? readableTag : tagStr);
            tag.setBackgroundDrawable(new RoundSideRectDrawable(colorTag));
            tag.setTag(R.id.tag, tg.groupName + ":" + tagStr);
            tag.setOnClickListener(this);
            tag.setOnLongClickListener(this);
        }
    }
}
Also used : Context(android.content.Context) GalleryTagGroup(com.hippo.ehviewer.client.data.GalleryTagGroup) EhTagDatabase(com.hippo.ehviewer.client.EhTagDatabase) RoundSideRectDrawable(com.hippo.drawable.RoundSideRectDrawable) AutoWrapLayout(com.hippo.widget.AutoWrapLayout) LayoutInflater(android.view.LayoutInflater) TextView(android.widget.TextView) ObservedTextView(com.hippo.widget.ObservedTextView) AttrResources(com.hippo.android.resource.AttrResources) Resources(android.content.res.Resources) SuppressLint(android.annotation.SuppressLint) LinearLayout(android.widget.LinearLayout)

Example 2 with GalleryTagGroup

use of com.hippo.ehviewer.client.data.GalleryTagGroup in project EhViewer by seven332.

the class GalleryDetailParser method parseTagGroups.

/**
 * Parse tag groups with regular expressions
 */
@NonNull
private static GalleryTagGroup[] parseTagGroups(String body) throws EhException {
    List<GalleryTagGroup> list = new LinkedList<>();
    Matcher m = PATTERN_TAG_GROUP.matcher(body);
    while (m.find()) {
        GalleryTagGroup tagGroup = new GalleryTagGroup();
        tagGroup.groupName = ParserUtils.trim(m.group(1));
        parseGroup(tagGroup, m.group(0));
        list.add(tagGroup);
    }
    return list.toArray(new GalleryTagGroup[list.size()]);
}
Also used : GalleryTagGroup(com.hippo.ehviewer.client.data.GalleryTagGroup) Matcher(java.util.regex.Matcher) LinkedList(java.util.LinkedList) NonNull(androidx.annotation.NonNull)

Example 3 with GalleryTagGroup

use of com.hippo.ehviewer.client.data.GalleryTagGroup in project EhViewer by seven332.

the class GalleryDetailParser method parseTagGroup.

@Nullable
private static GalleryTagGroup parseTagGroup(Element element) {
    try {
        GalleryTagGroup group = new GalleryTagGroup();
        String nameSpace = element.child(0).text();
        // Remove last ':'
        nameSpace = nameSpace.substring(0, nameSpace.length() - 1);
        group.groupName = nameSpace;
        Elements tags = element.child(1).children();
        for (int i = 0, n = tags.size(); i < n; i++) {
            String tag = tags.get(i).text();
            // Sometimes parody tag is followed with '|' and english translate, just remove them
            int index = tag.indexOf('|');
            if (index >= 0) {
                tag = tag.substring(0, index).trim();
            }
            group.addTag(tag);
        }
        return group.size() > 0 ? group : null;
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        e.printStackTrace();
        return null;
    }
}
Also used : GalleryTagGroup(com.hippo.ehviewer.client.data.GalleryTagGroup) Elements(org.jsoup.select.Elements) Nullable(androidx.annotation.Nullable)

Example 4 with GalleryTagGroup

use of com.hippo.ehviewer.client.data.GalleryTagGroup in project EhViewer by seven332.

the class GalleryListParser method parseGalleryInfo.

private static GalleryInfo parseGalleryInfo(Element e) {
    GalleryInfo gi = new GalleryInfo();
    // Title, gid, token (required), tags
    Element glname = JsoupUtils.getElementByClass(e, "glname");
    if (glname != null) {
        Element a = JsoupUtils.getElementByTag(glname, "a");
        if (a == null) {
            Element parent = glname.parent();
            if (parent != null && "a".equals(parent.tagName())) {
                a = parent;
            }
        }
        if (a != null) {
            GalleryDetailUrlParser.Result result = GalleryDetailUrlParser.parse(a.attr("href"));
            if (result != null) {
                gi.gid = result.gid;
                gi.token = result.token;
            }
        }
        Element child = glname;
        Elements children = glname.children();
        while (children.size() != 0) {
            child = children.get(0);
            children = child.children();
        }
        gi.title = child.text().trim();
        Element tbody = JsoupUtils.getElementByTag(glname, "tbody");
        if (tbody != null) {
            ArrayList<String> tags = new ArrayList<>();
            GalleryTagGroup[] groups = GalleryDetailParser.parseTagGroups(tbody.children());
            for (GalleryTagGroup group : groups) {
                for (int j = 0; j < group.size(); j++) {
                    tags.add(group.groupName + ":" + group.getTagAt(j));
                }
            }
            gi.simpleTags = tags.toArray(new String[tags.size()]);
        }
    }
    if (gi.title == null) {
        return null;
    }
    // Category
    gi.category = EhUtils.UNKNOWN;
    Element ce = JsoupUtils.getElementByClass(e, "cn");
    if (ce == null) {
        ce = JsoupUtils.getElementByClass(e, "cs");
    }
    if (ce != null) {
        gi.category = EhUtils.getCategory(ce.text());
    }
    // Thumb
    Element glthumb = JsoupUtils.getElementByClass(e, "glthumb");
    if (glthumb != null) {
        Element img = glthumb.select("div:nth-child(1)>img").first();
        if (img != null) {
            // Thumb size
            Matcher m = PATTERN_THUMB_SIZE.matcher(img.attr("style"));
            if (m.find()) {
                gi.thumbWidth = NumberUtils.parseIntSafely(m.group(2), 0);
                gi.thumbHeight = NumberUtils.parseIntSafely(m.group(1), 0);
            } else {
                Log.w(TAG, "Can't parse gallery info thumb size");
                gi.thumbWidth = 0;
                gi.thumbHeight = 0;
            }
            // Thumb url
            String url = img.attr("data-src");
            if (TextUtils.isEmpty(url)) {
                url = img.attr("src");
            }
            if (TextUtils.isEmpty(url)) {
                url = null;
            }
            gi.thumb = EhUtils.handleThumbUrlResolution(url);
        }
        // Pages
        Element div = glthumb.select("div:nth-child(2)>div:nth-child(2)>div:nth-child(2)").first();
        if (div != null) {
            Matcher matcher = PATTERN_PAGES.matcher(div.text());
            if (matcher.find()) {
                gi.pages = NumberUtils.parseIntSafely(matcher.group(1), 0);
            }
        }
    }
    // Try extended and thumbnail version
    if (gi.thumb == null) {
        Element gl = JsoupUtils.getElementByClass(e, "gl1e");
        if (gl == null) {
            gl = JsoupUtils.getElementByClass(e, "gl3t");
        }
        if (gl != null) {
            Element img = JsoupUtils.getElementByTag(gl, "img");
            if (img != null) {
                // Thumb size
                Matcher m = PATTERN_THUMB_SIZE.matcher(img.attr("style"));
                if (m.find()) {
                    gi.thumbWidth = NumberUtils.parseIntSafely(m.group(2), 0);
                    gi.thumbHeight = NumberUtils.parseIntSafely(m.group(1), 0);
                } else {
                    Log.w(TAG, "Can't parse gallery info thumb size");
                    gi.thumbWidth = 0;
                    gi.thumbHeight = 0;
                }
                gi.thumb = EhUtils.handleThumbUrlResolution(img.attr("src"));
            }
        }
    }
    // Posted
    gi.favoriteSlot = -2;
    Element posted = e.getElementById("posted_" + gi.gid);
    if (posted != null) {
        gi.posted = posted.text().trim();
        gi.favoriteSlot = parseFavoriteSlot(posted.attr("style"));
    }
    if (gi.favoriteSlot == -2) {
        gi.favoriteSlot = EhDB.containLocalFavorites(gi.gid) ? -1 : -2;
    }
    // Rating
    Element ir = JsoupUtils.getElementByClass(e, "ir");
    if (ir != null) {
        gi.rating = NumberUtils.parseFloatSafely(parseRating(ir.attr("style")), -1.0f);
        // TODO The gallery may be rated even if it doesn't has one of these classes
        gi.rated = ir.hasClass("irr") || ir.hasClass("irg") || ir.hasClass("irb");
    }
    // Uploader and pages
    Element gl = JsoupUtils.getElementByClass(e, "glhide");
    int uploaderIndex = 0;
    int pagesIndex = 1;
    if (gl == null) {
        // For extended
        gl = JsoupUtils.getElementByClass(e, "gl3e");
        uploaderIndex = 3;
        pagesIndex = 4;
    }
    if (gl != null) {
        Elements children = gl.children();
        if (children.size() > uploaderIndex) {
            Element a = children.get(uploaderIndex).children().first();
            if (a != null) {
                gi.uploader = a.text().trim();
            }
        }
        if (children.size() > pagesIndex) {
            Matcher matcher = PATTERN_PAGES.matcher(children.get(pagesIndex).text());
            if (matcher.find()) {
                gi.pages = NumberUtils.parseIntSafely(matcher.group(1), 0);
            }
        }
    }
    // For thumbnail
    Element gl5t = JsoupUtils.getElementByClass(e, "gl5t");
    if (gl5t != null) {
        Element div = gl5t.select("div:nth-child(2)>div:nth-child(2)").first();
        if (div != null) {
            Matcher matcher = PATTERN_PAGES.matcher(div.text());
            if (matcher.find()) {
                gi.pages = NumberUtils.parseIntSafely(matcher.group(1), 0);
            }
        }
    }
    gi.generateSLang();
    return gi;
}
Also used : GalleryTagGroup(com.hippo.ehviewer.client.data.GalleryTagGroup) Matcher(java.util.regex.Matcher) Element(org.jsoup.nodes.Element) GalleryInfo(com.hippo.ehviewer.client.data.GalleryInfo) ArrayList(java.util.ArrayList) Elements(org.jsoup.select.Elements)

Aggregations

GalleryTagGroup (com.hippo.ehviewer.client.data.GalleryTagGroup)4 Matcher (java.util.regex.Matcher)2 Elements (org.jsoup.select.Elements)2 SuppressLint (android.annotation.SuppressLint)1 Context (android.content.Context)1 Resources (android.content.res.Resources)1 LayoutInflater (android.view.LayoutInflater)1 LinearLayout (android.widget.LinearLayout)1 TextView (android.widget.TextView)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1 AttrResources (com.hippo.android.resource.AttrResources)1 RoundSideRectDrawable (com.hippo.drawable.RoundSideRectDrawable)1 EhTagDatabase (com.hippo.ehviewer.client.EhTagDatabase)1 GalleryInfo (com.hippo.ehviewer.client.data.GalleryInfo)1 AutoWrapLayout (com.hippo.widget.AutoWrapLayout)1 ObservedTextView (com.hippo.widget.ObservedTextView)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 Element (org.jsoup.nodes.Element)1