snoo 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/examples/modmailer/Gemfile +4 -0
- data/examples/modmailer/modmail.rb +185 -0
- data/lib/snoo/subreddits.rb +3 -3
- data/lib/snoo/version.rb +1 -1
- metadata +4 -2
@@ -0,0 +1,185 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'snoo'
|
3
|
+
require 'highline/import'
|
4
|
+
require 'tempfile'
|
5
|
+
require 'optparse'
|
6
|
+
|
7
|
+
options = {}
|
8
|
+
OptionParser.new do |opts|
|
9
|
+
opts.banner = "Usage: mod_mailer.rb [options]"
|
10
|
+
|
11
|
+
opts.on("-n", "Don't actually send the messages or Log in to reddit.") do
|
12
|
+
options[:norun] = true
|
13
|
+
end
|
14
|
+
|
15
|
+
opts.on("-a", "Auto-mode. Disables yN prompt. Does not disable needed info prompts, so use the other flags.") do
|
16
|
+
options[:quiet] = true
|
17
|
+
end
|
18
|
+
|
19
|
+
opts.separator ""
|
20
|
+
opts.separator "Authentication"
|
21
|
+
opts.on("-u", "--username USERNAME", "The username you want to log into reddit with.") do |user|
|
22
|
+
options[:username] = user
|
23
|
+
end
|
24
|
+
opts.on("-p", "--password PASSWORD", "Your reddit password. This may show up in your terminal history.") do |pass|
|
25
|
+
options[:password] = pass
|
26
|
+
end
|
27
|
+
|
28
|
+
opts.separator ""
|
29
|
+
opts.separator "Message options"
|
30
|
+
opts.on("-r", "--subreddit SUBREDDIT", "The subreddit of the moderators you want to fetch from") do |subreddit|
|
31
|
+
if subreddit =~ /\A[A-Za-z0-9][A-Za-z0-9_]{2,20}\z/
|
32
|
+
options[:subreddit] = subreddit
|
33
|
+
else
|
34
|
+
raise "Invalid subreddit name"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
opts.on("-s", "--subject SUBJECT", "The subject of the modmail message") do |subject|
|
38
|
+
options[:subject] = subject
|
39
|
+
end
|
40
|
+
opts.on("-m", "--message MESSAGE", "The message you want to send.") do |msg|
|
41
|
+
options[:message] = message
|
42
|
+
end
|
43
|
+
end.parse!
|
44
|
+
|
45
|
+
# Some code i shamelessly copied from Pry.
|
46
|
+
def blocking_flag_for_editor(block, editor_name)
|
47
|
+
case editor_name
|
48
|
+
when /^emacsclient/
|
49
|
+
'--no-wait' unless block
|
50
|
+
when /^[gm]vim/
|
51
|
+
'--nofork' if block
|
52
|
+
when /^jedit/
|
53
|
+
'-wait' if block
|
54
|
+
when /^mate/, /^subl/
|
55
|
+
'-w' if block
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def start_line_syntax_for_editor(editor_name, file_name, line_number)
|
60
|
+
|
61
|
+
# special case for 1st line
|
62
|
+
return file_name if line_number <= 1
|
63
|
+
|
64
|
+
case editor_name
|
65
|
+
when /^[gm]?vi/, /^emacs/, /^nano/, /^pico/, /^gedit/, /^kate/
|
66
|
+
"+#{line_number} #{file_name}"
|
67
|
+
when /^mate/, /^geany/
|
68
|
+
"-l #{line_number} #{file_name}"
|
69
|
+
when /^subl/
|
70
|
+
"#{file_name}:#{line_number}"
|
71
|
+
when /^uedit32/
|
72
|
+
"#{file_name}/#{line_number}"
|
73
|
+
when /^jedit/
|
74
|
+
"#{file_name} +line:#{line_number}"
|
75
|
+
else
|
76
|
+
"+#{line_number} #{file_name}"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
reddit = Snoo::Client.new
|
81
|
+
|
82
|
+
if options[:username]
|
83
|
+
username = options[:username]
|
84
|
+
else
|
85
|
+
username = ask("Username: ") { |c| c.readline = true }
|
86
|
+
end
|
87
|
+
|
88
|
+
if options[:password]
|
89
|
+
password = options[:password]
|
90
|
+
else
|
91
|
+
password = ask("Password: ") { |c| c.echo = '*' ; c.readline = true}
|
92
|
+
end
|
93
|
+
|
94
|
+
if options[:norun]
|
95
|
+
puts HighLine.color("Not actually logging in due to -n", :cyan)
|
96
|
+
else
|
97
|
+
puts HighLine.color("Logging in", :yellow)
|
98
|
+
reddit.log_in(username, password)
|
99
|
+
puts HighLine.color("Success", :green)
|
100
|
+
end
|
101
|
+
|
102
|
+
if options[:subreddit]
|
103
|
+
subreddit = options[:subreddit]
|
104
|
+
else
|
105
|
+
puts "Subreddit you want mods for"
|
106
|
+
subreddit = ask("/r/ ") do |r|
|
107
|
+
# Checks to see if its a valid subreddit, using the regex reddit itself uses
|
108
|
+
r.validate = /\A[A-Za-z0-9][A-Za-z0-9_]{2,20}\z/
|
109
|
+
r.responses[:not_valid] = "Thats not a valid subreddit. Lets try again:"
|
110
|
+
r.readline = true
|
111
|
+
end
|
112
|
+
end
|
113
|
+
mods = []
|
114
|
+
puts HighLine.color("Getting mods from #{HighLine.color(subreddit, :blue)}", :yellow)
|
115
|
+
reddit.get_moderators(subreddit)['data']['children'].each do |moderator|
|
116
|
+
mods << moderator["name"]
|
117
|
+
end
|
118
|
+
puts HighLine.color("Got #{mods.size} mods!", :green)
|
119
|
+
|
120
|
+
# Store the message in a hash
|
121
|
+
message = {}
|
122
|
+
|
123
|
+
if options[:subject]
|
124
|
+
message['subject'] = options[:subject]
|
125
|
+
else
|
126
|
+
message['subject'] = ask("Subject: ") do |subj|
|
127
|
+
subj.validate = /\A.*\z/
|
128
|
+
subj.responses[:not_valid] = "Subjects can't contain newlines, doofus"
|
129
|
+
subj.readline = true
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
if options[:message]
|
134
|
+
message['message'] = options[:message]
|
135
|
+
else
|
136
|
+
modmessage_path = Tempfile.new("modmsg.md").path
|
137
|
+
|
138
|
+
File.open(modmessage_path, 'w') do |f|
|
139
|
+
f.puts "// Edit your moderator message below"
|
140
|
+
f.puts "// Lines that begin with // are comments. They will not be sent."
|
141
|
+
f.puts "// If the file is empty, the program will terminate"
|
142
|
+
f.puts "\n"
|
143
|
+
f.puts "// Moderators this will message:"
|
144
|
+
mods.each do |mod|
|
145
|
+
f.puts "// * #{mod}"
|
146
|
+
end
|
147
|
+
end
|
148
|
+
editor_name = ENV['EDITOR']
|
149
|
+
system "#{editor_name} #{blocking_flag_for_editor(true, editor_name)} #{start_line_syntax_for_editor(editor_name, modmessage_path, 4)}"
|
150
|
+
|
151
|
+
message['message'] = File.read(modmessage_path)
|
152
|
+
end
|
153
|
+
raise "Empty message" unless message['message'].size > 0
|
154
|
+
|
155
|
+
# Strip the comments
|
156
|
+
message['message'].gsub!(/^\/{2}.*$\n/, '').chomp!
|
157
|
+
|
158
|
+
raise "Empty message" unless message['message'].size > 0
|
159
|
+
|
160
|
+
puts HighLine.color("This is a preview of your message:", :yellow)
|
161
|
+
puts "Subject: #{message['subject']}"
|
162
|
+
puts "-" * 4
|
163
|
+
puts message['message']
|
164
|
+
puts "-" * 4
|
165
|
+
|
166
|
+
unless options[:quiet]
|
167
|
+
exit unless agree(HighLine.color("Do you want to send a message to #{mods.size} moderators? ", :yellow)) do |agreement|
|
168
|
+
agreement.default = 'N'
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
if options[:norun]
|
173
|
+
puts HighLine.color("Not sending messages due to -n", :cyan)
|
174
|
+
else
|
175
|
+
puts HighLine.color("Sending messages", :green)
|
176
|
+
count = 0
|
177
|
+
mods.each do |mod|
|
178
|
+
count += 1
|
179
|
+
puts HighLine.color("Sending message to #{HighLine.color(mod, :blue)}", :yellow)
|
180
|
+
reddit.send_pm(mod, message['subject'], message['message'])
|
181
|
+
puts HighLine.color("Success!, #{count}/#{mods.size} mods done", :green)
|
182
|
+
puts "Sleeping for 3s"
|
183
|
+
sleep(3)
|
184
|
+
end
|
185
|
+
end
|
data/lib/snoo/subreddits.rb
CHANGED
@@ -122,8 +122,8 @@ module Snoo
|
|
122
122
|
raise ArgumentError, "condition must be one of subscriber, contributor, moderator; is #{opts[:condition]}" unless %w{subscriber contributor moderator}.include?(opts[:condition]) or opts[:condition].nil?
|
123
123
|
raise ArgumentError, "limit must be within 1..100; is #{opts[:limit]}" unless (1..100).include?(opts[:limit]) or opts[:limit].nil?
|
124
124
|
url = "/reddits/mine/%s.json" % (opts[:condition] if opts[:condition])
|
125
|
-
opts.delete
|
126
|
-
query =
|
125
|
+
opts.delete :condition
|
126
|
+
query = opts
|
127
127
|
get(url, query: query)
|
128
128
|
end
|
129
129
|
|
@@ -140,7 +140,7 @@ module Snoo
|
|
140
140
|
raise ArgumentError, "limit must be within 1..100; is #{opts[:limit]}" unless (1..100).include?(opts[:limit]) or opts[:limit].nil?
|
141
141
|
|
142
142
|
url = "/reddits/%s.json" % (opts[:condition] if opts[:condition])
|
143
|
-
opts.delete
|
143
|
+
opts.delete :condition
|
144
144
|
query = opts
|
145
145
|
|
146
146
|
get(url, query: query)
|
data/lib/snoo/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: snoo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-10-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: httparty
|
@@ -79,6 +79,8 @@ files:
|
|
79
79
|
- examples/flairbot/README.md
|
80
80
|
- examples/flairbot/config.yaml
|
81
81
|
- examples/flairbot/flair.rb
|
82
|
+
- examples/modmailer/Gemfile
|
83
|
+
- examples/modmailer/modmail.rb
|
82
84
|
- lib/snoo.rb
|
83
85
|
- lib/snoo/account.rb
|
84
86
|
- lib/snoo/exceptions.rb
|