Class: Scraper

Inherits:
Object
  • Object
show all
Defined in:
lib/trophy_scraper/scraper.rb

Constant Summary collapse

TROPHY_PAGE_URL =
"http://profiles.us.playstation.com/playstation/psn/profiles/%s"
TROPHY_DATA_URL =
"http://profiles.us.playstation.com/playstation/psn/profile/%s/get_ordered_trophies_data"
TROPHY_DETAILED_DATA_URL =
"http://profiles.us.playstation.com/playstation/psn/profile/%s/get_ordered_title_details_data"

Instance Method Summary collapse

Constructor Details

#initialize(username) ⇒ Scraper

Returns a new instance of Scraper.



6
7
8
9
10
11
12
# File 'lib/trophy_scraper/scraper.rb', line 6

def initialize(username)
  @username = username
  @agent = WWW::Mechanize.new
  @update_date = Time.now
  @account_trophy_page = nil
  self.
end

Instance Method Details

#get_game_detail_stats(game_uri, trophy_levels) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/trophy_scraper/scraper.rb', line 48

def get_game_detail_stats(game_uri, trophy_levels)
  trophy_info = Hash.new
  hidden_trophy_counter = 1
  
  titleId = game_uri.split("/")[-1]
  trophy_data = @agent.post(TROPHY_DETAILED_DATA_URL % [@username], "titleId" => titleId, "sortBy" => "id_asc")
  
  trophy_data.search(".slotcontent").each { |slot|
    trophy = Trophy.new
    trophy.name = slot.search(".trophyTitleSortField").text.strip
    trophy.description = slot.search(".subtext").text.strip
    
    # Game is a "spoiler trophy", ignore for now
    if trophy.name == "" && trophy.description == ""
      trophy.name = "Hidden Trophy %i" % hidden_trophy_counter
      trophy.description = "Hidden Trophy"
      hidden_trophy_counter += 1
    end
    
    trophy_level = slot.search(".trophyholder .trophyimage").first[:title]
    trophy_levels.each do |level|
      if trophy_level.downcase.include?(level.name.downcase)
        level.trophies << trophy
      end
    end
    
    unlock_date = slot.search(".lastTrophyTime").text.strip
    
    if unlock_date.length > 0
      trophy_info[trophy] = unlock_date
    else
      trophy_info[trophy] = nil
    end
      
  }
  $logger.debug("Found trophies #{trophy_info}")
  return trophy_info
end

#get_game_summary_statsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/trophy_scraper/scraper.rb', line 26

def get_game_summary_stats()
  trophy_data = @agent.get(TROPHY_DATA_URL % [@username])
  summary_stats = Hash.new
  
  game_slots = trophy_data.search(".slot")
  game_slots.each { |game_data|
    game = Game.new
    game.title = game_data.search(".titletext a").text.strip
    game.uri = game_data.search(".titletext a").first[:href].strip
    game.logo_uri = game_data.search(".titlelogo img").first[:src].strip
    #TODO Make sure we got the right amount of trophy columns
    trophies_earned = Hash.new
    trophies_earned[:bronze] = game_data.search(".trophycount.normal .trophycontent")[0].text.strip
    trophies_earned[:silver] = game_data.search(".trophycount.normal .trophycontent")[1].text.strip
    trophies_earned[:gold] = game_data.search(".trophycount.normal .trophycontent")[2].text.strip
    trophies_earned[:platinum] = game_data.search(".trophycount.normal .trophycontent")[3].text.strip
    
    summary_stats[game] = trophies_earned
  }
  return summary_stats
end

#get_user_summary_statsObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/trophy_scraper/scraper.rb', line 14

def get_user_summary_stats
  stats = UserStat.new
  stats.level = @account_trophy_page.search("#leveltext").text.strip
  stats.percent_to_next_level = @account_trophy_page.search(".progresstext").text.strip.delete("%")
  stats.number_of_bronze = @account_trophy_page.search(".podium .bronze").text.strip.split[0]
  stats.number_of_silver = @account_trophy_page.search(".podium .silver").text.strip.split[0]
  stats.number_of_gold = @account_trophy_page.search(".podium .gold").text.strip.split[0]
  stats.number_of_platinum = @account_trophy_page.search(".podium .platinum").text.strip.split[0]
  $logger.debug("Found user stats #{stats}")
  return stats
end