br.com.objectos.jabuticava.bvmf.rf.cri.CriWgetGuice Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jabuticava-bvmf-cri Show documentation
Show all versions of jabuticava-bvmf-cri Show documentation
Coleta de dados sobre CRIs na 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.jabuticava.bvmf.rf.cri;
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.Executors;
import java.util.concurrent.Future;
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 CriWgetGuice implements CriWget {
private static final Logger logger = LoggerFactory.getLogger(CriWget.class);
private final ExecutorService executor;
private final CriForm form;
@Inject
public CriWgetGuice(CriForm form) {
int cpus = Runtime.getRuntime().availableProcessors();
this.executor = Executors.newFixedThreadPool(cpus * 4);
this.form = form;
}
@Override
public List connect() {
try {
Document doc = form.post();
List links;
links = new ListagemParser(doc).get();
List> lazyFutures;
lazyFutures = transform(links, new ToFuture());
List> futures;
futures = ImmutableList.copyOf(lazyFutures);
List cris;
cris = transform(futures, new ToCri());
return ImmutableList.copyOf(cris);
} catch (IOException e) {
return ImmutableList.of();
}
}
private class ToFuture implements Function> {
@Override
public Future apply(CriLink link) {
Get get = new Get(link);
return executor.submit(get);
}
}
private class ToCri implements Function, Cri> {
@Override
public Cri apply(Future future) {
try {
return future.get();
} catch (InterruptedException e) {
return new CriErro();
} catch (ExecutionException e) {
return new CriErro();
}
}
}
private static class Get implements Callable {
private final CriLink link;
public Get(CriLink link) {
this.link = link;
}
@Override
public Cri call() throws Exception {
String href = link.getHref();
Document doc = Jsoup.connect(href).get();
Cri cri = new CriParser(doc).get();
CriSerie serie = cri.getSerie();
logger.info("got :<< {}", serie != null ? serie.getCodigoTitulo() : "");
return cri;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy