lookupurl 0.0.0
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.
- checksums.yaml +7 -0
- data/lib/lookupurl.rb +93 -0
- metadata +44 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: b0d86a099a83f6f58a0b1f6eaf974ab64f783af8
|
|
4
|
+
data.tar.gz: bbadc55def823f9485bd1fad9505b1efe00d4c10
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 5a734c2f313435c262cd18d746fa49bf93959959127e8c51e672093c55cdcfcfde73977617c49e54f27b1decf7ec27a3f7edead0edc087c6fcafc914efdfa9a5
|
|
7
|
+
data.tar.gz: acb415a06af932eea79f2a486c220976c893537676651ced34c37458613a2a359f02f2ce5c7dd815492b9b3614b79e5fce8e52a437efcbab8d0fe3e989faa45d
|
data/lib/lookupurl.rb
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
require 'uri'
|
|
2
|
+
require 'httparty'
|
|
3
|
+
require 'nokogiri'
|
|
4
|
+
|
|
5
|
+
class LookUpUrl
|
|
6
|
+
attr_accessor :title, :description, :host, :url, :image_url
|
|
7
|
+
def crawl_url(link)
|
|
8
|
+
url = link
|
|
9
|
+
|
|
10
|
+
@url = url
|
|
11
|
+
@host = URI.parse(url).host
|
|
12
|
+
|
|
13
|
+
begin
|
|
14
|
+
# get the url content
|
|
15
|
+
response = HTTParty.get(url)
|
|
16
|
+
# /get the url content
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# nokogiri in action
|
|
20
|
+
doc = Nokogiri::HTML(response)
|
|
21
|
+
|
|
22
|
+
#----- description ----
|
|
23
|
+
@description = ""
|
|
24
|
+
doc.xpath("//meta[@name='description']/@content").each do |attr|
|
|
25
|
+
@description = attr.value
|
|
26
|
+
end
|
|
27
|
+
if @description == ""
|
|
28
|
+
doc.xpath("//p").each do |attr|
|
|
29
|
+
@description = attr.text
|
|
30
|
+
break
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
if @description == ""
|
|
34
|
+
doc.xpath("//meta[@name='description']/@content").each do |attr|
|
|
35
|
+
@description = ""
|
|
36
|
+
@description = attr.value
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
if @description == ""
|
|
40
|
+
doc.xpath("//meta[@property='og:description']/@content").each do |attr|
|
|
41
|
+
@description = ""
|
|
42
|
+
@description = attr.value
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
#----- /description --------
|
|
46
|
+
#----- images --------
|
|
47
|
+
images = []
|
|
48
|
+
doc.xpath("//img/@src").each do |attr|
|
|
49
|
+
images << attr.value if attr.value.include? ".jp" or attr.value.include? ".png"
|
|
50
|
+
end
|
|
51
|
+
doc.xpath("//meta[@property='og:image']/@content").each do |attr|
|
|
52
|
+
if attr.value.include? ".jp" or attr.value.include? ".png"
|
|
53
|
+
images = []
|
|
54
|
+
images << attr.value
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
image = images.first
|
|
58
|
+
#----- /images --------
|
|
59
|
+
#
|
|
60
|
+
#----- title --------
|
|
61
|
+
if doc.at_css("title")
|
|
62
|
+
@title = doc.at_css("title").text
|
|
63
|
+
else
|
|
64
|
+
@title = nil
|
|
65
|
+
end
|
|
66
|
+
#----- /title --------
|
|
67
|
+
# /nokogiri in action
|
|
68
|
+
#
|
|
69
|
+
|
|
70
|
+
# rescue
|
|
71
|
+
rescue
|
|
72
|
+
@title = @host
|
|
73
|
+
@description = @url
|
|
74
|
+
return false
|
|
75
|
+
end
|
|
76
|
+
# end rescue
|
|
77
|
+
if image == "" || image == nil
|
|
78
|
+
@image_url = ""
|
|
79
|
+
else
|
|
80
|
+
@image_url = image.force_encoding('iso-8859-1').encode('utf-8')
|
|
81
|
+
end
|
|
82
|
+
if @title == "" || @title == nil
|
|
83
|
+
@title = @host
|
|
84
|
+
else
|
|
85
|
+
@title = @title.force_encoding('iso-8859-1').encode('utf-8').to_s[0..100]
|
|
86
|
+
end
|
|
87
|
+
if @description == "" || @description == nil
|
|
88
|
+
@description = @url
|
|
89
|
+
else
|
|
90
|
+
@description = @description.force_encoding('iso-8859-1').encode('utf-8')
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: lookupurl
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Evan Purnama
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2015-04-05 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: A simple look up url gem
|
|
14
|
+
email: e@qiscus.com
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/lookupurl.rb
|
|
20
|
+
homepage: ''
|
|
21
|
+
licenses:
|
|
22
|
+
- MIT
|
|
23
|
+
metadata: {}
|
|
24
|
+
post_install_message:
|
|
25
|
+
rdoc_options: []
|
|
26
|
+
require_paths:
|
|
27
|
+
- lib
|
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
34
|
+
requirements:
|
|
35
|
+
- - ">="
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: '0'
|
|
38
|
+
requirements: []
|
|
39
|
+
rubyforge_project:
|
|
40
|
+
rubygems_version: 2.2.2
|
|
41
|
+
signing_key:
|
|
42
|
+
specification_version: 4
|
|
43
|
+
summary: Hola!
|
|
44
|
+
test_files: []
|