Example usage for org.jsoup.nodes Element attr

List of usage examples for org.jsoup.nodes Element attr

Introduction

In this page you can find the example usage for org.jsoup.nodes Element attr.

Prototype

public String attr(String attributeKey) 

Source Link

Document

Get an attribute's value by its key.

Usage

From source file:org.confab.PhpBB3Parser.java

public List<Forum> parseForums(Document root, BulletinBoard parent) {
    Utilities.debug("parseForums");

    List<Forum> ret = new ArrayList<Forum>();

    // get table//from ww  w .  jav  a2  s. c om
    Elements forum_tables = root.select("ul[class=topiclist forums]");
    assert !forum_tables.isEmpty() : root.html();

    for (Element forum_table : forum_tables) {
        Elements els_li = forum_table.select("li.row");
        assert !els_li.isEmpty();
        for (Element el_li : els_li) {
            Forum new_forum = new Forum(parent);

            // Get the forum url
            Elements els_a = el_li.select("a.forumtitle");
            Element el_a = els_a.first();
            assert el_a != null;
            new_forum.url = el_a.attr("href");
            assert new_forum.url != null;
            Utilities.debug("new_forum.url : " + new_forum.url);

            // Get the title text
            new_forum.title = el_a.text();
            assert new_forum.title != null;
            Utilities.debug("new_forum.title : " + new_forum.title);

            // Check for any subforums in remaining a elements
            els_a.remove(els_a.first());
            for (Element _el_a : els_a) {
                Forum sub_forum = new Forum(parent);
                sub_forum.url = el_a.attr("href");
                assert sub_forum.url != null;
                sub_forum.title = el_a.text();
                assert sub_forum.title != null;
                new_forum.subForums.add(sub_forum);
                Utilities.debug("added subForum: " + sub_forum.title);
            }

            // Get the description/message of this topic
            String el_description = el_a.parent().text();
            if (el_description != null) {
                new_forum.description = el_description;
            } else {
                new_forum.description = "";
            }
            Utilities.debug("new_forum.description : " + new_forum.description);

            Utilities.debug("new_forum.parent.url : " + new_forum.parent.url);

            ret.add(new_forum);
            Utilities.debug("-----");
        }
    }
    Utilities.debug("end parseForums");
    return ret;
}

From source file:org.javiermoreno.torrentscratcher.Runner.java

public Movie enrichMovieWithFilmAffinity(Movie movie) {
    try {/*from  w  w w . j a v a2s. c  o m*/
        String url = "http://www.filmaffinity.com/es/search.php?stext={title}&stype=all";
        String title = URLEncoder.encode(movie.getTitle(), "UTF8");
        url = url.replace("{title}", title);
        Document doc = Jsoup.connect(url).get();
        if (doc.select("[property=og:title]").size() == 0) {
            // several results found, take the first
            Element firstResult = doc.select(".item-search .mc-title a").first();
            if (firstResult == null) {
                // filmaffinity search engine failed
                log.warn("FilmAffinity 404: " + movie.getTitle());
                return movie;
            }
            url = "http://www.filmaffinity.com" + firstResult.attr("href");
            doc = Jsoup.connect(url).get();
        }
        movie.setFilmAffinityId(doc.select("div.rate-movie-box").attr("data-movie-id"));
        Elements movieInfo = doc.select("dl.movie-info");
        String originalTitle = movieInfo.select("dd").eq(0).text();
        originalTitle = originalTitle.replaceAll("\\([^\\(]*\\)", "").replaceAll("\\[[^\\(]*\\]", "")
                .replaceAll("aka$", "").trim();
        movie.setOriginalTitle(originalTitle);
        movie.setDescription(movieInfo.select("dd").eq(11).text());
    } catch (IOException ex) {
        log.warn(ex.getMessage());
    }
    return movie;
}

From source file:org.keycloak.testsuite.admin.concurrency.ConcurrentLoginTest.java

protected HttpUriRequest handleLogin(String html, String username, String password)
        throws UnsupportedEncodingException {
    log.debug("Extracting form's data...");

    // Keycloak form id
    Element loginform = Jsoup.parse(html).getElementById("kc-form-login");
    String method = loginform.attr("method");
    String action = loginform.attr("action");

    List<NameValuePair> paramList = new ArrayList<>();

    for (Element inputElement : loginform.getElementsByTag("input")) {
        String key = inputElement.attr("name");

        if (key.equals("username")) {
            paramList.add(new BasicNameValuePair(key, username));
        } else if (key.equals("password")) {
            paramList.add(new BasicNameValuePair(key, password));
        }// w w  w.j av a  2s. c  o m
    }

    boolean isPost = method != null && "post".equalsIgnoreCase(method);

    if (isPost) {
        HttpPost req = new HttpPost(action);

        UrlEncodedFormEntity formEntity;
        try {
            formEntity = new UrlEncodedFormEntity(paramList, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        req.setEntity(formEntity);

        return req;
    } else {
        throw new UnsupportedOperationException("not supported yet!");
    }
}

From source file:eu.masconsult.bgbanking.banks.dskbank.DskClient.java

@Override
public String authenticate(String username, String password)
        throws IOException, ParseException, CaptchaException {
    final HttpResponse resp;
    final ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair(PARAM_USERNAME, username));
    params.add(new BasicNameValuePair(PARAM_PASSWORD, password));
    final HttpEntity entity;
    try {/*from w ww. ja v  a  2 s  .  c o m*/
        entity = new UrlEncodedFormEntity(params);
    } catch (final UnsupportedEncodingException e) {
        // this should never happen.
        throw new IllegalStateException(e);
    }
    String uri = BASE_URL + "?"
            + URLEncodedUtils.format(Arrays.asList(new BasicNameValuePair(XML_ID, AUTH_XML_ID)), ENCODING);
    Log.i(TAG, "Authenticating to: " + uri);
    final HttpPost post = new HttpPost(uri);
    post.addHeader(entity.getContentType());
    post.setHeader("Accept", "*/*");
    post.setEntity(entity);
    try {
        resp = getHttpClient().execute(post);

        if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            throw new ParseException("login: unhandled http status " + resp.getStatusLine().getStatusCode()
                    + " " + resp.getStatusLine().getReasonPhrase());
        }

        String response = EntityUtils.toString(resp.getEntity());
        Log.v(TAG, "response = " + response);

        Document doc = Jsoup.parse(response, BASE_URL);
        Element mainForm = doc.getElementById("mainForm");
        if (mainForm == null) {
            throw new ParseException("login: missing mainForm");
        }

        String action = BASE_URL + mainForm.attr("action");
        Log.v(TAG, "action=" + action);
        UrlQuerySanitizer sanitizer = new UrlQuerySanitizer(action);
        String user_id = sanitizer.getValue(PARAM_USER_ID);
        String session_id = sanitizer.getValue(PARAM_SESSION_ID);

        if (user_id == null || "".equals(user_id) || session_id == null || "".equals(session_id)) {
            if (doc.getElementsByClass("redtext").size() > 0) {
                // bad authentication
                return null;
            } else {
                // TODO handle captcha
                Elements captcha = doc.select("input[name=captcha_hkey]");
                if (captcha != null && captcha.size() == 1) {
                    String captchaHash = captcha.first().attr("value");
                    String captchaUri = BASE_URL + "?"
                            + URLEncodedUtils
                                    .format(Arrays.asList(new BasicNameValuePair(XML_ID, CAPTCHA_XML_ID),
                                            new BasicNameValuePair("captcha_key", captchaHash)), ENCODING);
                    throw new CaptchaException(captchaUri);
                }
                throw new ParseException("no user_id or session_id: " + action);
            }
        }

        return URLEncodedUtils.format(Arrays.asList(new BasicNameValuePair(PARAM_USER_ID, user_id),
                new BasicNameValuePair(PARAM_SESSION_ID, session_id)), ENCODING);
    } catch (ClientProtocolException e) {
        throw new IOException(e.getMessage());
    }
}

From source file:net.parser.JobParser.java

public List<Job> getFeaturedJobIdAndLink(List<Job> jobs) {

    Job job = null;//from w ww . j  a va 2  s .  co  m
    Employer employer = null;
    Elements elements = doc.select("#featured-jobs li");

    for (Element jobElement : elements) {

        employer = new Employer();
        Elements empElements = jobElement.select(".logo");

        if (empElements.size() == 1) {
            employer.setName(empElements.get(0).attr("title"));
            employer.setId(0);
        } else {
            String linkEmployer = empElements.get(0).attr("href");
            employer.setLink(linkEmployer);

            linkEmployer = linkEmployer.substring(linkEmployer.indexOf("/") + 1, linkEmployer.length());
            linkEmployer = linkEmployer.substring(linkEmployer.indexOf("/") + 1, linkEmployer.length());
            linkEmployer = linkEmployer.substring(linkEmployer.indexOf("/") + 1, linkEmployer.length());
            linkEmployer = linkEmployer.substring(linkEmployer.indexOf("/") + 1, linkEmployer.length());
            String id = linkEmployer.substring(0, linkEmployer.indexOf("/"));

            employer.setId(Integer.parseInt(id));
            employer.setName(empElements.get(1).attr("title"));
        }

        Elements aElements = jobElement.select("span a");
        for (Element aJob : aElements) {
            job = new Job();
            String linkJob = aJob.attr("href");
            linkJob = linkJob.replaceFirst("www", "m");
            job.setLink(linkJob);

            String id = aJob.attr("data-id");
            job.setId(Integer.parseInt(id));

            job.setEmployer(employer);
            jobs.add(job);
        }
    }
    return jobs;
}

From source file:com.app.rest.ExperianIntegrationService.java

@POST
@Path("/questionForCustomer")
@Produces({ MediaType.APPLICATION_JSON })
@Consumes(MediaType.APPLICATION_JSON)//from  www .j  a  v  a2s.c  o  m
public ResponseModel getQuestion(String inputJsonObj) {

    ResponseModel responseMap = new ResponseModel();

    //String requestParams = (String) inputJsonObj.get("input");      

    String message = "";
    String jsessionId2 = "";
    String responseJson = null;
    String logMarker = null;
    try {
        Map map = parseJson(inputJsonObj);
        logMarker = map.get("LOG_MARKER").toString();
        jsessionId2 = map.get("jsessionId2").toString();

        logger.info("getQuestion  ~ " + (map.get("LOG_MARKER") == null ? "NOT_GIVEN" : map.get("LOG_MARKER"))
                + " ~ jsessionId2: " + (jsessionId2 == null ? "null" : jsessionId2) + "~Log Marker 1");

        while (true) {

            ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();

            params.add(new BasicNameValuePair("answer", map.get("answer").toString()));
            params.add(new BasicNameValuePair("questionId", map.get("qid").toString()));
            params.add(new BasicNameValuePair("stgOneHitId", map.get("stgOneHitId").toString()));
            params.add(new BasicNameValuePair("stgTwoHitId", map.get("stgTwoHitId").toString()));
            String request = getQuery(params);
            Map questionMap = HttpConnection.generateQuestionForConsumer(jsessionId2, request);

            responseJson = (String) questionMap.get("responseJson");

            if (responseJson.equalsIgnoreCase("passedReport")) {
                String pdfData = (String) questionMap.get("showHtmlReportForCreditReport");
                Document doc = Jsoup.parse(pdfData);
                Element input = doc.select("input[name=xmlResponse]").first();
                String response = input.attr("value");
                responseMap.setErrorMessage("Success");
                responseMap.setXmlResponse(response);
                responseMap.setTotalResponse(pdfData);
            }

            if (responseJson.equalsIgnoreCase("next")) {
                questionMap.put("jsessionId2", jsessionId2);
                responseMap.setResponseMap(questionMap);
            }

            if (responseJson.equalsIgnoreCase("systemError")) {
                responseMap.setErrorMessage("systemError");
            }

            if (responseJson.equalsIgnoreCase("inCorrectAnswersGiven")) {
                responseMap.setErrorMessage("inCorrectAnswersGiven");
            }

            if (responseJson.equalsIgnoreCase("insufficientQuestion")) {
                responseMap.setErrorMessage("insufficientQuestion");
            }

            if (responseJson.equalsIgnoreCase("error") || responseJson.equalsIgnoreCase("creditReportEmpty")) {
                responseMap.setErrorMessage("creditReportEmpty");
            }
            return responseMap;
        }

    } catch (Exception e) {
        logger.info("getQuestion  ~ " + (logMarker == null ? "NOT_GIVEN" : logMarker) + " ~ jsessionId2: "
                + (jsessionId2 == null ? "null" : jsessionId2) + "~Log Marker 2");
        responseMap.setErrorMessage("Error occured");
        responseMap.setExceptionString(e.toString());
        return responseMap;
    }

}

From source file:prince.app.ccm.tools.Task.java

public String getFormParams(String html, String username, String password) throws UnsupportedEncodingException {

    System.out.println("Extracting form's data...");

    Document doc = Jsoup.parse(html);

    // Google form id
    Element loginform = doc.getElementById("contenido_right");
    Elements loginaction = doc.getElementsByTag("form");
    Element form = loginaction.first();
    log = MAIN_PAGE + form.attr("action");
    Log.e(TAG, "Action: " + log);
    Elements inputElements = loginform.getElementsByTag("input");
    List<String> paramList = new ArrayList<String>();
    for (Element inputElement : inputElements) {
        String key = inputElement.attr("name");
        String value = inputElement.attr("value");

        if (key.equals("usuario")) {
            value = username;//from   ww  w.  j  a  va  2 s.  co m
            paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8"));
        } else if (key.equals("contrasena")) {
            value = password;
            paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8"));
        }
    }

    // build parameters list
    StringBuilder result = new StringBuilder();
    for (String param : paramList) {
        if (result.length() == 0) {
            result.append(param);
        } else {
            result.append("&" + param);
        }
    }

    Log.d(TAG, "Done in getFormParams: " + result.toString());
    return result.toString();
}

From source file:com.wheelermarine.android.publicAccesses.Updater.java

@Override
protected Integer doInBackground(URL... urls) {

    try {//from  w ww .  j  a va2  s . c  o  m
        final DatabaseHelper db = new DatabaseHelper(context);

        SQLiteDatabase database = db.getWritableDatabase();
        if (database == null)
            throw new IllegalStateException("Unable to open database!");

        database.beginTransaction();
        try {
            // Clear out the old data.
            database.delete(DatabaseHelper.PublicAccessEntry.TABLE_NAME, null, null);

            // Connect to the web server and locate the FTP download link.
            Log.v(TAG, "Finding update: " + urls[0]);
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    progress.setMessage("Locating update...");
                    progress.setIndeterminate(true);
                }
            });
            Document doc = Jsoup.connect(urls[0].toString()).timeout(timeout * 1000).userAgent(userAgent).get();
            URL dataURL = null;
            for (Element element : doc.select("a")) {
                if (element.hasAttr("href") && element.attr("href").startsWith("ftp://ftp.dnr.state.mn.us")) {
                    dataURL = new URL(element.attr("href"));
                }
            }

            // Make sure the download URL was fund.
            if (dataURL == null)
                throw new FileNotFoundException("Unable to locate data URL.");

            // Connect to the FTP server and download the update.
            Log.v(TAG, "Downloading update: " + dataURL);
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    progress.setMessage("Downloading update...");
                    progress.setIndeterminate(true);
                }
            });
            FTPClient ftp = new FTPClient();
            try {
                ftp.setConnectTimeout(timeout * 1000);
                ftp.setDefaultTimeout(timeout * 1000);
                ftp.connect(dataURL.getHost());
                ftp.enterLocalPassiveMode();

                // After connection attempt, you should check the reply code
                // to verify success.
                if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                    ftp.disconnect();
                    throw new IOException("FTP server refused connection: " + ftp.getReplyString());
                }

                // Login using the standard anonymous credentials.
                if (!ftp.login("anonymous", "anonymous")) {
                    ftp.disconnect();
                    throw new IOException("FTP Error: " + ftp.getReplyString());
                }

                Map<Integer, Location> locations = null;

                // Download the ZIP archive.
                Log.v(TAG, "Downloading: " + dataURL.getFile());
                ftp.setFileType(FTP.BINARY_FILE_TYPE);
                InputStream in = ftp.retrieveFileStream(dataURL.getFile());
                if (in == null)
                    throw new FileNotFoundException(dataURL.getFile() + " was not found!");
                try {
                    ZipInputStream zin = new ZipInputStream(in);
                    try {
                        // Locate the .dbf entry in the ZIP archive.
                        ZipEntry entry;
                        while ((entry = zin.getNextEntry()) != null) {
                            if (entry.getName().endsWith(entryName)) {
                                readDBaseFile(zin, database);
                            } else if (entry.getName().endsWith(shapeEntryName)) {
                                locations = readShapeFile(zin);
                            }
                        }
                    } finally {
                        try {
                            zin.close();
                        } catch (Exception e) {
                            // Ignore this error.
                        }
                    }
                } finally {
                    in.close();
                }

                if (locations != null) {
                    final int recordCount = locations.size();
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            progress.setIndeterminate(false);
                            progress.setMessage("Updating locations...");
                            progress.setMax(recordCount);
                        }
                    });

                    int progress = 0;
                    for (int recordNumber : locations.keySet()) {
                        PublicAccess access = db.getPublicAccessByRecordNumber(recordNumber);
                        Location loc = locations.get(recordNumber);
                        access.setLatitude(loc.getLatitude());
                        access.setLongitude(loc.getLongitude());
                        db.updatePublicAccess(access);
                        publishProgress(++progress);
                    }
                }
            } finally {
                if (ftp.isConnected())
                    ftp.disconnect();
            }
            database.setTransactionSuccessful();
            return db.getPublicAccessesCount();
        } finally {
            database.endTransaction();
        }
    } catch (Exception e) {
        error = e;
        Log.e(TAG, "Error loading data: " + e.getLocalizedMessage(), e);
        return -1;
    }
}

From source file:de.geeksfactory.opacclient.apis.Littera.java

protected SearchRequestResult executeSearch(List<SearchQuery> query, int pageIndex)
        throws IOException, OpacErrorException, JSONException {
    final String searchUrl;
    if (!initialised) {
        start();/*w w  w .  j  a va  2s .  c  o  m*/
    }
    try {
        searchUrl = buildSearchUrl(query, pageIndex);
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
    final String html = httpGet(searchUrl, getDefaultEncoding());
    final Document doc = Jsoup.parse(html);

    final Element navigation = doc.select(".result_view .navigation").first();
    final int totalResults = navigation != null ? parseTotalResults(navigation.text()) : 0;

    final Element ul = doc.select(".result_view ul.list").first();
    final List<SearchResult> results = new ArrayList<>();
    for (final Element li : ul.children()) {
        if (li.hasClass("zugangsmonat")) {
            continue;
        }
        final SearchResult result = new SearchResult();
        final Element title = li.select(".titelinfo a").first();
        result.setId(getQueryParamsFirst(title.attr("href")).get("id"));
        result.setInnerhtml(title.text() + "<br>" + title.parent().nextElementSibling().text());
        result.setNr(results.size());
        result.setPage(pageIndex);
        result.setType(MEDIA_TYPES.get(li.select(".statusinfo .ma").text()));
        result.setCover(getCover(li));
        final String statusImg = li.select(".status img").attr("src");
        result.setStatus(statusImg.contains("-yes") ? SearchResult.Status.GREEN
                : statusImg.contains("-no") ? SearchResult.Status.RED : null);
        results.add(result);
    }
    return new SearchRequestResult(results, totalResults, pageIndex);
}