Java tutorial
/* * Copyright 2014 Nicholas Liu * * 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 ca.zadrox.dota2esportticker.util; import android.content.ContentValues; import android.net.Uri; import android.util.Log; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.util.concurrent.Callable; import ca.zadrox.dota2esportticker.data.MatchContract; import ca.zadrox.dota2esportticker.data.http.match.model.BundledMatchItem; import static ca.zadrox.dota2esportticker.util.LogUtils.LOGD; import static ca.zadrox.dota2esportticker.util.LogUtils.LOGE; /** * Created by Acco on 10/13/2014. */ public class MatchGetter implements Callable<BundledMatchItem> { private static final String TAG = MatchGetter.class.getSimpleName(); private static final String BASE_URL = "http://www.gosugamers.net"; private String matchUrl; private boolean mHasResult; public MatchGetter(String matchUrl, boolean hasResult) { this.matchUrl = matchUrl; mHasResult = hasResult; } @Override public BundledMatchItem call() { try { String lastPathSegment = Uri.parse(matchUrl).getLastPathSegment(); long matchId = Long.parseLong(lastPathSegment.substring(0, lastPathSegment.indexOf("-"))); String rawHtml = new OkHttpClient().newCall(new Request.Builder().url(matchUrl).build()).execute() .body().string(); String processedHtml = ""; processedHtml += rawHtml.substring(rawHtml.indexOf("<div class=\"box box-match-page\">"), rawHtml.indexOf("<div class=\"content\">")); processedHtml += rawHtml.substring(rawHtml.indexOf("<div class=\"ad event-ad\">"), rawHtml.indexOf("<a class=\"howto-itembet\"")); try { processedHtml += rawHtml.substring(rawHtml.indexOf("<div class=\"match-opponents\">"), rawHtml.indexOf("<div id=\"betting-tabs\" ")); } catch (IndexOutOfBoundsException e) { LOGD(TAG, "Page does not have betting tabs. Attempting to use comment section"); try { processedHtml += rawHtml.substring(rawHtml.indexOf("<div class=\"match-opponents\">"), rawHtml.indexOf("<div id=\"comments\" ")); } catch (IndexOutOfBoundsException e2) { LOGE(TAG, "Well, not sure what next to substring then..."); } } Document doc = Jsoup.parse(processedHtml); if (!mHasResult) { return new BundledMatchItem(parseMatchDetails(doc, matchId)); } else { return new BundledMatchItem(parseMatchDetails(doc, matchId), parseResultDetails(doc, matchId)); } } catch (Exception e) { Log.e(TAG, "WHAT. " + matchUrl); e.printStackTrace(); return null; } } private ContentValues parseMatchDetails(Document doc, long matchId) { Element opponentOne = doc.getElementsByClass("opponent1").first(); Element opponentTwo = doc.getElementsByClass("opponent2").first(); Element tournament = doc.getElementsByClass("tournament-image").first(); Element tournamentString = doc.getElementsByTag("a").first(); String tournamentUrl = BASE_URL + tournamentString.attr("href"); String tournamentName = tournamentString.text(); if (tournamentName.length() == 0) { tournamentName = tournament.attr("alt"); } ContentValues matchValues = new ContentValues(); matchValues.put(MatchContract.SeriesEntry._ID, matchId); matchValues.put(MatchContract.SeriesEntry.COLUMN_GG_MATCH_PAGE, matchUrl); matchValues.put(MatchContract.SeriesEntry.COLUMN_BEST_OF, doc.getElementsByClass("bestof").first().text()); try { matchValues.put(MatchContract.SeriesEntry.COLUMN_DATETIME, TimeUtils.toDBTime(doc.getElementsByClass("datetime").first().text())); } catch (NullPointerException e) { Log.d(TAG, "No datetime retrieved, logging match as being at epoch"); matchValues.put(MatchContract.SeriesEntry.COLUMN_DATETIME, 1); } matchValues.put(MatchContract.SeriesEntry.COLUMN_TEAM_ONE_NAME, opponentOne.getElementsByTag("a").first().text().replaceAll(" ?\\.?\\-?-?Dot[aA][\\s]?2", "")); matchValues.put(MatchContract.SeriesEntry.COLUMN_PATH_TEAM_ONE_FLAG, BASE_URL + opponentOne.getElementsByClass("team-player").first().attr("src")); matchValues.put(MatchContract.SeriesEntry.COLUMN_TEAM_TWO_NAME, opponentTwo.getElementsByTag("a").first().text().replaceAll(" ?\\.?\\-?-?Dot[aA][\\s]?2", "")); matchValues.put(MatchContract.SeriesEntry.COLUMN_PATH_TEAM_TWO_FLAG, BASE_URL + opponentTwo.getElementsByClass("team-player").first().attr("src")); matchValues.put(MatchContract.SeriesEntry.COLUMN_TOURNAMENT_NAME, tournamentName); matchValues.put(MatchContract.SeriesEntry.COLUMN_TOURNAMENT_URL, tournamentUrl); matchValues.put(MatchContract.SeriesEntry.COLUMN_PATH_TOURNAMENT_LOGO, BASE_URL + tournament.attr("src")); return matchValues; } private ContentValues parseResultDetails(Document doc, long matchId) { Elements scorebox = doc.getElementsByClass("vs").first().getElementsByTag("span"); ContentValues resultValues; try { resultValues = new ContentValues(); resultValues.put(MatchContract.ResultEntry._ID, matchId); resultValues.put(MatchContract.ResultEntry.COLUMN_TEAM_ONE_SCORE, scorebox.get(2).text()); resultValues.put(MatchContract.ResultEntry.COLUMN_TEAM_TWO_SCORE, scorebox.get(3).text()); return resultValues; } catch (IndexOutOfBoundsException e) { Log.d(TAG, "Score isn't final, not updating score."); } return null; } }