ayadn 1.3.2 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 02ca97ccdd5de6aeed988a3553764dfaeffd2cb6
4
- data.tar.gz: d1cfff95ffc3d561bb66d19198530efcbdf945f5
3
+ metadata.gz: 6276f752eb491dd844708b87857de9e31ce9e908
4
+ data.tar.gz: 53d08a3828136827d3125f47cdd340d924b6d55f
5
5
  SHA512:
6
- metadata.gz: 08706585750bc87fe826d5dff980c8736c67263051404cf8079f58240205be753aea42d7ab24e7308c13b1ef129914cdeeadf4c5b1e7bd33a1dac90bf2a9fa65
7
- data.tar.gz: d39427719037015468568672dec0950fe2e231accaf9e0db8821c72e014743c774352bf1cb0af38ac5117d72b1041ab4c8810bb0062d12b661329a598ec01072
6
+ metadata.gz: 7b629af3a71e42b0e9fa1b4d6082dba04cc744ba5effa5609eca9d0cc3f8bbd8ad14ca5eab57144323c6c1acd9f03806250d00009f8bc816fec2d52611365964
7
+ data.tar.gz: a4c36beb941ee96e3215783ec75fca897e62e0708e09a4238016d4ea2e6088b0073adf7c1d4e1447ecf407b39077b0d7e171f723101c49fcaf24cec5275ef86d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 1.4.0 - 'Florian'
2
+
3
+ - New feature: embed one or several pictures within a post (in write, post, reply, and pm)
4
+ - Nowplaying inserts preview URL and album art from iTunes Store (you can prevent this with: `--no-url`)
5
+
1
6
  # 1.3.2 - 'Max'
2
7
 
3
8
  - Fixed: Global stream crashed if NiceRank was enabled but unreachable
data/lib/ayadn/action.rb CHANGED
@@ -732,22 +732,6 @@ module Ayadn
732
732
  end
733
733
  end
734
734
 
735
- def post(args)
736
- begin
737
- @view.clear_screen
738
- puts Status.posting
739
- resp = Post.new.post(args)
740
- FileOps.save_post(resp) if Settings.options[:backup][:auto_save_sent_posts]
741
- @view.clear_screen
742
- puts Status.yourpost
743
- @view.show_posted(resp)
744
- rescue => e
745
- Errors.global_error("action/post", args, e)
746
- ensure
747
- Databases.close_all
748
- end
749
- end
750
-
751
735
  def auto(options)
752
736
  begin
753
737
  @view.clear_screen
@@ -769,29 +753,60 @@ module Ayadn
769
753
  end
770
754
  end
771
755
 
772
- def write
756
+ def post(args, options)
773
757
  begin
758
+ writer = Post.new
759
+ @view.clear_screen
760
+ if options['embed']
761
+ puts Status.uploading(options['embed'])
762
+ resp = writer.send_embedded(args.join(" "), FileOps.make_paths(options['embed']))
763
+ else
764
+ puts Status.posting
765
+ resp = writer.post(args)
766
+ end
767
+ FileOps.save_post(resp) if Settings.options[:backup][:auto_save_sent_posts]
768
+ @view.clear_screen
769
+ puts Status.yourpost
770
+ @view.show_posted(resp)
771
+ rescue => e
772
+ Errors.global_error("action/post", [args, options], e)
773
+ ensure
774
+ Databases.close_all
775
+ end
776
+ end
777
+
778
+ def write(options)
779
+ begin
780
+ files = FileOps.make_paths(options['embed']) if options['embed']
774
781
  writer = Post.new
775
782
  puts Status.writing
776
783
  puts Status.post
777
784
  lines_array = writer.compose
778
785
  writer.check_post_length(lines_array)
786
+ text = lines_array.join("\n")
787
+ if options['embed']
788
+ @view.clear_screen
789
+ puts Status.uploading(options['embed'])
790
+ resp = writer.send_embedded(text, files)
791
+ else
792
+ resp = writer.send_post(text)
793
+ end
779
794
  @view.clear_screen
780
795
  puts Status.posting
781
- resp = writer.send_post(lines_array.join("\n"))
782
796
  FileOps.save_post(resp) if Settings.options[:backup][:auto_save_sent_posts]
783
797
  @view.clear_screen
784
798
  puts Status.yourpost
785
799
  @view.show_posted(resp)
786
800
  rescue => e
787
- Errors.global_error("action/write", lines_array.join(" "), e)
801
+ Errors.global_error("action/write", [text, options], e)
788
802
  ensure
789
803
  Databases.close_all
790
804
  end
791
805
  end
792
806
 
793
- def pmess(username)
807
+ def pmess(username, options = {})
794
808
  begin
809
+ files = FileOps.make_paths(options['embed']) if options['embed']
795
810
  missing_username if username.empty?
796
811
  temp = Workers.add_arobase_if_missing(username)
797
812
  username = [temp]
@@ -800,15 +815,21 @@ module Ayadn
800
815
  puts Status.message
801
816
  lines_array = messenger.compose
802
817
  messenger.check_message_length(lines_array)
818
+ text = lines_array.join("\n")
803
819
  @view.clear_screen
804
- puts Status.posting
805
- resp = messenger.send_pm(username, lines_array.join("\n"))
820
+ if options['embed']
821
+ puts Status.uploading(options['embed'])
822
+ resp = messenger.send_pm_embedded(username, text, files)
823
+ else
824
+ puts Status.posting
825
+ resp = messenger.send_pm(username, text)
826
+ end
806
827
  FileOps.save_message(resp) if Settings.options[:backup][:auto_save_sent_messages]
807
828
  @view.clear_screen
808
829
  puts Status.yourmessage
809
830
  @view.show_posted(resp)
810
831
  rescue => e
811
- Errors.global_error("action/pmess", username, e)
832
+ Errors.global_error("action/pmess", [username, options], e)
812
833
  ensure
813
834
  Databases.close_all
814
835
  end
@@ -836,8 +857,9 @@ module Ayadn
836
857
  end
837
858
  end
838
859
 
839
- def reply(post_id)
860
+ def reply(post_id, options = {})
840
861
  begin
862
+ files = FileOps.make_paths(options['embed']) if options['embed']
841
863
  post_id = get_real_post_id(post_id)
842
864
  puts Status.replying_to(post_id)
843
865
  replied_to = @api.get_details(post_id)
@@ -856,36 +878,58 @@ module Ayadn
856
878
  poster.check_post_length(lines_array)
857
879
  @view.clear_screen
858
880
  reply = poster.reply(lines_array.join("\n"), Workers.new.build_posts([replied_to['data']]))
859
- puts Status.posting
860
- resp = poster.send_reply(reply, post_id)
881
+ if options['embed']
882
+ puts Status.uploading(options['embed'])
883
+ resp = poster.send_reply_embedded(reply, post_id, files)
884
+ else
885
+ puts Status.posting
886
+ resp = poster.send_reply(reply, post_id)
887
+ end
861
888
  FileOps.save_post(resp) if Settings.options[:backup][:auto_save_sent_posts]
862
889
  @view.clear_screen
863
890
  puts Status.done
864
891
  render_view(@api.get_convo(post_id, {}), {})
865
892
  rescue => e
866
- Errors.global_error("action/reply", post_id, e)
893
+ Errors.global_error("action/reply", [post_id, options], e)
867
894
  ensure
868
895
  Databases.close_all
869
896
  end
870
897
  end
871
898
 
872
- def nowplaying
899
+ def nowplaying(options = {})
873
900
  begin
874
901
  Databases.close_all
875
902
  abort(Status.error_only_osx) unless Settings.config[:platform] =~ /darwin/
876
- itunes = get_track_infos
903
+ itunes = get_track_infos()
877
904
  itunes.each {|el| abort(Status.empty_fields) if el.length == 0}
878
905
  @view.clear_screen
879
- text_to_post = "#nowplaying\nTitle: ‘#{itunes.track}’\nArtist: #{itunes.artist}\nfrom ‘#{itunes.album}’"
906
+ unless options['no_url']
907
+ store = itunes_request(itunes)
908
+ end
909
+ text_to_post = "#nowplaying\n \nTitle: ‘#{itunes.track}’\nArtist: #{itunes.artist}\nfrom ‘#{itunes.album}’"
880
910
  puts Status.writing
881
- show_nowplaying(text_to_post)
911
+ show_nowplaying("\n#{text_to_post}", options, store)
912
+ text_to_post += "\n \n[> Listen](#{store['preview']})" unless options['no_url']
882
913
  unless STDIN.getch == ("y" || "Y")
883
914
  puts "\nCanceled.\n\n".color(:red)
884
915
  exit
885
916
  end
886
917
  puts "\n"
887
918
  puts Status.yourpost
888
- @view.show_posted(Post.new.post([text_to_post]))
919
+ if options['no_url'].nil?
920
+ visible, track, artwork = true, store['track'], store['artwork']
921
+ else
922
+ visible, track, artwork = false, false, false
923
+ end
924
+ dic = {
925
+ 'text' => text_to_post,
926
+ 'title' => track,
927
+ 'artwork' => artwork,
928
+ 'width' => 1200,
929
+ 'height' => 1200,
930
+ 'visible' => visible
931
+ }
932
+ @view.show_posted(Post.new.send_nowplaying(dic))
889
933
  rescue => e
890
934
  puts Status.wtf
891
935
  Errors.global_error("action/nowplaying", itunes, e)
@@ -927,6 +971,24 @@ module Ayadn
927
971
 
928
972
  private
929
973
 
974
+ def itunes_request itunes
975
+ regex_exotics = /[~:-;,?!\'&`^=+<>*%()\/"“”’°£$€.…]/
976
+ store_artist = itunes.artist.gsub(regex_exotics, ' ').split(' ').join('+')
977
+ store_track = itunes.track.gsub(regex_exotics, ' ').split(' ').join('+')
978
+ #store_album = itunes.album.gsub(regex_exotics, ' ').split(' ').join('+')
979
+ itunes_url = "https://itunes.apple.com/search?term=#{store_artist}&term=#{store_track}&media=music&entity=musicTrack"
980
+ results = JSON.load(CNX.download(itunes_url))['results']
981
+ candidate = results[0]
982
+ {
983
+ 'artist' => candidate['artistName'],
984
+ 'track' => candidate['trackName'],
985
+ 'preview' => candidate['previewUrl'],
986
+ 'artwork' => candidate['artworkUrl100'].gsub('100x100', '1200x1200'),
987
+ 'request' => itunes_url,
988
+ 'results' => results
989
+ }
990
+ end
991
+
930
992
  def splitter_all words
931
993
  [words].collect {|w| w.split(' ')}
932
994
  end
@@ -1228,9 +1290,13 @@ module Ayadn
1228
1290
  maker.new(artist.chomp!, album.chomp!, track.chomp!)
1229
1291
  end
1230
1292
 
1231
- def show_nowplaying(text)
1293
+ def show_nowplaying(text, options, store)
1232
1294
  puts "\nYour post:\n".color(:cyan)
1233
- puts text + "\n\n"
1295
+ if options['no_url']
1296
+ puts text + "\n\n\n"
1297
+ else
1298
+ puts text + "\n\nAlbum artwork + 30 sec preview for track '#{store['track']}' by '#{store['artist']}' will be inserted in the post.\n\n\n".color(:green)
1299
+ end
1234
1300
  puts "Do you confirm? (y/N) ".color(:yellow)
1235
1301
  end
1236
1302
 
data/lib/ayadn/app.rb CHANGED
@@ -347,22 +347,25 @@ module Ayadn
347
347
  desc "post Your text", "Simple post to App.net (-P)"
348
348
  map "-P" => :post
349
349
  long_desc Descriptions.post
350
+ option :embed, aliases: "-e", type: :array, desc: "Embed one or several pictures in the new post"
350
351
  def post(*args)
351
- Action.new.post(args)
352
+ Action.new.post(args, options)
352
353
  end
353
354
 
354
355
  desc "write", "Multi-line post to App.net (-W)"
355
356
  map "compose" => :write
356
357
  map "-W" => :write
357
358
  long_desc Descriptions.write
359
+ option :embed, aliases: "-e", type: :array, desc: "Embed one or several pictures in the new post"
358
360
  def write
359
- Action.new.write
361
+ Action.new.write(options)
360
362
  end
361
363
 
362
364
  desc "pm @USERNAME", "Send a private message to @username"
363
365
  long_desc Descriptions.pmess
366
+ option :embed, aliases: "-e", type: :array, desc: "Embed one or several pictures in the new post"
364
367
  def pm(*username)
365
- Action.new.pmess(username)
368
+ Action.new.pmess(username, options)
366
369
  end
367
370
 
368
371
  desc "send CHANNEL", "Send a message to a CHANNEL (-C)"
@@ -375,9 +378,10 @@ module Ayadn
375
378
  desc "reply POST", "Reply to post n°POST (-R)"
376
379
  map "-R" => :reply
377
380
  long_desc Descriptions.reply
381
+ option :embed, aliases: "-e", type: :array, desc: "Embed one or several pictures in the new post"
378
382
  def reply(id)
379
383
  ayadn = Action.new
380
- ayadn.reply(id)
384
+ ayadn.reply(id, options)
381
385
  end
382
386
 
383
387
  desc "auto", "Auto post every line of input to App.net"
@@ -415,8 +419,9 @@ module Ayadn
415
419
  desc "nowplaying", "Post the current iTunes track (-np)"
416
420
  map "-np" => :nowplaying
417
421
  long_desc Descriptions.nowplaying
422
+ option :no_url, type: :boolean, desc: "Don't append preview or album art at the end of the post"
418
423
  def nowplaying
419
- Action.new.nowplaying
424
+ Action.new.nowplaying(options)
420
425
  end
421
426
 
422
427
  desc "random", "Show random posts from App.net (-rnd)"
data/lib/ayadn/cnx.rb CHANGED
@@ -2,6 +2,14 @@
2
2
  module Ayadn
3
3
  class CNX
4
4
 
5
+ def self.download url
6
+ begin
7
+ RestClient.get(url) {|response, request, result| response}
8
+ rescue => e
9
+ Errors.global_error("cnx.rb/download", url, e)
10
+ end
11
+ end
12
+
5
13
  def self.get url
6
14
  working = true
7
15
  begin
data/lib/ayadn/fileops.rb CHANGED
@@ -34,6 +34,36 @@ module Ayadn
34
34
  Dir.exist?(Dir.home + "/ayadn/data")
35
35
  end
36
36
 
37
+ def self.upload_files files
38
+ files.map do |file|
39
+ puts "\n#{file}\n\n"
40
+ JSON.load(self.upload(file, Settings.user_token))
41
+ end
42
+ end
43
+
44
+ def self.upload(path, token)
45
+ begin
46
+ file = Regexp.escape(path).gsub!('\.', '.')
47
+ case File.extname(path).downcase
48
+ when ".png"
49
+ `curl -k -H 'Authorization: BEARER #{token}' https://api.app.net/files -F 'type=com.ayadn.files' -F "content=@#{file};type=image/png" -F 'public=true' -X POST`
50
+ when ".gif"
51
+ `curl -k -H 'Authorization: BEARER #{token}' https://api.app.net/files -F 'type=com.ayadn.files' -F "content=@#{file};type=image/gif" -F 'public=true' -X POST`
52
+ else #jpg or jpeg or JPG or JPEG, automatically recognized as such
53
+ `curl -k -H 'Authorization: BEARER #{token}' https://api.app.net/files -F 'type=com.ayadn.files' -F content=@#{file} -F 'public=true' -X POST`
54
+ end
55
+ rescue Errno::ENOENT
56
+ abort(Status.no_curl)
57
+ end
58
+ end
59
+
60
+ def self.make_paths(files_array)
61
+ files_array.map do |file|
62
+ abort(Status.bad_path) unless File.exist?(file)
63
+ File.absolute_path(file)
64
+ end
65
+ end
66
+
37
67
  private
38
68
 
39
69
  def get_users(list)
data/lib/ayadn/post.rb CHANGED
@@ -21,6 +21,102 @@ module Ayadn
21
21
  # post
22
22
  end
23
23
 
24
+ def send_embedded text, files
25
+ send_embedded_pictures({'text' => text, 'data' => FileOps.upload_files(files)})
26
+ end
27
+
28
+ def send_reply_embedded text, reply_to, files
29
+ send_reply_embedded_pictures({'text' => text, 'reply_to' => reply_to, 'data' => FileOps.upload_files(files)})
30
+ end
31
+
32
+ def send_pm_embedded username, text, files
33
+ send_pm_embedded_pictures({'text' => text, 'username' => username, 'data' => FileOps.upload_files(files)})
34
+ end
35
+
36
+ def send_embedded_pictures dic
37
+ send_content(Endpoints.new.posts_url, payload_embedded(dic))
38
+ end
39
+
40
+ def send_reply_embedded_pictures dic
41
+ send_content(Endpoints.new.posts_url, payload_reply_embedded(dic))
42
+ end
43
+
44
+ def send_pm_embedded_pictures dic
45
+ url = Endpoints.new.pm_url
46
+ url << "?include_annotations=1&access_token=#{Ayadn::Settings.user_token}"
47
+ send_content(url, payload_pm_embedded(dic))
48
+ end
49
+
50
+ def send_nowplaying dic
51
+ send_content(Endpoints.new.posts_url, payload_nowplaying(dic))
52
+ end
53
+
54
+ def payload_nowplaying dic
55
+ ann = annotations()
56
+ if dic['visible'] == true
57
+ ann << {
58
+ "type" => "net.app.core.oembed",
59
+ "value" => {
60
+ "version" => "1.0",
61
+ "type" => "photo",
62
+ "width" => dic['width'],
63
+ "height" => dic['height'],
64
+ "title" => dic['title'],
65
+ "url" => dic['artwork'],
66
+ "embeddable_url" => dic['artwork']
67
+ }
68
+ }
69
+ end
70
+ {
71
+ "text" => dic['text'],
72
+ "entities" => entities,
73
+ "annotations" => ann
74
+ }
75
+ end
76
+
77
+ def payload_embedded dic
78
+ {
79
+ "text" => dic['text'],
80
+ "entities" => entities,
81
+ "annotations" => annotations_embedded(dic)
82
+ }
83
+ end
84
+
85
+ def payload_reply_embedded dic
86
+ {
87
+ "text" => dic['text'],
88
+ "reply_to" => dic['reply_to'],
89
+ "entities" => entities,
90
+ "annotations" => annotations_embedded(dic)
91
+ }
92
+ end
93
+
94
+ def payload_pm_embedded dic
95
+ {
96
+ "text" => dic['text'],
97
+ "entities" => entities,
98
+ "destinations" => dic['username'],
99
+ "annotations" => annotations_embedded(dic)
100
+ }
101
+ end
102
+
103
+ def annotations_embedded dic
104
+ base = annotations()
105
+ dic['data'].each do |obj|
106
+ base << {
107
+ "type" => "net.app.core.oembed",
108
+ "value" => {
109
+ "+net.app.core.file" => {
110
+ "file_id" => obj['data']['id'],
111
+ "file_token" => obj['data']['file_token'],
112
+ "format" => "oembed"
113
+ }
114
+ }
115
+ }
116
+ end
117
+ return base
118
+ end
119
+
24
120
  # def auto_classic
25
121
  # loop do
26
122
  # begin
@@ -82,14 +178,11 @@ module Ayadn
82
178
  end
83
179
 
84
180
  def send_pm(username, text)
85
- url = Endpoints.new.pm_url
86
- url << "?include_annotations=1&access_token=#{Ayadn::Settings.user_token}"
87
- send_content(url, payload_pm(username, text))
181
+ send_content(Endpoints.new.pm_url, payload_pm(username, text))
88
182
  end
89
183
 
90
184
  def send_message(channel_id, text)
91
- url = Endpoints.new.messages(channel_id, {})
92
- send_content(url, payload_basic(text))
185
+ send_content(Endpoints.new.messages(channel_id, {}), payload_basic(text))
93
186
  end
94
187
 
95
188
  # def send_log(data)
@@ -98,13 +191,11 @@ module Ayadn
98
191
  # end
99
192
 
100
193
  def send_post(text)
101
- url = Endpoints.new.posts_url
102
- send_content(url, payload_basic(text))
194
+ send_content(Endpoints.new.posts_url, payload_basic(text))
103
195
  end
104
196
 
105
197
  def send_reply(text, post_id)
106
- url = Endpoints.new.posts_url
107
- send_content(url, payload_reply(text, post_id))
198
+ send_content(Endpoints.new.posts_url, payload_reply(text, post_id))
108
199
  end
109
200
 
110
201
  def send_content(url, payload)
@@ -185,6 +276,8 @@ module Ayadn
185
276
  }
186
277
  end
187
278
 
279
+
280
+
188
281
  def payload_basic(text)
189
282
  {
190
283
  "text" => text,
data/lib/ayadn/status.rb CHANGED
@@ -10,6 +10,10 @@ module Ayadn
10
10
  def self.downloading
11
11
  "Downloading from ADN...\n\n".inverse
12
12
  end
13
+ def self.uploading files
14
+ files.length > 1 ? pl = "s" : pl = ""
15
+ "\nUploading file#{pl} to ADN...".color(:cyan)
16
+ end
13
17
  def self.posting
14
18
  "Posting to ADN...\n\n".inverse
15
19
  end
@@ -277,5 +281,11 @@ module Ayadn
277
281
  def self.no_text
278
282
  "\n\nYou should provide some text.\n\n".color(:red)
279
283
  end
284
+ def self.bad_path
285
+ "\n\nCouldn't upload this file (path seems wrong).\n\n".color(:red)
286
+ end
287
+ def self.no_curl
288
+ "\n\nAyadn needs 'curl' to upload files. Please install 'curl' (or check that it's properly declared in your $PATH).\n\n".color(:red)
289
+ end
280
290
  end
281
291
  end
data/lib/ayadn/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Ayadn
3
- VERSION = "1.3.2"
3
+ VERSION = "1.4.0"
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ayadn
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Dejonckheere
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-22 00:00:00.000000000 Z
11
+ date: 2014-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor