Search in sources :

Example 6 with AwfulError

use of com.ferg.awfulapp.util.AwfulError in project Awful.apk by Awful.

the class Reply method getReplyData.

public static final ContentValues getReplyData(Document data, ContentValues results) throws AwfulError {
    try {
        Element formKey = data.getElementsByAttributeValue("name", "formkey").first();
        Element formCookie = data.getElementsByAttributeValue("name", "form_cookie").first();
        results.put(AwfulPost.FORM_KEY, formKey.val());
        results.put(AwfulPost.FORM_COOKIE, formCookie.val());
    } catch (Exception e) {
        throw new AwfulError("Failed to load reply");
    }
    return results;
}
Also used : AwfulError(com.ferg.awfulapp.util.AwfulError) Element(org.jsoup.nodes.Element)

Example 7 with AwfulError

use of com.ferg.awfulapp.util.AwfulError in project Awful.apk by Awful.

the class AwfulMessage method processMessageList.

public static void processMessageList(ContentResolver contentInterface, Document data, int folder) throws AwfulError {
    ArrayList<ContentValues> msgList = new ArrayList<ContentValues>();
    /**
     *METHOD One: Parse PM links. Easy, but only contains id+title.*
     */
    /*TagNode[] messagesParent = data.getElementsByAttValue("name", "form", true, true);
		if(messagesParent.length > 0){
			TagNode[] messages = messagesParent[0].getElementsByName("a", true);
			for(TagNode msg : messages){
				String href = msg.getAttributeByName("href");
				if(href != null){
					AwfulMessage pm = new AwfulMessage(Integer.parseInt(href.replaceAll("\\D", "")));
					pm.mTitle = msg.getText().toString();
					pm.mAuthor = "";
					msgList.add(pm);
				}
			}
		}else{
			Log.e("AwfulMessage","Failed to parse message parent");
			return null;//we'll use this to show that the load failed. i am still lazy.
		}*/
    /**
     *METHOD Two: Parse table structure, hard and quick to break.*
     */
    Elements messagesParent = data.getElementsByAttributeValue("name", "form");
    if (messagesParent.size() > 0) {
        Elements messages = messagesParent.first().getElementsByTag("tr");
        for (Element msg : messages) {
            if (msg == messages.get(0)) {
                continue;
            }
            ContentValues pm = new ContentValues();
            // fuck i hate scraping shit.
            // no usable identifiers on the PM list, no easy method to find author/post date.
            // this will break if they change the display structure.
            Elements row = msg.getElementsByTag("td");
            if (row != null && row.size() > 4) {
                // TODO abandon hope, all ye who enter
                // row[0] - icon, newpm.gif - sublevel
                // row[1] - post icon TODO if we ever add icon support - sublevel
                // row[2] - pm subject/link - sublevel
                // row[3] - sender
                // row[4] - date
                Element href = row.get(2).getElementsByTag("a").first();
                pm.put(ID, Integer.parseInt(href.attr("href").replaceAll("\\D", "")));
                pm.put(TITLE, href.text());
                Element icon = row.get(1).getElementsByTag("img").first();
                if (icon == null) {
                    pm.put(ICON, "");
                } else {
                    pm.put(ICON, icon.attr("src"));
                }
                pm.put(AUTHOR, row.get(3).text());
                pm.put(DATE, row.get(4).text());
                pm.put(CONTENT, " ");
                if (row.first().getElementsByTag("img").first().attr("src").endsWith("newpm.gif")) {
                    pm.put(UNREAD, 1);
                } else if (row.first().getElementsByTag("img").first().attr("src").endsWith("pmreplied.gif")) {
                    pm.put(UNREAD, 2);
                } else {
                    pm.put(UNREAD, 0);
                }
                pm.put(FOLDER, folder);
                msgList.add(pm);
            }
        }
    } else {
        throw new AwfulError("Failed to parse message parent");
    }
    contentInterface.bulkInsert(CONTENT_URI, msgList.toArray(new ContentValues[msgList.size()]));
}
Also used : ContentValues(android.content.ContentValues) AwfulError(com.ferg.awfulapp.util.AwfulError) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) Elements(org.jsoup.select.Elements)

Example 8 with AwfulError

use of com.ferg.awfulapp.util.AwfulError in project Awful.apk by Awful.

the class ProfileRequest method handleResponse.

@Override
protected Void handleResponse(Document doc) throws AwfulError {
    Element formkey = doc.getElementsByAttributeValue("name", "formkey").first();
    if (formkey != null) {
        try {
            getPreferences().setPreference(Keys.IGNORE_FORMKEY, formkey.val());
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        throw new AwfulError("Profile page did not load");
    }
    Element title = doc.getElementsByClass("title").first();
    if (null != title) {
        String userTitle = "";
        Elements titleTags = title.getAllElements();
        for (Element tag : titleTags) {
            String tagName = tag.tagName();
            if ("br".equals(tagName)) {
                break;
            } else if ("img".equals(tagName)) {
                userTitle = tag.attr("src");
                break;
            }
        }
        try {
            getPreferences().setPreference(Keys.USER_TITLE, userTitle);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}
Also used : AwfulError(com.ferg.awfulapp.util.AwfulError) Element(org.jsoup.nodes.Element) Elements(org.jsoup.select.Elements)

Aggregations

AwfulError (com.ferg.awfulapp.util.AwfulError)8 Element (org.jsoup.nodes.Element)4 Elements (org.jsoup.select.Elements)4 ContentValues (android.content.ContentValues)2 RedirectTask (com.ferg.awfulapp.task.RedirectTask)2 AwfulURL (com.ferg.awfulapp.thread.AwfulURL)2 ArrayList (java.util.ArrayList)2 Activity (android.app.Activity)1 ProgressDialog (android.app.ProgressDialog)1 Intent (android.content.Intent)1 AsyncTask (android.os.AsyncTask)1 Bundle (android.os.Bundle)1 Snackbar (android.support.design.widget.Snackbar)1 DialogFragment (android.support.v4.app.DialogFragment)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 Html (android.text.Html)1 LayoutInflater (android.view.LayoutInflater)1 Menu (android.view.Menu)1 MenuInflater (android.view.MenuInflater)1