Advertisement
Guest User

SuperPlacarRequest.java

a guest
Feb 26th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.61 KB | None | 0 0
  1. package br.com.thiengo.superplacar.extras;
  2.  
  3. import android.os.AsyncTask;
  4. import android.util.Log;
  5.  
  6. import org.jsoup.Jsoup;
  7. import org.jsoup.nodes.Document;
  8. import org.jsoup.nodes.Element;
  9. import org.jsoup.select.Elements;
  10. import java.io.IOException;
  11. import java.lang.ref.WeakReference;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14.  
  15. import br.com.thiengo.superplacar.MainActivity;
  16. import br.com.thiengo.superplacar.domain.Gol;
  17. import br.com.thiengo.superplacar.domain.Jogo;
  18. import br.com.thiengo.superplacar.domain.Time;
  19.  
  20.  
  21. public class SuperPlacarRequest extends AsyncTask<Void, Void, List<Jogo>> {
  22.     private WeakReference<MainActivity> activity;
  23.  
  24.     public SuperPlacarRequest( MainActivity activity ){
  25.         this.activity = new WeakReference<>( activity );
  26.     }
  27.  
  28.     @Override
  29.     protected List<Jogo> doInBackground(Void... voids) {
  30.  
  31.         Document html = null;
  32.         List<Jogo> jogos = new ArrayList<>();
  33.  
  34.         try {
  35.             html = Jsoup.connect("http://www.superplacar.com.br/").get();
  36.             Elements campeonatos = html.select("div.container-fluid.campeonato");
  37.  
  38.             for( int i = 0; i < campeonatos.size(); i++ ){
  39.                 Log.i("Log", "Campeonato: "+campeonatos.get( i ).select("h4").text());
  40.                 Element item = campeonatos.get( i ).nextElementSibling();
  41.  
  42.                 while( item != null
  43.                         && item.tagName().equalsIgnoreCase("li") ){
  44.  
  45.                     Element time = item.select("div.time-status span.time").get(0);
  46.                     Element status = item.select("div.time-status span.status").get(0);
  47.                     Element timeTag1 = item.select("div.team1").get(0);
  48.                     Element timeTag2 = item.select("div.team2").get(0);
  49.  
  50.                     Time time1 = getTime( timeTag1, true );
  51.                     Time time2 = getTime( timeTag2, false );
  52.                     Jogo jogo = new Jogo();
  53.  
  54.                     Log.i("Log", "Times: "+time1.getNome()+" vs "+time2.getNome() );
  55.  
  56.                     jogo.setInicio( time.text() );
  57.                     jogo.setStatus( status.text() );
  58.                     jogo.setTime1( time1 );
  59.                     jogo.setTime2( time2 );
  60.                     jogos.add( jogo );
  61.  
  62.                     item = item.nextElementSibling();
  63.                 }
  64.             }
  65.         }
  66.         catch (IOException e) {
  67.             e.printStackTrace();
  68.         }
  69.  
  70.         return jogos;
  71.     }
  72.  
  73.     private Time getTime( Element timeTag, boolean casa ){
  74.         int indice = casa ? 1 : 2;
  75.         Time time = new Time();
  76.         time.setNome( timeTag.select("span.team"+indice+"-name").text() );
  77.         time.setImagemUrl( timeTag.select("img").attr("src") );
  78.  
  79.         String goalsString = timeTag.select("span.team"+indice+"-score").text();
  80.         int goals = goalsString.isEmpty() ? 0 : Integer.parseInt( goalsString );
  81.         time.setGols( goals );
  82.  
  83.         time.getGolsLista().addAll( getGolsLista( timeTag ) );
  84.         return time;
  85.     }
  86.  
  87.     private List<Gol> getGolsLista(Element timeTag ){
  88.         Elements golsLista = timeTag.select("ul.goal-players li");
  89.         List<Gol> gols = new ArrayList<>();
  90.  
  91.         for( Element g : golsLista ){
  92.             Gol gol = new Gol();
  93.             gol.setNome( g.select(".name").text() );
  94.             gol.setTime( g.select(".time").text() );
  95.             gols.add(gol);
  96.         }
  97.         return gols;
  98.     }
  99.  
  100.     @Override
  101.     protected void onPostExecute(List<Jogo> jogos) {
  102.         super.onPostExecute( jogos );
  103.  
  104.         if( activity.get() != null ){
  105.             activity.get().updateLista( jogos );
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement