nehm 1.5.4 → 1.5.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/nehm/playlist.rb CHANGED
@@ -1,16 +1,18 @@
1
- class Playlist
2
- attr_reader :name
1
+ module Nehm
2
+ class Playlist
3
+ attr_reader :name
3
4
 
4
- def initialize(name)
5
- @name = name
6
- end
5
+ def initialize(name)
6
+ @name = name
7
+ end
7
8
 
8
- def add_track(track_path)
9
- puts 'Adding to iTunes'
10
- AppleScript.add_track_to_playlist(track_path, @name)
11
- end
9
+ def add_track(track_path)
10
+ puts 'Adding to iTunes'
11
+ AppleScript.add_track_to_playlist(track_path, @name)
12
+ end
12
13
 
13
- def to_s
14
- @name
14
+ def to_s
15
+ @name
16
+ end
15
17
  end
16
18
  end
@@ -1,43 +1,47 @@
1
- module PlaylistManager
2
- def self.playlist
3
- @temp_playlist || default_user_playlist || music_master_library
4
- end
1
+ module Nehm
2
+ module PlaylistManager
3
+ def self.playlist
4
+ @temp_playlist || default_user_playlist || music_master_library unless OS.linux?
5
+ end
6
+
7
+ def self.set_playlist
8
+ loop do
9
+ playlist = HighLine.new.ask('Enter name of default iTunes playlist to which you want add tracks (press Enter to set it to default iTunes Music library)')
5
10
 
6
- def self.set_playlist
7
- loop do
8
- playlist = HighLine.new.ask('Enter name of default iTunes playlist to which you want add tracks (press Enter to set it to default iTunes Music library)')
9
- if playlist == ''
10
- Cfg[:playlist] = nil
11
- puts Paint['Default iTunes playlist unset', :green]
12
- break
11
+ # If entered nothing, unset iTunes playlist
12
+ if playlist == ''
13
+ Cfg[:playlist] = nil
14
+ puts Paint['Default iTunes playlist unset', :green]
15
+ break
16
+ end
17
+
18
+ if AppleScript.list_of_playlists.include? playlist
19
+ Cfg[:playlist] = playlist
20
+ puts Paint["Default iTunes playlist set up to #{playlist}", :green]
21
+ break
22
+ else
23
+ puts Paint['Invalid playlist name. Please enter correct name', :red]
24
+ end
13
25
  end
26
+ end
14
27
 
28
+ def self.temp_playlist=(playlist)
15
29
  if AppleScript.list_of_playlists.include? playlist
16
- Cfg[:playlist] = playlist
17
- puts Paint["Default iTunes playlist set up to #{playlist}", :green]
18
- break
30
+ @temp_playlist = Playlist.new(playlist)
19
31
  else
20
32
  puts Paint['Invalid playlist name. Please enter correct name', :red]
33
+ exit
21
34
  end
22
35
  end
23
- end
24
-
25
- def self.temp_playlist=(playlist)
26
- if AppleScript.list_of_playlists.include? playlist
27
- @temp_playlist = Playlist.new(playlist)
28
- else
29
- puts Paint['Invalid playlist name. Please enter correct name', :red]
30
- exit
31
- end
32
- end
33
36
 
34
- module_function
37
+ module_function
35
38
 
36
- def default_user_playlist
37
- Playlist.new(Cfg[:playlist]) unless Cfg[:playlist].nil?
38
- end
39
+ def default_user_playlist
40
+ Playlist.new(Cfg[:playlist]) unless Cfg[:playlist].nil?
41
+ end
39
42
 
40
- def music_master_library
41
- Playlist.new(AppleScript.music_master_library)
43
+ def music_master_library
44
+ Playlist.new(AppleScript.music_master_library)
45
+ end
42
46
  end
43
47
  end
data/lib/nehm/track.rb CHANGED
@@ -1,58 +1,60 @@
1
- class Track
2
- attr_reader :hash
1
+ module Nehm
2
+ class Track
3
+ attr_reader :hash
3
4
 
4
- def initialize(hash)
5
- @hash = hash
6
- end
5
+ def initialize(hash)
6
+ @hash = hash
7
+ end
7
8
 
8
- def artist
9
- if @hash['title'].include?('-')
10
- title = @hash['title'].split('-')
11
- title[0].rstrip
12
- else
13
- @hash['user']['username']
9
+ def artist
10
+ if @hash['title'].include?('-')
11
+ title = @hash['title'].split('-')
12
+ title[0].rstrip
13
+ else
14
+ @hash['user']['username']
15
+ end
14
16
  end
15
- end
16
17
 
17
- def artwork
18
- Artwork.new(self)
19
- end
18
+ def artwork
19
+ Artwork.new(self)
20
+ end
20
21
 
21
- def file_name
22
- "#{name}.mp3".tr("/'\"", '')
23
- end
22
+ def file_name
23
+ "#{name}.mp3".tr("/'\"", '')
24
+ end
24
25
 
25
- def file_path
26
- File.join(PathManager.dl_path, file_name)
27
- end
26
+ def file_path
27
+ File.join(PathManager.dl_path, file_name)
28
+ end
28
29
 
29
- def id
30
- @hash['id'].to_s
31
- end
30
+ def id
31
+ @hash['id']
32
+ end
32
33
 
33
- # Use in Get.dl and in Track.file_name
34
- def name
35
- artist + ' - ' + title
36
- end
34
+ # Used in Get#dl and in Track#file_name
35
+ def name
36
+ artist + ' - ' + title
37
+ end
37
38
 
38
- def streamable?
39
- @hash['streamable']
40
- end
39
+ def streamable?
40
+ @hash['streamable']
41
+ end
41
42
 
42
- def title
43
- if @hash['title'].include?('-')
44
- title = @hash['title'].split('-')
45
- title[1].lstrip
46
- else
47
- @hash['title']
43
+ def title
44
+ if @hash['title'].include?('-')
45
+ title = @hash['title'].split('-')
46
+ title[1].lstrip
47
+ else
48
+ @hash['title']
49
+ end
48
50
  end
49
- end
50
51
 
51
- def url
52
- "#{@hash['stream_url']}?client_id=#{Client::CLIENT_ID}"
53
- end
52
+ def url
53
+ "#{@hash['stream_url']}?client_id=#{Client::CLIENT_ID}"
54
+ end
54
55
 
55
- def year
56
- @hash['created_at'][0..3].to_i
56
+ def year
57
+ @hash['created_at'][0..3].to_i
58
+ end
57
59
  end
58
60
  end
data/lib/nehm/user.rb CHANGED
@@ -1,78 +1,81 @@
1
1
  require 'json'
2
2
  require 'faraday'
3
3
 
4
- class User
5
- SOUNDCLOUD_MAX_LIMIT = 180
4
+ module Nehm
5
+ class User
6
+ # Max limit of tracks for correct SoundCloud requests
7
+ SOUNDCLOUD_MAX_LIMIT = 180
6
8
 
7
- def initialize(id)
8
- @id = id
9
- end
10
-
11
- def likes(count)
12
- # Method to_i return 0, if there aren't any numbers in string
13
- if count == 0
14
- puts Paint['Invalid number of likes!', :red]
15
- exit
9
+ def initialize(id)
10
+ @id = id
16
11
  end
17
12
 
18
- d = count / SOUNDCLOUD_MAX_LIMIT
19
- m = count % SOUNDCLOUD_MAX_LIMIT
20
- d = m == 0 ? d : d + 1
13
+ def likes(count)
14
+ # Method to_i return 0, if there aren't any numbers in string
15
+ if count == 0
16
+ puts Paint['Invalid number of likes!', :red]
17
+ exit
18
+ end
21
19
 
22
- likes = []
23
- d.times do |i|
24
- limit = count > SOUNDCLOUD_MAX_LIMIT ? SOUNDCLOUD_MAX_LIMIT : count
25
- count -= SOUNDCLOUD_MAX_LIMIT
20
+ d = count / SOUNDCLOUD_MAX_LIMIT
21
+ m = count % SOUNDCLOUD_MAX_LIMIT
22
+ d = m == 0 ? d : d + 1
26
23
 
27
- likes += Client.get("/users/#{@id}/favorites?limit=#{limit}&offset=#{(i)*SOUNDCLOUD_MAX_LIMIT}")
28
- end
24
+ likes = []
25
+ d.times do |i|
26
+ limit = count > SOUNDCLOUD_MAX_LIMIT ? SOUNDCLOUD_MAX_LIMIT : count
27
+ count -= SOUNDCLOUD_MAX_LIMIT
29
28
 
30
- if likes.empty?
31
- puts Paint['There are no likes yet :(', :red]
32
- exit
33
- end
29
+ likes += Client.get("/users/#{@id}/favorites?limit=#{limit}&offset=#{(i)*SOUNDCLOUD_MAX_LIMIT}")
30
+ end
34
31
 
35
- likes.map { |hash| Track.new(hash) }
36
- end
32
+ if likes.empty?
33
+ puts Paint['There are no likes yet :(', :red]
34
+ exit
35
+ end
37
36
 
38
- # Post is last track/repost in profile
39
- def posts(count)
40
- # Method to_i return 0, if there aren't any numbers in string
41
- if count == 0
42
- puts Paint['Invalid number of posts!', :red]
43
- exit
37
+ likes.map! { |hash| Track.new(hash) }
44
38
  end
45
39
 
46
- d = count / SOUNDCLOUD_MAX_LIMIT
47
- m = count % SOUNDCLOUD_MAX_LIMIT
48
- d = m == 0 ? d : d + 1
40
+ # Post is last track/repost in profile
41
+ def posts(count)
42
+ # Method to_i return 0, if there aren't any numbers in string
43
+ if count == 0
44
+ puts Paint['Invalid number of posts!', :red]
45
+ exit
46
+ end
49
47
 
50
- # Official SC API wrapper doesn't support posts
51
- # So I should get posts by HTTP requests
52
- conn = Faraday.new(url: 'https://api-v2.soundcloud.com/')
48
+ d = count / SOUNDCLOUD_MAX_LIMIT
49
+ m = count % SOUNDCLOUD_MAX_LIMIT
50
+ d = m == 0 ? d : d + 1
53
51
 
54
- posts = []
55
- d.times do |i|
56
- limit = count > SOUNDCLOUD_MAX_LIMIT ? SOUNDCLOUD_MAX_LIMIT : count
57
- count -= SOUNDCLOUD_MAX_LIMIT
52
+ # Official SC API wrapper doesn't support posts
53
+ # So I should get posts by HTTP requests
54
+ conn = Faraday.new(url: 'https://api-v2.soundcloud.com/')
58
55
 
59
- response = conn.get("/profile/soundcloud:users:#{@id}?limit=#{limit}&offset=#{i*SOUNDCLOUD_MAX_LIMIT}")
60
- parsed = JSON.parse(response.body)
61
- collection = parsed['collection']
56
+ posts = []
57
+ d.times do |i|
58
+ limit = count > SOUNDCLOUD_MAX_LIMIT ? SOUNDCLOUD_MAX_LIMIT : count
59
+ count -= SOUNDCLOUD_MAX_LIMIT
62
60
 
63
- break if collection.nil?
61
+ response = conn.get("/profile/soundcloud:users:#{@id}?limit=#{limit}&offset=#{i*SOUNDCLOUD_MAX_LIMIT}")
62
+ parsed = JSON.parse(response.body)
63
+ collection = parsed['collection']
64
64
 
65
- posts += collection
66
- end
65
+ break if collection.nil?
67
66
 
68
- if posts.empty?
69
- puts Paint['There are no posts yet :(', :red]
70
- exit
71
- end
67
+ posts += collection
68
+ end
72
69
 
73
- rejected = posts.reject! { |hash| hash['type'] == 'playlist' }
74
- puts Paint["Was skipped #{rejected.count} playlist(s) (nehm doesn't download playlists)", :yellow] if rejected
70
+ if posts.empty?
71
+ puts Paint['There are no posts yet :(', :red]
72
+ exit
73
+ end
75
74
 
76
- posts.map { |hash| Track.new(hash['track']) }
75
+ rejected = posts.reject! { |hash| hash['type'] == 'playlist' }
76
+ puts Paint["Was skipped #{rejected.length} playlist(s) (nehm doesn't download playlists)", :yellow] if rejected
77
+
78
+ posts.map! { |hash| Track.new(hash['track']) }
79
+ end
77
80
  end
78
81
  end
@@ -1,59 +1,61 @@
1
- module UserManager
2
- def self.user
3
- @temp_user || default_user
4
- end
1
+ module Nehm
2
+ module UserManager
3
+ def self.user
4
+ @temp_user || default_user
5
+ end
5
6
 
6
- def self.logged_in?
7
- Cfg.key?(:default_id)
8
- end
7
+ def self.logged_in?
8
+ Cfg.key?(:default_id)
9
+ end
9
10
 
10
- def self.log_in
11
- loop do
12
- permalink = HighLine.new.ask('Please enter your permalink (last word in your profile url): ')
11
+ def self.log_in
12
+ loop do
13
+ permalink = HighLine.new.ask('Please enter your permalink (last word in your profile url): ')
14
+ user = get_user(permalink)
15
+ if user
16
+ Cfg[:default_id] = user.id
17
+ Cfg[:permalink] = permalink
18
+ puts Paint['Successfully logged in!', :green]
19
+ break
20
+ else
21
+ puts Paint['Invalid permalink. Please enter correct permalink', :red]
22
+ end
23
+ end
24
+ end
25
+
26
+ def self.temp_user=(permalink)
13
27
  user = get_user(permalink)
14
28
  if user
15
- Cfg[:default_id] = user.id
16
- Cfg[:permalink] = permalink
17
- puts Paint['Successfully logged in!', :green]
18
- break
29
+ @temp_user = User.new(user.id)
19
30
  else
20
31
  puts Paint['Invalid permalink. Please enter correct permalink', :red]
32
+ exit
21
33
  end
22
34
  end
23
- end
24
35
 
25
- def self.temp_user=(permalink)
26
- user = get_user(permalink)
27
- if user
28
- @temp_user = User.new(user.id)
29
- else
30
- puts Paint['Invalid permalink. Please enter correct permalink', :red]
31
- exit
32
- end
33
- end
36
+ module_function
34
37
 
35
- module_function
36
-
37
- def default_user
38
- if UserManager.logged_in?
39
- User.new(Cfg[:default_id])
40
- else
41
- puts Paint["You didn't logged in", :red]
42
- puts "Login from #{Paint['nehm configure', :yellow]} or use #{Paint['[from PERMALINK]', :yellow]} option"
43
- exit
38
+ def default_user
39
+ if UserManager.logged_in?
40
+ User.new(Cfg[:default_id])
41
+ else
42
+ puts Paint["You didn't logged in", :red]
43
+ puts "Login from #{Paint['nehm configure', :yellow]} or use #{Paint['[from PERMALINK]', :yellow]} option"
44
+ exit
45
+ end
44
46
  end
45
- end
46
47
 
47
- def get_user(permalink)
48
- begin
49
- user = Client.get('/resolve', url: "https://soundcloud.com/#{permalink}")
50
- rescue SoundCloud::ResponseError => e
51
- if e.message =~ /404/
52
- user = nil
53
- else
54
- raise e
48
+ def get_user(permalink)
49
+ begin
50
+ user = Client.get('/resolve', url: "https://soundcloud.com/#{permalink}")
51
+ rescue SoundCloud::ResponseError => e
52
+ if e.message =~ /404/
53
+ user = nil
54
+ else
55
+ raise e
56
+ end
55
57
  end
58
+ user
56
59
  end
57
- user
58
60
  end
59
61
  end