
br.com.objectos.bvmf.fii.FiiWgetGuice Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bvmf Show documentation
Show all versions of bvmf Show documentation
Coletores de informações do site BM&F Bovespa.
The newest version!
/*
* Copyright 2013 Objectos, Fábrica de Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package br.com.objectos.bvmf.fii;
import static com.google.common.collect.Lists.transform;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import br.com.objectos.core.util.concurrent.MoreExecutors;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.inject.Inject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author [email protected] (Marcio Endo)
*/
class FiiWgetGuice implements FiiWget {
private static final Logger logger = LoggerFactory.getLogger(FiiWget.class);
private final ExecutorService executor;
private final FiiUrl url;
@Inject
public FiiWgetGuice(FiiUrl url) {
executor = MoreExecutors.newCoreSizedNamed(4, "fii-wget");
this.url = url;
}
@Override
public List connect() {
try {
String _url = url.get();
Document doc = Jsoup.connect(_url).get();
List links;
links = new ListagemParser(doc).get();
List> lazyFutures;
lazyFutures = transform(links, new ToFuture());
List> futures;
futures = ImmutableList.copyOf(lazyFutures);
List fiis;
fiis = transform(futures, new ToFii());
return ImmutableList.copyOf(fiis);
} catch (IOException e) {
return ImmutableList.of();
}
}
private class ToFuture implements Function> {
@Override
public Future apply(FiiLink link) {
Get get = new Get(link);
return executor.submit(get);
}
}
private class ToFii implements Function, Fii> {
@Override
public Fii apply(Future future) {
try {
return future.get();
} catch (InterruptedException e) {
return new FiiErro();
} catch (ExecutionException e) {
return new FiiErro();
}
}
}
private static class Get implements Callable {
private final FiiLink link;
public Get(FiiLink link) {
this.link = link;
}
@Override
public Fii call() throws Exception {
String href = link.getHref();
Document doc = Jsoup.connect(href).get();
Fii fii = new FiiParser(doc).get();
logger.info("got :<< {}", fii.getNome());
return fii;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy