ud 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/bin/ud CHANGED
@@ -1,12 +1,36 @@
1
1
  #! /usr/bin/env ruby
2
2
  # -*- coding: UTF-8 -*-
3
3
 
4
+ require 'trollop'
4
5
  require 'ud'
5
6
 
6
- if ARGV.size == 1
7
- q = UD.query(ARGV[0])
8
- UD.format_results(q)
9
- else
10
- puts "Usage:\n\tud <word>"
7
+ opts = Trollop.options do
8
+ version "ud #{UD.version}"
9
+ banner <<-EOS
10
+ UD is a command-line tool to scrape definitions from the Urban Dictionary.
11
+
12
+ Usage:
13
+ ud [options] <word(s)>
14
+ where [options] are:
15
+ EOS
16
+
17
+ opt :ratio, 'Filter by upvotes/downvotes ratio', :type => :float, :default => 0.0
18
+ opt :count, 'Limit the number of definitions', :type => :int, :default => 10, :short => '-n'
19
+ opt :color, 'Use colorized output', :default => true
20
+ opt :up, 'Shortcut for \'-r 2\''
21
+
22
+ end
23
+
24
+ Trollop.die :ratio, 'must be non-negative' if opts[:ratio] < 0
25
+ Trollop.die :count, 'must be non-negative' if opts[:count] < 0
26
+
27
+ opts[:ratio] = 2 if opts[:up]
28
+
29
+ if ARGV.empty?
30
+ puts 'Error: No word provided. Use -h or --help to see the help.'
31
+ exit 1
11
32
  end
12
33
 
34
+ q = UD.query(ARGV.join(' '), opts)
35
+ puts UD.format_results(q, opts[:color])
36
+
data/lib/ud.rb CHANGED
@@ -6,90 +6,137 @@ require 'json'
6
6
  require 'open-uri'
7
7
  require 'nokogiri'
8
8
 
9
- module UD
9
+ require File.dirname(__FILE__) + '/ud/formatting'
10
10
 
11
- # Get the search URL to query for a given term
12
- def UD.search_url(term)
13
- param = URI.encode_www_form('term' => term)
14
- "http://www.urbandictionary.com/define.php?#{param}"
15
- end
11
+ # This module provide some methods to scrape definitions from the Urban
12
+ # Dictionary website.
13
+ module UD
16
14
 
17
- # Get the thumbs (up/down) for a list of definitions (ids)
18
- def UD.thumbs(ids)
15
+ # The current version of the module
16
+ def UD.version
17
+ '0.1.1'
18
+ end
19
19
 
20
- param = URI.encode_www_form('ids' => ids.join(','))
21
- json = open "http://api.urbandictionary.com/v0/uncacheable?#{param}"
20
+ # Get the search URL to query for a given term.
21
+ # [term] the term to search for. It must be a string, spaces are allowed
22
+ def UD.search_url(term='')
23
+ param = URI.encode_www_form('term' => term)
24
+ "http://www.urbandictionary.com/define.php?#{param}"
25
+ end
22
26
 
23
- response = JSON.parse(json.read)
24
- thumbs = {}
27
+ # Get the thumbs (up/down) for a list of definitions' ids.
28
+ # This is an helper for internal usage.
29
+ def UD.thumbs(ids)
25
30
 
26
- response['thumbs'].each do |t|
31
+ param = URI.encode_www_form('ids' => ids.join(','))
32
+ json = open "http://api.urbandictionary.com/v0/uncacheable?#{param}"
27
33
 
28
- thumbs[t['defid']] = {
29
- :up => t['thumbs_up'],
30
- :down => t['thumbs_down']
31
- }
34
+ response = JSON.parse(json.read)
35
+ thumbs = {}
32
36
 
33
- end
37
+ response['thumbs'].each do |t|
34
38
 
35
- thumbs
36
- end
39
+ thumbs[t['defid']] = {
40
+ :up => t['thumbs_up'],
41
+ :down => t['thumbs_down']
42
+ }
37
43
 
38
- # Get the text of an element
39
- def UD.text(el)
40
- el.text.strip.gsub(/\r/, "\n")
41
- rescue
42
- ''
43
44
  end
44
45
 
45
- # Query the website and return a list of definitions for the provided term.
46
- # This list may be empty if there's no results. It only scraps the first
47
- # page of results, since it's generally sufficient.
48
- def UD.query(term)
49
- url = search_url(term)
50
- doc = Nokogiri::HTML(open(url))
46
+ thumbs
47
+ end
51
48
 
52
- return [] unless doc.css('#not_defined_yet').empty?
49
+ # Get the text of an element. This is an helper for internal usage.
50
+ def UD.text(el)
51
+ el.text.strip.gsub(/\r/, "\n")
52
+ rescue
53
+ ''
54
+ end
53
55
 
54
- words = doc.css('.word[data-defid]')
55
- ids = words.map { |w| w.attr('data-defid') }
56
+ # Query the website and return a list of definitions for the provided term.
57
+ # This list may be empty if there's no result. It only scraps the first
58
+ # page of results.
59
+ # [term] the term to search for
60
+ # [opts] options. This is used by the command-line tool. +:count+ is the
61
+ # maximum number of results to return, +:ratio+ is the minimum
62
+ # upvotes/downvotes ratio. Other options may be added in the future.
63
+ def UD.query(term, *opts)
56
64
 
57
- thumbs = thumbs(ids)
65
+ opts = {:count => 10, :ratio => 0.0}.merge(opts[0] || {})
58
66
 
59
- ids.map do |id|
67
+ return [] if opts[:count] <= 0
60
68
 
61
- word = text doc.css(".word[data-defid=\"#{id}\"] > span").first
62
- body = doc.css("#entry_#{id}")
63
- t = thumbs[id.to_i] || {}
69
+ url = search_url(term)
70
+ doc = Nokogiri::HTML(open(url))
64
71
 
65
- {
66
- :id => id,
67
- :word => word,
68
- :definition => text(body.css('.definition')),
69
- :example => text(body.css('.example')),
70
- :upvotes => t[:up],
71
- :downvotes => t[:down]
72
+ return [] unless doc.css('#not_defined_yet').empty?
72
73
 
73
- }
74
+ words = doc.css('.word[data-defid]')
75
+ ids = words.take(opts[:count]).map { |w| w.attr('data-defid') }
74
76
 
75
- end
77
+ thumbs = thumbs(ids)
76
78
 
79
+ if opts[:ratio] > 0
80
+ ids.delete_if do |id|
81
+ t = thumbs[id.to_i] || {:up => 1, :down => 1}
82
+ (t[:up] / t[:down].to_f) < opts[:ratio]
83
+ end
77
84
  end
78
85
 
79
- # Format results for output, and print them
80
- def UD.format_results(results)
86
+ ids.map do |id|
81
87
 
82
- results.each do |r|
88
+ word = text doc.css(".word[data-defid=\"#{id}\"] > span").first
89
+ body = doc.css("#entry_#{id}")
90
+ t = thumbs[id.to_i] || {}
83
91
 
84
- puts "* #{r[:word]} (#{r[:upvotes]}/#{r[:downvotes]}):"
85
- puts ''
86
- puts " \t#{r[:definition].gsub(/\n/, "\n\t")}\n"
87
- puts ' Example:'
88
- puts " \t#{r[:example].gsub(/\n/, "\n\t")}"
89
- puts "\n\n"
92
+ {
93
+ :id => id,
94
+ :word => word,
95
+ :definition => text(body.css('.definition')),
96
+ :example => text(body.css('.example')),
97
+ :upvotes => t[:up],
98
+ :downvotes => t[:down]
90
99
 
91
- end
100
+ }
92
101
 
93
102
  end
94
103
 
104
+ end
105
+
106
+ # Format results for output
107
+ # [results] this must be an array of results, as returned by +UD.query+.
108
+ def UD.format_results(results, color=true)
109
+ require 'colored' if color
110
+
111
+ t = ' ' # 4-spaces tab
112
+
113
+ results.map do |r|
114
+
115
+ word = r[:word]
116
+ upvotes = r[:upvotes]
117
+ downvotes = r[:downvotes]
118
+
119
+ if (color)
120
+ word = word.bold
121
+ upvotes = upvotes.to_s.green
122
+ downvotes = downvotes.to_s.red
123
+ end
124
+
125
+ votes = "#{upvotes}/#{downvotes}"
126
+ definition = UD::Formatting.fit(r[:definition], 75).map {|l| t+l}.join("\n")
127
+ example = UD::Formatting.fit(r[:example], 75).map {|l| t+l}.join("\n")
128
+
129
+ s = ''
130
+
131
+ s << "* #{word} (#{votes}):\n"
132
+ s << "\n"
133
+ s << definition
134
+ s << "\n\n Example:\n"
135
+ s << example
136
+ s << "\n\n"
137
+
138
+ end.join("\n")
139
+
140
+ end
141
+
95
142
  end
@@ -0,0 +1,15 @@
1
+ #! /usr/bin/env ruby
2
+ # -*- coding: UTF-8 -*-
3
+
4
+ module UD
5
+ module Formatting
6
+
7
+ # Fit a text in a given width (number of chars). It returns
8
+ # a list of lines of text.
9
+ def self.fit(txt, width=79)
10
+ # from http://stackoverflow.com/a/7567210/735926
11
+ txt.split("\n").map{|l| l.scan(/(.{1,#{width}})(?:\s|$)/m) }.flatten
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,73 @@
1
+ #! /usr/bin/env ruby
2
+ # -*- coding: UTF-8 -*-
3
+
4
+ class FakeHTMLElement
5
+
6
+ attr_reader :text
7
+
8
+ def initialize(txt='')
9
+ @text = txt
10
+ end
11
+ end
12
+
13
+ class UD_Formatting_test < Test::Unit::TestCase
14
+
15
+ # == UD#text == #
16
+
17
+ def test_text_empty
18
+ el = FakeHTMLElement.new
19
+ assert_equal('', UD.text(el))
20
+ end
21
+
22
+ def test_text_trailing_spaces
23
+ el = FakeHTMLElement.new('foo ')
24
+ assert_equal('foo', UD.text(el))
25
+
26
+ el = FakeHTMLElement.new(" bar \n")
27
+ assert_equal('bar', UD.text(el))
28
+ end
29
+
30
+ def test_text_newline
31
+ el = FakeHTMLElement.new("a\rb")
32
+ assert_equal("a\nb", UD.text(el))
33
+
34
+ el = FakeHTMLElement.new("a\nb")
35
+ assert_equal("a\nb", UD.text(el))
36
+ end
37
+
38
+ def test_text_invalid_element
39
+ assert_equal('', UD.text(nil))
40
+ assert_equal('', UD.text(true))
41
+ assert_equal('', UD.text('foo'))
42
+ end
43
+
44
+ # == UD#format_results == #
45
+
46
+ def test_format_results_empty_list
47
+ assert_equal('', UD.format_results([]))
48
+ end
49
+
50
+ def test_format_results_one_element
51
+ res = {
52
+ :word => 'XYZ',
53
+ :upvotes => 42,
54
+ :downvotes => 78,
55
+ :definition => 'xyz',
56
+ :example => 'zyx'
57
+ }
58
+
59
+ output = UD.format_results([res], false).strip
60
+ expected = <<EOS
61
+ * XYZ (42/78):
62
+
63
+ xyz
64
+
65
+ Example:
66
+ zyx
67
+ EOS
68
+
69
+ assert_equal(expected.strip, output)
70
+
71
+ end
72
+
73
+ end
@@ -0,0 +1,111 @@
1
+ #! /usr/bin/env ruby
2
+ # -*- coding: UTF-8 -*-
3
+
4
+ require File.dirname(__FILE__) + '/fake_responses'
5
+
6
+ class UD_Formatting_test < Test::Unit::TestCase
7
+
8
+ ROOT_URL = 'http://www.urbandictionary.com'
9
+
10
+ # == UD#search_url == #
11
+
12
+ def test_search_url_empty_term
13
+ assert_equal(
14
+ "#{ROOT_URL}/define.php?term=",
15
+ UD.search_url())
16
+ assert_equal(
17
+ "#{ROOT_URL}/define.php?term=",
18
+ UD.search_url(''))
19
+ end
20
+
21
+ def test_search_url_spaces_in_term
22
+ assert_equal(
23
+ "#{ROOT_URL}/define.php?term=a+b",
24
+ UD.search_url('a b'))
25
+ end
26
+
27
+ def test_search_url_encode_special_chars
28
+ assert_equal(
29
+ "#{ROOT_URL}/define.php?term=%3D",
30
+ UD.search_url('='))
31
+ end
32
+
33
+ def test_search_url
34
+ assert_equal(
35
+ "#{ROOT_URL}/define.php?term=foo",
36
+ UD.search_url('foo'))
37
+ end
38
+
39
+ # == UD#thumbs == #
40
+
41
+ def test_thumbs_no_ids
42
+ assert_equal({}, UD.thumbs([]))
43
+ end
44
+
45
+ def test_thumbs_one_id
46
+ assert_equal({42 =>{:up => 2, :down => 1}}, UD.thumbs([42]))
47
+ end
48
+
49
+ # == UD#query == #
50
+
51
+ def test_query_no_results
52
+ assert_equal([], UD.query('nothing'))
53
+ end
54
+
55
+ def test_query_two_results
56
+
57
+ expected = [
58
+ {
59
+ :id => '1',
60
+ :word => 'foo',
61
+ :definition => 'A',
62
+ :example => 'AA',
63
+ :upvotes => 1,
64
+ :downvotes => 1
65
+
66
+ },
67
+ {
68
+ :id => '2',
69
+ :word => 'bar',
70
+ :definition => 'B',
71
+ :example => 'BB',
72
+ :upvotes => 2,
73
+ :downvotes => 1
74
+ }
75
+ ]
76
+
77
+ assert_equal(expected, UD.query('two'))
78
+ end
79
+
80
+ def test_query_count
81
+ expected = [
82
+ {
83
+ :id => '1',
84
+ :word => 'foo',
85
+ :definition => 'A',
86
+ :example => 'AA',
87
+ :upvotes => 1,
88
+ :downvotes => 1
89
+ }
90
+ ]
91
+
92
+ assert_equal(expected, UD.query('two', :count => 1))
93
+ end
94
+
95
+ def test_query_ratio
96
+ expected = [
97
+ {
98
+ :id => '2',
99
+ :word => 'bar',
100
+ :definition => 'B',
101
+ :example => 'BB',
102
+ :upvotes => 2,
103
+ :downvotes => 1
104
+ }
105
+ ]
106
+
107
+ assert_equal(expected, UD.query('two', :ratio => 1.5))
108
+ end
109
+
110
+ end
111
+
data/tests/tests.rb ADDED
@@ -0,0 +1,28 @@
1
+ #! /usr/bin/env ruby
2
+ # -*- coding: UTF-8 -*-
3
+
4
+ require 'test/unit'
5
+ require 'simplecov'
6
+
7
+ test_dir = File.expand_path( File.dirname(__FILE__) )
8
+
9
+ SimpleCov.start { add_filter '/tests/' } if ENV['COVERAGE']
10
+
11
+ require 'ud'
12
+
13
+ for t in Dir.glob( File.join( test_dir, '*_tests.rb' ) )
14
+ require t
15
+ end
16
+
17
+ class UDTests < Test::Unit::TestCase
18
+
19
+ # == UD#version == #
20
+
21
+ def test_ud_version
22
+ assert(UD.version =~ /^\d+\.\d+\.\d+/)
23
+ end
24
+
25
+ end
26
+
27
+
28
+ exit Test::Unit::AutoRunner.run
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ud
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,136 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-03 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2013-06-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.5.9
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.5.9
30
+ - !ruby/object:Gem::Dependency
31
+ name: json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.8.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.8.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: trollop
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '2.0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: colored
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '1.2'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '1.2'
78
+ - !ruby/object:Gem::Dependency
79
+ name: simplecov
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rake
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: test-unit
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: fakeweb
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
14
142
  description: Get words' definitions from Urban Dictionary on the command-line.
15
143
  email: batifon@yahoo.fr
16
144
  executables:
@@ -19,6 +147,10 @@ extensions: []
19
147
  extra_rdoc_files: []
20
148
  files:
21
149
  - lib/ud.rb
150
+ - lib/ud/formatting.rb
151
+ - tests/tests.rb
152
+ - tests/query_tests.rb
153
+ - tests/formatting_tests.rb
22
154
  - bin/ud
23
155
  homepage: https://github.com/bfontaine/ud
24
156
  licenses:
@@ -45,4 +177,7 @@ rubygems_version: 1.8.25
45
177
  signing_key:
46
178
  specification_version: 3
47
179
  summary: Urban Dictionary unofficial scrapper
48
- test_files: []
180
+ test_files:
181
+ - tests/tests.rb
182
+ - tests/query_tests.rb
183
+ - tests/formatting_tests.rb