-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTerminalBlog.rb
More file actions
executable file
·179 lines (159 loc) · 4.13 KB
/
TerminalBlog.rb
File metadata and controls
executable file
·179 lines (159 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/ruby
require 'rubygems'
require 'optparse'
require 'ostruct'
require 'google/apis/blogger_v3'
require 'google/api_client/client_secrets'
require 'json'
require 'launchy'
class TerminalBlog
@blogId = '' # the blog id
@isDraft = true # option to upload as draft. --publish argument overrides it
@postfile = '' # path to blogpost file
@f = nil # the read file
@title = 'Default Title' # default post title. -t argument overrides it
@labels = "label, label1" # default, comma-delimited list of labels for the post. -l argument overrides it
@body = nil
@options = nil
@client_secrets = nil
@auth_client = nil
@Blogger = nil
@service = nil
@args = ARGV
def self.main
tblog
argvCheck
requireTitle(@isDraft,@title)
authenticate
readFile
buildLabelList
buildBodyObject
getBlogId
postToBloggerService
displayPostsMetadata
end
def self.getBlogId
print "Enter your blogId: "
@blogId = STDIN.gets.to_i
end
def self.argvCheck
puts "...checking arguments..."
wait
if @args.size < 2 && @args[0] != "-h"
puts "-h for help"
puts "Posts are uploaded as drafts by default.\nUse --publish if you want to publish immediately"
exit
else
argvHandle
end
end
def self.argvHandle
puts "...processing arguments..."
wait
@options = OpenStruct.new
OptionParser.new do |opt|
opt.on('-f FI/LE/PATH', 'The path to the blogpost file') do |o|
@postfile = o
end
opt.on('-t "My Title"', 'The blogpost title') do |o|
@title = o
end
opt.on('-l "label, label1"', 'Comma-deliminated list of labels for the post') do |o|
@labels = o
end
opt.on('--publish','Publish the post directly, instead of uploading as draft') do |o|
@isDraft = false
end
opt.on('Example: TerminalBlog.rb -f path/to/file.txt -t "Terminal Post" -l "terminal, post" --publish')
end.parse!
end
def self.requireTitle(draft,title)
puts "...checking for title..."
wait
if draft == false && title == 'Default Title'
abort("You must provide a title if you want to publish.")
end
end
def self.authenticate
puts "...authenticating with Blogger..."
wait
@client_secrets = Google::APIClient::ClientSecrets.load("client_secrets.json")
@auth_client = @client_secrets.to_authorization
@auth_client.update!(:scope => 'https://www.googleapis.com/auth/blogger',
:redirect_uri => 'urn:ietf:wg:oauth:2.0:oob')
if File.exist?("userkey") == false
auth_uri = @auth_client.authorization_uri.to_s
Launchy.open(auth_uri)
print 'Paste the code from the auth response page: '
credential = gets
@auth_client.code = credential
@auth_client.fetch_access_token!
# store key for future reference
userkey = File.new("userkey","w")
userkey.puts(@auth_client.fetch_access_token.to_s)
initBloggerService(@auth_client)
else
credential = File.read("userkey")
@auth_client.code = credential
@auth_client.fetch_access_token!
initBloggerService(@auth_client)
end
end
def self.initBloggerService(authClient)
puts "...initializing Blogger service..."
wait
@Blogger = Google::Apis::BloggerV3
@service = @Blogger::BloggerService.new
@service.authorization = authClient
end
def self.readFile
puts "...reading blog file..."
wait
begin
@f = File.read(@postfile)
rescue Exception => e
abort("Error opening file. Aborting...")
end
end
def self.buildLabelList
puts "...attaching labels..."
wait
@labels_list = @labels.split(',')
end
def self.buildBodyObject
puts "...creating blog post..."
wait
@body = {
"content": @f,
"title": @title,
"labels": @labels_list
}
end
def self.postToBloggerService
puts "...posting to Blogger..."
wait
begin
@post = @service.insert_post(@blogId, post_object = @body, is_draft: @isDraft)
rescue Exception => e
abort("Google didn't like our post :(")
end
end
def self.displayPostsMetadata
puts "...generating operation details..."
wait
puts "Title: %s" % @post.title
puts "isDraft: %s" % @isDraft
if @isDraft == false
puts "URL: %s" % @post.url
end
puts "Labels: %s" % @post.labels
end
def self.wait
sleep(1)
end
def self.tblog
puts "...initializing..."
wait
end
main
end