Class: Factorio::Mod

Inherits:
Object
  • Object
show all
Defined in:
lib/factorio/mod/mod.rb,
lib/factorio/mod/version.rb,
lib/factorio/mod/download.rb

Overview

Info interface

example:

mod = Factorio::Mod.new 'LTN-easier'
puts "The author of #{mod.title} is #{mod.author}"
puts "The #{mod.title} is under #{mod.license}"
puts "It latest version can download at #{mod.latest_download[:uri]}"
puts "It latest version for factorio 0.16 can download at " \
  "#{mod.latest_download('0.16')[:uri]}"

Defined Under Namespace

Classes: Download

Constant Summary collapse

MOD_URI =

factorio Mod portal website URI

'https://mods.factorio.com'
VERSION =
'0.5.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, cookie = '') ⇒ Mod

Returns a new instance of Mod.

Parameters:

  • name (String)

    name of the Mod

  • cookie (String) (defaults to: '')

    logged in cookie



26
27
28
29
# File 'lib/factorio/mod/mod.rb', line 26

def initialize(name, cookie = '')
  @name = name
  @mod = Cache.new "#{MOD_URI}/mod/#{ERB::Util.url_encode @name}", cookie
end

Instance Attribute Details

#nameObject (readonly) Also known as: id, string_id

Returns the value of attribute name.



20
21
22
# File 'lib/factorio/mod/mod.rb', line 20

def name
  @name
end

Class Method Details

.bind_td_to_th(table_header, line) ⇒ Download

Map each value with table header.

Parameters:

  • table_header (Array)

    table header in order

  • line (Nokogiri::HTML)

    <tr>

Returns:



48
49
50
51
52
53
54
# File 'lib/factorio/mod/mod.rb', line 48

def self.bind_td_to_th(table_header, line)
  line.css('td').each_with_index.each_with_object(Download.new) \
    do |(item, index), obj|
    key = table_header[index]
    obj.set(key, item)
  end
end

.extend_uri(rel) ⇒ String

Extend relative links to absolute links

Parameters:

  • rel (String)

    relative link

Returns:

  • (String)

    absolute link

Raises:



118
119
120
121
122
# File 'lib/factorio/mod/mod.rb', line 118

def self.extend_uri(rel)
  raise NotLoginError if rel =~ %r{^/login}

  MOD_URI + rel
end

.get_href(item) ⇒ String

Get href in Nokogiri node

Parameters:

  • item (Nokogiri::HTML)

Returns:

  • (String)

    relative link



127
128
129
# File 'lib/factorio/mod/mod.rb', line 127

def self.get_href(item)
  extend_uri(item.xpath('a/@href').text)
end

Get the logged in cookie by username and password.

Examples:

print 'username: '
user = gets.chomp
print 'password: '
pass = gets.chomp
cookie = Factorio::Mod. user, pass

Parameters:

  • username (String)
  • password (String)

Returns:

  • (String)

    The logged in cookie



103
104
105
106
107
108
109
110
111
112
# File 'lib/factorio/mod/mod.rb', line 103

def self.(username, password)
  csrf_token, cookie = csrf_and_cookie
  data = URI.encode_www_form(
    'csrf_token' => csrf_token,
    'username' => username,
    'password' => password
  )
  resp = Net::HTTP.post(URI(MOD_URI + '/login'), data, 'Cookie' => cookie)
  resp.response['set-cookie'].split('; ').first
end

Get the logged in cookie by webdriver.

Examples:

# use firefox nightly
Selenium::WebDriver::Firefox::Binary.path =
  `which firefox-nightly`.chomp
# open firefox
driver = Selenium::WebDriver.for :firefox
cookie = Factorio::Mod.(driver)
# the browser will automatically close

Parameters:

  • driver (Selenium::WebDriver::Driver)

    Web Driver

  • autoclose (Bool) (defaults to: true)

    automatically close the browser after logging in

Returns:

  • (String)

    The logged in cookie



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/factorio/mod/mod.rb', line 69

def self.(driver, autoclose = true)
  begin
    driver.navigate.to 'https://mods.factorio.com/login'
  rescue Net::ReadTimeout
    retry
  end

  sleep(1) while driver.current_url == 'https://mods.factorio.com/login'
  session = driver.manage.cookie_named('session')[:value]
  driver.quit if autoclose

  "session=#{session}"
end

Instance Method Details

#authorString

Get the author of the Mod.

Returns:

  • (String)

    author



171
172
173
# File 'lib/factorio/mod/mod.rb', line 171

def author
  @mod.any.xpath('//div[@class="mod-card-author"]/a').text
end

#deprecated?Bool

Whether Mod is deprecated

Returns:

  • (Bool)

    is deprecated



177
178
179
# File 'lib/factorio/mod/mod.rb', line 177

def deprecated?
  !@mod.any.xpath('//span[@class="mod-card-title-deprecated"]').empty?
end

#download_listArray<Download>

Get the download for all of version.

Returns:



202
203
204
205
206
207
208
# File 'lib/factorio/mod/mod.rb', line 202

def download_list
  table = @mod.downloads.css 'table.mod-page-downloads-table'
  head = table.css('thead tr th').map(&:text)
  table.css('tbody tr').map do |line|
    Mod.bind_td_to_th head, line
  end
end

#download_timesInteger

Get the totle number of downloads of Mod.

Returns:

  • (Integer)

    number of downloads



183
184
185
186
# File 'lib/factorio/mod/mod.rb', line 183

def download_times
  xpath = "//span[@class='mod-card-info-tag'][@title='Downloads']/div"
  @mod.any.xpath(xpath).text.to_i
end

#gitString

Get the git repo URI that can be cloned.

Returns:

  • (String)

    git URI



152
153
154
# File 'lib/factorio/mod/mod.rb', line 152

def git
  github &.+ '.git'
end

#githubString

Get the GitHub URI.

Returns:

  • (String)

    GitHub URI



146
147
148
# File 'lib/factorio/mod/mod.rb', line 146

def github
  @mod.any.xpath('//a[@class="source-code-link"]/@href').text.presence
end

#latest_download(version = nil) ⇒ Download

Get the latest download of the Mod.

Parameters:

  • version (String) (defaults to: nil)

    factorio version

Returns:

Raises:



191
192
193
194
195
196
197
198
# File 'lib/factorio/mod/mod.rb', line 191

def latest_download(version = nil)
  return download_list.last if version.blank?

  download_list.each do |download|
    return download if download.game_version == version.to_s
  end
  raise NoDownloadError, "Can't found #{name} for factorio #{version}"
end

#latest_download_uriString Also known as: latest_download_link

Get latest download uri by download button in card

Returns:

  • (String)

    download uri



212
213
214
# File 'lib/factorio/mod/mod.rb', line 212

def latest_download_uri
  Mod.get_href(@mod.any.css('.mod-download-button'))
end

#licenseString

Get the license that the Mod use.

Returns:

  • (String)

    license name



158
159
160
# File 'lib/factorio/mod/mod.rb', line 158

def license
  data_table('License').xpath('a').text
end

#license_uriString Also known as: license_link

Get the license URI.

Returns:

  • (String)

    license URI



164
165
166
# File 'lib/factorio/mod/mod.rb', line 164

def license_uri
  data_table('License').xpath('a/@href').to_s
end

#summaryString

Get the summary of the Mod

Returns:

  • (String)

    summary



140
141
142
# File 'lib/factorio/mod/mod.rb', line 140

def summary
  @mod.any.css('.mod-card-summary').text
end

#titleString Also known as: display_name

Get the title (also known as display name) of the Mod

Returns:

  • (String)

    title



133
134
135
# File 'lib/factorio/mod/mod.rb', line 133

def title
  @mod.any.css('.mod-card-title *').text
end