Search in sources :

Example 1 with AwfulPost

use of com.ferg.awfulapp.thread.AwfulPost in project Awful.apk by Awful.

the class AnnouncementsFragment method showAnnouncements.

/**
 * Fire off the network request to get and show the current announcements.
 * Not caching these in the DB! Maybe later! Probably not!!
 */
private void showAnnouncements() {
    Context context = getContext().getApplicationContext();
    statusFrog.setStatusText(R.string.announcements_status_fetching).showSpinner(true);
    queueRequest(new AnnouncementsRequest(context).build(this, new AwfulRequest.AwfulResultCallback<List<AwfulPost>>() {

        @Override
        public void success(List<AwfulPost> result) {
            AnnouncementsManager.getInstance().markAllRead();
            // update the status frog if there are no announcements, otherwise hide it and display them
            if (result.size() < 1) {
                statusFrog.setStatusText(R.string.announcements_status_none).showSpinner(false);
            } else {
                webView.setVisibility(View.VISIBLE);
                // these page params don't mean anything in the context of the announcement page
                // we just want it to a) display ok, and b) not let the user click anything bad
                bodyHtml = ThreadDisplay.getHtml(result, AwfulPreferences.getInstance(), 1, 1);
                if (webView != null) {
                    webView.refreshPageContents(true);
                }
                statusFrog.setVisibility(View.INVISIBLE);
            }
        }

        @Override
        public void failure(VolleyError error) {
            statusFrog.setStatusText(R.string.announcements_status_failed).showSpinner(false);
            Timber.w("Announcement get failed!\n" + error.getMessage());
        }
    }));
}
Also used : Context(android.content.Context) VolleyError(com.android.volley.VolleyError) AwfulPost(com.ferg.awfulapp.thread.AwfulPost) AnnouncementsRequest(com.ferg.awfulapp.task.AnnouncementsRequest) List(java.util.List)

Example 2 with AwfulPost

use of com.ferg.awfulapp.thread.AwfulPost in project Awful.apk by Awful.

the class AnnouncementsRequest method parseAnnouncement.

@NonNull
private static List<AwfulPost> parseAnnouncement(@NonNull Document aThread) {
    List<AwfulPost> results = new ArrayList<>();
    AwfulPreferences prefs = AwfulPreferences.getInstance();
    // grab all the main announcement sections - these contain *most* of the data we need :/
    Elements mainAnnouncements = aThread.select("#main_full  tr[valign='top']");
    Log.d(TAG, "parseAnnouncement: found" + mainAnnouncements.size() + " announcements");
    for (Element announcementSection : mainAnnouncements) {
        AwfulPost announcement = new AwfulPost();
        Element author = announcementSection.select(".author").first();
        if (author != null) {
            announcement.setUsername(author.text());
        }
        Element regDate = announcementSection.select(".registered").first();
        if (regDate != null) {
            announcement.setRegDate(regDate.text());
        }
        Element avatar = announcementSection.select(".title img").first();
        if (avatar != null) {
            tryConvertToHttps(avatar);
            announcement.setAvatar(avatar.attr("src"));
        }
        // not sure if this ever appears for announcements but whatever, may as well
        Element editedBy = announcementSection.select(".editedby").first();
        if (editedBy != null) {
            announcement.setEdited("<i>" + editedBy.text() + "</i>");
        }
        // announcements have their post date in a whole other section directly after the announcement section
        Element postDateSection = announcementSection.nextElementSibling();
        if (postDateSection != null) {
            Element postDate = postDateSection.select(".postdate").first();
            if (postDate != null) {
                announcement.setDate(postDate.text());
            }
        }
        Element postBody = announcementSection.select(".postbody").first();
        if (postBody != null) {
            // process videos, images and links and store the resulting post HTML
            AwfulPost.convertVideos(postBody, prefs.inlineYoutube);
            for (Element image : postBody.getElementsByTag("img")) {
                AwfulPost.processPostImage(image, false, prefs);
            }
            for (Element link : postBody.getElementsByTag("a")) {
                tryConvertToHttps(link);
            }
            announcement.setContent(postBody.html());
        }
        // I guess this is important...?
        announcement.setEditable(false);
        results.add(announcement);
        Log.i(TAG, Integer.toString(mainAnnouncements.size()) + " posts found, " + results.size() + " posts parsed.");
    }
    return results;
}
Also used : AwfulPost(com.ferg.awfulapp.thread.AwfulPost) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) Elements(org.jsoup.select.Elements) AwfulPreferences(com.ferg.awfulapp.preferences.AwfulPreferences) NonNull(android.support.annotation.NonNull)

Aggregations

AwfulPost (com.ferg.awfulapp.thread.AwfulPost)2 Context (android.content.Context)1 NonNull (android.support.annotation.NonNull)1 VolleyError (com.android.volley.VolleyError)1 AwfulPreferences (com.ferg.awfulapp.preferences.AwfulPreferences)1 AnnouncementsRequest (com.ferg.awfulapp.task.AnnouncementsRequest)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Element (org.jsoup.nodes.Element)1 Elements (org.jsoup.select.Elements)1