<commit_before>package com.thangiee.LoLWithFriends.api

import org.jsoup.Jsoup
import org.jsoup.nodes.Document

import scala.collection.JavaConversions._
import scala.collection.mutable.ListBuffer
import scala.util.Try

/**
 * Class implementation to retrieve statistics for a player from http://www.lolskill.net
 *
 * @param name
 * @param region
 */
class LoLSkill(name: String, region: String) extends LoLStatistics {
  private val baseServerUrl = "http://www.lolskill.net/summoner/"
  private val url = baseServerUrl + region + "/" + name
  private val doc: Document = Try(Jsoup.connect(url).timeout(10000).get).get
  println("[*] Connecting to: " + url)
  if (doc.body().text().contains("currently unavailable")) throw new IllegalStateException("Service is currently unavailable. Please try again later!")

  override def level(): Int = parse("div[class=realm]").flatMap[Int](getNumber[Int]).getOrElse(1)

  override def win(): Int = parse("span[class=wins]").flatMap[Int](getNumber[Int]).getOrElse(0)

  override def lose(): Int = parse("span[class=losses]").flatMap[Int](getNumber[Int]).getOrElse(0)

  override def kda(): String = parse("p[class=kda]").getOrElse("N/A")

  override def leagueName(): String = parse("p[class=leaguename]").getOrElse("N/A")

  override def leaguePoints(): String = parse("p[class=leaguepoints]").getOrElse("N/A")

  override def leagueTier(): String = parse("p[class=tier]").flatMap[String](s => Try(s.split(" ").head)).getOrElse("N/A")

  override def leagueDivision(): String = parse("p[class=tier]").flatMap[String](s => Try(s.split(" ").last)).getOrElse("N/A")

  override def topChampions(): List[Champion] = {
    // 0  |    1   |    2     |  3    |4 |5   |  6   |7   |8     |9   |10    | 11 |12   |13    | 14<commit_msg>change to doing multiple attempts to get data from website<commit_after>package com.thangiee.LoLWithFriends.api

import org.jsoup.Jsoup
import org.jsoup.nodes.Document

import scala.collection.JavaConversions._
import scala.collection.mutable.ListBuffer
import scala.util.{Failure, Success, Try}

/**
 * Class implementation to retrieve statistics for a player from http://www.lolskill.net
 *
 * @param name
 * @param region
 */
class LoLSkill(name: String, region: String) extends LoLStatistics {
  private val baseServerUrl = "http://www.lolskill.net/summoner/"
  private val url = baseServerUrl + region + "/" + name
  private var doc: Document = _
  fetchDocument()

  private def fetchDocument(): Unit = {
    // do multiple attempts to get the document(aka html stuff)
    for (attempt ← 1 to 5) {
      println("[*] Attempt " + attempt + "|Connecting to: " + url)
      Try(Jsoup.connect(url).timeout(5000).get()) match {
        case Success(respond) ⇒    // got respond from website
          if (!respond.text().contains("currently unavailable")) {  // website respond with it been busy
            doc = respond; return
          } else if (attempt == 5) {  // all atempts used up
            throw new IllegalStateException("Service is currently unavailable. Please try again later!")
          } else {
            Thread.sleep(150)
          }
        case Failure(e) ⇒ throw e // no respond
      }
    }
  }

  override def level(): Int = parse("div[class=realm]").flatMap[Int](getNumber[Int]).getOrElse(1)

  override def win(): Int = parse("span[class=wins]").flatMap[Int](getNumber[Int]).getOrElse(0)

  override def lose(): Int = parse("span[class=losses]").flatMap[Int](getNumber[Int]).getOrElse(0)

  override def kda(): String = parse("p[class=kda]").getOrElse("N/A")

  override def leagueName(): String = parse("p[class=leaguename]").getOrElse("N/A")

  override def leaguePoints(): String = parse("p[class=leaguepoints]").getOrElse("N/A")

  override def leagueTier(): String = parse("p[class=tier]").flatMap[String](s => Try(s.split(" ").head)).getOrElse("N/A")

  override def leagueDivision(): String = parse("p[class=tier]").flatMap[String](s => Try(s.split(" ").last)).getOrElse("N/A")

  override def topChampions(): List[Champion] = {
    // 0  |    1   |    2     |  3    |4 |5   |  6   |7   |8     |9   |10    | 11 |12   |13    | 14
{'fraction_non_alphanumeric': 0.1582385676264215, 'fraction_numerical': 0.014759254778611178, 'mean_word_length': 3.8578143360752057, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3132759', 'n_tokens_mistral': 1387, 'n_tokens_neox': 1311, 'n_words': 322}