Module: Natalia::Joysound
- Defined in:
- lib/natalia/services/joysound.rb
Constant Summary collapse
- SORT_MAP =
{ title: %w[name asc].freeze, title_desc: %w[name desc].freeze, popularity: %w[popular asc].freeze, popularity_desc: %w[popular desc].freeze, date: %w[new asc].freeze, date_desc: %w[new desc].freeze, artist: %w[artist asc].freeze, artist_desc: %w[artist desc].freeze }.freeze
- KIND_MAP =
{ title: 'song', artist: 'selArtist', service: 'selService' # 配信機種的なやつ? }.freeze
Class Method Summary collapse
Class Method Details
.get(id) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/natalia/services/joysound.rb', line 72 def self.get(id) response = Natalia::Utils.curl_post('https://mspxy.joysound.com/Common/Lyric') do |request| request.content_type = 'application/x-www-form-urlencoded; charset=UTF-8' request['X-Jsp-App-Name'] = '0000800' request.set_form_data( kind: 'naviGroupId', selSongNo: id, interactionFlg: '0', apiVer: '1.0' ) end return nil if response.code == '404' response.value json = JSON.parse(response.body.force_encoding('utf-8')) { source: self, id: json['naviGroupId'], title: json['songName'], artist: json['artistName'], artist_id: json['artistId'], lyricist: json['lyricist'], composer: json['composer'], lyrics: json['lyricList']&.find {|e| e['statusCode'] == '1'}&.[]('lyric')&.strip, raw_data: json } end |
.search(keyword, type: :title, sort: :popularity_desc) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/natalia/services/joysound.rb', line 25 def self.search(keyword, type: :title, sort: :popularity_desc) sort = SORT_MAP[sort] raise ArgumentError, "unsupported sort type `#{sort}`" unless sort raise ArgumentError, "unsupported search type `#{type}`" unless KIND_MAP.key?(type) response = Natalia::Utils.curl_post('https://mspxy.joysound.com/Common/ContentsList') do |request| request.content_type = 'application/x-www-form-urlencoded; charset=UTF-8' request['X-Jsp-App-Name'] = '0000800' request.set_form_data( format: 'all', kindCnt: '1', # この数の分だけ kind1, word1, match1 を指定できるっぽい start: '1', count: '999', sort: sort[0], order: sort[1], kind1: KIND_MAP[type], word1: keyword, match1: type == :title ? 'partial' : 'exact', # partial / front / exact apiVer: '1.0' ) end response.value json = JSON.parse(response.body.force_encoding('utf-8')) contents_list = json['contentsList'] contents_list.select! do |e| e['serviceTypeList'].any? do |service| # なんかこの 2 つを除外して残らないと歌詞が配信されていないっぽい? service['serviceType'] != '001000000' && service['serviceType'] != '100000000' end end contents_list.map do |e| { source: self, id: e['naviGroupId'], title: e['songName'], artist: e['artistName'], artist_id: e['artistId'], lyricist: e['lyricist'], composer: e['composer'], raw_data: e } end end |