distraction 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/bin/distraction +39 -0
- data/lib/distraction.rb +121 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZmYyNDY1YmNkNjBiMzExY2JjNTQ3MzUxODdlYTY5OWE5ZDFhNGY1NA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
YzE3YTNiMTg1YmQ3YzViOTAzMGQxZWIxN2I2OGRkYWJkYzNkMzNhMQ==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NDZmYmY4MWY5YjFjMWFkOWY1MzBkYzQxN2YyMjM1NjE5NDBlNDg2YzgzMGQ4
|
10
|
+
NGE3MWIxZDQ1ZmU2YmUzYjc4NGQ0MzFkOTcwMjA3OTcwZTEyYWZlYjRhZjlj
|
11
|
+
NjA1MjZjYWFiYzIwODQ3OGZiODhhYWYxNmUzNWVlM2U0ZDdmOGI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
Yzc5ZjI0YmI5NzU5NDQ2N2QyNTg4ZTYzNWQ5OWEyMGUxMzBlNjIyODQ4MTA5
|
14
|
+
NmMyMmEyMTA4YzM1NzZiZGRhOTYzMGYyZDlhODk4NTYwMzBiMGFkOWJkOWVk
|
15
|
+
N2ZhYzMwNzBmOGQxMjlkMmRhNjcxN2Q0MzVlMDNjZWEwOGIwYjc=
|
data/bin/distraction
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'distraction'
|
4
|
+
require 'rubygems'
|
5
|
+
|
6
|
+
begin
|
7
|
+
# get arguments
|
8
|
+
args = ARGV.join(" ")
|
9
|
+
abort(Gem::Specification::load("distraction.gemspec").version.to_s) if args == "--version" || args == "-v"
|
10
|
+
|
11
|
+
case args
|
12
|
+
when /^reddit/
|
13
|
+
args = args.split(" ")
|
14
|
+
if args[1]
|
15
|
+
Distraction.reddit_top(args[1])
|
16
|
+
else
|
17
|
+
Distraction.reddit_top("all")
|
18
|
+
end
|
19
|
+
when "fact"
|
20
|
+
Distraction.reddit_fact(false)
|
21
|
+
when "fact open"
|
22
|
+
Distraction.reddit_fact(true)
|
23
|
+
when /^vid/
|
24
|
+
Distraction.reddit_vid()
|
25
|
+
when "hn"
|
26
|
+
Distraction.hn()
|
27
|
+
else
|
28
|
+
puts "I can't do that! But I can do the following:\n\n"\
|
29
|
+
" distraction reddit Displays the top ten posts on the Reddit homepage\n"\
|
30
|
+
" distraction reddit [subreddit] Displays the top ten posts in [subreddit]\n"\
|
31
|
+
" distraction fact Displays an interesting fact\n"\
|
32
|
+
" distraction fact open Displays a fact and opens up a URL with more information\n"\
|
33
|
+
" distraction vid Displays a random video\n"\
|
34
|
+
" distraction hn Displays the top ten posts on Hacker News"\
|
35
|
+
end
|
36
|
+
|
37
|
+
rescue Interrupt => e # handle CTRL + C cleanly
|
38
|
+
puts ""
|
39
|
+
end
|
data/lib/distraction.rb
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
class Distraction
|
5
|
+
# display the top link from TIL
|
6
|
+
def self.reddit_fact(open)
|
7
|
+
begin
|
8
|
+
ping = Net::HTTP.get(URI('http://www.reddit.com/r/todayilearned.json?limit=5'))
|
9
|
+
listing = JSON.parse(ping)
|
10
|
+
listing = listing['data']['children']
|
11
|
+
|
12
|
+
# check if NSFW
|
13
|
+
result = listing.each do |l|
|
14
|
+
break l['data'] if (!l['data']['over_18'])
|
15
|
+
end
|
16
|
+
result_title = result['title'].gsub('TIL', 'Did you know...')
|
17
|
+
result_domain = result['domain']
|
18
|
+
result_url = result['url']
|
19
|
+
|
20
|
+
case open
|
21
|
+
when true
|
22
|
+
puts result_title + "\n\nOpening link from (#{result_domain}) in 10 seconds... CTRL + C to quit."
|
23
|
+
sleep(10)
|
24
|
+
`open #{result_url}`
|
25
|
+
when false
|
26
|
+
puts result_title + "\n\nYou can type 'distraction fact open' to open the link in a browser."
|
27
|
+
end
|
28
|
+
|
29
|
+
rescue
|
30
|
+
puts "Reddit isn't responding right now. Let's try again later."
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# display the top ten links on Reddit frontpage
|
35
|
+
def self.reddit_top(subreddit)
|
36
|
+
begin
|
37
|
+
ping = Net::HTTP.get(URI('http://www.reddit.com/r/' + subreddit + '.json?limit=10'))
|
38
|
+
listing = JSON.parse(ping)
|
39
|
+
listing = listing['data']['children']
|
40
|
+
|
41
|
+
listing.each_with_index do |l, i|
|
42
|
+
line = " " + (i+1).to_s.rjust(2, " ") + ". " + l['data']['title'] + " (" + l['data']['domain'] + ")"
|
43
|
+
if l['data']['over_18']
|
44
|
+
line = line + " (NSFW)"
|
45
|
+
end
|
46
|
+
puts line
|
47
|
+
end
|
48
|
+
puts "\nWhich link would you like to open?"
|
49
|
+
story = STDIN.gets.chomp()
|
50
|
+
story = story.to_i
|
51
|
+
|
52
|
+
if ((story < 11) && (story > 0))
|
53
|
+
result = listing[story-1]['data']
|
54
|
+
result_domain = result['domain']
|
55
|
+
result_url = result['url']
|
56
|
+
puts "\nOpening link from (#{result_domain}) in 5 seconds... CTRL + C to quit."
|
57
|
+
sleep(5)
|
58
|
+
`open #{result_url}`
|
59
|
+
else
|
60
|
+
puts "Come on, son."
|
61
|
+
end
|
62
|
+
rescue
|
63
|
+
puts "Oops, either Reddit isn't responding or that subreddit doesn't exist!"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# picks a random video off r/videos
|
68
|
+
def self.reddit_vid()
|
69
|
+
begin
|
70
|
+
ping = Net::HTTP.get(URI('http://www.reddit.com/r/videos.json')) # gets 25 top vids
|
71
|
+
listing = JSON.parse(ping)
|
72
|
+
listing = listing['data']['children']
|
73
|
+
nsfw_flag = true # true means it is nsfw
|
74
|
+
randomVideo = 0
|
75
|
+
while nsfw_flag
|
76
|
+
randomVideo = rand(24) # random between 0 and 24
|
77
|
+
nsfw_flag = listing[randomVideo]['data']['over_18']
|
78
|
+
end
|
79
|
+
result = listing[randomVideo]['data']
|
80
|
+
result_domain = result['domain']
|
81
|
+
result_url = result['url']
|
82
|
+
puts "Opening video from (#{result_domain}) in 5 seconds... CTRL + C to quit."
|
83
|
+
sleep(5)
|
84
|
+
`open #{result_url}`
|
85
|
+
rescue
|
86
|
+
puts "Oops, something went wrong! Let's try again later."
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
# display the top ten links on HN homepage
|
91
|
+
def self.hn()
|
92
|
+
begin
|
93
|
+
ping = Net::HTTP.get(URI('http://api.ihackernews.com/page/'))
|
94
|
+
listing = JSON.parse(ping)
|
95
|
+
listing = listing['items']
|
96
|
+
i = 1
|
97
|
+
while (i < 11)
|
98
|
+
line = " " + i.to_s.rjust(2, " ") + ". " + listing[i]['title']
|
99
|
+
i += 1
|
100
|
+
puts line
|
101
|
+
end
|
102
|
+
puts "\nWhich article would you like to open?"
|
103
|
+
story = STDIN.gets.chomp()
|
104
|
+
story = story.to_i
|
105
|
+
|
106
|
+
if ((story < 11) && (story > 0))
|
107
|
+
result_url = listing[story]['url']
|
108
|
+
if result_url.start_with?("/comments/")
|
109
|
+
result_url = "http://news.ycombinator.com/item?id=#{listing[story]['id']}"
|
110
|
+
end
|
111
|
+
puts "\nOpening link (#{result_url}) in 5 seconds... CTRL + C to quit."
|
112
|
+
sleep(5)
|
113
|
+
`open #{result_url}`
|
114
|
+
else
|
115
|
+
puts "Come on, son."
|
116
|
+
end
|
117
|
+
rescue
|
118
|
+
puts "The HN API we're using isn't responding right now. Let's try again later."
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: distraction
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Frank Wu
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-08 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email: me@frankjwu.com
|
15
|
+
executables:
|
16
|
+
- distraction
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/distraction.rb
|
21
|
+
- bin/distraction
|
22
|
+
homepage:
|
23
|
+
licenses: []
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 2.0.5
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: A quick distraction in your command line for when you're way too deep in
|
45
|
+
your code.
|
46
|
+
test_files: []
|