Search in sources :

Example 1 with UrlBean

use of org.aisen.weibo.sina.sinasdk.bean.UrlBean in project AisenWeiBo by wangdan.

the class VideoPlayerActivity method playWithShort.

private void playWithShort(final String url) {
    shortUri = Uri.parse(url);
    Logger.d(TAG, "short : " + url);
    new WorkTask<Void, Void, VideoBean>() {

        @Override
        protected void onPrepare() {
            super.onPrepare();
            layoutContent.setVisibility(View.GONE);
            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public VideoBean workInBackground(Void... params) throws TaskException {
            String id = KeyGenerator.generateMD5(url);
            // VideoBean videoBean = SinaDB.getDB().selectById(null, VideoBean.class, id);
            // if (videoBean != null && !TextUtils.isEmpty(videoBean.getImage()) && !TextUtils.isEmpty(videoBean.getVideoUrl())) {
            // return videoBean;
            // }
            UrlsBean urlsBean = SinaSDK.getInstance(AppContext.getAccount().getAccessToken()).urlShort2Long(url);
            if (urlsBean != null && urlsBean.getUrls() != null && urlsBean.getUrls().size() > 0) {
                UrlBean urlBean = urlsBean.getUrls().get(0);
                VideoBean videoBean = new VideoBean();
                videoBean.setIdStr(KeyGenerator.generateMD5(url));
                videoBean.setShortUrl(urlBean.getUrl_short());
                videoBean.setLongUrl(urlBean.getUrl_long());
                int repeat = 8;
                while (repeat-- > 0) {
                    try {
                        if (VideoService.isSinaVideo(urlBean.getUrl_long())) {
                            videoBean.setType(VideoService.TYPE_VIDEO_SINA);
                            videoBean = VideoService.getVideoFromSinaVideo(videoBean);
                        } else if (VideoService.isWeipai(urlBean.getUrl_long())) {
                            videoBean.setType(VideoService.TYPE_VIDEO_WEIPAI);
                            videoBean = VideoService.getVideoFromWeipai(videoBean);
                        } else if (VideoService.isMeipai(urlBean.getUrl_long())) {
                            videoBean.setType(VideoService.TYPE_VIDEO_MEIPAI);
                            videoBean = VideoService.getVideoFromMeipai(videoBean);
                        }
                        repeat = 0;
                        if (!TextUtils.isEmpty(videoBean.getVideoUrl())) {
                            if (!TextUtils.isEmpty(videoBean.getImage())) {
                                SinaDB.getDB().update(null, videoBean);
                            }
                            return videoBean;
                        }
                    } catch (Throwable e) {
                        e.printStackTrace();
                    }
                }
            }
            throw new TaskException("解析短链接失败");
        }

        @Override
        protected void onSuccess(VideoBean videoBean) {
            super.onSuccess(videoBean);
            playWithVideo(url, videoBean);
        }

        @Override
        protected void onFailure(TaskException exception) {
            super.onFailure(exception);
            if (isDestory())
                return;
            new MaterialDialog.Builder(VideoPlayerActivity.this).forceStacking(true).content(R.string.video_short_faild).positiveText(R.string.video2browser).negativeText(R.string.video_again).onPositive(new MaterialDialog.SingleButtonCallback() {

                @Override
                public void onClick(MaterialDialog dialog, DialogAction which) {
                    toBrowser(url);
                }
            }).onNegative(new MaterialDialog.SingleButtonCallback() {

                @Override
                public void onClick(MaterialDialog dialog, DialogAction which) {
                    playWithShort(url);
                }
            }).show();
        }
    }.execute();
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) TaskException(org.aisen.android.network.task.TaskException) UrlBean(org.aisen.weibo.sina.sinasdk.bean.UrlBean) VideoBean(org.aisen.weibo.sina.support.bean.VideoBean) DialogAction(com.afollestad.materialdialogs.DialogAction) UrlsBean(org.aisen.weibo.sina.sinasdk.bean.UrlsBean)

Example 2 with UrlBean

use of org.aisen.weibo.sina.sinasdk.bean.UrlBean in project AisenWeiBo by wangdan.

the class VideoService method parseCommentURL.

public static void parseCommentURL(List<StatusComment> comments) throws TaskException {
    if (comments.size() == 0)
        return;
    List<String> shortUrlList = new ArrayList<>();
    Map<String, List<StatusComment>> url2status = new HashMap<>();
    // 把未解析的短连接拎出来
    for (StatusComment comment : comments) {
        String content = comment.getText();
        if (!TextUtils.isEmpty(content)) {
            SpannableString spannableString = SpannableString.valueOf(content);
            Linkify.addLinks(spannableString, Pattern.compile("http://[a-zA-Z0-9+&@#/%?=~_\\-|!:,\\.;]*[a-zA-Z0-9+&@#/%=~_|]"), "http://");
            URLSpan[] urlSpans = spannableString.getSpans(0, spannableString.length(), URLSpan.class);
            for (URLSpan urlSpan : urlSpans) {
                if (!urlSpan.getURL().startsWith("http://t.cn/"))
                    continue;
                shortUrlList.add(urlSpan.getURL());
                List<StatusComment> l = url2status.get(urlSpan.getURL());
                if (l == null) {
                    l = new ArrayList<>();
                    url2status.put(urlSpan.getURL(), l);
                }
                if (!l.contains(comment))
                    l.add(comment);
            }
        }
    }
    if (shortUrlList.size() > 0) {
        do {
            String[] parseArr = new String[20];
            for (int i = 0; i < parseArr.length; i++) {
                if (shortUrlList.size() > 0) {
                    parseArr[i] = shortUrlList.remove(0);
                }
            }
            Logger.w(TAG, parseArr);
            UrlsBean urlsBean = SinaSDK.getInstance(AppContext.getAccount().getAccessToken()).urlShort2Long(parseArr);
            for (UrlBean urlBean : urlsBean.getUrls()) {
                String id = KeyGenerator.generateMD5(urlBean.getUrl_short());
                List<StatusComment> statusList = url2status.get(urlBean.getUrl_short());
                for (StatusComment s : statusList) {
                    s.setVideoUrl(urlBean);
                    VideoBean videoBean = SinaDB.getDB().selectById(null, VideoBean.class, id);
                    boolean isNew = false;
                    if (videoBean == null) {
                        videoBean = new VideoBean();
                        isNew = true;
                    }
                    videoBean.setIdStr(id);
                    videoBean.setShortUrl(urlBean.getUrl_short());
                    videoBean.setLongUrl(urlBean.getUrl_long());
                    if (isSinaVideo(urlBean.getUrl_long())) {
                        videoBean.setType(VideoService.TYPE_VIDEO_SINA);
                        s.setPicture(true);
                    } else if (isWeipai(urlBean.getUrl_long())) {
                        videoBean.setType(VideoService.TYPE_VIDEO_WEIPAI);
                        s.setPicture(true);
                    } else if (isPhoto(urlBean.getUrl_long())) {
                        videoBean.setType(VideoService.TYPE_PHOTO);
                        s.setPicture(true);
                    } else {
                        videoBean.setType(VideoService.TYPE_VIDEO_NONE);
                    }
                    if (isNew) {
                        SinaDB.getDB().insert(null, videoBean);
                    } else {
                        SinaDB.getDB().update(null, videoBean);
                    }
                    Logger.v(TAG, "Id[%s], Type[%d], 短链[%s], 长链[%s]", videoBean.getIdStr(), videoBean.getType(), urlBean.getUrl_short(), urlBean.getUrl_long());
                }
            }
        } while (shortUrlList.size() > 0);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SpannableString(android.text.SpannableString) URLSpan(android.text.style.URLSpan) SpannableString(android.text.SpannableString) UrlBean(org.aisen.weibo.sina.sinasdk.bean.UrlBean) VideoBean(org.aisen.weibo.sina.support.bean.VideoBean) StatusComment(org.aisen.weibo.sina.sinasdk.bean.StatusComment) ArrayList(java.util.ArrayList) List(java.util.List) UrlsBean(org.aisen.weibo.sina.sinasdk.bean.UrlsBean)

Example 3 with UrlBean

use of org.aisen.weibo.sina.sinasdk.bean.UrlBean in project AisenWeiBo by wangdan.

the class VideoService method parseStatusURL.

public static void parseStatusURL(List<StatusContent> statusContents) throws TaskException {
    if (statusContents.size() == 0)
        return;
    List<String> shortUrlList = new ArrayList<>();
    Map<String, List<StatusContent>> url2status = new HashMap<>();
    // 把未解析的短连接拎出来
    for (StatusContent statusContent : statusContents) {
        String content = statusContent.getText();
        if (!TextUtils.isEmpty(content)) {
            SpannableString spannableString = SpannableString.valueOf(content);
            Linkify.addLinks(spannableString, Pattern.compile("http://[a-zA-Z0-9+&@#/%?=~_\\-|!:,\\.;]*[a-zA-Z0-9+&@#/%=~_|]"), "http://");
            URLSpan[] urlSpans = spannableString.getSpans(0, spannableString.length(), URLSpan.class);
            for (URLSpan urlSpan : urlSpans) {
                if (!urlSpan.getURL().startsWith("http://t.cn/"))
                    continue;
                shortUrlList.add(urlSpan.getURL());
                List<StatusContent> l = url2status.get(urlSpan.getURL());
                if (l == null) {
                    l = new ArrayList<>();
                    url2status.put(urlSpan.getURL(), l);
                }
                if (!l.contains(statusContent))
                    l.add(statusContent);
            }
        }
        if (statusContent.getRetweeted_status() != null) {
            statusContent = statusContent.getRetweeted_status();
            content = statusContent.getText();
            if (!TextUtils.isEmpty(content)) {
                SpannableString spannableString = SpannableString.valueOf(content);
                Linkify.addLinks(spannableString, Pattern.compile("http://[a-zA-Z0-9+&@#/%?=~_\\-|!:,\\.;]*[a-zA-Z0-9+&@#/%=~_|]"), "http://");
                URLSpan[] urlSpans = spannableString.getSpans(0, spannableString.length(), URLSpan.class);
                for (URLSpan urlSpan : urlSpans) {
                    if (!urlSpan.getURL().startsWith("http://t.cn/"))
                        continue;
                    shortUrlList.add(urlSpan.getURL());
                    List<StatusContent> l = url2status.get(urlSpan.getURL());
                    if (l == null) {
                        l = new ArrayList<>();
                        url2status.put(urlSpan.getURL(), l);
                    }
                    if (!l.contains(statusContent))
                        l.add(statusContent);
                }
            }
        }
    }
    if (shortUrlList.size() > 0) {
        do {
            String[] parseArr = new String[20];
            for (int i = 0; i < parseArr.length; i++) {
                if (shortUrlList.size() > 0) {
                    parseArr[i] = shortUrlList.remove(0);
                }
            }
            Logger.w(TAG, parseArr);
            UrlsBean urlsBean = SinaSDK.getInstance(AppContext.getAccount().getAccessToken()).urlShort2Long(parseArr);
            for (UrlBean urlBean : urlsBean.getUrls()) {
                String id = KeyGenerator.generateMD5(urlBean.getUrl_short());
                List<StatusContent> statusList = url2status.get(urlBean.getUrl_short());
                for (StatusContent s : statusList) {
                    s.setVideoUrl(urlBean);
                    boolean isNew = false;
                    VideoBean videoBean = SinaDB.getDB().selectById(null, VideoBean.class, id);
                    if (videoBean == null) {
                        videoBean = new VideoBean();
                        isNew = true;
                    }
                    videoBean.setIdStr(id);
                    videoBean.setShortUrl(urlBean.getUrl_short());
                    videoBean.setLongUrl(urlBean.getUrl_long());
                    if (isSinaVideo(urlBean.getUrl_long())) {
                        videoBean.setType(VideoService.TYPE_VIDEO_SINA);
                        s.setVideo(true);
                    } else if (isWeipai(urlBean.getUrl_long())) {
                        videoBean.setType(VideoService.TYPE_VIDEO_WEIPAI);
                        s.setVideo(true);
                    } else if (isMeipai(urlBean.getUrl_long())) {
                        videoBean.setType(VideoService.TYPE_VIDEO_MEIPAI);
                        s.setVideo(true);
                    } else if (isPhoto(urlBean.getUrl_long())) {
                        videoBean.setType(VideoService.TYPE_PHOTO);
                    } else {
                        videoBean.setType(VideoService.TYPE_VIDEO_NONE);
                    }
                    if (isNew) {
                        SinaDB.getDB().insert(null, videoBean);
                    } else {
                        SinaDB.getDB().update(null, videoBean);
                    }
                    Logger.v(TAG, "Id[%s], Type[%d], 短链[%s], 长链[%s]", videoBean.getIdStr(), videoBean.getType(), urlBean.getUrl_short(), urlBean.getUrl_long());
                }
            }
        } while (shortUrlList.size() > 0);
    }
}
Also used : StatusContent(org.aisen.weibo.sina.sinasdk.bean.StatusContent) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SpannableString(android.text.SpannableString) URLSpan(android.text.style.URLSpan) SpannableString(android.text.SpannableString) UrlBean(org.aisen.weibo.sina.sinasdk.bean.UrlBean) VideoBean(org.aisen.weibo.sina.support.bean.VideoBean) ArrayList(java.util.ArrayList) List(java.util.List) UrlsBean(org.aisen.weibo.sina.sinasdk.bean.UrlsBean)

Aggregations

UrlBean (org.aisen.weibo.sina.sinasdk.bean.UrlBean)3 UrlsBean (org.aisen.weibo.sina.sinasdk.bean.UrlsBean)3 VideoBean (org.aisen.weibo.sina.support.bean.VideoBean)3 SpannableString (android.text.SpannableString)2 URLSpan (android.text.style.URLSpan)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 DialogAction (com.afollestad.materialdialogs.DialogAction)1 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)1 TaskException (org.aisen.android.network.task.TaskException)1 StatusComment (org.aisen.weibo.sina.sinasdk.bean.StatusComment)1 StatusContent (org.aisen.weibo.sina.sinasdk.bean.StatusContent)1