buzzsprout-cli 0.1.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.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +271 -0
  4. data/bin/buzzsprout +6 -0
  5. data/lib/buzzsprout/animation/wordmark.rb +41 -0
  6. data/lib/buzzsprout/animation.rb +181 -0
  7. data/lib/buzzsprout/cli.rb +215 -0
  8. data/lib/buzzsprout/client.rb +306 -0
  9. data/lib/buzzsprout/color.rb +195 -0
  10. data/lib/buzzsprout/commands/base.rb +188 -0
  11. data/lib/buzzsprout/commands/brand_affiliations.rb +98 -0
  12. data/lib/buzzsprout/commands/chapters.rb +58 -0
  13. data/lib/buzzsprout/commands/contributors.rb +119 -0
  14. data/lib/buzzsprout/commands/downloads.rb +139 -0
  15. data/lib/buzzsprout/commands/episodes.rb +116 -0
  16. data/lib/buzzsprout/commands/fan_mail.rb +216 -0
  17. data/lib/buzzsprout/commands/feed_verifications.rb +74 -0
  18. data/lib/buzzsprout/commands/listings.rb +159 -0
  19. data/lib/buzzsprout/commands/login.rb +264 -0
  20. data/lib/buzzsprout/commands/podcasts.rb +85 -0
  21. data/lib/buzzsprout/commands/podrolls.rb +74 -0
  22. data/lib/buzzsprout/commands/skills.rb +144 -0
  23. data/lib/buzzsprout/commands/status.rb +64 -0
  24. data/lib/buzzsprout/commands/team.rb +75 -0
  25. data/lib/buzzsprout/commands/transcripts.rb +100 -0
  26. data/lib/buzzsprout/commands/update.rb +36 -0
  27. data/lib/buzzsprout/commands/uploads.rb +134 -0
  28. data/lib/buzzsprout/config.rb +125 -0
  29. data/lib/buzzsprout/formatter.rb +68 -0
  30. data/lib/buzzsprout/group_help.rb +74 -0
  31. data/lib/buzzsprout/oauth.rb +203 -0
  32. data/lib/buzzsprout/options.rb +155 -0
  33. data/lib/buzzsprout/podcast_picker.rb +328 -0
  34. data/lib/buzzsprout/redact.rb +65 -0
  35. data/lib/buzzsprout/updater.rb +230 -0
  36. data/lib/buzzsprout/version.rb +5 -0
  37. data/skills/README.md +62 -0
  38. data/skills/brand_affiliations/SKILL.md +52 -0
  39. data/skills/buzzsprout/SKILL.md +111 -0
  40. data/skills/chapters/SKILL.md +49 -0
  41. data/skills/contributors/SKILL.md +58 -0
  42. data/skills/episodes/SKILL.md +61 -0
  43. data/skills/fan_mail/SKILL.md +53 -0
  44. data/skills/feed_verifications/SKILL.md +51 -0
  45. data/skills/insertion_points/SKILL.md +50 -0
  46. data/skills/listings/SKILL.md +52 -0
  47. data/skills/podcasts/SKILL.md +57 -0
  48. data/skills/podrolls/SKILL.md +49 -0
  49. data/skills/soundbites/SKILL.md +51 -0
  50. data/skills/stats/SKILL.md +65 -0
  51. data/skills/team/SKILL.md +10 -0
  52. data/skills/transcripts/SKILL.md +60 -0
  53. data/skills/uploads/SKILL.md +68 -0
  54. metadata +102 -0
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+
5
+ module Buzzsprout
6
+ module Commands
7
+ class Downloads < Base
8
+ USAGE = 'buzzsprout downloads today|yesterday|YYYY-MM-DD'
9
+ HELP_WHEN_BARE = true
10
+ DATE_TOKEN = /\A(\d{4}-\d{2}-\d{2}|today|yesterday)\z/i
11
+ HELP_PAGE = {
12
+ name: 'downloads',
13
+ summary: 'Show download totals for one day (podcast or episode).',
14
+ usage: [
15
+ 'buzzsprout downloads today|yesterday|YYYY-MM-DD',
16
+ 'buzzsprout episode <id> downloads today|yesterday|YYYY-MM-DD'
17
+ ],
18
+ aliases: %w[downloads stats],
19
+ flags: [
20
+ ['--date DATE', 'Alternate to a positional date (YYYY-MM-DD, today, yesterday)'],
21
+ ['--page N', 'Page of episode downloads'],
22
+ ['--per-page N', 'Page size of episode downloads']
23
+ ],
24
+ examples: [
25
+ 'buzzsprout downloads today',
26
+ 'buzzsprout downloads yesterday',
27
+ 'buzzsprout downloads 2026-09-01',
28
+ 'buzzsprout episode 788881 downloads today',
29
+ 'buzzsprout episode 788881 downloads yesterday',
30
+ 'buzzsprout episode 788881 downloads 2026-09-01'
31
+ ]
32
+ }.freeze
33
+
34
+ attr_accessor :episode_id
35
+
36
+ def run
37
+ return group_help if misplaced_episode_subcommand?
38
+
39
+ date = take_date!
40
+ if episode_id
41
+ show_episode(date)
42
+ else
43
+ show_podcast(date)
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def incomplete?
50
+ !date_present?
51
+ end
52
+
53
+ def misplaced_episode_subcommand?
54
+ episode_id.nil? && @args[0] == 'episode'
55
+ end
56
+
57
+ def date_present?
58
+ !flag_date.to_s.empty? || @args.any? { |arg| date_token?(arg) }
59
+ end
60
+
61
+ def flag_date
62
+ @options.fields['date']
63
+ end
64
+
65
+ def date_token?(value)
66
+ value.to_s.match?(DATE_TOKEN)
67
+ end
68
+
69
+ def take_date!
70
+ token = flag_date.to_s.strip
71
+ if token.empty?
72
+ idx = @args.index { |arg| date_token?(arg) }
73
+ token = @args.delete_at(idx).to_s if idx
74
+ end
75
+ resolve_date(token)
76
+ end
77
+
78
+ def resolve_date(token)
79
+ value = token.to_s.strip
80
+ raise 'date is required (today, yesterday, or YYYY-MM-DD)' if value.empty?
81
+
82
+ case value.downcase
83
+ when 'today' then Date.today.strftime('%Y-%m-%d')
84
+ when 'yesterday' then (Date.today - 1).strftime('%Y-%m-%d')
85
+ else value
86
+ end
87
+ end
88
+
89
+ def show_podcast(date)
90
+ podcast_id = podcast_id_from_args!
91
+ data = client.get("/#{podcast_id}/downloads", query: { date: date })
92
+ output(data) do
93
+ @formatter.heading("Downloads for #{date}")
94
+ Array(data).each { |row| @io.puts "episode #{row['episode_id']}: #{row['total']}" }
95
+ @io.puts 'No downloads that day.' if Array(data).empty?
96
+ end
97
+ 0
98
+ end
99
+
100
+ def show_episode(date)
101
+ podcast_id = episode_podcast_id
102
+ query = { date: date }
103
+ query[:page] = @options.fields['page'] if @options.fields['page']
104
+ query[:per_page] = @options.fields['per_page'] if @options.fields['per_page']
105
+ data = client.get("/#{podcast_id}/#{episode_id}/downloads", query: query)
106
+ output(data) do
107
+ @formatter.heading("Episode #{episode_id} downloads for #{date}")
108
+ rows = data.is_a?(Hash) ? data.dig('episodes', 0, 'downloads') : nil
109
+ Array(rows).each do |row|
110
+ @io.puts [row['app'], row['device'], row['city'], row['region'], row['country_code'], row['media_type']].compact.join(' • ')
111
+ end
112
+ end
113
+ 0
114
+ end
115
+
116
+ def episode_podcast_id
117
+ if @args.size >= 1 && !@args[0].to_s.start_with?('-') && !date_token?(@args[0])
118
+ return @args.shift
119
+ end
120
+
121
+ explicit = @options.podcast_id || ENV['BUZZSPROUT_PODCAST_ID']
122
+ return explicit unless explicit.to_s.empty?
123
+
124
+ default = Config.saved_podcast_id
125
+ return default if default
126
+
127
+ if interactive_podcast_picker?
128
+ chosen = pick_podcast!(announce: true)
129
+ Config.save(default_podcast_id: chosen['id'])
130
+ return chosen['id']
131
+ end
132
+
133
+ raise PodcastPicker::MISSING_ID
134
+ end
135
+ end
136
+
137
+ Stats = Downloads
138
+ end
139
+ end
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Buzzsprout
4
+ module Commands
5
+ class Episodes < Base
6
+ USAGE = 'buzzsprout episodes [command] [flags]'
7
+ HELP_PAGE = {
8
+ summary: 'List and manage episodes for the current podcast.',
9
+ usage: 'buzzsprout episodes <command> [podcast_id] [episode_id]',
10
+ aliases: %w[episodes episode],
11
+ commands: [
12
+ ['list', 'List episodes (default)'],
13
+ ['show', 'Show one episode'],
14
+ ['create', 'Create an episode (--title)'],
15
+ ['update', 'Update episode fields'],
16
+ ['publish', 'Publish an episode'],
17
+ ['unpublish', 'Unpublish (set private)'],
18
+ ['<id> downloads DATE', 'Episode downloads for today, yesterday, or YYYY-MM-DD']
19
+ ],
20
+ examples: [
21
+ 'buzzsprout episodes list',
22
+ 'buzzsprout episodes show 10 788881',
23
+ 'buzzsprout episodes create --title "Pilot" --private',
24
+ 'buzzsprout episode 788881 downloads today'
25
+ ]
26
+ }.freeze
27
+
28
+ def run
29
+ verb = @args.shift
30
+ case verb
31
+ when nil, 'list'
32
+ list
33
+ when 'show'
34
+ show
35
+ when 'create'
36
+ create
37
+ when 'update'
38
+ update
39
+ when 'publish'
40
+ publish(false)
41
+ when 'unpublish'
42
+ publish(true)
43
+ when /\A\d+\z/
44
+ @args.unshift(verb)
45
+ if @args[1] && @args[1] =~ /\A\d+\z/
46
+ show
47
+ else
48
+ list
49
+ end
50
+ else
51
+ group_help
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def list
58
+ podcast_id = podcast_id_from_args!
59
+ episodes = client.get("/#{podcast_id}/episodes")
60
+ output(episodes) do
61
+ @formatter.heading("Episodes for podcast #{podcast_id}")
62
+ Array(episodes).each do |episode|
63
+ vis = episode['private'] ? 'private' : 'public'
64
+ @io.puts "#{episode['id']} #{episode['title']} (#{vis}, downloads: #{episode['total_downloads']})"
65
+ end
66
+ end
67
+ 0
68
+ end
69
+
70
+ def show
71
+ podcast_id = podcast_id_from_args!
72
+ episode_id = shift_id!('episode_id')
73
+ episode = client.get("/#{podcast_id}/episodes/#{episode_id}")
74
+ output(episode) do
75
+ @formatter.show_record("Episode #{episode['id']}", episode, %w[
76
+ id title audio_url artwork_url description artist tags published_at
77
+ duration guid custom_url episode_number season_number episode_type
78
+ explicit private total_downloads
79
+ ])
80
+ end
81
+ 0
82
+ end
83
+
84
+ def create
85
+ podcast_id = podcast_id_from_args!
86
+ body = @options.merge_body
87
+ raise 'title is required (use --title or --body)' if body['title'].to_s.empty?
88
+
89
+ episode = client.post("/#{podcast_id}/episodes", body: body)
90
+ output(episode) { @io.puts "Created episode #{episode && episode['id']}: #{episode && episode['title']}" }
91
+ 0
92
+ end
93
+
94
+ def update
95
+ podcast_id = podcast_id_from_args!
96
+ episode_id = shift_id!('episode_id')
97
+ body = @options.merge_body
98
+ raise 'Provide fields to update (--title, --body JSON, …)' if body.empty?
99
+
100
+ episode = client.patch("/#{podcast_id}/episodes/#{episode_id}", body: body)
101
+ output(episode) { @io.puts "Updated episode #{episode_id}" }
102
+ 0
103
+ end
104
+
105
+ def publish(private_flag)
106
+ podcast_id = podcast_id_from_args!
107
+ episode_id = shift_id!('episode_id')
108
+ body = @options.merge_body('private' => private_flag)
109
+ episode = client.patch("/#{podcast_id}/episodes/#{episode_id}", body: body)
110
+ verb = private_flag ? 'Unpublished' : 'Published'
111
+ output(episode) { @io.puts "#{verb} episode #{episode_id}" }
112
+ 0
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,216 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Buzzsprout
4
+ module Commands
5
+ class FanMail < Base
6
+ USAGE = 'buzzsprout fan-mail [command] [flags]'
7
+ HELP_PAGE = {
8
+ summary: 'Read and moderate podcast fan mail.',
9
+ usage: 'buzzsprout fan-mail <command> [id]',
10
+ aliases: %w[fan-mail fan_mail],
11
+ commands: [
12
+ ['list', 'List fan mail (default)'],
13
+ ['show', 'Show one message'],
14
+ ['publish', 'Publish a message'],
15
+ ['unpublish', 'Unpublish a message'],
16
+ ['read', 'Mark a message as read'],
17
+ ['block', 'Block the sender']
18
+ ],
19
+ examples: [
20
+ 'buzzsprout fan-mail',
21
+ 'buzzsprout fan-mail publish 42'
22
+ ]
23
+ }.freeze
24
+
25
+ def run
26
+ verb = @args.shift || 'list'
27
+ case verb
28
+ when 'list' then list
29
+ when 'show' then show
30
+ when 'publish' then publish(true)
31
+ when 'unpublish' then publish(false)
32
+ when 'read' then read
33
+ when 'block' then block
34
+ else
35
+ group_help
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def list
42
+ podcast_id = podcast_id_from_args!
43
+ data = client.get("/#{podcast_id}/fan_mail")
44
+ output(data) do
45
+ @formatter.heading("Fan mail for podcast #{podcast_id}")
46
+ Array(data).each do |row|
47
+ @io.puts "#{row['id']} [#{row['status']}] #{row['text'].to_s[0, 80]}"
48
+ end
49
+ end
50
+ 0
51
+ end
52
+
53
+ def show
54
+ podcast_id = podcast_id_from_args!
55
+ id = shift_id!('id')
56
+ data = client.get("/#{podcast_id}/fan_mail/#{id}")
57
+ output(data) { @formatter.show_record("Fan mail #{id}", data, %w[id status published blocked text sender_info city state country created_at message_type]) }
58
+ 0
59
+ end
60
+
61
+ def publish(flag)
62
+ podcast_id = podcast_id_from_args!
63
+ id = shift_id!('id')
64
+ data = client.patch("/#{podcast_id}/fan_mail/#{id}", body: { published: flag })
65
+ output(data) { @io.puts "#{flag ? 'Published' : 'Unpublished'} fan mail #{id}" }
66
+ 0
67
+ end
68
+
69
+ def read
70
+ podcast_id = podcast_id_from_args!
71
+ id = shift_id!('id')
72
+ data = client.post("/#{podcast_id}/fan_mail/#{id}/read")
73
+ output(data) { @io.puts "Marked fan mail #{id} as read" }
74
+ 0
75
+ end
76
+
77
+ def block
78
+ podcast_id = podcast_id_from_args!
79
+ id = shift_id!('id')
80
+ data = client.post("/#{podcast_id}/fan_mail/#{id}/block")
81
+ output(data) { @io.puts "Blocked sender for fan mail #{id}" }
82
+ 0
83
+ end
84
+ end
85
+
86
+ class InsertionPoints < Base
87
+ USAGE = 'buzzsprout insertion-points <command> [flags]'
88
+ HELP_WHEN_BARE = true
89
+ HELP_PAGE = {
90
+ summary: 'Manage dynamic insertion points on an episode.',
91
+ usage: 'buzzsprout insertion-points <command> <episode_id> [id]',
92
+ aliases: %w[insertion-points insertion_points],
93
+ commands: [
94
+ ['list', 'List insertion points'],
95
+ ['create', 'Create one (--timestamp or --seconds)'],
96
+ ['update', 'Update an insertion point'],
97
+ ['delete', 'Delete an insertion point']
98
+ ],
99
+ examples: [
100
+ 'buzzsprout insertion-points list 788881',
101
+ 'buzzsprout insertion-points create 788881 --seconds 30'
102
+ ]
103
+ }.freeze
104
+
105
+ def run
106
+ verb = @args.shift
107
+ case verb
108
+ when 'list' then list
109
+ when 'create' then create
110
+ when 'update' then update
111
+ when 'delete' then delete
112
+ else
113
+ group_help
114
+ end
115
+ end
116
+
117
+ private
118
+
119
+ def list
120
+ podcast_id = podcast_id_from_args!
121
+ episode_id = shift_id!('episode_id')
122
+ data = client.get("/#{podcast_id}/episodes/#{episode_id}/insertion_points")
123
+ output(data) do
124
+ @formatter.heading("Insertion points for episode #{episode_id}")
125
+ Array(data).each { |row| @io.puts "#{row['id']} #{row['timestamp']} (#{row['seconds']}s) eligible=#{row['eligible']}" }
126
+ end
127
+ 0
128
+ end
129
+
130
+ def create
131
+ podcast_id = podcast_id_from_args!
132
+ episode_id = shift_id!('episode_id')
133
+ body = @options.merge_body
134
+ raise 'Provide --timestamp or --seconds' if body['timestamp'].nil? && body['seconds'].nil?
135
+
136
+ data = client.post("/#{podcast_id}/episodes/#{episode_id}/insertion_points", body: body)
137
+ output(data) { @io.puts "Created insertion point #{data && data['id']}" }
138
+ 0
139
+ end
140
+
141
+ def update
142
+ podcast_id = podcast_id_from_args!
143
+ episode_id = shift_id!('episode_id')
144
+ id = shift_id!('id')
145
+ data = client.patch("/#{podcast_id}/episodes/#{episode_id}/insertion_points/#{id}", body: @options.merge_body)
146
+ output(data) { @io.puts "Updated insertion point #{id}" }
147
+ 0
148
+ end
149
+
150
+ def delete
151
+ podcast_id = podcast_id_from_args!
152
+ episode_id = shift_id!('episode_id')
153
+ id = shift_id!('id')
154
+ data = client.delete("/#{podcast_id}/episodes/#{episode_id}/insertion_points/#{id}")
155
+ output(data) { @io.puts "Deleted insertion point #{id}" }
156
+ 0
157
+ end
158
+ end
159
+
160
+ class Soundbites < Base
161
+ USAGE = 'buzzsprout soundbites <command> [flags]'
162
+ HELP_WHEN_BARE = true
163
+ HELP_PAGE = {
164
+ summary: 'Create or inspect an episode soundbite.',
165
+ usage: 'buzzsprout soundbites <command> <episode_id>',
166
+ aliases: %w[soundbites soundbite],
167
+ commands: [
168
+ ['show', 'Show the soundbite'],
169
+ ['create', 'Create a soundbite'],
170
+ ['delete', 'Delete the soundbite']
171
+ ],
172
+ examples: [
173
+ 'buzzsprout soundbites show 788881',
174
+ 'buzzsprout soundbites create 788881 --start-time 00:01:00 --duration 30'
175
+ ]
176
+ }.freeze
177
+
178
+ def run
179
+ verb = @args.shift
180
+ case verb
181
+ when 'show' then show
182
+ when 'create' then create
183
+ when 'delete' then delete
184
+ else
185
+ group_help
186
+ end
187
+ end
188
+
189
+ private
190
+
191
+ def show
192
+ podcast_id = podcast_id_from_args!
193
+ episode_id = shift_id!('episode_id')
194
+ data = client.get("/#{podcast_id}/episodes/#{episode_id}/soundbite")
195
+ output(data) { @formatter.show_record("Soundbite for episode #{episode_id}", data, %w[id creating_video start_time duration aspect_ratio artwork]) }
196
+ 0
197
+ end
198
+
199
+ def create
200
+ podcast_id = podcast_id_from_args!
201
+ episode_id = shift_id!('episode_id')
202
+ data = client.post("/#{podcast_id}/episodes/#{episode_id}/soundbite", body: @options.merge_body)
203
+ output(data) { @io.puts "Started soundbite #{data && data['id']} (creating_video=#{data && data['creating_video']})" }
204
+ 0
205
+ end
206
+
207
+ def delete
208
+ podcast_id = podcast_id_from_args!
209
+ episode_id = shift_id!('episode_id')
210
+ data = client.delete("/#{podcast_id}/episodes/#{episode_id}/soundbite")
211
+ output(data) { @io.puts "Deleted soundbite for episode #{episode_id}" }
212
+ 0
213
+ end
214
+ end
215
+ end
216
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Buzzsprout
4
+ module Commands
5
+ class FeedVerifications < Base
6
+ USAGE = 'buzzsprout feed-verifications [command] [flags]'
7
+ HELP_PAGE = {
8
+ summary: 'Manage podcast feed verification tags.',
9
+ usage: 'buzzsprout feed-verifications <command> [id]',
10
+ aliases: %w[feed-verifications feed_verifications],
11
+ commands: [
12
+ ['list', 'List verification tags (default)'],
13
+ ['create', 'Create a tag (--value)'],
14
+ ['update', 'Update a tag'],
15
+ ['delete', 'Delete a tag']
16
+ ],
17
+ examples: [
18
+ 'buzzsprout feed-verifications',
19
+ 'buzzsprout feed-verifications create --value "abc123"'
20
+ ]
21
+ }.freeze
22
+
23
+ def run
24
+ verb = @args.shift || 'list'
25
+ case verb
26
+ when 'list' then list
27
+ when 'create' then create
28
+ when 'update' then update
29
+ when 'delete' then delete
30
+ else
31
+ group_help
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def list
38
+ podcast_id = podcast_id_from_args!
39
+ data = client.get("/#{podcast_id}/feed_verifications")
40
+ output(data) do
41
+ @formatter.heading("Feed verifications for podcast #{podcast_id}")
42
+ Array(data).each { |row| @io.puts "#{row['id']} #{row['purpose']} #{row['value']}" }
43
+ end
44
+ 0
45
+ end
46
+
47
+ def create
48
+ podcast_id = podcast_id_from_args!
49
+ body = @options.merge_body
50
+ raise 'value is required' if body['value'].to_s.empty?
51
+
52
+ data = client.post("/#{podcast_id}/feed_verifications", body: body)
53
+ output(data) { @io.puts "Created feed verification #{data && data['id']}" }
54
+ 0
55
+ end
56
+
57
+ def update
58
+ podcast_id = podcast_id_from_args!
59
+ id = shift_id!('id')
60
+ data = client.patch("/#{podcast_id}/feed_verifications/#{id}", body: @options.merge_body)
61
+ output(data) { @io.puts "Updated feed verification #{id}" }
62
+ 0
63
+ end
64
+
65
+ def delete
66
+ podcast_id = podcast_id_from_args!
67
+ id = shift_id!('id')
68
+ data = client.delete("/#{podcast_id}/feed_verifications/#{id}")
69
+ output(data) { @io.puts "Deleted feed verification #{id}" }
70
+ 0
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,159 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Buzzsprout
4
+ module Commands
5
+ class Listings < Base
6
+ USAGE = 'buzzsprout listings [directory] [url|status]'
7
+ HELP_PAGE = {
8
+ summary: 'Directory listings for a podcast.',
9
+ usage: [
10
+ 'buzzsprout listings',
11
+ 'buzzsprout listings <directory> [url|status]',
12
+ 'buzzsprout listings submit-all'
13
+ ],
14
+ aliases: %w[listings],
15
+ commands: [
16
+ ['list', 'List all directories (default)'],
17
+ ['<directory>', 'One directory: name, status, and url'],
18
+ ['<directory> url', "Print that directory's URL"],
19
+ ['<directory> status', 'Print listed / submitted / unlisted / failed'],
20
+ ['submit-all', 'Submit remaining one-click directories']
21
+ ],
22
+ examples: [
23
+ 'buzzsprout listings',
24
+ 'buzzsprout listings Apple url',
25
+ 'buzzsprout listings Apple status',
26
+ 'buzzsprout listings Spotify',
27
+ 'buzzsprout listings submit-all'
28
+ ]
29
+ }.freeze
30
+ FIELD_TOKENS = %w[url status].freeze
31
+
32
+ def run
33
+ case @args[0]
34
+ when nil, 'list'
35
+ @args.shift if @args[0] == 'list'
36
+ list
37
+ when 'submit-all', 'submit_all', 'submit'
38
+ @args.shift
39
+ submit_all
40
+ else
41
+ show_directory
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def list
48
+ list_for(listing_podcast_id!)
49
+ end
50
+
51
+ def show_directory
52
+ field = take_field!
53
+ podcast_id = listing_podcast_id!
54
+ query = @args.join(' ').strip
55
+ if query.empty?
56
+ raise 'Missing directory. Example: buzzsprout listings Apple url' if field
57
+
58
+ return list_for(podcast_id)
59
+ end
60
+
61
+ data = client.get("/#{podcast_id}/listings")
62
+ listing = find_listing!(Array(data), query)
63
+ output(listing) do
64
+ if field
65
+ @io.puts listing[field].to_s
66
+ else
67
+ @io.puts compact_listing(listing)
68
+ end
69
+ end
70
+ 0
71
+ end
72
+
73
+ def list_for(podcast_id)
74
+ data = client.get("/#{podcast_id}/listings")
75
+ output(data) do
76
+ @formatter.heading("Listings for podcast #{podcast_id}")
77
+ Array(data).each { |row| @io.puts "#{row['directory']} #{row['status']} #{row['url']}" }
78
+ end
79
+ 0
80
+ end
81
+
82
+ def submit_all
83
+ podcast_id = listing_podcast_id!
84
+ data = client.post("/#{podcast_id}/listings/submit_all")
85
+ output(data) { @io.puts "Submitted remaining one-click directories for podcast #{podcast_id}" }
86
+ 0
87
+ end
88
+
89
+ def take_field!
90
+ token = @args.last.to_s
91
+ return unless FIELD_TOKENS.include?(token.downcase)
92
+
93
+ @args.pop.downcase
94
+ end
95
+
96
+ def listing_podcast_id!
97
+ if @args[0].to_s.match?(/\A\d+\z/)
98
+ return @args.shift
99
+ end
100
+
101
+ explicit = @options.podcast_id || ENV['BUZZSPROUT_PODCAST_ID']
102
+ return explicit unless explicit.to_s.empty?
103
+
104
+ default = Config.saved_podcast_id
105
+ return default if default
106
+
107
+ if interactive_podcast_picker?
108
+ chosen = pick_podcast!(announce: true)
109
+ Config.save(default_podcast_id: chosen['id'])
110
+ return chosen['id']
111
+ end
112
+
113
+ raise PodcastPicker::MISSING_ID
114
+ end
115
+
116
+ def find_listing!(rows, query)
117
+ needle = query.downcase
118
+ exact_dir = rows.select { |row| row['directory'].to_s.downcase == needle }
119
+ return unique_listing!(exact_dir, query) if exact_dir.any?
120
+
121
+ exact_name = rows.select { |row| row['name'].to_s.downcase == needle }
122
+ return unique_listing!(exact_name, query) if exact_name.any?
123
+
124
+ prefix = rows.select do |row|
125
+ row['directory'].to_s.downcase.start_with?(needle) ||
126
+ row['name'].to_s.downcase.start_with?(needle)
127
+ end
128
+ return unique_listing!(prefix, query) if prefix.any?
129
+
130
+ substr = rows.select do |row|
131
+ row['directory'].to_s.downcase.include?(needle) ||
132
+ row['name'].to_s.downcase.include?(needle)
133
+ end
134
+ return unique_listing!(substr, query) if substr.any?
135
+
136
+ raise "No listing matched #{query.inspect}."
137
+ end
138
+
139
+ def unique_listing!(rows, query)
140
+ return rows.first if rows.size == 1
141
+
142
+ labels = rows.map { |row| listing_label(row) }.uniq
143
+ raise "Multiple listings match #{query.inspect}: #{labels.join(', ')}. Be more specific."
144
+ end
145
+
146
+ def listing_label(row)
147
+ dir = row['directory'].to_s
148
+ name = row['name'].to_s
149
+ return dir if name.empty? || name == dir
150
+
151
+ "#{dir} (#{name})"
152
+ end
153
+
154
+ def compact_listing(row)
155
+ [row['name'] || row['directory'], row['status'], row['url']].compact.join(' ')
156
+ end
157
+ end
158
+ end
159
+ end