vk_music_loader 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 11cfe23b6159ba7d08706798eab0e8a0f28c37f3
4
+ data.tar.gz: 683f6784ac82fe6a11833d2a64016a3a4e83a53f
5
+ SHA512:
6
+ metadata.gz: 1794e5ae7fa4c128e2975d36231d30d46b69faff7e276359acbbab5188062db6aa0e186898f0f1ebb7b85ac4672a45d72d98b1e25dba5c3f90d5d33a722a0d0f
7
+ data.tar.gz: 463449e9f906f3a3bb1700f0c27651f5370b5cd5225c1857909c8d88553b70d765aeb0c6ee09e1465e9efa3723da5e689d51ef24eba1c11ead3b37031ab66687
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Anatoly Ryabov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # VkMusicLoader
2
+
3
+
4
+ ## Installation
5
+
6
+ ```
7
+ gem install vk_music_loader
8
+ ```
9
+
10
+ ## Usage
11
+
12
+ ```
13
+ vk_music_loader -id user_or_group_id [-app your_standalone_app_id] [-folder folder_path_to_download_music]
14
+ ```
15
+
16
+
17
+ ## Contributing
18
+
19
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/vk_music_loader.
20
+
21
+
22
+ ## License
23
+
24
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
25
+
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'vk_music_loader'
4
+
5
+ opts = Slop.parse do |o|
6
+ o.integer 'app', '-app', '--app', '-application', '-application', default: 5377636 # author's application (you can use it)
7
+ o.integer 'id', '-id', '--id', '-user-id', '--user-id', '-group-id', '--group-id'
8
+ o.string 'folder', '-folder', '--folder', 'path', '-path', '--path', default: 'audio'
9
+ end
10
+
11
+ VkMusicLoader.call(opts)
@@ -0,0 +1,21 @@
1
+ require 'vk_music_loader/version'
2
+
3
+ require 'rubygems'
4
+ require 'time'
5
+ require 'cgi'
6
+ require 'uri'
7
+ require 'net/http'
8
+ require 'openssl'
9
+ require 'json'
10
+ require 'slop'
11
+ require 'launchy'
12
+
13
+ require 'vk_music_loader/authorizer'
14
+ require 'vk_music_loader/songs_downloader'
15
+
16
+ module VkMusicLoader
17
+ def self.call(opts)
18
+ auth_token = Authorizer.new(opts[:app]).perform
19
+ SongsDownloader.new(auth_token, opts[:id], opts[:folder]).perform
20
+ end
21
+ end
@@ -0,0 +1,57 @@
1
+ module VkMusicLoader
2
+ class Authorizer
3
+ AUTH_FILE_PATH = File.expand_path('~') + '/.vk_auth_data'
4
+
5
+ def initialize(app_id)
6
+ @app_id = app_id
7
+ end
8
+
9
+ def perform
10
+ get_auth_token
11
+ end
12
+
13
+ private
14
+
15
+ attr_reader :app_id
16
+
17
+ def get_auth_params_from_browser_bar
18
+ Launchy.open("https://oauth.vk.com/authorize?client_id=#{app_id}&scope=audio&redirect_uri=http://oauth.vk.com/blank.html&display=page&response_type=token")
19
+ ARGV.clear
20
+ puts 'Paste full URL from browser bar and press ENTER: '
21
+ CGI.parse(URI(gets.chomp.strip).fragment)
22
+ end
23
+
24
+ def save_auth_params_to_file
25
+ auth_params = get_auth_params_from_browser_bar
26
+ auth_file = File.open(AUTH_FILE_PATH, 'w')
27
+ auth_file.puts(Time.new + auth_params['expires_in'].first.to_i)
28
+ auth_file.puts(auth_params['access_token'].first)
29
+ auth_file.puts(auth_params['user_id'].first)
30
+ auth_file.close
31
+ end
32
+
33
+ def get_auth_params_from_file
34
+ auth_file = File.open(AUTH_FILE_PATH, 'r')
35
+
36
+ expires_in = auth_file.readline
37
+ access_token = auth_file.readline.chomp
38
+ auth_file.close
39
+
40
+ access_token if Time.parse(expires_in) > Time.new
41
+ end
42
+
43
+ def get_auth_token
44
+ begin
45
+ auth_token = get_auth_params_from_file
46
+ unless auth_token.nil?
47
+ auth_token
48
+ else
49
+ raise 'The validity of the token has expired'
50
+ end
51
+ rescue
52
+ save_auth_params_to_file
53
+ retry
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,49 @@
1
+ module VkMusicLoader
2
+ class SongsDownloader
3
+ def initialize(auth_token, user_id, audio_folder_path)
4
+ @auth_token = auth_token
5
+ @user_id = user_id
6
+ @audio_folder_path = audio_folder_path
7
+ end
8
+
9
+ def perform
10
+ download_songs(get_playlist(auth_token))
11
+ end
12
+
13
+ private
14
+
15
+ attr_reader :auth_token, :user_id, :audio_folder_path
16
+
17
+ def get_playlist(auth_token)
18
+ http = Net::HTTP.new('api.vk.com', 443)
19
+ http.use_ssl = true
20
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
21
+
22
+ req = Net::HTTP::Get.new("https://api.vk.com/method/audio.get?owner_id=#{user_id}&v=5.50&access_token=#{auth_token}")
23
+ res = http.request(req)
24
+
25
+ JSON.parse(res.body)
26
+ end
27
+
28
+ def download_songs(playlist)
29
+ Dir.mkdir(audio_folder_path) unless File.exists?(audio_folder_path)
30
+
31
+ songs = playlist['response']['items']
32
+
33
+ songs.each do |song|
34
+ song_url = URI.parse(song['url'])
35
+ file_name = "#{song['artist']} - #{song['title']}.mp3"
36
+ file_path = "#{audio_folder_path}/#{file_name}"
37
+
38
+ unless File.file?(file_path)
39
+ File.open(file_path, 'w') do |f|
40
+ f.write Net::HTTP.get(song_url)
41
+ f.close
42
+
43
+ puts "Downloaded: #{file_name}"
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,22 @@
1
+
2
+ require 'rubygems'
3
+ require 'time'
4
+ require 'cgi'
5
+ require 'uri'
6
+ require 'net/http'
7
+ require 'openssl'
8
+ require 'json'
9
+ require 'slop'
10
+ require 'launchy'
11
+
12
+ require '../vk_music_loader/authorizer'
13
+ require '../vk_music_loader/songs_downloader'
14
+
15
+ opts = Slop.parse do |o|
16
+ o.integer 'app', '-app', '--app', '-application', '-application', default: 5377636 # author's application (you can use it)
17
+ o.integer 'id', '-id', '--id', '-user-id', '--user-id', '-group-id', '--group-id'
18
+ o.string 'folder', '-folder', '--folder', 'path', '-path', '--path', default: 'audio'
19
+ end
20
+
21
+ auth_token = VkMusicLoader::Authorizer.new(opts[:app]).perform
22
+ VkMusicLoader::SongsDownloader.new(auth_token, opts[:id], opts[:folder]).perform
@@ -0,0 +1,3 @@
1
+ module VkMusicLoader
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vk_music_loader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anatoly Ryabov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: launchy
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: slop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ description: Download your VK playlist in one command
42
+ email:
43
+ - a.ryabov1993@gmail.com
44
+ executables:
45
+ - vk_music_loader
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - LICENSE.txt
50
+ - README.md
51
+ - bin/vk_music_loader
52
+ - lib/vk_music_loader.rb
53
+ - lib/vk_music_loader/authorizer.rb
54
+ - lib/vk_music_loader/songs_downloader.rb
55
+ - lib/vk_music_loader/trash.rb
56
+ - lib/vk_music_loader/version.rb
57
+ homepage: https://github.com/m1neral/vk_music_loader
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.5.1
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Simple gem to download VK user/group audio playlist.
81
+ test_files: []
82
+ has_rdoc: