Class: Subscene::Subtitle

Inherits:
Object
  • Object
show all
Defined in:
lib/subscene/subtitle.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Subtitle

Returns a new instance of Subtitle


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/subscene/subtitle.rb', line 6

def initialize(attributes = {})
  @attributes = attributes
  @id         = @attributes[:id]
  @name       = @attributes[:name]
  @lang       = @attributes[:lang]
  @user       = @attributes[:user]
  @user_id    = @attributes[:user_id]
  @comment    = @attributes[:comment]
  @rating     = @attributes[:rating]
  @downloads  = @attributes[:downloads]
  @framerate  = @attributes[:framerate]
  @created_at = @attributes[:created_at]
  @download_url = @attributes[:download_url]
  @hearing_impaired = @attributes[:hearing_impaired]
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes


3
4
5
# File 'lib/subscene/subtitle.rb', line 3

def attributes
  @attributes
end

#commentObject

Returns the value of attribute comment


3
4
5
# File 'lib/subscene/subtitle.rb', line 3

def comment
  @comment
end

#created_atObject

Returns the value of attribute created_at


3
4
5
# File 'lib/subscene/subtitle.rb', line 3

def created_at
  @created_at
end

#download_urlObject

Returns the value of attribute download_url


3
4
5
# File 'lib/subscene/subtitle.rb', line 3

def download_url
  @download_url
end

#downloadsObject

Returns the value of attribute downloads


3
4
5
# File 'lib/subscene/subtitle.rb', line 3

def downloads
  @downloads
end

#framerateObject

Returns the value of attribute framerate


3
4
5
# File 'lib/subscene/subtitle.rb', line 3

def framerate
  @framerate
end

#hearing_impairedObject

Returns the value of attribute hearing_impaired


3
4
5
# File 'lib/subscene/subtitle.rb', line 3

def hearing_impaired
  @hearing_impaired
end

#idObject

Returns the value of attribute id


3
4
5
# File 'lib/subscene/subtitle.rb', line 3

def id
  @id
end

#langObject

Returns the value of attribute lang


3
4
5
# File 'lib/subscene/subtitle.rb', line 3

def lang
  @lang
end

#nameObject

Returns the value of attribute name


3
4
5
# File 'lib/subscene/subtitle.rb', line 3

def name
  @name
end

#ratingObject

Returns the value of attribute rating


3
4
5
# File 'lib/subscene/subtitle.rb', line 3

def rating
  @rating
end

#userObject

Returns the value of attribute user


3
4
5
# File 'lib/subscene/subtitle.rb', line 3

def user
  @user
end

#user_idObject

Returns the value of attribute user_id


3
4
5
# File 'lib/subscene/subtitle.rb', line 3

def user_id
  @user_id
end

Class Method Details

.build(html) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/subscene/subtitle.rb', line 40

def self.build(html)
  new({
    id: (html.css("nav.comment-sub a").to_s.match(/subtitleId=(\d+)/)[1] rescue nil),
    name: (html.css("li.release").children.last.text.strip rescue nil),
    lang: (html.css("a#downloadButton").text.match(/Download (.*)\n/)[1] rescue nil),
    user: (html.css("li.author").text.strip rescue nil),
    user_id: (html.css("li.author a").attribute("href").value.match(/\d+/).to_s rescue nil),
    comment: (html.css("div.comment").text.strip rescue nil),
    rating: (html.css("div.rating").text.strip rescue nil),
    downloads: (html.css("div.details li[contains('Downloads')]").children.last.text.strip rescue nil),
    framerate: (html.css("div.details li[contains('Framerate')]").children.last.text.strip rescue nil),
    created_at: (html.css("div.details li[contains('Online')]").children.last.text.strip rescue nil),
    download_url: (html.css("a#downloadButton").attr("href").value rescue nil),
    hearing_impaired: (html.css("div.details li[contains('Hearing')]").children.last.text.strip != "No" rescue nil)
  })
end

Instance Method Details

#downloadObject

Public: Download a Subtitle file.

Examples

Subscene.find(136037).download
# => #<Faraday::Response #@env={:body=>"PK\u00...",
  :response_headers=>{"content-type"=>"application/x-zip-compressed"} [..]>

Returns a Faraday::Response instance.


32
33
34
35
36
37
38
# File 'lib/subscene/subtitle.rb', line 32

def download
  conn = Faraday.new(url: Subscene::ENDPOINT)
  conn.post do |req|
    req.url download_url
    req.headers['Referer'] = "#{Subscene::ENDPOINT}/#{id}"
  end
end