facebook-cli 1.3.16 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/fbcli.rb +47 -42
  3. data/lib/fbcli/facebook.rb +24 -74
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d3859d8e0b0e3a0defe8390292b70dd40fc9f298
4
- data.tar.gz: 3903b59c2894edd571f6e384356c4b9f1c3c7170
3
+ metadata.gz: 4e825bc8b6a218bd1cb559e8e4061b733d0ce3eb
4
+ data.tar.gz: 45cc917afd6dc9dcc6ec529a267c665760374bcd
5
5
  SHA512:
6
- metadata.gz: 3114a77b78c19c5381d64e8d5faf1538dd9a1975106f45fc8ae74ea624daa612ac59bb96ca28c1a28b6e9b57ada0cabceba8ddf4de855e7a8f835982f4326a90
7
- data.tar.gz: 7415d2da7ef29bb80aa49b72c7b97e7ae8e3f741621d5b8fdd1f8b48b1f9c77950d425611f2caa8a54e6d63090f1866901deb37cadff5ac8e1298b493c28b129
6
+ metadata.gz: 1971e9c34e24970570284fbcb6303388e53699f54ca7f8292fdaf650ae13399138e901db8b0e249524d957faaf41a53b45ab788c14739a4a0fe4bfdad435e204
7
+ data.tar.gz: b372d6298015575340ac147c374a02fc912c1383c2be24c220f6a437c488e9b58d100155c57fc69f469089ee268a2680e4f7c86a743c5cf93c1b14ec7048d2d2
data/lib/fbcli.rb CHANGED
@@ -10,16 +10,17 @@ include GLI::App
10
10
 
11
11
  program_desc "Facebook command line interface"
12
12
 
13
- version '1.3.16'
13
+ version '1.4.0'
14
14
 
15
15
  flag [:token], :desc => 'Provide Facebook access token', :required => false
16
- flag [:pages, :p], :desc => 'Max pages', :required => false, :default_value => -1
16
+ flag [:pages, :p], :desc => 'Max pages', :required => false, :type => Integer, :default_value => -1
17
17
 
18
18
  def link(path)
19
19
  "https://www.facebook.com/#{path}"
20
20
  end
21
21
 
22
- def link_to_post(profile_id, post_id)
22
+ def link_to_post(full_post_id)
23
+ profile_id, post_id = full_post_id.split '_', 2
23
24
  link "#{profile_id}/posts/#{post_id}"
24
25
  end
25
26
 
@@ -34,7 +35,9 @@ def save_config
34
35
  end
35
36
  end
36
37
 
37
- pre do |global_options,command|
38
+ pre do |global_options, command|
39
+ $global_options = global_options # They're supposed to be global, right?
40
+
38
41
  if command.name == :config
39
42
  $config = {}
40
43
  else
@@ -61,6 +64,11 @@ pre do |global_options,command|
61
64
  end
62
65
  end
63
66
 
67
+ # Let access token passed from the command line take precedence
68
+ if not global_options[:token].nil?
69
+ $config['access_token'] = global_options[:token]
70
+ end
71
+
64
72
  # Success
65
73
  true
66
74
  end
@@ -76,7 +84,7 @@ desc "Save Facebook application ID and secret"
76
84
  command :config do |c|
77
85
  c.flag [:appid], :desc => 'Facebook application ID', :required => true
78
86
  c.flag [:appsecret], :desc => 'Facebook application secret', :required => true
79
- c.action do |global_options,options,args|
87
+ c.action do |global_options, options|
80
88
  $config['app_id'] = options['appid'].to_i
81
89
  $config['app_secret'] = options['appsecret']
82
90
 
@@ -91,7 +99,7 @@ end
91
99
  desc "Log into Facebook and receive an access token"
92
100
  command :login do |c|
93
101
  c.flag [:port], :desc => 'Local TCP port to serve Facebook login redirect page', :default_value => '3333'
94
- c.action do |global_options,options,args|
102
+ c.action do |global_options, options|
95
103
  token, expiration = FBCLI::listen_for_auth_code(options['port'], $config['app_id'], $config['app_secret'])
96
104
 
97
105
  if not token.nil?
@@ -110,16 +118,16 @@ end
110
118
 
111
119
  desc "Deauthorize your access token"
112
120
  command :logout do |c|
113
- c.action do |global_options,options,args|
114
- FBCLI::logout global_options
121
+ c.action do
122
+ FBCLI::logout
115
123
  puts "You are now logged out."
116
124
  end
117
125
  end
118
126
 
119
127
  desc "Show your name and profile ID"
120
128
  command :me do |c|
121
- c.action do |global_options,options,args|
122
- FBCLI::request_object global_options, "me" do |data|
129
+ c.action do
130
+ FBCLI::request_object "me" do |data|
123
131
  puts "Name: #{data["name"]}"
124
132
  puts "ID: #{data["id"]}"
125
133
  end
@@ -130,14 +138,14 @@ desc "Post a message or image to your timeline"
130
138
  arg_name "message"
131
139
  command :post do |c|
132
140
  c.flag [:i, :image], :desc => 'File or URL of image to post'
133
- c.action do |global_options,options,args|
141
+ c.action do |global_options, options, args|
134
142
  if options['image'].nil?
135
- profile_id, post_id = FBCLI::publish_post global_options, args[0]
143
+ full_post_id = FBCLI::publish_post args[0]
136
144
  else
137
- profile_id, post_id = FBCLI::publish_photo global_options, args[0], options['image']
145
+ full_post_id = FBCLI::publish_image args[0], options['image']
138
146
  end
139
147
 
140
- puts "Your post: #{link_to_post profile_id, post_id}"
148
+ puts "Your post: #{link_to_post full_post_id}"
141
149
  end
142
150
  end
143
151
 
@@ -149,7 +157,7 @@ command :postlink do |c|
149
157
  c.flag [:d, :description], :desc => 'Link description'
150
158
  c.flag [:c, :caption], :desc => 'Link caption'
151
159
  c.flag [:i, :image], :desc => 'Link image URL'
152
- c.action do |global_options,options,args|
160
+ c.action do |global_options, options, args|
153
161
  link_metadata = {
154
162
  "name" => options['name'],
155
163
  "link" => args[0],
@@ -158,16 +166,16 @@ command :postlink do |c|
158
166
  "picture" => options['image']
159
167
  }
160
168
 
161
- profile_id, post_id = FBCLI::publish_link global_options, options['message'], link_metadata
169
+ full_post_id = FBCLI::publish_post options['message'], link_metadata
162
170
 
163
- puts "Your post: #{link_to_post profile_id, post_id}"
171
+ puts "Your post: #{link_to_post full_post_id}"
164
172
  end
165
173
  end
166
174
 
167
175
  desc "List the pages you have 'Liked'"
168
176
  command :likes do |c|
169
- c.action do |global_options,options,args|
170
- FBCLI::page_items global_options, 'likes', '' do |item|
177
+ c.action do
178
+ FBCLI::page_items 'likes', '' do |item|
171
179
  puts item["name"]
172
180
  puts link item["id"]
173
181
  end
@@ -183,8 +191,8 @@ long_desc %(
183
191
  See: https://developers.facebook.com/docs/apps/faq#faq_1694316010830088
184
192
  )
185
193
  command :friends do |c|
186
- c.action do |global_options,options,args|
187
- FBCLI::page_items global_options, 'taggable_friends' do |item|
194
+ c.action do
195
+ FBCLI::page_items 'taggable_friends' do |item|
188
196
  puts item['name']
189
197
  end
190
198
  end
@@ -192,12 +200,10 @@ end
192
200
 
193
201
  desc "List the posts on your profile"
194
202
  command :feed do |c|
195
- c.action do |global_options,options,args|
196
- FBCLI::page_items global_options, "feed", '- - -' do |item|
197
- profile_id, post_id = item["id"].split '_', 2
198
-
203
+ c.action do
204
+ FBCLI::page_items "feed", '- - -' do |item|
199
205
  puts item["message"] if item.has_key?("message")
200
- puts link_to_post profile_id, post_id
206
+ puts link_to_post item["id"]
201
207
  puts "Created: #{date_str(item["created_time"])}"
202
208
  end
203
209
  end
@@ -211,15 +217,15 @@ end
211
217
 
212
218
  desc "List photos you have uploaded"
213
219
  command :photos do |c|
214
- c.action do |global_options,options,args|
215
- FBCLI::page_items global_options, "photos?type=uploaded", '- - -', &handlePhoto
220
+ c.action do
221
+ FBCLI::page_items "photos?type=uploaded", '- - -', &handlePhoto
216
222
  end
217
223
  end
218
224
 
219
225
  desc "List photos you are tagged in"
220
226
  command :photosof do |c|
221
- c.action do |global_options,options,args|
222
- FBCLI::page_items global_options, "photos", '- - -', &handlePhoto
227
+ c.action do
228
+ FBCLI::page_items "photos", '- - -', &handlePhoto
223
229
  end
224
230
  end
225
231
 
@@ -231,19 +237,19 @@ end
231
237
 
232
238
  desc "List videos you have uploaded"
233
239
  command :videos do |c|
234
- c.action do |global_options,options,args|
235
- FBCLI::page_items global_options, "videos?type=uploaded", '- - -', &handleVideo
240
+ c.action do
241
+ FBCLI::page_items "videos?type=uploaded", '- - -', &handleVideo
236
242
  end
237
243
  end
238
244
 
239
245
  desc "List videos you are tagged in"
240
246
  command :videosof do |c|
241
- c.action do |global_options,options,args|
242
- FBCLI::page_items global_options, "videos", '- - -', &handleVideo
247
+ c.action do
248
+ FBCLI::page_items "videos", '- - -', &handleVideo
243
249
  end
244
250
  end
245
251
 
246
- def list_events(global_options, past = false)
252
+ def list_events(past = false)
247
253
  now = Time.new
248
254
 
249
255
  filter = lambda { |item|
@@ -251,7 +257,7 @@ def list_events(global_options, past = false)
251
257
  not ((past and starts < now) ^ (not past and starts > now))
252
258
  }
253
259
 
254
- FBCLI::page_items global_options, "events", '- - -', filter do |item|
260
+ FBCLI::page_items "events", '- - -', filter do |item|
255
261
  starts = Time.parse(item['start_time'])
256
262
 
257
263
  unless item['end_time'].nil?
@@ -272,25 +278,24 @@ end
272
278
 
273
279
  desc "List your upcoming events"
274
280
  command :events do |c|
275
- c.action do |global_options,options,args|
276
- list_events global_options
281
+ c.action do
282
+ list_events
277
283
  end
278
284
  end
279
285
 
280
286
  desc "List your past events"
281
287
  command :pastevents do |c|
282
- c.action do |global_options,options,args|
283
- list_events global_options, true
288
+ c.action do
289
+ list_events true
284
290
  end
285
291
  end
286
292
 
287
293
  desc "Show event details"
288
294
  arg_name "[ids...]"
289
295
  command :event do |c|
290
- c.action do |global_options,options,args|
296
+ c.action do |global_options, options, args|
291
297
  args.each_with_index do |id, index|
292
298
  FBCLI::request_object(
293
- global_options,
294
299
  id,
295
300
  :fields => 'name,description,place,owner,start_time,end_time,attending_count,declined_count,maybe_count,is_canceled'
296
301
  ) do |item|
@@ -3,12 +3,7 @@ require 'koala'
3
3
  module FBCLI
4
4
  @@api = nil
5
5
 
6
- def self.init_api(global_options)
7
- # Access token passed from the command line takes precedence
8
- if not global_options[:token].nil?
9
- $config['access_token'] = global_options[:token]
10
- end
11
-
6
+ def self.init_api
12
7
  if $config['access_token'].nil? or $config['access_token'].empty?
13
8
  exit_now! "You must first acquire an access token; run: #{APP_NAME} login"
14
9
  end
@@ -27,53 +22,41 @@ module FBCLI
27
22
  str
28
23
  end
29
24
 
30
- def self.logout(global_options)
31
- if @@api.nil?
32
- @@api = init_api(global_options)
33
- end
25
+ def self.api_call(lambda)
26
+ @@api = init_api if @@api.nil?
34
27
 
35
28
  begin
36
- @@api.delete_object("me/permissions")
29
+ lambda.call(@@api)
37
30
  rescue Koala::Facebook::APIError => e
38
31
  exit_now! koala_error_str e
39
32
  end
40
33
  end
41
34
 
42
- def self.request_object(global_options, id, options = {})
43
- if @@api.nil?
44
- @@api = init_api(global_options)
45
- end
35
+ def self.logout
36
+ api_call lambda { |api| api.delete_object("me/permissions") }
37
+ end
46
38
 
47
- begin
48
- @@api.get_object(id, options) do |data|
39
+ def self.request_object(id, options = {})
40
+ api_call lambda { |api|
41
+ api.get_object(id, options) do |data|
49
42
  yield data
50
43
  end
51
- rescue Koala::Facebook::APIError => e
52
- exit_now! koala_error_str e
53
- end
44
+ }
54
45
  end
55
46
 
56
- def self.request_personal_connections(global_options, cmd)
57
- if @@api.nil?
58
- @@api = init_api(global_options)
59
- end
60
-
61
- begin
62
- data = @@api.get_connections("me", cmd)
63
- rescue Koala::Facebook::APIError => e
64
- exit_now! koala_error_str e
65
- end
66
-
67
- data
47
+ def self.request_personal_connections(cmd)
48
+ api_call lambda { |api|
49
+ api.get_connections("me", cmd)
50
+ }
68
51
  end
69
52
 
70
- def self.page_items(global_options, cmd, separator = nil, filter = nil)
71
- items = request_personal_connections(global_options, cmd)
53
+ def self.page_items(cmd, separator = nil, filter = nil)
54
+ items = request_personal_connections(cmd)
72
55
 
73
56
  virgin = true
74
57
  count = 0
75
58
 
76
- while not (items.nil? or count == global_options['pages'].to_i) do
59
+ while not (items.nil? or count == $global_options['pages']) do
77
60
  items.each_with_index { |item, idx|
78
61
  if filter.nil? or not filter.call(item)
79
62
  unless separator.nil? or virgin
@@ -91,46 +74,13 @@ module FBCLI
91
74
  end
92
75
  end
93
76
 
94
- def self.publish_post(global_options, msg)
95
- if @@api.nil?
96
- @@api = init_api(global_options)
97
- end
98
-
99
- begin
100
- profile_id, post_id = @@api.put_wall_post(msg)['id'].split '_', 2
101
- rescue Koala::Facebook::APIError => e
102
- exit_now! koala_error_str e
103
- end
104
-
105
- [profile_id, post_id]
77
+ def self.publish_post(msg, link_metadata = {})
78
+ result = api_call lambda { |api| api.put_wall_post(msg, link_metadata) }
79
+ result['id']
106
80
  end
107
81
 
108
- def self.publish_link(global_options, msg, link_metadata)
109
- if @@api.nil?
110
- @@api = init_api(global_options)
111
- end
112
-
113
- begin
114
- profile_id, post_id = @@api.put_wall_post(msg, link_metadata)['id'].split '_', 2
115
- rescue Koala::Facebook::APIError => e
116
- exit_now! koala_error_str e
117
- end
118
-
119
- [profile_id, post_id]
120
- end
121
-
122
- def self.publish_photo(global_options, msg, image_file_or_url)
123
- if @@api.nil?
124
- @@api = init_api(global_options)
125
- end
126
-
127
- begin
128
- result = @@api.put_picture(image_file_or_url, {:message => msg})
129
- profile_id, post_id = result['post_id'].split '_', 2
130
- rescue Koala::Facebook::APIError => e
131
- exit_now! koala_error_str e
132
- end
133
-
134
- [profile_id, post_id]
82
+ def self.publish_image(msg, image_file_or_url)
83
+ result = api_call lambda { |api| api.put_picture(image_file_or_url, {:message => msg}) }
84
+ result['post_id']
135
85
  end
136
86
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: facebook-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.16
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ildar Sagdejev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-30 00:00:00.000000000 Z
11
+ date: 2016-10-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A limited command line interface to Facebook
14
14
  email: specious@gmail.com