usaidwat 0.0.3 → 0.0.4
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.
- data/bin/usaidwat +6 -0
- data/lib/usaidwat/reddit.rb +76 -92
- data/lib/usaidwat/utils.rb +25 -0
- data/lib/usaidwat/version.rb +1 -1
- metadata +5 -4
data/bin/usaidwat
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
require 'usaidwat'
|
4
|
+
require 'usaidwat/utils'
|
4
5
|
|
5
6
|
|
6
7
|
VERSION = "usaidwat v#{USaidWat::VERSION}"
|
@@ -34,7 +35,12 @@ if ARGV.length == 0
|
|
34
35
|
else
|
35
36
|
subreddit = ARGV.shift
|
36
37
|
comments = reddit_user.comments_for_subreddit subreddit
|
38
|
+
unless comments
|
39
|
+
puts "No comments by #{reddit_user.username} for #{subreddit}."
|
40
|
+
exit 0
|
41
|
+
end
|
37
42
|
is_first = true
|
43
|
+
run_pager
|
38
44
|
width = `tput cols`.to_i rescue 80
|
39
45
|
comments.each do |c|
|
40
46
|
puts '-' * width unless is_first
|
data/lib/usaidwat/reddit.rb
CHANGED
@@ -8,7 +8,7 @@ require 'json'
|
|
8
8
|
|
9
9
|
class Hash
|
10
10
|
def to_query
|
11
|
-
data =
|
11
|
+
data = keys.inject([]) { |parts, key| parts << [URI.encode(key.to_s), URI.encode(self[key].to_s)] }
|
12
12
|
data.map{ |t| t.join '=' }.join '&'
|
13
13
|
end
|
14
14
|
end
|
@@ -17,47 +17,75 @@ module USaidWat
|
|
17
17
|
class RedditUser
|
18
18
|
COMMENT_COUNT = 100
|
19
19
|
COMMENTS_PER_PAGE = 25
|
20
|
+
CACHE_AGE = 300
|
20
21
|
|
21
22
|
attr_reader :username
|
22
23
|
|
23
24
|
def initialize(username)
|
24
25
|
@username = username
|
25
|
-
self.ensure_cache_dir!
|
26
26
|
end
|
27
27
|
|
28
28
|
def profile_url
|
29
|
-
"http://www.reddit.com/user/#{
|
29
|
+
"http://www.reddit.com/user/#{username}"
|
30
30
|
end
|
31
31
|
|
32
32
|
def comments_url
|
33
|
-
"#{
|
33
|
+
"#{profile_url}/comments"
|
34
34
|
end
|
35
35
|
|
36
36
|
def last_update
|
37
|
-
return Time.at 0 unless File.exists?
|
38
|
-
Time.parse File.open(
|
37
|
+
return Time.at 0 unless File.exists? cache_timestamp_file
|
38
|
+
Time.parse File.open(cache_timestamp_file).read.chomp
|
39
|
+
end
|
40
|
+
|
41
|
+
def update_cache(force=false)
|
42
|
+
FileUtils.mkdir_p cache_dir
|
43
|
+
|
44
|
+
is_outdated = Time.now - last_update > CACHE_AGE
|
45
|
+
if force or is_outdated
|
46
|
+
FileUtils.rm_rf cache_dir
|
47
|
+
FileUtils.mkdir_p cache_dir
|
48
|
+
|
49
|
+
comments = Array.new
|
50
|
+
after = nil
|
51
|
+
pages = COMMENT_COUNT / COMMENTS_PER_PAGE
|
52
|
+
pages.times do |i|
|
53
|
+
query = i == 0 ? '' : {:count => COMMENTS_PER_PAGE, :after => after}.to_query
|
54
|
+
url = "#{comments_url}.json?#{query}"
|
55
|
+
resp = Net::HTTP.get_response 'www.reddit.com', url
|
56
|
+
unless resp.code.to_i == 200
|
57
|
+
$stderr.puts "Could not retrieve comments: #{resp.message}"
|
58
|
+
return nil
|
59
|
+
end
|
60
|
+
resp = JSON.parse resp.body
|
61
|
+
if resp.key? 'error'
|
62
|
+
$stderr.puts "Could not retrieve comments: #{resp['error'] || 'Unknown error'}"
|
63
|
+
return nil
|
64
|
+
end
|
65
|
+
comments += resp['data']['children']
|
66
|
+
after = resp['data']['after']
|
67
|
+
end
|
68
|
+
cache_comments comments
|
69
|
+
end
|
39
70
|
end
|
40
71
|
|
41
|
-
def retrieve_comments
|
42
|
-
|
43
|
-
count = options[:count] || COMMENT_COUNT
|
44
|
-
cache = options[:cache] || true
|
45
|
-
self.destroy_cache!
|
46
|
-
comments = self.fetch_comments count
|
47
|
-
return nil unless comments
|
48
|
-
self.cache_comments comments if cache
|
72
|
+
def retrieve_comments
|
73
|
+
update_cache
|
49
74
|
subreddits = Hash.new { |h,k| h[k] = 0 }
|
50
|
-
|
51
|
-
|
52
|
-
|
75
|
+
Dir.chdir(comments_dir) do
|
76
|
+
Dir['*'].each do |sr|
|
77
|
+
next unless File.directory? sr
|
78
|
+
Dir["#{sr}/*"].each { |f| subreddits[sr] += 1 }
|
79
|
+
end
|
53
80
|
end
|
54
81
|
subreddits.sort { |a,b| b[1] <=> a[1] }
|
55
82
|
end
|
56
83
|
|
57
84
|
def comments_for_subreddit(subreddit)
|
58
|
-
|
85
|
+
update_cache
|
59
86
|
comments = []
|
60
|
-
subreddit_dir = File.join
|
87
|
+
subreddit_dir = File.join comments_dir, subreddit
|
88
|
+
return nil unless File.exists? subreddit_dir
|
61
89
|
Dir.chdir(subreddit_dir) do
|
62
90
|
Dir['*'].each do |f|
|
63
91
|
path = File.join subreddit_dir, f
|
@@ -68,83 +96,39 @@ module USaidWat
|
|
68
96
|
comments
|
69
97
|
end
|
70
98
|
|
71
|
-
def
|
72
|
-
|
73
|
-
Dir.chdir(self.comments_dir) do
|
74
|
-
Dir['*'].each do |sr|
|
75
|
-
next unless File.directory? sr
|
76
|
-
Dir["#{sr}/*"].each { |f| subreddits[sr] += 1 }
|
77
|
-
end
|
78
|
-
end
|
79
|
-
subreddits.sort { |a,b| b[1] <=> a[1] }
|
99
|
+
def to_s
|
100
|
+
"Reddit user: #{username}"
|
80
101
|
end
|
81
102
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
103
|
+
private
|
104
|
+
def cache_dir
|
105
|
+
File.join USaidWat::BASE_CACHE_DIR, username
|
106
|
+
end
|
107
|
+
|
108
|
+
def cache_timestamp_file
|
109
|
+
File.join cache_dir, 'updated'
|
110
|
+
end
|
111
|
+
|
112
|
+
def comments_dir
|
113
|
+
File.join cache_dir, 'comments'
|
114
|
+
end
|
115
|
+
|
116
|
+
def cache_comments(comments)
|
117
|
+
comments.each do |c|
|
118
|
+
cid = c['data']['id']
|
119
|
+
sr = c['data']['subreddit']
|
120
|
+
body = c['data']['body']
|
121
|
+
parent_cache_dir = subreddit_directory! sr
|
122
|
+
cache_file = File.join parent_cache_dir, cid
|
123
|
+
File.open(cache_file, 'w') { |f| f.write body }
|
98
124
|
end
|
99
|
-
|
100
|
-
after = resp['data']['after']
|
125
|
+
File.open(cache_timestamp_file, 'w') { |f| f.write Time.now }
|
101
126
|
end
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
cid = c['data']['id']
|
108
|
-
sr = c['data']['subreddit']
|
109
|
-
body = c['data']['body']
|
110
|
-
parent_cache_dir = self.subreddit_directory! sr
|
111
|
-
cache_file = File.join parent_cache_dir, cid
|
112
|
-
File.open(cache_file, 'w') { |f| f.write body }
|
127
|
+
|
128
|
+
def subreddit_directory!(subreddit)
|
129
|
+
path = File.join comments_dir, subreddit
|
130
|
+
FileUtils.mkdir_p path
|
131
|
+
path
|
113
132
|
end
|
114
|
-
File.open(self.cache_timestamp_file, 'w') { |f| f.write Time.now }
|
115
|
-
end
|
116
|
-
|
117
|
-
def cache_dir
|
118
|
-
File.join USaidWat::BASE_CACHE_DIR, self.username
|
119
|
-
end
|
120
|
-
|
121
|
-
def cache_timestamp_file
|
122
|
-
File.join self.cache_dir, 'updated'
|
123
|
-
end
|
124
|
-
|
125
|
-
def comments_dir
|
126
|
-
File.join self.cache_dir, 'comments'
|
127
|
-
end
|
128
|
-
|
129
|
-
def subreddit_directory!(subreddit)
|
130
|
-
Dir.mkdir self.comments_dir unless File.exists? self.comments_dir
|
131
|
-
cache = File.join self.comments_dir, subreddit
|
132
|
-
Dir.mkdir cache unless File.exists? cache
|
133
|
-
cache
|
134
|
-
end
|
135
|
-
|
136
|
-
def ensure_cache_dir!
|
137
|
-
Dir.mkdir USaidWat::BASE_CACHE_DIR unless File.exists? USaidWat::BASE_CACHE_DIR
|
138
|
-
Dir.mkdir self.cache_dir unless File.exists? self.cache_dir
|
139
|
-
end
|
140
|
-
|
141
|
-
def destroy_cache!
|
142
|
-
FileUtils.rm_rf self.cache_dir
|
143
|
-
self.ensure_cache_dir!
|
144
|
-
end
|
145
|
-
|
146
|
-
def to_s
|
147
|
-
"Reddit user: #{self.username}"
|
148
|
-
end
|
149
133
|
end
|
150
134
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# Inspired by: http://nex-3.com/posts/73-git-style-automatic-paging-in-ruby
|
2
|
+
def run_pager
|
3
|
+
return if RUBY_PLATFORM =~ /win32/
|
4
|
+
return unless STDOUT.tty?
|
5
|
+
|
6
|
+
read, write = IO.pipe
|
7
|
+
|
8
|
+
unless Kernel.fork
|
9
|
+
STDOUT.reopen(write)
|
10
|
+
STDERR.reopen(write) if STDERR.tty?
|
11
|
+
read.close
|
12
|
+
write.close
|
13
|
+
return
|
14
|
+
end
|
15
|
+
|
16
|
+
STDIN.reopen(read)
|
17
|
+
read.close
|
18
|
+
write.close
|
19
|
+
|
20
|
+
#ENV['LESS'] = 'FSRX'
|
21
|
+
|
22
|
+
Kernel.select [STDIN]
|
23
|
+
pager = ENV['PAGER'] || 'less'
|
24
|
+
exec pager rescue exec '/bin/sh', '-c', pager
|
25
|
+
end
|
data/lib/usaidwat/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: usaidwat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-02-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|
16
|
-
requirement: &
|
16
|
+
requirement: &70304619242880 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: 1.6.5
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70304619242880
|
25
25
|
description: View a user's last 100 Reddit comments, organized by subreddit.
|
26
26
|
email:
|
27
27
|
- michael@monkey-robot.com
|
@@ -37,6 +37,7 @@ files:
|
|
37
37
|
- bin/usaidwat
|
38
38
|
- lib/usaidwat.rb
|
39
39
|
- lib/usaidwat/reddit.rb
|
40
|
+
- lib/usaidwat/utils.rb
|
40
41
|
- lib/usaidwat/version.rb
|
41
42
|
- usaidwat.gemspec
|
42
43
|
homepage: ''
|