<commit_before>/*
 * Copyright 2016 Chaos
 *
 * 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 org.chaos.fx.cnbeta.details;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;

import org.chaos.fx.cnbeta.net.model.NewsContent;
import org.chaos.fx.cnbeta.wxapi.WXApiProvider;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;

import java.util.Locale;

import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

import static org.chaos.fx.cnbeta.details.ContentFragment.KEY_COMMENT_COUNT;
import static org.chaos.fx.cnbeta.details.ContentFragment.KEY_HTML_CONTENT;
import static org.chaos.fx.cnbeta.details.ContentFragment.KEY_SID;
import static org.chaos.fx.cnbeta.details.ContentFragment.KEY_TOPIC_LOGO;

/**
 * @author Chaos
 *         10/26/16
 */

public class ContentPresenter implements ContentContract.Presenter {

    private int mSid;
    private String mLogoLink;
    private String mHtmlContent;
    private int mCommentCount;

    private NewsContent mNewsContent;
    private ContentContract.View mView;


    private Subscription mContentSubscription;

    public ContentPresenter(Bundle arguments, ContentContract.View view) {
        mSid = arguments.getInt(KEY_SID);
        mLogoLink = arguments.getString(KEY_TOPIC_LOGO);
        mHtmlContent = arguments.getString(KEY_HTML_CONTENT);
        mCommentCount = arguments.getInt(KEY_COMMENT_COUNT);
        mView = view;
    }

    @Override
    public void shareUrlToWechat(Bitmap bitmap, boolean toTimeline) {
        WXApiProvider.shareUrl(String.format(Locale.getDefault(), "http://m.cnbeta.com/view_%d.htm", mSid),
                mNewsContent.getTitle(),
                Jsoup.parseBodyFragment(mNewsContent.getHomeText()).text(),
                bitmap, toTimeline);
    }

    @Override
    public void subscribe() {
        loadContent();
    }

    @Override
    public void unsubscribe() {
        mView.clearViewInContent();
        mContentSubscription.unsubscribe();
    }

    private void loadContent() {
        mContentSubscription = Observable.just(mHtmlContent)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, NewsContent>() {
                    @Override
                    public NewsContent call(String html) {
                        NewsContent newsContent = parseHtmlContent(html);
                        newsContent.setCommentCount(mCommentCount);
                        return newsContent;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<NewsContent>() {
                    @Override
                    public void call(NewsContent newsContent) {
                        mNewsContent = newsContent;
                        parseNewsContent(newsContent);
                    }
                });
    }

    private NewsContent parseHtmlContent(String html) {
        Element body = Jsoup.parse(html).body();
        String title = body.getElementById("news_title").text();
        String source = body.select("span.where").text();
        source = source.substring(3, source.length());
        String time = body.select("span.date").text();
        String homeText = body.select("div.introduction > p").text();
        String thumb = body.select("a > img[title]").attr("src").replace("http://static.<EMAIL>", "");
        String bodyText = body.getElementsByClass("content").html();
        String author = body.getElementsByClass("author").text();<commit_msg>移除 cnBeta 网页版正文的文字广告<commit_after>/*
 * Copyright 2016 Chaos
 *
 * 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 org.chaos.fx.cnbeta.details;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;

import org.chaos.fx.cnbeta.net.model.NewsContent;
import org.chaos.fx.cnbeta.wxapi.WXApiProvider;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;

import java.util.Locale;

import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

import static org.chaos.fx.cnbeta.details.ContentFragment.KEY_COMMENT_COUNT;
import static org.chaos.fx.cnbeta.details.ContentFragment.KEY_HTML_CONTENT;
import static org.chaos.fx.cnbeta.details.ContentFragment.KEY_SID;
import static org.chaos.fx.cnbeta.details.ContentFragment.KEY_TOPIC_LOGO;

/**
 * @author Chaos
 *         10/26/16
 */

public class ContentPresenter implements ContentContract.Presenter {

    private int mSid;
    private String mLogoLink;
    private String mHtmlContent;
    private int mCommentCount;

    private NewsContent mNewsContent;
    private ContentContract.View mView;


    private Subscription mContentSubscription;

    public ContentPresenter(Bundle arguments, ContentContract.View view) {
        mSid = arguments.getInt(KEY_SID);
        mLogoLink = arguments.getString(KEY_TOPIC_LOGO);
        mHtmlContent = arguments.getString(KEY_HTML_CONTENT);
        mCommentCount = arguments.getInt(KEY_COMMENT_COUNT);
        mView = view;
    }

    @Override
    public void shareUrlToWechat(Bitmap bitmap, boolean toTimeline) {
        WXApiProvider.shareUrl(String.format(Locale.getDefault(), "http://m.cnbeta.com/view_%d.htm", mSid),
                mNewsContent.getTitle(),
                Jsoup.parseBodyFragment(mNewsContent.getHomeText()).text(),
                bitmap, toTimeline);
    }

    @Override
    public void subscribe() {
        loadContent();
    }

    @Override
    public void unsubscribe() {
        mView.clearViewInContent();
        mContentSubscription.unsubscribe();
    }

    private void loadContent() {
        mContentSubscription = Observable.just(mHtmlContent)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, NewsContent>() {
                    @Override
                    public NewsContent call(String html) {
                        NewsContent newsContent = parseHtmlContent(html);
                        newsContent.setCommentCount(mCommentCount);
                        return newsContent;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<NewsContent>() {
                    @Override
                    public void call(NewsContent newsContent) {
                        mNewsContent = newsContent;
                        parseNewsContent(newsContent);
                    }
                });
    }

    private NewsContent parseHtmlContent(String html) {
        Element body = Jsoup.parse(html).body();
        String title = body.getElementById("news_title").text();
        String source = body.select("span.where").text();
        source = source.substring(3, source.length());
        String time = body.select("span.date").text();
        String homeText = body.select("div.introduction > p").text();
        String thumb = body.select("a > img[title]").attr("src").replace("http://static.cnbetacdn.com", "");

        Element contentElement = body.getElementsByClass("content").first();
        int elementSize = contentElement.childNodes().size();
        for (int i = elementSize - 1; i >= elementSize - 3; i--) {// 移除广告
            contentElement.childNodes().get(i).remove();
        }
        String bodyText = contentElement.html();

        String author = body.getElementsByClass("author").text();
{'fraction_non_alphanumeric': 0.10121638210021203, 'fraction_numerical': 0.004463787523713871, 'mean_word_length': 2.7830308146897424, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2143566', 'n_tokens_mistral': 2496, 'n_tokens_neox': 2429, 'n_words': 552}