Sha256: 14dbdea1542d051934d20d1bf752e1e115a144ef567c1da6eb1a5af3cd8d8f00

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

require 'simple-rss'
require 'ostruct'
require 'pry'

module Scraperd
  class Activity < ::OpenStruct
    
    def initialize(item)
      super(Hashie::Mash.new({
          id:         item[:guid].force_encoding("UTF-8"),
          title:      title_from_title(item[:title]),
          score:      score_from_title(item[:title]),
          link:       item[:link].force_encoding("UTF-8"),
          film_link:  "http://letterboxd.com/film/#{nicetitle_from_url(item[:link])}/".force_encoding("UTF-8"),
          watched_at: watched_at_from_description(item[:description]),
          added_at:   item[:pubDate]
        })
      )      
    end

    def nicetitle_from_url(url)
      url.split('/').last
    end

    def watched_at_from_description(description)
      string_date = description.match(/<p>Watched on (.*)<\/p>/)[1]
      Time.parse string_date
    end

    def title_from_title(title)
      title.split(' - ')[0..-2].join(' - ').force_encoding("UTF-8")
    end

    def score_from_title(title)
      html_score = title.split(' - ').last
      html_score.force_encoding("UTF-8")
      html_score.split(//).map{|ind_score| ind_score == "★" ? 2 : 1 }.inject{|sum,x| sum + x }
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scraperd-0.0.2 lib/scraperd/activity.rb