pinterest-dl 2.3.0 → 2.5.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.
@@ -0,0 +1,190 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../configuration'
4
+
5
+ module PinterestDL
6
+ module TUI
7
+ class State
8
+ CATEGORIES = [
9
+ { key: :all, label: 'All', type: :all, query: nil },
10
+ { key: :images, label: 'Images', type: :image, query: nil },
11
+ { key: :videos, label: 'Videos', type: :video, query: nil },
12
+ { key: :anime, label: 'Anime', type: :all, query: 'anime wallpaper' },
13
+ { key: :gaming, label: 'Gaming', type: :all, query: 'gaming aesthetic art' },
14
+ { key: :aesthetic, label: 'Aesthetic', type: :all, query: 'neon aesthetic' },
15
+ { key: :nature, label: 'Nature', type: :all, query: 'nature landscape photography' },
16
+ { key: :art, label: 'Art', type: :all, query: 'digital illustration art' }
17
+ ].freeze
18
+
19
+ IMAGE_QUALITIES = Configuration::VALID_QUALITIES
20
+ VIDEO_QUALITIES = %i[best 1080p 720p 480p].freeze
21
+ DEFAULT_SEARCH_LIMIT = 25
22
+ MIN_SEARCH_LIMIT = 1
23
+ MAX_SEARCH_LIMIT = 200
24
+
25
+ attr_accessor :mode, :category_index, :search_query, :input_buffer,
26
+ :all_pins, :pins, :selected_index, :queue_selected_index,
27
+ :image_quality_index, :video_quality_index,
28
+ :output_dir, :network_status, :status_message,
29
+ :error_banner, :loading, :grid_columns, :search_limit
30
+
31
+ def initialize(output_dir: './downloads', proxy_enabled: false, search_limit: DEFAULT_SEARCH_LIMIT)
32
+ @mode = :normal
33
+ @category_index = 0
34
+ @search_query = ''
35
+ @input_buffer = ''
36
+ @all_pins = []
37
+ @pins = []
38
+ @selected_index = 0
39
+ @queue_selected_index = 0
40
+ @image_quality_index = 0
41
+ @video_quality_index = 0
42
+ @output_dir = output_dir
43
+ @network_status = proxy_enabled ? :proxy : :online
44
+ @status_message = nil
45
+ @error_banner = nil
46
+ @loading = false
47
+ @grid_columns = 4
48
+ @search_limit = clamp_search_limit(search_limit)
49
+ @selected_pin_ids = {}
50
+ end
51
+
52
+ def update_search_limit(value)
53
+ @search_limit = clamp_search_limit(value)
54
+ end
55
+
56
+ def active_category
57
+ CATEGORIES[@category_index % CATEGORIES.size]
58
+ end
59
+
60
+ def select_category(index)
61
+ @category_index = [[index.to_i, 0].max, CATEGORIES.size - 1].min
62
+ apply_category_filter!
63
+ active_category
64
+ end
65
+
66
+ def cycle_category(step = 1)
67
+ @category_index = (@category_index + step) % CATEGORIES.size
68
+ apply_category_filter!
69
+ active_category
70
+ end
71
+
72
+ def selected_pin
73
+ return nil if @pins.empty?
74
+
75
+ @pins[@selected_index]
76
+ end
77
+
78
+ def toggle_pin_selection(pin = selected_pin)
79
+ return false unless pin
80
+
81
+ key = pin_key(pin)
82
+ if @selected_pin_ids.key?(key)
83
+ @selected_pin_ids.delete(key)
84
+ false
85
+ else
86
+ @selected_pin_ids[key] = true
87
+ true
88
+ end
89
+ end
90
+
91
+ def pin_selected?(pin)
92
+ return false unless pin
93
+
94
+ @selected_pin_ids.key?(pin_key(pin))
95
+ end
96
+
97
+ def selected_batch_count
98
+ @selected_pin_ids.size
99
+ end
100
+
101
+ def selected_batch_pins
102
+ @all_pins.select { |pin| @selected_pin_ids.key?(pin_key(pin)) }
103
+ end
104
+
105
+ def toggle_select_all_visible!
106
+ return if @pins.empty?
107
+
108
+ all_marked = @pins.all? { |p| pin_selected?(p) }
109
+ if all_marked
110
+ @pins.each { |p| @selected_pin_ids.delete(pin_key(p)) }
111
+ else
112
+ @pins.each { |p| @selected_pin_ids[pin_key(p)] = true }
113
+ end
114
+ end
115
+
116
+ def clear_selection!
117
+ @selected_pin_ids.clear
118
+ end
119
+
120
+ def image_quality
121
+ IMAGE_QUALITIES[@image_quality_index % IMAGE_QUALITIES.size]
122
+ end
123
+
124
+ def video_quality
125
+ VIDEO_QUALITIES[@video_quality_index % VIDEO_QUALITIES.size]
126
+ end
127
+
128
+ def cycle_quality!(step = 1)
129
+ pin = selected_pin
130
+ if pin && !pin[:video_url].to_s.empty?
131
+ @video_quality_index = (@video_quality_index + step) % VIDEO_QUALITIES.size
132
+ @status_message = "Video quality: #{video_quality}"
133
+ else
134
+ @image_quality_index = (@image_quality_index + step) % IMAGE_QUALITIES.size
135
+ @status_message = "Image quality: #{image_quality}"
136
+ end
137
+ end
138
+
139
+ def move_gallery(direction)
140
+ return if @pins.empty?
141
+
142
+ cols = [@grid_columns, 1].max
143
+ next_idx = case direction
144
+ when :left then @selected_index - 1
145
+ when :right then @selected_index + 1
146
+ when :up then @selected_index - cols
147
+ when :down then @selected_index + cols
148
+ else @selected_index
149
+ end
150
+ @selected_index = [[next_idx, 0].max, @pins.size - 1].min
151
+ end
152
+
153
+ def replace_pins(new_pins)
154
+ @all_pins = Array(new_pins)
155
+ @selected_pin_ids.clear
156
+ apply_category_filter!
157
+ end
158
+
159
+ def apply_category_filter!
160
+ cat = active_category
161
+ @pins = case cat[:type]
162
+ when :image
163
+ @all_pins.select { |p| !p[:image_url].to_s.empty? && p[:video_url].to_s.empty? }
164
+ when :video
165
+ @all_pins.reject { |p| p[:video_url].to_s.empty? }
166
+ else
167
+ @all_pins.dup
168
+ end
169
+ @selected_index = 0
170
+ end
171
+
172
+ def search_type_for_active_category
173
+ active_category[:type]
174
+ end
175
+
176
+ private
177
+
178
+ def clamp_search_limit(value)
179
+ num = value.to_i
180
+ num = DEFAULT_SEARCH_LIMIT unless num.positive?
181
+ [[num, MIN_SEARCH_LIMIT].max, MAX_SEARCH_LIMIT].min
182
+ end
183
+
184
+ def pin_key(pin)
185
+ id = pin[:id].to_s
186
+ id.empty? ? pin[:image_url].to_s : id
187
+ end
188
+ end
189
+ end
190
+ end
@@ -0,0 +1,281 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'io/console'
5
+ rescue LoadError
6
+ nil
7
+ end
8
+
9
+ module PinterestDL
10
+ module TUI
11
+ class Terminal
12
+ ENTER_ALT_SCREEN = "\e[?1049h"
13
+ LEAVE_ALT_SCREEN = "\e[?1049l"
14
+ HIDE_CURSOR = "\e[?25l"
15
+ SHOW_CURSOR = "\e[?25h"
16
+ ENABLE_MOUSE = "\e[?1000h\e[?1006h"
17
+ DISABLE_MOUSE = "\e[?1006l\e[?1000l"
18
+ SYNC_START = "\e[?2026h"
19
+ SYNC_END = "\e[?2026l"
20
+ CLEAR_HOME = "\e[2J\e[H"
21
+ CLEAR_EOL = "\e[K"
22
+ KITTY_CLEAR_IMAGES = "\e_Ga=d,d=A,q=2\e\\"
23
+ RESET = "\e[0m"
24
+
25
+ DEFAULT_WIDTH = 120
26
+ DEFAULT_HEIGHT = 34
27
+
28
+ attr_reader :input, :output, :graphics_protocol
29
+
30
+ def initialize(input: $stdin, output: $stdout, env: ENV)
31
+ @input = input
32
+ @output = output
33
+ @env = env
34
+ @active = false
35
+ @graphics_protocol = detect_graphics_protocol(env)
36
+ end
37
+
38
+ def kitty_graphics?
39
+ @graphics_protocol == :kitty
40
+ end
41
+
42
+ def tty?
43
+ @input.respond_to?(:tty?) && @input.tty? && @output.respond_to?(:tty?) && @output.tty?
44
+ end
45
+
46
+ def size
47
+ if @output.respond_to?(:winsize)
48
+ rows, cols = @output.winsize
49
+ return [cols, rows] if cols.to_i.positive? && rows.to_i.positive?
50
+ end
51
+ cols = @env['COLUMNS'].to_i
52
+ rows = @env['LINES'].to_i
53
+ [cols.positive? ? cols : DEFAULT_WIDTH, rows.positive? ? rows : DEFAULT_HEIGHT]
54
+ rescue StandardError
55
+ [DEFAULT_WIDTH, DEFAULT_HEIGHT]
56
+ end
57
+
58
+ def with_screen
59
+ enter_screen
60
+ if @input.respond_to?(:raw) && tty?
61
+ @input.raw { yield self }
62
+ else
63
+ yield self
64
+ end
65
+ ensure
66
+ leave_screen
67
+ end
68
+
69
+ def enter_screen
70
+ return if @active
71
+
72
+ @active = true
73
+ @output.print("#{ENTER_ALT_SCREEN}#{HIDE_CURSOR}#{ENABLE_MOUSE}#{CLEAR_HOME}")
74
+ @output.flush
75
+ end
76
+
77
+ def leave_screen
78
+ return unless @active
79
+
80
+ @active = false
81
+ clear_gfx = kitty_graphics? ? KITTY_CLEAR_IMAGES : ''
82
+ @output.print("#{clear_gfx}#{DISABLE_MOUSE}#{RESET}#{SHOW_CURSOR}#{LEAVE_ALT_SCREEN}")
83
+ @output.flush
84
+ end
85
+
86
+ def clear_screen
87
+ clear_gfx = kitty_graphics? ? KITTY_CLEAR_IMAGES : ''
88
+ @output.print("#{clear_gfx}#{CLEAR_HOME}")
89
+ @output.flush
90
+ end
91
+
92
+ def render_frame(frame_string, overlays: [])
93
+ lines = frame_string.split("\n", -1)
94
+ buf = +SYNC_START
95
+ buf << KITTY_CLEAR_IMAGES if kitty_graphics?
96
+ lines.each_with_index do |line, idx|
97
+ buf << "\e[#{idx + 1};1H#{line}#{CLEAR_EOL}#{RESET}"
98
+ end
99
+ buf << "\e[J"
100
+ if @graphics_protocol != :none && overlays && !overlays.empty?
101
+ overlays.each { |ov| buf << ov }
102
+ end
103
+ buf << SYNC_END
104
+ @output.print(buf)
105
+ @output.flush
106
+ end
107
+
108
+ def read_key(timeout: 0.08)
109
+ return nil unless select_ready?(timeout)
110
+
111
+ ch = read_utf8_char_nonblock
112
+ return nil if ch.nil?
113
+
114
+ decode_key(ch)
115
+ end
116
+
117
+ def decode_key(first_char)
118
+ case first_char
119
+ when "\x01" then :ctrl_a
120
+ when "\x03" then :ctrl_c
121
+ when "\r", "\n" then :enter
122
+ when "\t" then :tab
123
+ when "\x7f", "\b" then :backspace
124
+ when "\e" then read_escape_sequence
125
+ else first_char
126
+ end
127
+ end
128
+
129
+ private
130
+
131
+ def detect_graphics_protocol(env)
132
+ forced = env['PINTEREST_DL_GRAPHICS'].to_s.strip.downcase
133
+ return forced.to_sym if %w[kitty iterm2 sixel none].include?(forced)
134
+
135
+ term = env['TERM'].to_s.downcase
136
+ prog = env['TERM_PROGRAM'].to_s.downcase
137
+ lc_term = env['LC_TERMINAL'].to_s.downcase
138
+
139
+ if !env['KITTY_WINDOW_ID'].to_s.empty? || !env['GHOSTTY_RESOURCES_DIR'].to_s.empty? ||
140
+ term.include?('kitty') || term.include?('ghostty') || %w[wezterm ghostty konsole].include?(prog)
141
+ return :kitty
142
+ end
143
+
144
+ if %w[iterm.app tabby hyper warpterminal].include?(prog) || lc_term == 'iterm2'
145
+ return :iterm2
146
+ end
147
+
148
+ if !env['WT_SESSION'].to_s.empty? || term.include?('foot') || term.include?('mlterm') ||
149
+ term.include?('contour') || term.include?('sixel')
150
+ return :sixel
151
+ end
152
+
153
+ :none
154
+ end
155
+
156
+ def select_ready?(timeout)
157
+ return true unless @input.is_a?(IO)
158
+
159
+ !IO.select([@input], nil, nil, timeout).nil?
160
+ rescue IOError, Errno::EBADF, Errno::EINTR
161
+ false
162
+ end
163
+
164
+ def read_byte_nonblock
165
+ return @input.getbyte unless @input.is_a?(IO)
166
+
167
+ chunk = @input.read_nonblock(1)
168
+ chunk&.getbyte(0)
169
+ rescue IO::WaitReadable, IOError, Errno::EINTR
170
+ nil
171
+ end
172
+
173
+ def read_char_nonblock
174
+ byte = read_byte_nonblock
175
+ byte&.chr(Encoding::ASCII_8BIT)
176
+ end
177
+
178
+ def read_utf8_char_nonblock
179
+ first = read_byte_nonblock
180
+ return nil if first.nil?
181
+
182
+ needed = utf8_byte_length(first)
183
+ return first.chr(Encoding::UTF_8) if needed == 1
184
+
185
+ bytes = [first]
186
+ (needed - 1).times do
187
+ select_ready?(0.02)
188
+ nxt = read_byte_nonblock
189
+ break if nxt.nil?
190
+
191
+ bytes << nxt
192
+ end
193
+
194
+ str = bytes.pack('C*').force_encoding(Encoding::UTF_8)
195
+ str.valid_encoding? ? str : nil
196
+ rescue StandardError
197
+ nil
198
+ end
199
+
200
+ def utf8_byte_length(first_byte)
201
+ return 1 if first_byte < 0x80
202
+ return 2 if (first_byte & 0xE0) == 0xC0
203
+ return 3 if (first_byte & 0xF0) == 0xE0
204
+ return 4 if (first_byte & 0xF8) == 0xF0
205
+
206
+ 1
207
+ end
208
+
209
+ def read_escape_sequence
210
+ return :escape unless select_ready?(0.02)
211
+
212
+ second = read_char_nonblock
213
+ return :escape if second.nil?
214
+ return :escape unless second == '['
215
+
216
+ third = read_char_nonblock
217
+ return parse_sgr_mouse if third == '<'
218
+
219
+ decode_csi_key(third)
220
+ end
221
+
222
+ def decode_csi_key(third)
223
+ case third
224
+ when 'A' then :up
225
+ when 'B' then :down
226
+ when 'C' then :right
227
+ when 'D' then :left
228
+ when 'Z' then :shift_tab
229
+ when '3'
230
+ read_char_nonblock
231
+ :delete
232
+ when '5'
233
+ read_char_nonblock
234
+ :page_up
235
+ when '6'
236
+ read_char_nonblock
237
+ :page_down
238
+ else
239
+ :escape
240
+ end
241
+ end
242
+
243
+ def parse_sgr_mouse
244
+ buf = +''
245
+ 24.times do
246
+ ch = read_char_nonblock
247
+ break if ch.nil?
248
+
249
+ return build_mouse_event(buf, pressed: ch == 'M') if %w[M m].include?(ch)
250
+
251
+ buf << ch
252
+ end
253
+ :escape
254
+ end
255
+
256
+ def build_mouse_event(payload, pressed:)
257
+ parts = payload.split(';')
258
+ return :escape unless parts.size == 3
259
+
260
+ code = parts[0].to_i
261
+ col = parts[1].to_i
262
+ row = parts[2].to_i
263
+ button = decode_mouse_button(code)
264
+ return :escape unless button
265
+
266
+ { type: :mouse, button: button, x: col, y: row, pressed: pressed }
267
+ end
268
+
269
+ def decode_mouse_button(code)
270
+ return :wheel_up if code == 64
271
+ return :wheel_down if code == 65
272
+
273
+ case code & 0b11
274
+ when 0 then :left
275
+ when 1 then :middle
276
+ when 2 then :right
277
+ end
278
+ end
279
+ end
280
+ end
281
+ end