class Rsteamshot::Screenshot

Public: Represents an image that has been uploaded to Steam of a screenshot taken in a Steam app.

Attributes

app[R]

Public: Returns the Rsteamshot::App in which this screenshot was taken.

comment_count[R]

Public: Returns Integer count of how many comments people have left on this screenshot.

date[R]

Public: Returns the DateTime when this screenshot was uploaded.

details_url[R]

Public: Returns String URL to the Steam page that shows details about this screenshot.

file_size[R]

Public: Returns a String describing how large the screenshot is, e.g., 0.547 MB.

full_size_url[R]

Public: Returns String URL to the full-size image.

height[R]

Public: Returns Integer pixel height of the screenshot.

like_count[R]

Public: Returns Integer count of how many people have voted for this screenshot.

medium_url[R]

Public: Returns String URL to a medium-size version of the image.

title[R]

Public: Returns a String of how the user described this screenshot, or nil.

user_name[R]

Public: Returns String name of the Steam user who uploaded the screenshot.

user_url[R]

Public: Returns String URL to the profile of the Steam user who uploaded the screenshot.

width[R]

Public: Returns Integer pixel width of the screenshot.

Public Class Methods

new(attrs = {}) click to toggle source

Public: Initialize a screenshot with the given attributes.

attrs - the Hash of attributes for this screenshot

:title - how the user described this screenshot
:details_url - URL to the Steam page that shows details about this screenshot
:full_size_url - URL to the full-size image
:medium_url - URL to a medium-size version of the image
:user_name - name of the Steam user who uploaded the screenshot
:user_url - URL to the profile of the Steam user who uploaded the screenshot
:date - the date and time when this screenshot was uploaded
:file_size - describes how large the screenshot is, e.g., 0.547 MB
:width - pixel width of the screenshot
:height - pixel height of the screenshot
:like_count - number of likes this screenshot has on Steam
:comment_count - number of comments this screenshot has received on Steam
# File lib/rsteamshot/screenshot.rb, line 59
def initialize(attrs = {})
  attrs.each { |key, value| instance_variable_set("@#{key}", value) }

  fetch_details unless has_details?
end

Public Instance Methods

file_size_in_bytes() click to toggle source

Public: file_size parsed into bytes

Returns a Integer

# File lib/rsteamshot/screenshot.rb, line 102
def file_size_in_bytes
  unit_multiplier = 1000
  size, unit = self.file_size.split(' ')
  bytes = case unit.downcase
    when 'kb' then size.to_f * unit_multiplier
    when 'mb' then size.to_f * (unit_multiplier ** 2)
    when 'gb' then size.to_f * (unit_multiplier ** 3)
    else size.to_f
  end
  bytes.to_i
end
inspect() click to toggle source

Public: A detailed representation of this screenshot.

Returns a String.

# File lib/rsteamshot/screenshot.rb, line 95
def inspect
  self.class.name + '<' + JSON.generate(to_h) + '>'
end
to_h() click to toggle source

Public: Get a hash representation of this screenshot.

Returns a Hash.

# File lib/rsteamshot/screenshot.rb, line 68
def to_h
  result = { details_url: details_url }
  result[:title] = title if title
  result[:full_size_url] = full_size_url if full_size_url
  result[:medium_url] = medium_url if medium_url
  result[:user_name] = user_name if user_name
  result[:user_url] = user_url if user_url
  result[:date] = date if date
  result[:file_size] = file_size if file_size
  result[:width] = width if width
  result[:height] = height if height
  result[:like_count] = like_count if like_count
  result[:comment_count] = comment_count if comment_count
  result[:app] = app.to_h if app
  result
end
to_json() click to toggle source

Public: Get a JSON representation of this screenshot.

Returns a String.

# File lib/rsteamshot/screenshot.rb, line 88
def to_json
  JSON.pretty_generate(to_h)
end

Private Instance Methods

app_from(page) click to toggle source
# File lib/rsteamshot/screenshot.rb, line 159
def app_from(page)
  app_id = app_id_from(page)
  app_name = app_name_from(page)

  if app_id && app_name
    Rsteamshot::App.new(id: app_id, name: app_name)
  end
end
app_id_from(page) click to toggle source
# File lib/rsteamshot/screenshot.rb, line 168
def app_id_from(page)
  site_info = page.at('.apphub_OtherSiteInfo')
  return unless site_info

  store_link = site_info.at('a')
  return unless store_link

  store_url = store_link['href']
  return unless store_url

  # e.g., "http://store.steampowered.com/app/22330" => "22330"
  store_url.downcase.split('/app/').last
end
app_name_from(page) click to toggle source
# File lib/rsteamshot/screenshot.rb, line 182
def app_name_from(page)
  name_el = page.at('.apphub_AppName')
  return unless name_el

  name_el.text.strip.gsub(/[[:space:]]\z/, '')
end
comment_count_from(page) click to toggle source
# File lib/rsteamshot/screenshot.rb, line 216
def comment_count_from(page)
  header_el = page.at('.commentthread_count_label')
  return 0 unless header_el

  span = header_el.at('span')
  return 0 unless span

  text = span.text.strip.gsub(/[[:space:]]\z/, '')
  if text.length > 0
    text.to_i
  else
    0
  end
end
date_from(raw_date_str) click to toggle source
# File lib/rsteamshot/screenshot.rb, line 258
def date_from(raw_date_str)
  format = if raw_date_str.include?(',')
    '%b %d, %Y @ %l:%M%P'
  else
    '%b %d @ %l:%M%P'
  end
  DateTime.strptime(raw_date_str, format)
end
details_block_from(page) click to toggle source
# File lib/rsteamshot/screenshot.rb, line 189
def details_block_from(page)
  details_blocks = page.search('.rightDetailsBlock')
  details_blocks.detect do |details_block|
    labels_container = details_block.at('.detailsStatsContainerLeft')
    !labels_container.nil?
  end
end
dimensions_from(dimension_str) click to toggle source
# File lib/rsteamshot/screenshot.rb, line 267
def dimensions_from(dimension_str)
  if dimension_str
    dimension_str.split(' x ').map { |str| str.to_i }
  else
    []
  end
end
fetch_details() click to toggle source
# File lib/rsteamshot/screenshot.rb, line 120
def fetch_details
  return unless details_url

  Mechanize.new.get(details_url) do |page|
    link = page.at('.actualmediactn a')
    @full_size_url = link['href'] if link

    @medium_url = medium_url_from(page)
    @like_count = like_count_from(page)
    @comment_count = comment_count_from(page)

    author = page.at('.creatorsBlock')
    @user_name = user_name_from(author)
    @user_url = user_url_from(author)

    @app = app_from(page)

    details_block = details_block_from(page)
    if details_block
      labels_container = details_block.at('.detailsStatsContainerLeft')
      if labels_container
        label_els = labels_container.search('.detailsStatLeft')
        labels = label_els.map { |el| el.text.strip.gsub(/[[:space:]]\z/, '') }
      end

      values_container = details_block.at('.detailsStatsContainerRight')
      if values_container
        value_els = values_container.search('.detailsStatRight')
        values = value_els.map { |el| el.text.strip.gsub(/[[:space:]]\z/, '') }
      end

      labelled_values = labels.zip(values).to_h
      @file_size = labelled_values['File Size']
      @date = date_from(labelled_values['Posted'])
      @width, @height = dimensions_from(labelled_values['Size'])
    end
  end
end
has_details?() click to toggle source
# File lib/rsteamshot/screenshot.rb, line 116
def has_details?
  !full_size_url.nil? && !medium_url.nil?
end
like_count_from(page) click to toggle source
# File lib/rsteamshot/screenshot.rb, line 204
def like_count_from(page)
  rate_el = page.at('.rateUpCount')
  return 0 unless rate_el

  text = rate_el.text.strip.gsub(/[[:space:]]\z/, '')
  if text.length > 0
    text.to_i
  else
    0
  end
end
medium_url_from(page) click to toggle source
# File lib/rsteamshot/screenshot.rb, line 197
def medium_url_from(page)
  img = page.at('img#ActualMedia')
  return unless img

  img['src']
end
user_name_from(author) click to toggle source
# File lib/rsteamshot/screenshot.rb, line 231
def user_name_from(author)
  container = author.at('.friendBlockContent')
  return unless container

  all_text = container.text
  online_status = container.at('.friendSmallText')
  return all_text.strip.gsub(/[[:space:]]\z/, '') unless online_status

  status_text = online_status.text
  index = all_text.index(status_text)

  user_name_text = if index
    all_text.slice(0, index)
  else
    all_text
  end

  user_name_text.strip.gsub(/[[:space:]]\z/, '')
end
user_url_from(author) click to toggle source
# File lib/rsteamshot/screenshot.rb, line 251
def user_url_from(author)
  author_link = author.at('.friendBlockLinkOverlay')
  return unless author_link

  author_link['href']
end