List of usage examples for org.jsoup.nodes Element attr
public String attr(String attributeKey)
From source file:org.commonjava.aprox.folo.ftest.urls.StoreOneAndSourceStoreUrlInHtmlListingTest.java
@Test public void storeOneFileAndVerifyItInParentDirectoryListing() throws Exception { final byte[] data = "this is a test".getBytes(); final ByteArrayInputStream stream = new ByteArrayInputStream(data); final String root = "/path/to/"; final String path = root + "foo.txt"; final String track = "track"; content.store(track, hosted, STORE, path, stream); final AproxClientHttp http = getHttp(); final HttpGet request = http.newRawGet(content.contentUrl(track, hosted, STORE, root)); request.addHeader("Accept", "text/html"); final CloseableHttpClient hc = http.newClient(); final CloseableHttpResponse response = hc.execute(request); final InputStream listing = response.getEntity().getContent(); final String html = IOUtils.toString(listing); // TODO: Charset!! final Document doc = Jsoup.parse(html); for (final Element item : doc.select("a.source-link")) { final String fname = item.text(); System.out.printf("Listing contains: '%s'\n", fname); final String href = item.attr("href"); final String expected = client.content().contentUrl(hosted, STORE); assertThat(fname + " does not have a href", href, notNullValue()); assertThat(fname + " has incorrect link: '" + href + "' (" + href.getClass().getName() + ")\nshould be: '" + expected + "' (String)", href, equalTo(expected)); }/*from ww w . j a va 2s. co m*/ }
From source file:org.commonjava.indy.folo.ftest.urls.StoreOneAndSourceStoreUrlInHtmlListingTest.java
@Test public void storeOneFileAndVerifyItInParentDirectoryListing() throws Exception { final byte[] data = "this is a test".getBytes(); final ByteArrayInputStream stream = new ByteArrayInputStream(data); final String root = "/path/to/"; final String path = root + "foo.txt"; final String track = "track"; content.store(track, hosted, STORE, path, stream); final IndyClientHttp http = getHttp(); final HttpGet request = http.newRawGet(content.contentUrl(track, hosted, STORE, root)); request.addHeader("Accept", "text/html"); final CloseableHttpClient hc = http.newClient(); final CloseableHttpResponse response = hc.execute(request); final InputStream listing = response.getEntity().getContent(); final String html = IOUtils.toString(listing); // TODO: Charset!! final Document doc = Jsoup.parse(html); for (final Element item : doc.select("a.source-link")) { final String fname = item.text(); System.out.printf("Listing contains: '%s'\n", fname); final String href = item.attr("href"); final String expected = client.content().contentUrl(hosted, STORE); assertThat(fname + " does not have a href", href, notNullValue()); assertThat(fname + " has incorrect link: '" + href + "' (" + href.getClass().getName() + ")\nshould be: '" + expected + "' (String)", href, equalTo(expected)); }//from w ww . j av a 2 s .co m }
From source file:neembuu.release1.externalImpl.linkhandler.SaveVideoYoutubeLinkHandlerProvider.java
private BasicLinkHandler.Builder saveVideoExtraction(TrialLinkHandler tlh, int retryCount) throws Exception { String url = tlh.getReferenceLinkString(); BasicLinkHandler.Builder linkHandlerBuilder = BasicLinkHandler.Builder.create(); try {// w w w.j a va2s. co m DefaultHttpClient httpClient = NHttpClient.getNewInstance(); String requestUrl = "http://www.save-video.com/download.php?url=" + URLEncoder.encode(url, "UTF-8"); final String responseString = NHttpClientUtils.getData(requestUrl, httpClient); //Set the group name as the name of the video String nameOfVideo = getVideoName(url); String fileName = "text"; linkHandlerBuilder.setGroupName(nameOfVideo); long c_duration = -1; Document doc = Jsoup.parse(responseString); Elements elements = doc.select(".sv-download-links ul li a"); for (Element element : elements) { String singleUrl = element.attr("href"); if (!singleUrl.startsWith("DownloadFile.php")) { fileName = element.text(); singleUrl = Utils.normalize(singleUrl); LOGGER.log(Level.INFO, "Normalized URL: {0}", singleUrl); long length = NHttpClientUtils.calculateLength(singleUrl, httpClient); //LOGGER.log(Level.INFO,"Length: " + length); if (length <= 0) { continue; /*skip this url*/ } BasicOnlineFile.Builder fileBuilder = linkHandlerBuilder.createFile(); try { // finding video/audio length // String dur = StringUtils.stringBetweenTwoStrings(singleUrl, "dur=", "&"); // long duration = (int)(Double.parseDouble(dur)*1000); // if(c_duration < 0 ){ c_duration = duration; } // fileBuilder.putLongPropertyValue(PropertyProvider.LongProperty.MEDIA_DURATION_IN_MILLISECONDS, duration); // LOGGER.log(Level.INFO,"dur="+dur); } catch (NumberFormatException a) { // ignore } try { // finding the quality short name // String type = fileName.substring(fileName.indexOf("(")+1); String type = fileName; fileBuilder.putStringPropertyValue(PropertyProvider.StringProperty.VARIANT_DESCRIPTION, type); LOGGER.log(Level.INFO, "type={0}", type); } catch (Exception a) { a.printStackTrace(); } fileName = nameOfVideo + " " + fileName; fileBuilder.setName(fileName).setUrl(singleUrl).setSize(length).next(); } } for (OnlineFile of : linkHandlerBuilder.getFiles()) { long dur = of.getPropertyProvider() .getLongPropertyValue(PropertyProvider.LongProperty.MEDIA_DURATION_IN_MILLISECONDS); if (dur < 0 && c_duration > 0 && of.getPropertyProvider() instanceof BasicPropertyProvider) { ((BasicPropertyProvider) of.getPropertyProvider()).putLongPropertyValue( PropertyProvider.LongProperty.MEDIA_DURATION_IN_MILLISECONDS, c_duration); } } } catch (Exception ex) { ex.printStackTrace(); } return linkHandlerBuilder; }
From source file:com.webcrawler.manager.impl.ImageManagerImpl.java
@Override public List<ImageDTO> getImageData(final String url) throws IOException, IllegalArgumentException, InterruptedException, ExecutionException { if (url == null || url.equals("")) { throw new IllegalArgumentException("Set URL first"); }/*from www. java 2 s . co m*/ Callable<List<ImageDTO>> callable = new Callable<List<ImageDTO>>() { @Override public List<ImageDTO> call() throws Exception { System.out.println("Retrieving image data from url " + url); Document document = null; Elements media = null; List<ImageDTO> images = new ArrayList<ImageDTO>(); try { document = Jsoup.connect(url).get(); media = document.select("[src]"); } catch (Exception e) { e.printStackTrace(); return images; } System.out.println("# of images: " + media.size()); for (Element src : media) { if (src.tagName().equals("img")) { ImageDTO dto = new ImageDTO(); dto.setUrlAddress(src.attr("abs:src")); dto.setFileName(getFileName(src.attr("abs:src"))); images.add(dto); } } return images; } }; Future<List<ImageDTO>> result = executorService.submit(callable); return result.get(); }
From source file:MySpaceParser.java
private void parseSingleFile(File file) throws Exception { Document htmlFile = null;/*w ww .ja va 2 s. c o m*/ try { htmlFile = Jsoup.parse(file, "ISO-8859-1"); } catch (Exception e) { e.printStackTrace(); } // Elements parents =htmlFile.getElementsByClass("cover"); Elements parents = htmlFile.getElementsByTag("section"); String title = "*^*"; String artist = "*^*"; String url = "*^*"; String imageurl = "*^*"; String pageTitle = "*^*"; String description = "*^*"; String songid = "*^*"; String genre = "*^*"; String album = "*^*"; String year = "*^*"; boolean isVideo = false; Elements titles = htmlFile.getElementsByTag("title"); Elements metas = htmlFile.getElementsByTag("meta"); for (Element meta : metas) { String name = meta.attr("name"); String prop = meta.attr("property"); if (prop.equals("og:video")) { System.out.println(); url = meta.attr("content"); String arr[] = url.split("/"); songid = arr[arr.length - 1]; title = arr[arr.length - 2]; artist = arr[arr.length - 4]; isVideo = true; } if (name.equals("description")) { // System.out.println(); description = meta.attr("content"); } } for (Element Pagetitle : titles) { pageTitle = Pagetitle.html(); // System.out.println(pageTitle); break; } if (isVideo) { SongData s = new SongData(title, url, album, artist, year, genre, imageurl); s.setPagetitle(pageTitle); s.setDescrption(description); index.put(songid, s); return; } if (parents.isEmpty() && !isVideo) { return; } else { // boolean isVideo = false; titles = htmlFile.getElementsByTag("title"); metas = htmlFile.getElementsByTag("meta"); for (Element meta : metas) { String name = meta.attr("name"); String prop = meta.attr("property"); if (prop.equals("og:video")) { System.out.println(); url = meta.attr("content"); String arr[] = url.split("/"); songid = arr[arr.length - 1]; isVideo = true; } if (name.equals("description")) { // System.out.println(); description = meta.attr("content"); } } for (Element Pagetitle : titles) { pageTitle = Pagetitle.html(); // System.out.println(pageTitle); break; } for (Element e : parents) { if (e.attr("id").equals("song")) { Elements e1 = e.children(); for (Element e2 : e1) { if (e2.attr("id").equals("actions")) { Elements e3 = e2.children(); int count = 0; for (Element e4 : e3) { if (count == 1) { songid = e4.attr("data-song-id"); album = e4.attr("data-album-title"); title = e4.attr("data-title"); artist = e4.attr("data-artist-name"); url = "www.myspace.com" + e4.attr("data-url"); genre = e4.attr("data-genre-name"); imageurl = e4.attr("data-image-url"); SongData s = new SongData(title, url, album, artist, year, genre, imageurl); s.setPagetitle(pageTitle); s.setDescrption(description); index.put(songid, s); } count++; } // System.out.println(); } } // System.out.println(e.attr("id")); } } //System.out.println(); } }
From source file:edu.rowan.app.carousel.CarouselFetch.java
@Override protected CarouselFeature[] doInBackground(Void... params) { String rowanURL = "http://rowan.edu"; ArrayList<CarouselFeature> cfeatures = new ArrayList<CarouselFeature>(); long lastUpdated = prefs.getLong(LAST_UPDATE, -1); if (lastUpdated > 0) { long timeDiff = Calendar.getInstance().getTimeInMillis() - lastUpdated; int hours = (int) (timeDiff / (60 * 60 * 1000)); if (hours < UPDATE_INTERVAL) { // just load saved features cfeatures.addAll(loadFeaturesFromPreferences()); // System.out.println("Loaded features from prefernces"); return cfeatures.toArray(new CarouselFeature[cfeatures.size()]); }//ww w .ja v a 2 s. c o m } // ELSE: Attempt to update // but check if we have available connection try { // Download + Parse Rowan's homepage for features //Toast.makeText(context, "Updating CarouselView", Toast.LENGTH_SHORT).show(); DOUH CAN"T DO THIS Document document = Jsoup.connect(rowanURL).get(); Elements features = document.select(".feature "); for (Element feature : features) { String title = feature.select(".title a span").first().text(); String description = feature.select(".description a").first().text(); Element link = feature.select("a").first(); String linkURL = link.attr("abs:href"); String imageURL = link.select("img").first().attr("abs:src"); CarouselFeature cFeature = new CarouselFeature(title, description, linkURL, imageURL, RECEIVER, context); cfeatures.add(cFeature); } saveDataToPreferences(cfeatures); } catch (IOException e1) { e1.printStackTrace(); return null; } return cfeatures.toArray(new CarouselFeature[cfeatures.size()]); }
From source file:br.ufsc.das.gtscted.shibbauth.ShibAuthenticationActivity.java
/** Called when the activity is first created. */ @Override/*from w w w. ja va 2s .com*/ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.idp_selection); loginButton = (Button) findViewById(R.id.loginButton); backButton = (Button) findViewById(R.id.backButton); usernameTxt = (EditText) findViewById(R.id.usernameTxt); passwordTxt = (EditText) findViewById(R.id.passwordTxt); idpSpinner = (Spinner) findViewById(R.id.idpSpinner); //Configura o ArrayAdapter do spinner. ArrayAdapter<CharSequence> spinnerArrayAdapter; spinnerArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item); spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); idpSpinner.setAdapter(spinnerArrayAdapter); // Obtm os parmetros passados pela Activity anterior // (no caso, a pgina do WAYF como uma String e o // nico cookie da Connection usada anteriormente) Bundle bundle = this.getIntent().getExtras(); String wayfHtml = bundle.getString("html_source"); final String wayfLocation = bundle.getString("wayf_location"); final SerializableCookie receivedCookie = (SerializableCookie) bundle.getSerializable("cookie"); //Obtm todos os tags de nome "option", que correspondem // aos IdPs, da pgina do WAYF. Document wayfDocument = Jsoup.parse(wayfHtml); idpElements = wayfDocument.select("option"); //Popula o spinner com os nomes dos IdPs encontrados. for (Element idpElement : idpElements) { String idpName = idpElement.text(); spinnerArrayAdapter.add(idpName); } // Obtm o caminho para o qual deve ser passado o IdP do usurio. formElements = wayfDocument.select("form"); for (Element formElement : formElements) { if (formElement.attr("id").equals("IdPList")) { wayfActionPath = formElement.attr("action"); } } loginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Obtm a URL correspondente ao idP selecionado no spinner. int selectedIdpPosition = idpSpinner.getSelectedItemPosition(); Element selectedIdp = idpElements.get(selectedIdpPosition); selectedIdpUrl = selectedIdp.attr("value"); try { // Obtm os campos "username" e "password" fornecidos // pelo usurio e necessrios para a autenticao. String username = usernameTxt.getText().toString(); String password = passwordTxt.getText().toString(); // Cria um novo objeto Connection, e adiciona o // cookie passado pela Activity anterior. Connection connection = new Connection(); BasicClientCookie newCookie = new BasicClientCookie(receivedCookie.getName(), receivedCookie.getValue()); newCookie.setDomain(receivedCookie.getDomain()); connection.addCookie(newCookie); // Tenta realizar a autenticao no IdP selecionado. O resultado corresponde // pgina para a qual o cliente redirecionado em caso de autenticao // bem-sucedida. String authResult = connection.authenticate(wayfLocation, wayfActionPath, selectedIdpUrl, username, password); // Apenas mostra o recurso que o usurio queria acessar (neste caso, mostra a pg. de // "Homologao de atributos"). Intent newIntent = new Intent(ShibAuthenticationActivity.this.getApplicationContext(), TestActivity.class); Bundle bundle = new Bundle(); bundle.putString("arg", authResult); newIntent.putExtras(bundle); startActivity(newIntent); } catch (IOException e) { String message = "IOException - problema na conexo"; Toast toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG); toast.show(); } catch (Exception e) { String message = "Exception - problema na autenticao"; Toast toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG); toast.show(); } } }); backButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); }
From source file:com.liato.bankdroid.banking.banks.Hemkop.java
@Override protected LoginPackage preLogin() throws BankException, ClientProtocolException, IOException { urlopen = new Urllib(context, CertificateReader.getCertificates(context, R.raw.cert_hemkop)); urlopen.setAllowCircularRedirects(true); response = urlopen.open("https://www.hemkop.se/Mina-sidor/Logga-in/"); Document d = Jsoup.parse(response); Element e = d.getElementById("__VIEWSTATE"); if (e == null || e.attr("value") == null) { throw new BankException(res.getText(R.string.unable_to_find).toString() + " ViewState."); }/*from ww w. j a v a 2 s .c om*/ String viewState = e.attr("value"); e = d.getElementById("__EVENTVALIDATION"); if (e == null || e.attr("value") == null) { throw new BankException(res.getText(R.string.unable_to_find).toString() + " EventValidation."); } String eventValidation = e.attr("value"); List<NameValuePair> postData = new ArrayList<NameValuePair>(); postData.add(new BasicNameValuePair("__EVENTTARGET", "ctl00$MainContent$BtnLogin")); postData.add(new BasicNameValuePair("__EVENTARGUMENT", "")); postData.add(new BasicNameValuePair("__VIEWSTATE", viewState)); postData.add(new BasicNameValuePair("__SCROLLPOSITIONX", "0")); postData.add(new BasicNameValuePair("__SCROLLPOSITIONY", "266")); postData.add(new BasicNameValuePair("__EVENTVALIDATION", eventValidation)); postData.add(new BasicNameValuePair("ctl00$uiTopMenu$Search", "")); postData.add(new BasicNameValuePair("ctl00$MainContent$tbUsername", username)); postData.add(new BasicNameValuePair("ctl00$MainContent$tbPassword", password)); return new LoginPackage(urlopen, postData, response, "https://www.hemkop.se/Mina-sidor/Logga-in/"); }
From source file:com.anhao.spring.service.impl.PhotosServiceImpl.java
@Override public void process(Document doc) { Elements links = doc.select("section ul li"); for (Element li : links) { Element figure = li.child(0); String wallpaperId = figure.attr("data-wallpaper-id"); /**//www .j a v a 2s. com * ??? */ String tempUUID = jobPhotosDAO.findByWallpaperId(wallpaperId); if (StringUtils.isNotEmpty(tempUUID)) { logger.info("wallpapers id {} thumbnail exist.", wallpaperId); //??wallpaperID? ??? continue; } String thumbnail = "http://alpha.wallhaven.cc/wallpapers/thumb/small/th-" + wallpaperId + ".jpg"; String full = "http://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-" + wallpaperId + ".jpg"; /** * fastdfs?fastdfs?/tmp/ */ boolean smallStatus = download(thumbnail, "/tmp/small" + wallpaperId + ".jpg"); boolean fullStatus = download(full, "/tmp/full" + wallpaperId + ".jpg"); if (smallStatus && fullStatus) { File thumbnailFile = new File("/tmp/small" + wallpaperId + ".jpg"); String thumbnailPath = storageService.upload(thumbnailFile); thumbnailFile.delete(); File sourceFile = new File("/tmp/full" + wallpaperId + ".jpg"); String sourceFilePath = storageService.upload(sourceFile); EasyImage easyImage = new EasyImage(sourceFile); //??? //sourceFile.delete(); String uuid = UUID.randomUUID().toString(); Photos photos = new Photos(); photos.setId(uuid); photos.setTitle(wallpaperId); photos.setWidth(easyImage.getWidth()); photos.setHeight(easyImage.getHeight()); photos.setSize(sourceFile.length()); photos.setCreate_date(new Date()); photos.setModify_date(new Date()); photos.setLarge(sourceFilePath); photos.setMedium(thumbnailPath); photos.setOrders(1); photos.setSource(sourceFilePath); photos.setThumbnail(thumbnailPath); photos.setAlbum_id("ff8081814f7e13d8014f7e18a95a0000"); photos.setMember_id("1"); photos.setWallhaven(wallpaperId); photos.setStorage_host("http://123.57.240.11"); jobPhotosDAO.add(photos); //2015-10-18 ? getWallpaperTags(wallpaperId); //2015-10-18 ? ? //? photosColorsService.generateColors(sourceFile, uuid); } else { logger.info("wallpapers id {} thumbnail or fullImage not exist.", wallpaperId); } } }
From source file:com.normalexception.app.rx8club.task.AdminTask.java
@Override protected Void doInBackground(Void... params) { try {/*www . j a v a 2 s . c om*/ Log.d(TAG, progressText.get(doType)); if (this.doType == DELETE_THREAD) { HtmlFormUtils.adminTypePost(doType, token, thread, deleteResponse); } else HtmlFormUtils.adminTypePost(doType, token, thread, null); if (this.doType == MOVE_THREAD) { String response = HtmlFormUtils.getResponseUrl(); Log.d(TAG, "Response: " + response); Document doc = Jsoup.parse(HtmlFormUtils.getResponseContent()); threadTitle = HtmlFormUtils.getInputElementValueByName(doc, "title"); Log.d(TAG, "Thread Title: " + threadTitle); Elements selects = doc.select("select[name=destforumid] > option"); for (Element select : selects) { selectOptions.put(select.text(), Integer.parseInt(select.attr("value"))); } Log.d(TAG, "Parsed " + selectOptions.keySet().size() + " options"); } } catch (ClientProtocolException e) { Log.e(TAG, e.getMessage(), e); } catch (IOException e) { Log.e(TAG, e.getMessage(), e); } return null; }