huginn_themoviedb_agent 0.2.2

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
+ SHA256:
3
+ metadata.gz: 8deeeaa4ad2ba1c85a4d264b36f14fa377ad6162e2157885e895f50086aa18ae
4
+ data.tar.gz: 746760c49574992bb3ac74a83146e29a549bbc1c70bef5bce0488cbc2382fac0
5
+ SHA512:
6
+ metadata.gz: 7b3407d0123096ab6deffbed5e97b61da6635e7d65ee3aa366968b4149c179583b6529fe42abc4c84329e196cee7afd24ffad377dbbd8c075d18fd308a888ac5
7
+ data.tar.gz: fc03cb9f61d510bb997ebd815cdf1113e24ec99a784da296db2c39d655d5f4d132f980be38f891aa297d6683382b566537e3042810967999ee202c28b9e1d468
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2023 Nicolas Germain
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,227 @@
1
+ module Agents
2
+ class ThemoviedbAgent < Agent
3
+ include FormConfigurable
4
+ can_dry_run!
5
+ no_bulk_receive!
6
+ default_schedule 'every_1h'
7
+
8
+ description do
9
+ <<-MD
10
+ The moviedb Agent interacts with Themoviedb API.
11
+
12
+ `debug` is used for verbose mode.
13
+
14
+ `token` is used for authentication.
15
+
16
+ `type` is for the wanted action like tv_show_details.
17
+
18
+ `expected_receive_period_in_days` is used to determine if the Agent is working. Set it to the maximum number of days
19
+ that you anticipate passing without this Agent receiving an incoming Event.
20
+ MD
21
+ end
22
+
23
+ event_description <<-MD
24
+ Events look like this:
25
+
26
+ {
27
+ "id": 3718988,
28
+ "name": "Jibaro",
29
+ "overview": "A deaf knight and a siren of myth become entwined in a deadly dance. A fatal attraction infused with blood, death and treasure.",
30
+ "vote_average": 8.123,
31
+ "vote_count": 65,
32
+ "air_date": "2022-05-21",
33
+ "episode_number": 9,
34
+ "episode_type": "finale",
35
+ "production_code": "",
36
+ "runtime": 18,
37
+ "season_number": 3,
38
+ "show_id": 86831,
39
+ "still_path": "/qWHGEsBFoh0Cq3AEWoMsxLZOayx.jpg",
40
+ "original_name": "Love, Death & Robots",
41
+ "event_type": "last episode"
42
+ }
43
+ MD
44
+
45
+ def default_options
46
+ {
47
+ 'type' => 'tv_show_details',
48
+ 'token' => '',
49
+ 'series_id' => '',
50
+ 'debug' => 'false',
51
+ 'emit_events' => 'true',
52
+ 'expected_receive_period_in_days' => '2',
53
+ }
54
+ end
55
+
56
+ form_configurable :token, type: :string
57
+ form_configurable :series_id, type: :string
58
+ form_configurable :debug, type: :boolean
59
+ form_configurable :emit_events, type: :boolean
60
+ form_configurable :expected_receive_period_in_days, type: :string
61
+ form_configurable :type, type: :array, values: ['tv_show_details']
62
+ def validate_options
63
+ errors.add(:base, "type has invalid value: should be 'tv_show_details'") if interpolated['type'].present? && !%w(tv_show_details).include?(interpolated['type'])
64
+
65
+ unless options['token'].present? || !['tv_show_details'].include?(options['type'])
66
+ errors.add(:base, "token is a required field")
67
+ end
68
+
69
+ unless options['series_id'].present? || !['tv_show_details'].include?(options['type'])
70
+ errors.add(:base, "series_id is a required field")
71
+ end
72
+
73
+ if options.has_key?('emit_events') && boolify(options['emit_events']).nil?
74
+ errors.add(:base, "if provided, emit_events must be true or false")
75
+ end
76
+
77
+ if options.has_key?('debug') && boolify(options['debug']).nil?
78
+ errors.add(:base, "if provided, debug must be true or false")
79
+ end
80
+
81
+ unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
82
+ errors.add(:base, "Please provide 'expected_receive_period_in_days' to indicate how many days can pass before this Agent is considered to be not working")
83
+ end
84
+ end
85
+
86
+ def working?
87
+ event_created_within?(options['expected_receive_period_in_days']) && !recent_error_logs?
88
+ end
89
+
90
+ def receive(incoming_events)
91
+ incoming_events.each do |event|
92
+ interpolate_with(event) do
93
+ log event
94
+ trigger_action
95
+ end
96
+ end
97
+ end
98
+
99
+ def check
100
+ trigger_action
101
+ end
102
+
103
+ private
104
+
105
+ def log_curl_output(code,body)
106
+
107
+ log "request status : #{code}"
108
+
109
+ if interpolated['debug'] == 'true'
110
+ log "body"
111
+ log body
112
+ end
113
+
114
+ end
115
+
116
+ def season_details(original_name,season_number,language)
117
+
118
+ url = URI("https://api.themoviedb.org/3/tv/#{interpolated['series_id']}/season/#{season_number}?language=#{language}")
119
+
120
+ http = Net::HTTP.new(url.host, url.port)
121
+ http.use_ssl = true
122
+
123
+ request = Net::HTTP::Get.new(url)
124
+ request["accept"] = 'application/json'
125
+ request["Authorization"] = "Bearer #{interpolated['token']}"
126
+
127
+ response = http.request(request)
128
+
129
+ log_curl_output(response.code,response.body)
130
+
131
+ payload = JSON.parse(response.body)
132
+
133
+ payload['episodes'].each do |episode|
134
+ if interpolated['emit_events'] == 'true'
135
+ event_created = episode.dup
136
+ event_created['original_name'] = original_name
137
+ event_created['event_type'] = 'new episode'
138
+ create_event payload: event_created
139
+ end
140
+ end
141
+
142
+ end
143
+
144
+ def account_details()
145
+
146
+ url = URI("https://api.themoviedb.org/3/account/null")
147
+
148
+ http = Net::HTTP.new(url.host, url.port)
149
+ http.use_ssl = true
150
+
151
+ request = Net::HTTP::Get.new(url)
152
+ request["accept"] = 'application/json'
153
+ request["Authorization"] = "Bearer #{interpolated['token']}"
154
+
155
+ response = http.request(request)
156
+
157
+ log_curl_output(response.code,response.body)
158
+
159
+ return response.body
160
+
161
+ end
162
+
163
+ def tv_show_details()
164
+
165
+ your_details = JSON.parse(account_details())
166
+ language = "#{your_details['iso_639_1']}" + "-" + "#{your_details['iso_3166_1']}"
167
+ url = URI("https://api.themoviedb.org/3/tv/#{interpolated['series_id']}?language=#{language}")
168
+
169
+ http = Net::HTTP.new(url.host, url.port)
170
+ http.use_ssl = true
171
+
172
+ request = Net::HTTP::Get.new(url)
173
+ request["accept"] = 'application/json'
174
+ request["Authorization"] = "Bearer #{interpolated['token']}"
175
+
176
+ response = http.request(request)
177
+
178
+ log_curl_output(response.code,response.body)
179
+
180
+ payload = JSON.parse(response.body)
181
+
182
+ if payload.to_s != memory['last_status']
183
+ if "#{memory['last_status']}" == ''
184
+ create_event payload: payload
185
+ else
186
+ last_status = memory['last_status']
187
+ if payload['last_air_date'] != last_status['last_air_date']
188
+ event_created = payload['last_episode_to_air'].dup
189
+ event_created['original_name'] = payload['original_name']
190
+ event_created['event_type'] = 'last episode'
191
+ create_event payload: event_created
192
+ end
193
+ payload['seasons'].each do | season |
194
+ found = false
195
+ last_status['seasons'].each do | seasonbis |
196
+ if season == seasonbis || season['id'] == seasonbis['id']
197
+ found = true
198
+ end
199
+ end
200
+ if interpolated['debug'] == 'true'
201
+ log found
202
+ end
203
+ if found == false
204
+ event_created = season.dup
205
+ event_created['original_name'] = payload['original_name']
206
+ event_created['event_type'] = 'new season'
207
+ create_event :payload => event_created
208
+ season_details(payload['original_name'],payload['number_of_seasons'],language)
209
+ end
210
+ end
211
+ end
212
+ memory['last_status'] = payload
213
+ end
214
+
215
+ end
216
+
217
+ def trigger_action
218
+
219
+ case interpolated['type']
220
+ when "tv_show_details"
221
+ tv_show_details()
222
+ else
223
+ log "Error: type has an invalid value (#{interpolated['type']})"
224
+ end
225
+ end
226
+ end
227
+ end
@@ -0,0 +1,4 @@
1
+ require 'huginn_agent'
2
+
3
+ #HuginnAgent.load 'huginn_themoviedb_agent/concerns/my_agent_concern'
4
+ HuginnAgent.register 'huginn_themoviedb_agent/themoviedb_agent'
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::ThemoviedbAgent do
5
+ before(:each) do
6
+ @valid_options = Agents::ThemoviedbAgent.new.default_options
7
+ @checker = Agents::ThemoviedbAgent.new(:name => "ThemoviedbAgent", :options => @valid_options)
8
+ @checker.user = users(:bob)
9
+ @checker.save!
10
+ end
11
+
12
+ pending "add specs here"
13
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: huginn_themoviedb_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.2
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Germain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-08-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 12.3.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 12.3.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: huginn_agent
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Write a longer description or delete this line.
56
+ email:
57
+ - ngermain@hihouhou.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE.txt
63
+ - lib/huginn_themoviedb_agent.rb
64
+ - lib/huginn_themoviedb_agent/themoviedb_agent.rb
65
+ - spec/themoviedb_agent_spec.rb
66
+ homepage: https://github.com/hihouhou/huginn_themoviedb_agent
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.3.3
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Write a short summary, because Rubygems requires one.
89
+ test_files:
90
+ - spec/themoviedb_agent_spec.rb