Search in sources :

Example 1 with ReaderIframeScanner

use of org.wordpress.android.ui.reader.utils.ReaderIframeScanner in project WordPress-Android by wordpress-mobile.

the class ReaderPostRenderer method resizeIframes.

/*
     * scan the content for iframes and make sure they're correctly sized for the device
     */
private void resizeIframes() {
    ReaderHtmlUtils.HtmlScannerListener iframeListener = new ReaderHtmlUtils.HtmlScannerListener() {

        @Override
        public void onTagFound(String tag, String src) {
            replaceIframeTag(tag, src);
        }
    };
    ReaderIframeScanner scanner = new ReaderIframeScanner(mRenderBuilder.toString());
    scanner.beginScan(iframeListener);
}
Also used : ReaderIframeScanner(org.wordpress.android.ui.reader.utils.ReaderIframeScanner) ReaderHtmlUtils(org.wordpress.android.ui.reader.utils.ReaderHtmlUtils)

Example 2 with ReaderIframeScanner

use of org.wordpress.android.ui.reader.utils.ReaderIframeScanner in project WordPress-Android by wordpress-mobile.

the class ReaderPost method fromJson.

public static ReaderPost fromJson(JSONObject json) {
    if (json == null) {
        throw new IllegalArgumentException("null json post");
    }
    ReaderPost post = new ReaderPost();
    post.postId = json.optLong("ID");
    post.blogId = json.optLong("site_ID");
    post.feedId = json.optLong("feed_ID");
    post.feedItemId = json.optLong("feed_item_ID");
    if (json.has("pseudo_ID")) {
        // read/ endpoint
        post.pseudoId = JSONUtils.getString(json, "pseudo_ID");
    } else {
        // sites/ endpoint
        post.pseudoId = JSONUtils.getString(json, "global_ID");
    }
    // remove HTML from the excerpt
    post.excerpt = HtmlUtils.fastStripHtml(JSONUtils.getString(json, "excerpt")).trim();
    post.text = JSONUtils.getString(json, "content");
    post.title = JSONUtils.getStringDecoded(json, "title");
    post.format = JSONUtils.getString(json, "format");
    post.url = JSONUtils.getString(json, "URL");
    post.shortUrl = JSONUtils.getString(json, "short_URL");
    post.setBlogUrl(JSONUtils.getString(json, "site_URL"));
    post.numLikes = json.optInt("like_count");
    post.isLikedByCurrentUser = JSONUtils.getBool(json, "i_like");
    post.isFollowedByCurrentUser = JSONUtils.getBool(json, "is_following");
    post.isExternal = JSONUtils.getBool(json, "is_external");
    post.isPrivate = JSONUtils.getBool(json, "site_is_private");
    post.isJetpack = JSONUtils.getBool(json, "is_jetpack");
    JSONObject jsonDiscussion = json.optJSONObject("discussion");
    if (jsonDiscussion != null) {
        post.isCommentsOpen = JSONUtils.getBool(jsonDiscussion, "comments_open");
        post.numReplies = jsonDiscussion.optInt("comment_count");
    } else {
        post.isCommentsOpen = JSONUtils.getBool(json, "comments_open");
        post.numReplies = json.optInt("comment_count");
    }
    // parse the author section
    assignAuthorFromJson(post, json.optJSONObject("author"));
    post.featuredImage = JSONUtils.getString(json, "featured_image");
    post.blogName = JSONUtils.getStringDecoded(json, "site_name");
    post.datePublished = JSONUtils.getString(json, "date");
    post.dateLiked = JSONUtils.getString(json, "date_liked");
    post.dateTagged = JSONUtils.getString(json, "tagged_on");
    // "score" only exists for search results
    post.score = json.optDouble("score");
    // if the post is untitled, make up a title from the excerpt
    if (!post.hasTitle() && post.hasExcerpt()) {
        post.title = extractTitle(post.excerpt, 50);
    }
    // remove html from title (rare, but does happen)
    if (post.hasTitle() && post.title.contains("<") && post.title.contains(">")) {
        post.title = HtmlUtils.stripHtml(post.title);
    }
    // parse the tags section
    assignTagsFromJson(post, json.optJSONObject("tags"));
    // parse the attachments
    JSONObject jsonAttachments = json.optJSONObject("attachments");
    if (jsonAttachments != null && jsonAttachments.length() > 0) {
        post.attachmentsJson = jsonAttachments.toString();
    }
    // site metadata - returned when ?meta=site was added to the request
    JSONObject jsonSite = JSONUtils.getJSONChild(json, "meta/data/site");
    if (jsonSite != null) {
        post.blogId = jsonSite.optInt("ID");
        post.blogName = JSONUtils.getString(jsonSite, "name");
        post.setBlogUrl(JSONUtils.getString(jsonSite, "URL"));
        post.isPrivate = JSONUtils.getBool(jsonSite, "is_private");
        JSONObject jsonSiteIcon = jsonSite.optJSONObject("icon");
        if (jsonSiteIcon != null) {
            post.blogImageUrl = JSONUtils.getString(jsonSiteIcon, "img");
        }
        // TODO: as of 29-Sept-2014, this is broken - endpoint returns false when it should be true
        post.isJetpack = JSONUtils.getBool(jsonSite, "jetpack");
    }
    // "discover" posts
    JSONObject jsonDiscover = json.optJSONObject("discover_metadata");
    if (jsonDiscover != null) {
        post.setDiscoverJson(jsonDiscover.toString());
    }
    // xpost info
    assignXpostIdsFromJson(post, json.optJSONArray("metadata"));
    // we can find a suitable image from the content
    if (!post.hasFeaturedImage() && post.hasImages()) {
        post.featuredImage = new ReaderImageScanner(post.text, post.isPrivate).getLargestImage(ReaderConstants.MIN_FEATURED_IMAGE_WIDTH);
    }
    // the content for a suitable featured video
    if (!post.hasFeaturedImage() && !post.hasFeaturedVideo() && post.getText().contains("<iframe")) {
        post.setFeaturedVideo(new ReaderIframeScanner(post.getText()).getFirstUsableVideo());
    }
    // "railcar" data - currently used in search streams, used by TrainTracks
    JSONObject jsonRailcar = json.optJSONObject("railcar");
    if (jsonRailcar != null) {
        post.setRailcarJson(jsonRailcar.toString());
    }
    // set the card type last since it depends on information contained in the post - note
    // that this is stored in the post table rather than calculated on-the-fly
    post.setCardType(ReaderCardType.fromReaderPost(post));
    return post;
}
Also used : ReaderIframeScanner(org.wordpress.android.ui.reader.utils.ReaderIframeScanner) JSONObject(org.json.JSONObject) ReaderImageScanner(org.wordpress.android.ui.reader.utils.ReaderImageScanner)

Aggregations

ReaderIframeScanner (org.wordpress.android.ui.reader.utils.ReaderIframeScanner)2 JSONObject (org.json.JSONObject)1 ReaderHtmlUtils (org.wordpress.android.ui.reader.utils.ReaderHtmlUtils)1 ReaderImageScanner (org.wordpress.android.ui.reader.utils.ReaderImageScanner)1