aperitifilm 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 79f21d08244d42dbbf48154c243ab9f53e16bfab
4
+ data.tar.gz: 33a414d69845a03fd2b0a50b569c5ebf8f2007a4
5
+ SHA512:
6
+ metadata.gz: f670be1ea6e4d140ba554b3d01902095c9b4543cb37924eb3cc77afce6e651aaad9ca0c200d49f7e64d3d109b3b0024412a208c3aeb4f09da23a6e3f3ee142d1
7
+ data.tar.gz: 93bd90bbec8ac0596d22b7bc258076f52e1d4adfe24493c566b2a0ecef9525780e1c047454e92280fa718744f23b91c0492b65ea2d63b5b5afe96606d72eac71
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aperitifilm.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Rafał Cieślak
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,33 @@
1
+ # Aperitifilm
2
+
3
+ Aperitifilm let's you see which movies you and your friends on Filmweb would like to watch the most together.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'aperitifilm'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install aperitifilm
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ Aperitifilm.run %w(username1 username2 username3)
25
+ ```
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it ( https://github.com/ravicious/aperitifilm/fork )
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'aperitifilm/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "aperitifilm"
8
+ spec.version = Aperitifilm::VERSION
9
+ spec.authors = ["Rafał Cieślak"]
10
+ spec.email = ["ravicious@gmail.com"]
11
+ spec.summary = %q{Let's you see which movies you and your friends on Filmweb would like to watch the most together.}
12
+ spec.homepage = "https://github.com/ravicious/aperitifilm"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_runtime_dependency "nokogiri", "~> 1.6.5"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.7"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "pry", "~> 0.10.1"
25
+ end
@@ -0,0 +1,100 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+
4
+ require_relative './aperitifilm/version.rb'
5
+ require_relative './aperitifilm/movie.rb'
6
+ require_relative './aperitifilm/ranking.rb'
7
+ require_relative './aperitifilm/ranking_position.rb'
8
+ require_relative './aperitifilm/blank_position.rb'
9
+
10
+ module Aperitifilm
11
+ def self.run(usernames, number_of_movies = 20)
12
+ usernames = Array(usernames)
13
+
14
+ movie_listings = usernames.map do |username|
15
+ puts "Fetching movies for #{username}"
16
+ fetch_movies(username)
17
+ end
18
+
19
+ ranking = create_ranking(movie_listings)
20
+
21
+ puts "Found #{ranking.positions.size} movies"
22
+
23
+ print_ranking(ranking, number_of_movies)
24
+ end
25
+
26
+ private
27
+
28
+ # TODO: move all of this stuff to separate classes
29
+
30
+ def self.fetch_movies(username)
31
+ movies_url = "http://www.filmweb.pl/user/#{username}/films/wanna-see"
32
+
33
+ doc = Nokogiri::HTML(open(movies_url)) do |config|
34
+ config.noent
35
+ end
36
+
37
+ raw_movies = doc.css(".wantToSeeSee td").each_slice(2)
38
+
39
+ ranking = Ranking.new
40
+
41
+ raw_movies.each do |raw_movie|
42
+ movie_cell, score_cell = raw_movie
43
+
44
+ title_attribute = movie_cell.attributes.fetch('sorttable_customkey')
45
+
46
+ movie_title = title_attribute.value
47
+
48
+ movie_link = movie_cell.at_css('a')
49
+
50
+ movie_id = movie_link.attributes.fetch('href').value
51
+ movie_id = movie_id[1..-1] # Cut the first char, which is /
52
+
53
+ movie_score = Integer(score_cell.attributes.fetch('sorttable_customkey').value)
54
+
55
+ movie = Movie.new(movie_id, movie_title)
56
+
57
+ ranking.add_position movie, movie_score
58
+ end
59
+
60
+ if ranking.positions.to_a.empty?
61
+ raise "#{username} has no movies that they would like to watch"
62
+ end
63
+
64
+ ranking
65
+ end
66
+
67
+ def self.create_ranking(movie_rankings)
68
+ all_movies = movie_rankings.map { |ranking| ranking.positions.map(&:item) }.flatten.uniq
69
+
70
+ ranking = Ranking.new
71
+
72
+ all_movies.map do |movie|
73
+ ranking.add_position movie, score_movie(movie, movie_rankings)
74
+ end
75
+
76
+ ranking
77
+ end
78
+
79
+ def self.score_movie(movie, rankings)
80
+ scores_sum = rankings.map { |ranking| ranking.find_position(movie.id) { BlankPosition.new }.score }.inject(:+)
81
+
82
+ # Bonus is the number of users who also want to watch a particular movie
83
+ # times the number of users.
84
+ bonus = rankings.count { |ranking| ranking.find_position(movie.id) { nil } } / rankings.size.to_f
85
+
86
+ scores_sum * bonus
87
+ end
88
+
89
+ def self.print_ranking(ranking, number_of_movies = 20)
90
+ string_format = "%-2s %-32s %4s %s"
91
+
92
+ puts string_format % %w(# Title Score URL)
93
+
94
+ ranking.positions_with_ordinal(number_of_movies) do |position, ordinal|
95
+ puts string_format % [ordinal, position.item.title, position.score.round(2), position.item.url]
96
+ end
97
+
98
+ nil
99
+ end
100
+ end
@@ -0,0 +1,7 @@
1
+ module Aperitifilm
2
+ class BlankPosition < RankingPosition
3
+ def initialize
4
+ super(nil, 0)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ module Aperitifilm
2
+ FILMWEB_URL = 'http://www.filmweb.pl'.freeze
3
+
4
+ class Movie < Struct.new(:id, :title)
5
+ def url
6
+ "#{Aperitifilm::FILMWEB_URL}/#{id}"
7
+ end
8
+
9
+ def ==(other_movie)
10
+ self.id == other_movie.id
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,48 @@
1
+ require_relative './ranking_position.rb'
2
+
3
+ module Aperitifilm
4
+ class Ranking
5
+ PositionNotFound = Class.new(KeyError)
6
+ PositionAlreadyExists = Class.new(RuntimeError)
7
+
8
+ def initialize
9
+ @positions = {}
10
+ end
11
+
12
+ def positions(limit = @positions.size)
13
+ return enum_for(:positions, limit) unless block_given?
14
+
15
+ limit = Integer(limit)
16
+
17
+ @positions.values.sort.take(limit).each do |position|
18
+ yield position
19
+ end
20
+ end
21
+
22
+ def positions_with_ordinal(limit)
23
+ return enum_for(:positions_with_ordinal) unless block_given?
24
+
25
+ positions(limit).each_with_index do |position, index|
26
+ yield position, index + 1
27
+ end
28
+ end
29
+
30
+ def find_position(id)
31
+ @positions.fetch(id) do
32
+ if block_given?
33
+ yield id
34
+ else
35
+ raise PositionNotFound, "Couldn't find a position with ID '#{id}'."
36
+ end
37
+ end
38
+ end
39
+
40
+ def add_position(item, score)
41
+ if @positions[item.id]
42
+ raise PositionAlreadyExists, "Position with an item with ID '#{item.id}' already exists."
43
+ else
44
+ @positions[item.id] = RankingPosition.new(item, score)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,16 @@
1
+ module Aperitifilm
2
+ class RankingPosition
3
+ include Comparable
4
+
5
+ attr_reader :item, :score
6
+
7
+ def initialize(item, score)
8
+ @item = item
9
+ @score = Float(score)
10
+ end
11
+
12
+ def <=>(other_position)
13
+ (self.score <=> other_position.score) * -1
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module Aperitifilm
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aperitifilm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Rafał Cieślak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.6.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.6.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.1
69
+ description:
70
+ email:
71
+ - ravicious@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - aperitifilm.gemspec
82
+ - lib/aperitifilm.rb
83
+ - lib/aperitifilm/blank_position.rb
84
+ - lib/aperitifilm/movie.rb
85
+ - lib/aperitifilm/ranking.rb
86
+ - lib/aperitifilm/ranking_position.rb
87
+ - lib/aperitifilm/version.rb
88
+ homepage: https://github.com/ravicious/aperitifilm
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.4.5
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Let's you see which movies you and your friends on Filmweb would like to
112
+ watch the most together.
113
+ test_files: []