ud 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bin/ud +12 -0
  2. data/lib/ud.rb +95 -0
  3. metadata +48 -0
data/bin/ud ADDED
@@ -0,0 +1,12 @@
1
+ #! /usr/bin/env ruby
2
+ # -*- coding: UTF-8 -*-
3
+
4
+ require 'ud'
5
+
6
+ if ARGV.size == 1
7
+ q = UD.query(ARGV[0])
8
+ UD.format_results(q)
9
+ else
10
+ puts "Usage:\n\tud <word>"
11
+ end
12
+
@@ -0,0 +1,95 @@
1
+ #! /usr/bin/env ruby
2
+ # -*- coding: UTF-8 -*-
3
+
4
+ require 'uri'
5
+ require 'json'
6
+ require 'open-uri'
7
+ require 'nokogiri'
8
+
9
+ module UD
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
16
+
17
+ # Get the thumbs (up/down) for a list of definitions (ids)
18
+ def UD.thumbs(ids)
19
+
20
+ param = URI.encode_www_form('ids' => ids.join(','))
21
+ json = open "http://api.urbandictionary.com/v0/uncacheable?#{param}"
22
+
23
+ response = JSON.parse(json.read)
24
+ thumbs = {}
25
+
26
+ response['thumbs'].each do |t|
27
+
28
+ thumbs[t['defid']] = {
29
+ :up => t['thumbs_up'],
30
+ :down => t['thumbs_down']
31
+ }
32
+
33
+ end
34
+
35
+ thumbs
36
+ end
37
+
38
+ # Get the text of an element
39
+ def UD.text(el)
40
+ el.text.strip.gsub(/\r/, "\n")
41
+ rescue
42
+ ''
43
+ end
44
+
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))
51
+
52
+ return [] unless doc.css('#not_defined_yet').empty?
53
+
54
+ words = doc.css('.word[data-defid]')
55
+ ids = words.map { |w| w.attr('data-defid') }
56
+
57
+ thumbs = thumbs(ids)
58
+
59
+ ids.map do |id|
60
+
61
+ word = text doc.css(".word[data-defid=\"#{id}\"] > span").first
62
+ body = doc.css("#entry_#{id}")
63
+ t = thumbs[id.to_i] || {}
64
+
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
+
73
+ }
74
+
75
+ end
76
+
77
+ end
78
+
79
+ # Format results for output, and print them
80
+ def UD.format_results(results)
81
+
82
+ results.each do |r|
83
+
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"
90
+
91
+ end
92
+
93
+ end
94
+
95
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ud
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Baptiste Fontaine
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-03 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Get words' definitions from Urban Dictionary on the command-line.
15
+ email: batifon@yahoo.fr
16
+ executables:
17
+ - ud
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/ud.rb
22
+ - bin/ud
23
+ homepage: https://github.com/bfontaine/ud
24
+ licenses:
25
+ - MIT
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.25
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: Urban Dictionary unofficial scrapper
48
+ test_files: []