ayadn 1.5.1 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ayadn/fileops.rb CHANGED
@@ -49,7 +49,7 @@ module Ayadn
49
49
  when ".gif"
50
50
  `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`
51
51
  else #jpg or jpeg or JPG or JPEG, automatically recognized as such
52
- `curl -k -H 'Authorization: BEARER #{token}' https://api.app.net/files -F 'type=com.ayadn.files' -F content=@#{file} -F 'public=true' -X POST`
52
+ `curl -k -H 'Authorization: BEARER #{token}' https://api.app.net/files -F 'type=com.ayadn.files' -F "content=@#{file}" -F 'public=true' -X POST`
53
53
  end
54
54
  rescue Errno::ENOENT
55
55
  abort(Status.no_curl)
data/lib/ayadn/mark.rb CHANGED
@@ -7,19 +7,20 @@ module Ayadn
7
7
  map "create" => :add
8
8
  def add(*args)
9
9
  begin
10
+ init
10
11
  unless args.empty?
11
12
  post_id, convo_title = args[0], args[1]
12
13
  else
13
- init
14
14
  abort Status.wrong_arguments
15
15
  end
16
16
  abort Status.error_missing_post_id unless post_id.is_integer?
17
17
  convo_title = post_id if convo_title.nil?
18
- action, workers, view, users, bucket = Action.new, Workers.new, View.new, [], []
18
+ api, workers, view = API.new, Workers.new, View.new
19
+ users, bucket = [], []
19
20
  view.clear_screen
20
21
  puts "\nAnalyzing conversation...\n".inverse
21
- stream = action.get_convo post_id, options
22
- posts = workers.build_posts(stream['data'].reverse)
22
+ resp = api.get_convo(post_id, options)
23
+ posts = workers.build_posts(resp['data'].reverse)
23
24
  posts.each do |id, post|
24
25
  users << "#{post[:original_poster]}"
25
26
  post[:mentions].each {|mention| users << "#{mention}"}
@@ -0,0 +1,184 @@
1
+ # encoding: utf-8
2
+ module Ayadn
3
+
4
+ class NowPlaying
5
+
6
+ require 'rss'
7
+
8
+ def initialize api, view, workers
9
+ @api = api
10
+ @view = view
11
+ @workers = workers
12
+ end
13
+
14
+ def lastfm options
15
+ begin
16
+ user = Settings.options[:nowplaying][:lastfm] || create_lastfm_user()
17
+ puts Status.fetching_from('Last.fm')
18
+ artist, track = get_lastfm_track_infos(user)
19
+ puts Status.itunes_store
20
+ store = lastfm_istore_request(artist, track) unless options['no_url']
21
+ text_to_post = "#nowplaying\n \nTitle: ‘#{track}’\nArtist: #{artist}"
22
+ post_nowplaying(text_to_post, store, options)
23
+ rescue => e
24
+ puts Status.wtf
25
+ Errors.global_error({error: e, caller: caller, data: [store, options]})
26
+ end
27
+ end
28
+
29
+ def itunes options
30
+ begin
31
+ abort(Status.error_only_osx) unless Settings.config[:platform] =~ /darwin/
32
+ puts Status.fetching_from('iTunes')
33
+ itunes = get_itunes_track_infos()
34
+ itunes.each {|el| abort(Status.empty_fields) if el.length == 0}
35
+ puts Status.itunes_store
36
+ store = itunes_istore_request(itunes) unless options['no_url']
37
+ text_to_post = "#nowplaying\n \nTitle: ‘#{itunes.track}’\nArtist: #{itunes.artist}\nfrom ‘#{itunes.album}’"
38
+ post_nowplaying(text_to_post, store, options)
39
+ rescue => e
40
+ puts Status.wtf
41
+ Errors.global_error({error: e, caller: caller, data: [itunes, store, options]})
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def get_lastfm_track_infos user
48
+ begin
49
+ url = "http://ws.audioscrobbler.com/2.0/user/#{user}/recenttracks.rss"
50
+ feed = RSS::Parser.parse(CNX.download(url))
51
+ lfm = feed.items[0].title.split(' – ')
52
+ return lfm[0], lfm[1]
53
+ rescue Interrupt
54
+ abort(Status.canceled)
55
+ end
56
+ end
57
+
58
+ def post_nowplaying text_to_post, store, options
59
+ begin
60
+ @view.clear_screen
61
+ puts Status.writing
62
+ show_nowplaying("\n#{text_to_post}", options, store)
63
+ unless options['no_url'] || store.nil?
64
+ text_to_post += "\n \n[iTunes Store](#{store['link']})"
65
+ end
66
+ abort(Status.canceled) unless STDIN.getch == ("y" || "Y")
67
+ puts "\n#{Status.yourpost}"
68
+ unless store.nil? || options['no_url']
69
+ visible, track, artwork, artwork_thumb, link, artist = true, store['track'], store['artwork'], store['artwork_thumb'], store['link'], store['artist']
70
+ else
71
+ visible, track, artwork, artwork_thumb, link, artist = false
72
+ end
73
+ if options['lastfm']
74
+ source = 'Last.fm'
75
+ else
76
+ source = 'iTunes'
77
+ end
78
+ dic = {
79
+ 'text' => text_to_post,
80
+ 'title' => track,
81
+ 'artist' => artist,
82
+ 'artwork' => artwork,
83
+ 'artwork_thumb' => artwork_thumb,
84
+ 'width' => 1200,
85
+ 'height' => 1200,
86
+ 'width_thumb' => 200,
87
+ 'height_thumb' => 200,
88
+ 'link' => link,
89
+ 'source' => source,
90
+ 'visible' => visible
91
+ }
92
+ @view.show_posted(Post.new.send_nowplaying(dic))
93
+ rescue => e
94
+ puts Status.wtf
95
+ Errors.global_error({error: e, caller: caller, data: [dic, store, options]})
96
+ end
97
+ end
98
+
99
+ def ask_lastfm_user
100
+ puts "\nPlease enter your Last.fm username:\n".color(:cyan)
101
+ begin
102
+ STDIN.gets.chomp!
103
+ rescue Interrupt
104
+ abort(Status.canceled)
105
+ end
106
+ end
107
+
108
+ def create_lastfm_user
109
+ Settings.options[:nowplaying][:lastfm] = ask_lastfm_user()
110
+ Settings.save_config
111
+ return Settings.options[:nowplaying][:lastfm]
112
+ end
113
+
114
+ def itunes_istore_request itunes
115
+ infos = itunes_reg([itunes.artist, itunes.track, itunes.album])
116
+ itunes_url = "https://itunes.apple.com/search?term=#{infos[0]}&term=#{infos[1]}&term=#{infos[2]}&media=music&entity=musicTrack"
117
+ get_itunes_store(itunes_url, itunes.artist, itunes.track)
118
+ end
119
+
120
+ def lastfm_istore_request artist, track
121
+ infos = itunes_reg([artist, track])
122
+ itunes_url = "https://itunes.apple.com/search?term=#{infos[0]}&term=#{infos[1]}&media=music&entity=musicTrack"
123
+ get_itunes_store(itunes_url, artist, track)
124
+ end
125
+
126
+ def get_itunes_store url, artist, track
127
+ results = JSON.load(CNX.download(URI.escape(url)))['results']
128
+ unless results.empty? || results.nil?
129
+
130
+ resp = results.select {|obj| obj['artistName'] == artist && obj['trackName'] == track}
131
+ candidate = resp[0] || results[0]
132
+
133
+ return {
134
+ 'code' => 200,
135
+ 'artist' => candidate['artistName'],
136
+ 'track' => candidate['trackName'],
137
+ 'preview' => candidate['previewUrl'],
138
+ 'link' => candidate['collectionViewUrl'],
139
+ 'artwork' => candidate['artworkUrl100'].gsub('100x100', '1200x1200'),
140
+ 'artwork_thumb' => candidate['artworkUrl100'].gsub('100x100', '600x600'),
141
+ 'request' => url,
142
+ 'results' => results
143
+ }
144
+ else
145
+ return {
146
+ 'code' => 404,
147
+ 'request' => url
148
+ }
149
+ end
150
+ end
151
+
152
+ def itunes_reg arr_of_itunes
153
+ regex_exotics = /[~:-;,?!\'&`^=+<>*%()\/"“”’°£$€.…]/
154
+ arr_of_itunes.map do |itune|
155
+ itune.gsub(regex_exotics, ' ').split(' ').join('+')
156
+ end
157
+ end
158
+
159
+ def get_itunes_track_infos
160
+ track = `osascript -e 'tell application "iTunes"' -e 'set trackName to name of current track' -e 'return trackName' -e 'end tell'`
161
+ if track.empty?
162
+ puts Status.no_itunes
163
+ Errors.warn "Nowplaying canceled: unable to get info from iTunes."
164
+ exit
165
+ end
166
+ album = `osascript -e 'tell application "iTunes"' -e 'set trackAlbum to album of current track' -e 'return trackAlbum' -e 'end tell'`
167
+ artist = `osascript -e 'tell application "iTunes"' -e 'set trackArtist to artist of current track' -e 'return trackArtist' -e 'end tell'`
168
+ maker = Struct.new(:artist, :album, :track)
169
+ maker.new(artist.chomp!, album.chomp!, track.chomp!)
170
+ end
171
+
172
+ def show_nowplaying(text, options, store)
173
+ puts "\nYour post:\n".color(:cyan)
174
+ if options['no_url'] || store['code'] != 200
175
+ puts text + "\n\n\n"
176
+ else
177
+ puts text + "\n\n\nThe iTunes Store thinks this track is: ".color(:green) + "'#{store['track']}'".color(:magenta) + " by ".color(:green) + "'#{store['artist']}'".color(:magenta) + ".\n\nAyadn will use these elements to insert album artwork and a link to the track.\n\n".color(:green)
178
+ end
179
+ puts "Is it ok? (y/N) ".color(:yellow)
180
+ end
181
+
182
+ end
183
+
184
+ end
@@ -0,0 +1,76 @@
1
+ # encoding: utf-8
2
+ module Ayadn
3
+
4
+ class NowWatching
5
+
6
+ require 'spotlite'
7
+
8
+ def initialize view
9
+ @view = view
10
+ @spotlite = Spotlite::Movie
11
+ end
12
+
13
+ def post args, options
14
+ puts "\nContacting IMDb.com...".color(:cyan)
15
+ response = find_by_title(args, options)
16
+ text = format_post(response)
17
+ show_post(text)
18
+ reg = /[~:-;,?!\'&`^=+<>*%()\/"“”’°£$€.…]/
19
+ filename = "#{response.title.downcase.strip.gsub(reg, '_').split(' ').join('_')}.jpg"
20
+ FileOps.download_url(filename, response.poster_url)
21
+ @view.clear_screen
22
+ puts "\nPosting and uploading the movie poster...\n".color(:green)
23
+ file = ["#{Settings.config[:paths][:downloads]}/#{filename}"]
24
+ dic = {
25
+ 'text' => text,
26
+ 'data' => FileOps.upload_files(file),
27
+ 'title' => response.title,
28
+ 'source' => 'IMDb'
29
+ }
30
+ resp = Post.new.send_movie(dic)
31
+ FileOps.save_post(resp) if Settings.options[:backup][:auto_save_sent_posts]
32
+ @view.clear_screen
33
+ puts Status.yourpost
34
+ @view.show_posted(resp)
35
+ end
36
+
37
+ def find_by_title args, options = {}
38
+ resp = @spotlite.find(args.join(' '))
39
+ if options['alt']
40
+ resp[1]
41
+ else
42
+ resp[0]
43
+ end
44
+ end
45
+
46
+ def format_post response
47
+ tag = Settings.options[:movie][:hashtag]
48
+ text_1 = "'#{response.title}' (#{response.year})"
49
+ link = "[IMDb](#{response.url})"
50
+ plot = format_plot(response, text_1)
51
+ "#{text_1}\n \n#{plot}\n \n#{link}\n \n##{tag}\n\n"
52
+ end
53
+
54
+ def format_plot response, text
55
+ max = 239 - (text.length + Settings.options[:movie][:hashtag].length)
56
+ short = max - 3
57
+ plot = response.description
58
+ if plot.length > max
59
+ "#{plot[0..short]}..."
60
+ else
61
+ plot
62
+ end
63
+ end
64
+
65
+ private
66
+
67
+ def show_post text
68
+ @view.clear_screen
69
+ puts "\nYour post:\n\n".color(:cyan)
70
+ puts text
71
+ puts "\nIs it ok? (y/N)".color(:yellow)
72
+ abort(Status.canceled) unless STDIN.getch == ("y" || "Y")
73
+ end
74
+
75
+ end
76
+ end
data/lib/ayadn/post.rb CHANGED
@@ -42,6 +42,46 @@ module Ayadn
42
42
  send_content(Endpoints.new.posts_url, payload_nowplaying(dic))
43
43
  end
44
44
 
45
+ def send_movie dic
46
+ send_content(Endpoints.new.posts_url, payload_movie(dic))
47
+ end
48
+
49
+ def send_tvshow dic
50
+ send_content(Endpoints.new.posts_url, payload_tvshow(dic))
51
+ end
52
+
53
+ def payload_movie dic
54
+ ann = annotations_embedded(dic)
55
+ ann << {
56
+ "type" => "com.ayadn.movie",
57
+ "value" => {
58
+ "title" => dic['title'],
59
+ "source" => dic['source']
60
+ }
61
+ }
62
+ {
63
+ "text" => dic['text'],
64
+ "entities" => entities,
65
+ "annotations" => ann
66
+ }
67
+ end
68
+
69
+ def payload_tvshow dic
70
+ ann = annotations_embedded(dic)
71
+ ann << {
72
+ "type" => "com.ayadn.tvshow",
73
+ "value" => {
74
+ "title" => dic['title'],
75
+ "source" => dic['source']
76
+ }
77
+ }
78
+ {
79
+ "text" => dic['text'],
80
+ "entities" => entities,
81
+ "annotations" => ann
82
+ }
83
+ end
84
+
45
85
  def payload_nowplaying dic
46
86
  ann = annotations()
47
87
  if dic['visible'] == true
@@ -0,0 +1,77 @@
1
+ # encoding: utf-8
2
+ module Ayadn
3
+
4
+ class Search
5
+
6
+ def initialize api, view, workers
7
+ @api = api
8
+ @view = view
9
+ @workers = workers
10
+ end
11
+
12
+ def hashtag(hashtag, options)
13
+ @view.downloading(options)
14
+ stream = @api.get_hashtag(hashtag)
15
+ Check.no_data(stream, 'hashtag')
16
+ if options[:extract]
17
+ @view.all_hashtag_links(stream, hashtag)
18
+ else
19
+ @view.render(stream, options)
20
+ end
21
+ end
22
+
23
+ def find(words, options)
24
+ @view.downloading(options)
25
+ stream = get_stream(words, options)
26
+ Check.no_data(stream, 'search')
27
+ if options[:users]
28
+ get_users(stream, options)
29
+ elsif options[:channels]
30
+ get_channels(stream, options)
31
+ else
32
+ get_generic(stream, words, options)
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def get_generic stream, words, options
39
+ if options[:extract]
40
+ @view.all_search_links(stream, words)
41
+ else
42
+ @view.render(stream, options)
43
+ end
44
+ end
45
+
46
+ def get_channels stream, options
47
+ @view.show_channels(stream, options)
48
+ end
49
+
50
+ def get_users stream, options
51
+ sorted = stream['data'].sort_by {|obj| obj['counts']['followers']}
52
+ sorted.each do |obj|
53
+ puts @view.big_separator
54
+ @view.show_userinfos(obj, nil, false)
55
+ end
56
+ end
57
+
58
+ def get_stream words, options
59
+ if options[:users]
60
+ @api.search_users words, options
61
+ elsif options[:annotations]
62
+ @api.search_annotations words, options
63
+ elsif options[:channels]
64
+ @api.search_channels words, options
65
+ elsif options[:messages]
66
+ words = words.split(',')
67
+ channel_id = @workers.get_channel_id_from_alias(words[0])
68
+ words.shift
69
+ @api.search_messages channel_id, words.join(','), options
70
+ else
71
+ @api.get_search words, options
72
+ end
73
+ end
74
+
75
+ end
76
+
77
+ end
data/lib/ayadn/set.rb CHANGED
@@ -15,6 +15,32 @@ module Ayadn
15
15
  scroll_config.save
16
16
  end
17
17
 
18
+ desc "movie ITEM VALUE", "Set values for movie (nowwatching)"
19
+ map "nowwatching" => :movie
20
+ def movie(*args)
21
+ movie_config = SetMovie.new
22
+ unless args.length != 2
23
+ movie_config.send(args[0], args[1])
24
+ else
25
+ abort(Status.error_missing_parameters)
26
+ end
27
+ movie_config.log(args)
28
+ movie_config.save
29
+ end
30
+
31
+ desc "tvshow ITEM VALUE", "Set values for tvshow (nowwatching)"
32
+ map "tv" => :tvshow
33
+ def tvshow(*args)
34
+ tvshow_config = SetTVShow.new
35
+ unless args.length != 2
36
+ tvshow_config.send(args[0], args[1])
37
+ else
38
+ abort(Status.error_missing_parameters)
39
+ end
40
+ tvshow_config.log(args)
41
+ tvshow_config.save
42
+ end
43
+
18
44
  desc "nicerank ITEM VALUE", "Set NiceRank filter values"
19
45
  long_desc Descriptions.set_nicerank
20
46
  def nicerank *args
@@ -148,6 +174,46 @@ module Ayadn
148
174
  end
149
175
  end
150
176
 
177
+ class SetMovie
178
+ def initialize
179
+ Settings.load_config
180
+ Settings.get_token
181
+ Settings.init_config
182
+ Logs.create_logger
183
+ end
184
+ def log(args)
185
+ x = "New value for '#{args[0]}' in 'Movie' => #{args[1]}"
186
+ puts "\n#{x}\n".color(:cyan)
187
+ Logs.rec.info x
188
+ end
189
+ def save
190
+ Settings.save_config
191
+ end
192
+ def hashtag(tag)
193
+ Settings.options[:movie][:hashtag] = tag
194
+ end
195
+ end
196
+
197
+ class SetTVShow
198
+ def initialize
199
+ Settings.load_config
200
+ Settings.get_token
201
+ Settings.init_config
202
+ Logs.create_logger
203
+ end
204
+ def log(args)
205
+ x = "New value for '#{args[0]}' in 'TV Show' => #{args[1]}"
206
+ puts "\n#{x}\n".color(:cyan)
207
+ Logs.rec.info x
208
+ end
209
+ def save
210
+ Settings.save_config
211
+ end
212
+ def hashtag(tag)
213
+ Settings.options[:tvshow][:hashtag] = tag
214
+ end
215
+ end
216
+
151
217
  class SetNiceRank
152
218
  def initialize
153
219
  Settings.load_config