Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.com.thiengo.superplacar.extras;
- import android.os.AsyncTask;
- import android.util.Log;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- import org.jsoup.nodes.Element;
- import org.jsoup.select.Elements;
- import java.io.IOException;
- import java.lang.ref.WeakReference;
- import java.util.ArrayList;
- import java.util.List;
- import br.com.thiengo.superplacar.MainActivity;
- import br.com.thiengo.superplacar.domain.Gol;
- import br.com.thiengo.superplacar.domain.Jogo;
- import br.com.thiengo.superplacar.domain.Time;
- public class SuperPlacarRequest extends AsyncTask<Void, Void, List<Jogo>> {
- private WeakReference<MainActivity> activity;
- public SuperPlacarRequest( MainActivity activity ){
- this.activity = new WeakReference<>( activity );
- }
- @Override
- protected List<Jogo> doInBackground(Void... voids) {
- Document html = null;
- List<Jogo> jogos = new ArrayList<>();
- try {
- html = Jsoup.connect("http://www.superplacar.com.br/").get();
- Elements campeonatos = html.select("div.container-fluid.campeonato");
- for( int i = 0; i < campeonatos.size(); i++ ){
- Log.i("Log", "Campeonato: "+campeonatos.get( i ).select("h4").text());
- Element item = campeonatos.get( i ).nextElementSibling();
- while( item != null
- && item.tagName().equalsIgnoreCase("li") ){
- Element time = item.select("div.time-status span.time").get(0);
- Element status = item.select("div.time-status span.status").get(0);
- Element timeTag1 = item.select("div.team1").get(0);
- Element timeTag2 = item.select("div.team2").get(0);
- Time time1 = getTime( timeTag1, true );
- Time time2 = getTime( timeTag2, false );
- Jogo jogo = new Jogo();
- Log.i("Log", "Times: "+time1.getNome()+" vs "+time2.getNome() );
- jogo.setInicio( time.text() );
- jogo.setStatus( status.text() );
- jogo.setTime1( time1 );
- jogo.setTime2( time2 );
- jogos.add( jogo );
- item = item.nextElementSibling();
- }
- }
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- return jogos;
- }
- private Time getTime( Element timeTag, boolean casa ){
- int indice = casa ? 1 : 2;
- Time time = new Time();
- time.setNome( timeTag.select("span.team"+indice+"-name").text() );
- time.setImagemUrl( timeTag.select("img").attr("src") );
- String goalsString = timeTag.select("span.team"+indice+"-score").text();
- int goals = goalsString.isEmpty() ? 0 : Integer.parseInt( goalsString );
- time.setGols( goals );
- time.getGolsLista().addAll( getGolsLista( timeTag ) );
- return time;
- }
- private List<Gol> getGolsLista(Element timeTag ){
- Elements golsLista = timeTag.select("ul.goal-players li");
- List<Gol> gols = new ArrayList<>();
- for( Element g : golsLista ){
- Gol gol = new Gol();
- gol.setNome( g.select(".name").text() );
- gol.setTime( g.select(".time").text() );
- gols.add(gol);
- }
- return gols;
- }
- @Override
- protected void onPostExecute(List<Jogo> jogos) {
- super.onPostExecute( jogos );
- if( activity.get() != null ){
- activity.get().updateLista( jogos );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement