<commit_before>
package DreamGuild::Utils::Roster;

use strict;
use warnings;
use JSON::XS;
use Data::Dumper;
use DreamGuild::DB;
use File::Path qw/make_path/;
use LWP::UserAgent;



sub new {
  my $class = shift;
  bless {}, $class;
}


sub get {

  my $url = shift;
  my $ua = LWP::UserAgent->new;
  my $response = $ua->get ($url);

  if ($response->is_success) {
    return $response->decoded_content;
  } else {
    return undef;
  }

}


sub get_player_list {
  my $self = shift;
  my $raw_content = get ('http://eu.battle.net/api/wow/guild/grim-batol/Dream?fields=members')
                        or die ('Unable to get member list');
  my $guild = decode_json ($raw_content);
  $self->{members} = $guild->{members};
  $self->get_player_details;
}


sub get_player_details {
  my $self = shift;
  for (@{$self->{members}}) {
    next if $_->{character}->{level} < 10;
    my $raw_content = get ('http://eu.battle.net/api/wow/character/Grim-Batol/' . $_->{character}->{name} . '?fields=items,progression,talents') or next;
    $_->{character}->{details} = decode_json ($raw_content);
  }
}



sub get_user_ids {
  my $self = shift;
  $self->{user_ids} = {};
  DreamGuild::DB::Roster->iterate (sub {
    $self->{user_ids}->{$_->name} = $_->id;
  });
}



sub update_ilvl {
  my ($self, $uid, $ilvl) = @_;

  unless (DreamGuild::DB::IlvlHistory->count ('WHERE uid = ? AND ilvl = ?',
                                              $uid,
                                              $ilvl)) {
    DreamGuild::DB::IlvlHistory->new (
      uid => $uid,
      ilvl => $ilvl,
      time => $self->{last_update}
    )->insert;
    
  };
}


sub download_avatar {

  my ($self, $avatar) = @_;

  my ($dir, $file) = $avatar =~ /^(.*)\/(.*?)$/;
  make_path ('share/data/avatars/' . $dir);

  my $ua = LWP::UserAgent->new;
  $ua->get ('https://eu.battle.net/static-render/eu/' . $avatar,
            ':content_file' => 'share/data/avatars/' . $avatar);

}


sub update_roster {
  my $self = shift;

  $self->{last_update} = time ();

  $self->get_user_ids;
  $self->get_player_list unless defined $self->{members};

  DreamGuild::DB->begin;

  for (@{$self->{members}}) {

    my $uid = defined ($self->{user_ids}->{$_->{character}->{name}}) ? 
                  $self->{user_ids}->{$_->{character}->{name}} : 0;

    my %user = (

      name          => $_->{character}->{name},
      class         => $_->{character}->{class},
      race          => $_->{character}->{race},
      level         => $_->{character}->{level},<commit_msg>download profile picture<commit_after>
package DreamGuild::Utils::Roster;

use strict;
use warnings;
use JSON::XS;
use Data::Dumper;
use DreamGuild::DB;
use File::Path qw/make_path/;
use LWP::UserAgent;



sub new {
  my $class = shift;
  bless {}, $class;
}


sub get {

  my $url = shift;
  my $ua = LWP::UserAgent->new;
  my $response = $ua->get ($url);

  if ($response->is_success) {
    return $response->decoded_content;
  } else {
    return undef;
  }

}


sub get_player_list {
  my $self = shift;
  my $raw_content = get ('http://eu.battle.net/api/wow/guild/grim-batol/Dream?fields=members')
                        or die ('Unable to get member list');
  my $guild = decode_json ($raw_content);
  $self->{members} = $guild->{members};
  $self->get_player_details;
}


sub get_player_details {
  my $self = shift;
  for (@{$self->{members}}) {
    next if $_->{character}->{level} < 10;
    my $raw_content = get ('http://eu.battle.net/api/wow/character/Grim-Batol/' . $_->{character}->{name} . '?fields=items,progression,talents') or next;
    $_->{character}->{details} = decode_json ($raw_content);
  }
}



sub get_user_ids {
  my $self = shift;
  $self->{user_ids} = {};
  DreamGuild::DB::Roster->iterate (sub {
    $self->{user_ids}->{$_->name} = $_->id;
  });
}



sub update_ilvl {
  my ($self, $uid, $ilvl) = @_;

  unless (DreamGuild::DB::IlvlHistory->count ('WHERE uid = ? AND ilvl = ?',
                                              $uid,
                                              $ilvl)) {
    DreamGuild::DB::IlvlHistory->new (
      uid => $uid,
      ilvl => $ilvl,
      time => $self->{last_update}
    )->insert;
    
  };
}


sub download_avatar {

  my ($self, $avatar) = @_;

  my ($dir, $file) = $avatar =~ /^(.*)\/(.*?)$/;
  make_path ('share/data/avatars/' . $dir);

  my $ua = LWP::UserAgent->new;
  $ua->get ('https://eu.battle.net/static-render/eu/' . $avatar,
            ':content_file' => 'share/data/avatars/' . $avatar);
  
  # FIXME: code repeat
  # Get profile picture
  $avatar =~ s/avatar/profilemain/;
  $ua->get ('https://eu.battle.net/static-render/eu/' . $avatar,
            ':content_file' => 'share/data/avatars/' . $avatar);

}


sub update_roster {
  my $self = shift;

  $self->{last_update} = time ();

  $self->get_user_ids;
  $self->get_player_list unless defined $self->{members};

  DreamGuild::DB->begin;

  for (@{$self->{members}}) {

    my $uid = defined ($self->{user_ids}->{$_->{character}->{name}}) ? 
                  $self->{user_ids}->{$_->{character}->{name}} : 0;

    my %user = (

      name          => $_->{character}->{name},
      class         => $_->{character}->{class},
      race          => $_->{character}->{race},
      level         => $_->{character}->{level},
{'fraction_non_alphanumeric': 0.2057877813504823, 'fraction_numerical': 0.0011348590883298658, 'mean_word_length': 2.5418620227729405, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 117, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '337590', 'n_tokens_mistral': 1992, 'n_tokens_neox': 1920, 'n_words': 361}