anilibria-api-ruby 1.0.1 → 1.0.4.1

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
  SHA256:
3
- metadata.gz: 7e8f99d41325b4d8e4d22877388d0cdeb216acc3ee3f4ad57cd7c3076973d9e9
4
- data.tar.gz: 2050b7e07d8dca2303ac52bc5097ff35ce39bb581f74533253c2caa46d7c64c6
3
+ metadata.gz: ba50d51a867a98e0bac039f02675eaf35c92d8fca7ca09d75a956907166c5911
4
+ data.tar.gz: fb9ca7bf541d71e4de811d47f14006dc06ee42d732e8cca79885f29dd2d42af2
5
5
  SHA512:
6
- metadata.gz: 51ef2354baa68cba34551fff411ad549f3650423b961f89357d811f646bfa0885b178b98f394cfeff6cf463e03a2dc5af70e7756e62a87bcad02435949b6129f
7
- data.tar.gz: ad841e35f03244b4f266288fa316ba4d43100003bdc9867d303e4dbde36ffec0a87c85f6efc441ccadaf0c42dd188f6af4593cf84f0492e79e1cb73865fcf248
6
+ metadata.gz: e1772aff9a59d15b457683f252217abc50a2bd94fa290d40c07629541ddcff62a41235c2ddcd3019bbdbf2f65a8c131837816bc54464ef70d2a60922a452eeec
7
+ data.tar.gz: cd96a4a22c0b71298a5775af3005b376e47c8e2048c52b831b6a61b6b9b8210a94f248b7f864160211fc6faae4e8cf1beb50629014933f84cd72b8df0756b92a
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- anilibria-api-ruby (1.0.1)
4
+ anilibria-api-ruby (1.0.4.1)
5
5
  dry-struct (~> 1.4)
6
6
  faraday (~> 2)
7
7
 
data/README.md CHANGED
@@ -32,11 +32,16 @@ Usage example:
32
32
  require 'anilibria/api'
33
33
 
34
34
  anilibria = Anilibria::Api::Client.new
35
+ # => #<Anilibria::Api::Client:0x00007f25c52fc1b8 ...
35
36
 
36
- anilibria.get_years # => [1996, 1998, 2001, 2003, 2004, 2005, 2006, ...]
37
+ anilibria.get_years
38
+ # => [1996, 1998, 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009, ...
37
39
 
38
- title = anilibria.get_title(code: 'steinsgate') # => #<Anilibria::Api::Types::Title id=8674 ...
39
- title.names.en # => "Steins;Gate"
40
+ title = anilibria.get_title(code: 'steinsgate')
41
+ # => #<Anilibria::Api::Types::Title id=8674 code="steinsgate" ...
42
+
43
+ title.names.en
44
+ # => "Steins;Gate"
40
45
  ```
41
46
 
42
47
  `anilibria` object implements [AniLibria API methods](https://github.com/anilibria/docs/blob/master/api_v2.md#method-list) as is. All methods are available in *snake_case* notation. Same with the `title` object - it implements the [Title](https://github.com/anilibria/docs/blob/master/api_v2.md#возвращаемые-значения-при-запросе-информации-о-тайтле) specification.
@@ -46,17 +51,20 @@ title.names.en # => "Steins;Gate"
46
51
  The gem supports authorization:
47
52
 
48
53
  ```ruby
49
- # session = anilibria.auth('wrong@example.com', 'WrongPassword') # => nil
54
+ # session = anilibria.auth('wrong@example.com', 'WrongPassword')
55
+ # => nil
50
56
 
51
57
  # session = anilibria.auth!('wrong@example.com', 'WrongPassword')
52
- # (raises an error Anilibria::Api::Exceptions::AuthError)
58
+ # raises an error Anilibria::Api::Exceptions::AuthError
53
59
 
54
- session = anilibria.auth('correct@example.com', 'CorrectPassword') # returns the session string
60
+ session = anilibria.auth('correct@example.com', 'CorrectPassword')
61
+ # => "VdZnNZRpEl9w2HcKNRyj1BWXLqdynpog"
55
62
 
56
- anilibria.add_favorite(title_id: 9114, session: session) # => true
63
+ anilibria.add_favorite(title_id: 9114, session: session)
64
+ # => true
57
65
 
58
66
  titles = anilibria.get_favorites(filter: 'id,code,names,description', session: session)
59
- # => [#<Anilibria::Api::Types::Title id=9114 code="shingeki-no-k...
67
+ # => [#<Anilibria::Api::Types::Title id=9114 code="shingeki-no-kyojin-the-final-season-part-2" ...
60
68
  ```
61
69
 
62
70
  ### API Version
@@ -64,7 +72,8 @@ titles = anilibria.get_favorites(filter: 'id,code,names,description', session: s
64
72
  You can get the currently used version of [AniLibria API](https://github.com/anilibria/docs/blob/master/api_v2.md):
65
73
 
66
74
  ```ruby
67
- anilibria.api_version # => "2.13.10"
75
+ anilibria.api_version
76
+ # => "2.13.10"
68
77
  ```
69
78
 
70
79
  ## Contributing
@@ -41,7 +41,10 @@ module Anilibria
41
41
  attr_reader :connection
42
42
 
43
43
  def initialize(url: ENDPOINT)
44
- @connection = Faraday.new(url: url) do |f|
44
+ @connection = Faraday.new(
45
+ url: url,
46
+ headers: { 'User-Agent' => "anilibria-api-ruby/#{VERSION}" }
47
+ ) do |f|
45
48
  f.request :url_encoded
46
49
  f.response :json, parser_options: { symbolize_names: true }
47
50
  end
@@ -60,7 +63,7 @@ module Anilibria
60
63
  def auth(mail, passwd)
61
64
  response = auth_response(mail, passwd)
62
65
 
63
- return unless response[:key] == 'success'
66
+ return unless auth_successful?(response)
64
67
 
65
68
  response[:sessionId]
66
69
  end
@@ -68,34 +71,50 @@ module Anilibria
68
71
  def auth!(mail, passwd)
69
72
  response = auth_response(mail, passwd)
70
73
 
71
- raise Exceptions::AuthError, response unless response[:key] == 'success'
74
+ unless auth_successful?(response)
75
+ raise(
76
+ Exceptions::AuthError.new(response),
77
+ 'Failed authorization attempt'
78
+ )
79
+ end
72
80
 
73
81
  response[:sessionId]
74
82
  end
75
83
 
76
84
  def api_version
77
- connection.get.headers['api-version']
85
+ connection.head.headers['API-Version']
78
86
  end
79
87
 
80
88
  private
81
89
 
90
+ def check_response!(response)
91
+ return if response.status == 200
92
+
93
+ if !response.body.respond_to?(:to_hash) ||
94
+ !response.body[:error].respond_to?(:to_hash)
95
+ raise Exceptions::ResponseError.new(
96
+ 'Unexpected response from API', response
97
+ )
98
+ end
99
+
100
+ raise Exceptions::ApiError.new(response), response.body.dig(:error, :message)
101
+ end
102
+
82
103
  def auth_response(mail, passwd)
83
104
  response = connection.post(
84
105
  AUTH_ENDPOINT,
85
106
  { mail: mail, passwd: passwd }
86
107
  )
87
108
 
88
- JSON.parse(response.body, { symbolize_names: true })
89
- end
90
-
91
- def check_response!(response)
92
- return if response.status == 200
93
-
94
- if !response.body.respond_to?(:to_hash) || !response.body.key?(:error)
95
- raise Exceptions::ResponseError.new('Unexpected response from API', response)
109
+ begin
110
+ JSON.parse(response.body, { symbolize_names: true })
111
+ rescue JSON::ParserError
112
+ {}
96
113
  end
114
+ end
97
115
 
98
- raise Exceptions::ApiError, response
116
+ def auth_successful?(response)
117
+ response[:key] == 'success' && response.key?(:sessionId)
99
118
  end
100
119
  end
101
120
  end
@@ -7,7 +7,8 @@ module Anilibria
7
7
  def initialize(response)
8
8
  error = response.body[:error].to_h
9
9
  @code = error[:code]
10
- super("#{error[:code]} #{error[:message]}", response)
10
+
11
+ super(error[:message], response)
11
12
  end
12
13
  end
13
14
  end
@@ -2,14 +2,25 @@ module Anilibria
2
2
  module Api
3
3
  module Exceptions
4
4
  class AuthError < Base
5
- attr_reader :err, :key, :mes
5
+ attr_reader :data
6
+
7
+ def initialize(data = {})
8
+ @data = data
6
9
 
7
- def initialize(response_body)
8
- @err = response_body[:err]
9
- @key = response_body[:key]
10
- @mes = response_body[:mes]
11
10
  super("#{mes} (#{key})")
12
11
  end
12
+
13
+ def err
14
+ data[:err]
15
+ end
16
+
17
+ def mes
18
+ data[:mes]
19
+ end
20
+
21
+ def key
22
+ data[:key]
23
+ end
13
24
  end
14
25
  end
15
26
  end
@@ -4,9 +4,10 @@ module Anilibria
4
4
  class ResponseError < Base
5
5
  attr_reader :response
6
6
 
7
- def initialize(message, response)
7
+ def initialize(msg = nil, response = nil)
8
8
  @response = response
9
- super(message)
9
+
10
+ super(msg)
10
11
  end
11
12
  end
12
13
  end
@@ -8,12 +8,23 @@ module Anilibria
8
8
  attribute? :string, DryTypes::String.optional
9
9
  end
10
10
 
11
+ class Posters < Base
12
+ class Poster < Base
13
+ attribute? :url, DryTypes::String.optional
14
+ attribute? :raw_base64_file, DryTypes::String.optional
15
+ end
16
+
17
+ %i[small medium original].each do |size|
18
+ attribute? size, Poster
19
+ end
20
+ end
21
+
11
22
  class Player < Base
12
23
  class Playlist < Base
13
24
  attribute? :serie, DryTypes::Strict::Integer
14
25
  attribute? :created_timestamp, DryTypes::Timestamp
15
26
 
16
- attribute? :hls do
27
+ attribute? :hls, Base do
17
28
  attribute? :fhd, DryTypes::String.optional
18
29
  attribute? :hd, DryTypes::String.optional
19
30
  attribute? :sd, DryTypes::String.optional
@@ -47,7 +58,6 @@ module Anilibria
47
58
  attribute? :name, DryTypes::Strict::String
48
59
  attribute? :announce, DryTypes::Array.of(DryTypes::Strict::String)
49
60
  attribute? :created_timestamp, DryTypes::Timestamp
50
-
51
61
  attribute? :files_list, DryTypes::Array.of(FilesList)
52
62
  end
53
63
 
@@ -56,7 +66,7 @@ module Anilibria
56
66
  attribute? :torrent_id, DryTypes::Strict::Integer
57
67
  attribute? :series, Title::Series
58
68
 
59
- attribute? :quality do
69
+ attribute? :quality, Base do
60
70
  attribute? :string, DryTypes::Strict::String
61
71
  attribute? :type, DryTypes::Strict::String
62
72
  attribute? :resolution, DryTypes::Strict::Integer
@@ -82,7 +92,7 @@ module Anilibria
82
92
  attribute? :id, DryTypes::Strict::Integer
83
93
  attribute? :code, DryTypes::Strict::String
84
94
 
85
- attribute? :names do
95
+ attribute? :names, Base do
86
96
  attribute? :ru, DryTypes::String.optional
87
97
  attribute? :en, DryTypes::String.optional
88
98
  attribute? :alternative, DryTypes::String.optional
@@ -90,27 +100,19 @@ module Anilibria
90
100
 
91
101
  attribute? :announce, DryTypes::String.optional
92
102
 
93
- attribute? :status do
103
+ attribute? :status, Base do
94
104
  attribute? :string, DryTypes::Strict::String
95
105
  attribute? :code, DryTypes::Strict::Integer
96
106
  end
97
107
 
98
- attribute? :posters do
99
- %i[small medium original].each do |poster|
100
- attribute? poster do
101
- attribute? :url, DryTypes::String.optional
102
- attribute? :raw_base64_file, DryTypes::String.optional
103
- end
104
- end
105
- end
106
-
108
+ attribute? :posters, Posters
107
109
  attribute? :updated, DryTypes::Timestamp.optional
108
110
  attribute? :last_change, DryTypes::Timestamp.optional
109
111
 
110
- attribute? :type do
112
+ attribute? :type, Base do
111
113
  attribute? :full_string, DryTypes::String.optional
112
114
  attribute? :string, DryTypes::String.optional
113
- attribute? :length, DryTypes::String.optional
115
+ attribute? :length, DryTypes::Integer.optional
114
116
  attribute? :series, DryTypes::Integer.optional
115
117
  attribute? :code, DryTypes::Integer.optional
116
118
  end
@@ -118,7 +120,7 @@ module Anilibria
118
120
  attribute? :genres, DryTypes::Array.of(DryTypes::Strict::String)
119
121
  attribute? :team, Types::Team
120
122
 
121
- attribute? :season do
123
+ attribute? :season, Base do
122
124
  attribute? :string, DryTypes::String.optional
123
125
  attribute? :code, DryTypes::Integer.optional
124
126
  attribute? :year, DryTypes::Integer.optional
@@ -128,7 +130,7 @@ module Anilibria
128
130
  attribute? :description, DryTypes::String.optional
129
131
  attribute? :in_favorites, DryTypes::Integer.optional
130
132
 
131
- attribute? :blocked do
133
+ attribute? :blocked, Base do
132
134
  attribute? :blocked, DryTypes::Strict::Bool
133
135
  attribute? :bakanim, DryTypes::Strict::Bool
134
136
  end
@@ -1,6 +1,6 @@
1
1
  module Anilibria
2
2
  module Api
3
- VERSION = '1.0.1'.freeze
3
+ VERSION = '1.0.4.1'.freeze
4
4
  public_constant :VERSION
5
5
  end
6
6
  end
data/sample/rss.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'anilibria/api'
2
+ require 'rss'
3
+
4
+ anilibria = Anilibria::Api::Client.new
5
+
6
+ today_ts = Date.today.to_time.to_i
7
+ raw_rss = anilibria.get_rss(since: today_ts)
8
+
9
+ feed = RSS::Parser.parse(raw_rss)
10
+
11
+ puts feed.channel.title
12
+ feed.items.each do |item|
13
+ puts "#{item.pubDate.strftime('%H:%M:%S')} | #{item.title}"
14
+ end
@@ -0,0 +1,25 @@
1
+ require 'anilibria/api'
2
+
3
+ WEEK_DAYS = %w[
4
+ Понедельник
5
+ Вторник
6
+ Среда
7
+ Четверг
8
+ Пятница
9
+ Суббота
10
+ Воскресенье
11
+ ].freeze
12
+
13
+ anilibria = Anilibria::Api::Client.new
14
+
15
+ schedules = anilibria.get_schedule(filter: 'names.ru')
16
+
17
+ schedules.each do |schedule|
18
+ puts "#{WEEK_DAYS[schedule.day]}:"
19
+
20
+ schedule.list.each do |title|
21
+ puts " #{title.names.ru}"
22
+ end
23
+
24
+ puts
25
+ end
@@ -0,0 +1,29 @@
1
+ require 'anilibria/api'
2
+
3
+ anilibria = Anilibria::Api::Client.new
4
+
5
+ limit = 10
6
+
7
+ puts 'Введите поисковый запрос:'
8
+ input = $stdin.gets.chomp
9
+ puts
10
+
11
+ titles = anilibria.search_titles(
12
+ search: input,
13
+ filter: 'id,names,season,type,team,genres[0]',
14
+ limit: limit
15
+ )
16
+
17
+ puts 'Результаты:'
18
+ puts
19
+
20
+ titles.each do |title|
21
+ puts " id: #{title.id}"
22
+ puts " #{title.names.ru} / #{title.names.en}"
23
+ puts " Сезон: #{title.season.string} #{title.season.year}"
24
+ puts " Тип: #{title.type.full_string}"
25
+ puts " Жанр: #{title.genres.join(', ')}"
26
+ puts
27
+ end
28
+
29
+ puts "Всего найдено #{titles.size}, лимит: #{limit}"
data/sample/title.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'anilibria/api'
2
+
3
+ anilibria = Anilibria::Api::Client.new
4
+
5
+ title = anilibria.get_random_title(remove: 'posters,player,torrents')
6
+ # title = anilibria.get_title(id: 495, remove: 'posters,player,torrents')
7
+
8
+ subtitle_team = title.team.translator + title.team.editing + title.team.decor
9
+
10
+ puts "#{title.names.ru} / #{title.names.en}"
11
+ puts
12
+ puts "Сезон: #{title.season.year} #{title.season.string}"
13
+ puts "Тип: #{title.type.full_string}"
14
+ puts "Жанры: #{title.genres.join(', ')}"
15
+ puts "Озвучка: #{title.team.voice.join(', ')}"
16
+ puts "Тайминг: #{title.team.timing.join(', ')}" if title.team.timing.any?
17
+ puts "Работа над субтитрами: #{subtitle_team.join(', ')}" if subtitle_team.any?
18
+ puts
19
+ puts title.description
20
+ puts
21
+ puts format('https://www.anilibria.tv/release/%<code>s.html', code: title.code)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anilibria-api-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Kozachenko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-05 00:00:00.000000000 Z
11
+ date: 2022-02-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-struct
@@ -71,6 +71,10 @@ files:
71
71
  - lib/anilibria/api/types/title.rb
72
72
  - lib/anilibria/api/types/youtube.rb
73
73
  - lib/anilibria/api/version.rb
74
+ - sample/rss.rb
75
+ - sample/schedule.rb
76
+ - sample/search_titles.rb
77
+ - sample/title.rb
74
78
  homepage: https://github.com/psychosocial88/anilibria-api-ruby
75
79
  licenses:
76
80
  - MIT