hnhome 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/hnhome +79 -0
- data/lib/hnhome_version.rb +3 -0
- metadata +64 -0
data/bin/hnhome
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
require 'uri'
|
5
|
+
require 'nokogiri'
|
6
|
+
|
7
|
+
LIST_FOLDER = Dir.home + "/.hnhome"
|
8
|
+
LIST_PATH = LIST_FOLDER + "/hnhome"
|
9
|
+
URL = "https://news.ycombinator.com/"
|
10
|
+
|
11
|
+
def hnhome_store
|
12
|
+
return if Dir.exists?(LIST_FOLDER)
|
13
|
+
Dir.mkdir(LIST_FOLDER)
|
14
|
+
end
|
15
|
+
|
16
|
+
def news_home_response
|
17
|
+
uri = URI(URL)
|
18
|
+
|
19
|
+
http = Net::HTTP.new(uri.host, uri.port);
|
20
|
+
http.use_ssl = true
|
21
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
22
|
+
|
23
|
+
req = Net::HTTP::Get.new(uri.request_uri)
|
24
|
+
|
25
|
+
response = http.request(req)
|
26
|
+
response
|
27
|
+
end
|
28
|
+
|
29
|
+
def process_tlc(f,tlc,i)
|
30
|
+
title = tlc[1][0].css('.title a').text
|
31
|
+
comment_root = tlc[1][1].css('.subtext a')
|
32
|
+
link_root = tlc[1][0].css('.title a')[0]['href']
|
33
|
+
comment = if comment_root.empty?
|
34
|
+
link = URL + link_root
|
35
|
+
comment_count = "N/A"
|
36
|
+
"none"
|
37
|
+
else
|
38
|
+
comment_count = comment_root[1].text
|
39
|
+
link = link_root
|
40
|
+
URL + comment_root[1]['href']
|
41
|
+
end
|
42
|
+
|
43
|
+
comment_text = comment_count == "N/A" ? "- N/A" : "- #{comment_count}"
|
44
|
+
|
45
|
+
colorful_index = "\e[31m#{i+1})\e[0m"
|
46
|
+
puts "#{colorful_index} #{title} #{comment_text}"
|
47
|
+
f.puts "#{title}:mistral:#{link}:mistral:#{comment}"
|
48
|
+
end
|
49
|
+
|
50
|
+
def link_open(type)
|
51
|
+
i = (type == "link") ? ARGV[0].to_i : ARGV[1].to_i
|
52
|
+
raise ArgumentError.new("Index must be between 1 and 30") if i < 1 || i > 30
|
53
|
+
tlc = IO.readlines(LIST_PATH)[i - 1]
|
54
|
+
choice = (type == "link") ? 1 : 2
|
55
|
+
`open #{tlc.split(":mistral:")[choice]}`
|
56
|
+
end
|
57
|
+
|
58
|
+
if ARGV.length == 0
|
59
|
+
if news_home_response.is_a?(Net::HTTPSuccess)
|
60
|
+
doc = Nokogiri::HTML(news_home_response.body)
|
61
|
+
else
|
62
|
+
raise "Shit happened."
|
63
|
+
end
|
64
|
+
arry = doc.css('table table tr')
|
65
|
+
arry = arry[1..89]
|
66
|
+
arry = arry.group_by {|e| arry.find_index(e) / 3}
|
67
|
+
|
68
|
+
hnhome_store
|
69
|
+
|
70
|
+
File.open(LIST_PATH,"w") do |f|
|
71
|
+
arry.each_with_index {|tlc,i| process_tlc(f,tlc,i)}
|
72
|
+
end
|
73
|
+
elsif ARGV.length == 1
|
74
|
+
link_open("link")
|
75
|
+
elsif ARGV.length == 2 && (ARGV[0] == '-c' || ARGV[0] == '--comment')
|
76
|
+
link_open("comment")
|
77
|
+
else
|
78
|
+
raise ArgumentError.new("Unidentified flying arguments.")
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hnhome
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mistral Contrastin
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-16 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: '0'
|
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: '0'
|
30
|
+
description: Lists Hacker News articles and opens the links/comments.
|
31
|
+
email: me@madgen.net
|
32
|
+
executables:
|
33
|
+
- hnhome
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- lib/hnhome_version.rb
|
38
|
+
- bin/hnhome
|
39
|
+
homepage: http://rubygems.org/gems/hnhome
|
40
|
+
licenses: []
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.8.23
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Command line tool for browsing Hacker News homepage.
|
64
|
+
test_files: []
|