cafe_basics 0.1.36 → 0.1.39

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 (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cafe_basics.rb +232 -63
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 23d6859bd69d4f65acca974255b9c01f40d8b4fcd232adae4b8aa5c12f21fe9b
4
- data.tar.gz: 388cd66278320bc004cc1421e184aff26f6925da0853ebbb9fb792623e308c9b
3
+ metadata.gz: 4b7d3f7b0073073b27e6cc35d4f42bd3c92d134e6c0d1f2f0c86479f22ee3eb6
4
+ data.tar.gz: 67833a88ebecd09acf24879cb4cacfbda0870fa422f624815c2388279fbef316
5
5
  SHA512:
6
- metadata.gz: b786be3d1c94cf8343fd6a609afad2d457c9705f830d3c65cd629dad62084fa1706814b367ca7478b2ba4832890661041950e93dcc304b462adfc29c148a8a23
7
- data.tar.gz: 1b192a2a4467afb78bfb8446e7637b3dd5b4749236f835a1aec0d19c128ba1af87e4f75e06dd61bd598249e5d381ba5ae00e40298998eba6e2867c810867f100
6
+ metadata.gz: 7bda928e68fdab7f7a224fea59d791d1cea8033cd2f297e8f56c332d697d4345776a5560e2245713e8cc5fe4a49895fddaeb22bc77c7da36923a5355fcef2da3
7
+ data.tar.gz: df5c31c0c0b68ad9ff3e0096103d0ab7ddc2c8c643e5c7c2f05cc4f1db284565ce4274edb39c5f539058aa067736adcd92ccc4df8ecd933f898db4dd34ac76d3
data/lib/cafe_basics.rb CHANGED
@@ -17,6 +17,8 @@ require 'cgi'
17
17
  require 'digest'
18
18
  require 'auto_click'
19
19
  require 'rainbow/refinement'
20
+ require 'httpclient'
21
+ require 'win32ole'
20
22
  include AutoClickMethods
21
23
  using Rainbow
22
24
  include Glimmer
@@ -241,8 +243,38 @@ end
241
243
  class Naver
242
244
  def initialize
243
245
  @seed = 1
246
+ kill_selenium_chrome #기존 창 모두 닫는 명령
247
+ sleep(1)
244
248
  end
245
249
 
250
+ def kill_selenium_chrome #기존 창 모두 닫는 코드
251
+ wmi = WIN32OLE.connect("winmgmts://")
252
+ chrome_procs = wmi.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'chrome.exe'")
253
+
254
+ chrome_procs.each do |proc|
255
+ cmd = proc.CommandLine
256
+ if cmd && cmd.include?("user-data-dir=C:/naver_cookie")
257
+ puts "→ 크롬 창 초기화: PID #{proc.ProcessId}"
258
+ begin
259
+ proc.Terminate
260
+ rescue
261
+ #puts "→ 이미 종료된 프로세스: #{proc.ProcessId}"
262
+ end
263
+ end
264
+ end
265
+
266
+ # chromedriver도 같이 종료
267
+ chromedrivers = wmi.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'chromedriver.exe'")
268
+ chromedrivers.each do |proc|
269
+ puts "→ 크롬 창 초기화: PID #{proc.ProcessId}"
270
+ begin
271
+ proc.Terminate
272
+ rescue
273
+ #puts "→ 이미 종료된 chromedriver: #{proc.ProcessId}"
274
+ end
275
+ end
276
+ end
277
+
246
278
 
247
279
  def chrome_setup(user_id, proxy)
248
280
  naver_cookie_dir = "C:/naver_cookie"
@@ -1964,21 +1996,81 @@ class Wordpress
1964
1996
 
1965
1997
 
1966
1998
 
1967
- def auto_image
1999
+ def auto_image(keyword = nil)
2000
+ keyword ||= @keyword
2001
+ puts "키워드: #{keyword}"
2002
+
2003
+ client = HTTPClient.new
2004
+ client.default_header = {
2005
+ 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '\
2006
+ '(KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36',
2007
+ 'Accept' => 'application/json, text/javascript, */*; q=0.01',
2008
+ 'Accept-Language' => 'en-US,en;q=0.9',
2009
+ 'Referer' => "https://unsplash.com/s/photos/#{URI.encode_www_form_component(keyword)}",
2010
+ 'X-Requested-With' => 'XMLHttpRequest'
2011
+ }
2012
+
2013
+ retry_count = 0
2014
+ max_retries = 10
2015
+ results = []
2016
+
1968
2017
  begin
1969
2018
  page = rand(1..15)
1970
- http = HTTP.get('https://unsplash.com/napi/photos?per_page=12&page='+page.to_s)
1971
- json = JSON.parse(http.to_s)
1972
- mm = Array.new
1973
- json.each do |i|
1974
- mm << i['urls']['full']
2019
+ url = "https://unsplash.com/napi/search/photos?query=#{URI.encode_www_form_component(keyword)}&page=#{page}&per_page=20"
2020
+ puts "Request URL: #{url}"
2021
+ res = client.get(url)
2022
+
2023
+ unless res.status == 200
2024
+ puts "HTTP Error: #{res.status}"
2025
+ raise "HTTP Error"
1975
2026
  end
1976
- url = mm.sample
1977
- Down.download(url, destination: "./image/memory.png")
1978
- rescue
1979
- puts 'auto_image 일시적 error 5초후 제시도...'
1980
- sleep(5)
2027
+
2028
+ json = JSON.parse(res.body)
2029
+ results = json['results']
2030
+ mm = []
2031
+
2032
+ results.each do |photo|
2033
+ full_url = photo.dig('urls', 'full').to_s
2034
+ regular_url = photo.dig('urls', 'regular').to_s
2035
+
2036
+ if full_url.start_with?("https://images.unsplash.com/photo-") &&
2037
+ regular_url.include?("1080")
2038
+ mm << full_url
2039
+ end
2040
+ end
2041
+
2042
+ if mm.empty?
2043
+ raise "No matching image"
2044
+ end
2045
+
2046
+ selected_url = mm.sample
2047
+ Down.download(selected_url, destination: "./image/memory.png")
2048
+ puts "이미지 다운로드 완료: #{selected_url}"
2049
+
2050
+ rescue => e
2051
+ retry_count += 1
2052
+ puts "auto_image 에러: #{e.message} (재시도 #{retry_count}/#{max_retries})"
2053
+ sleep(3)
2054
+ if retry_count < max_retries
1981
2055
  retry
2056
+ else
2057
+ puts "최대 재시도 초과. 조건 무시하고 랜덤 이미지 다운로드 시도..."
2058
+
2059
+ if results && !results.empty?
2060
+ random_photo = results.sample
2061
+ fallback_url = random_photo.dig('urls', 'full')
2062
+ if fallback_url
2063
+ Down.download(fallback_url, destination: "./image/memory.png")
2064
+ puts "랜덤 이미지 다운로드 완료: #{fallback_url}"
2065
+ else
2066
+ puts "랜덤 이미지 URL을 찾을 수 없습니다. 단색 배경 이미지 생성합니다."
2067
+ color_image
2068
+ end
2069
+ else
2070
+ puts "이미지 결과가 없어 다운로드할 수 없습니다. 단색 배경 이미지 생성합니다."
2071
+ color_image
2072
+ end
2073
+ end
1982
2074
  end
1983
2075
  end
1984
2076
 
@@ -2017,52 +2109,106 @@ class Wordpress
2017
2109
  img = Magick::Image.read('./image/memory.png').first
2018
2110
  width = img.columns
2019
2111
  height = img.rows
2020
- begin
2021
- if @data['image_type'][0].checked? or @data['image_type'][2].checked?
2022
- img.resize!(w, w*(height.to_f/width.to_f))
2023
- else
2024
- img.resize!(w, w)
2112
+
2113
+ # '원본' 선택된 경우, 리사이징을 하지 않고 원본 이미지를 그대로 반환
2114
+ if w == 'original'
2115
+ return img # 원본 이미지 그대로 반환
2116
+ else
2117
+ begin
2118
+ if @data['image_type'][0].checked? or @data['image_type'][2].checked?
2119
+ # 비율을 맞추어 리사이징
2120
+ img.resize!(w, w * (height.to_f / width.to_f))
2121
+ else
2122
+ # 정사각형으로 리사이징
2123
+ img.resize!(w, w)
2124
+ end
2125
+ rescue
2126
+ img.resize!(w, w) # 예외 처리 시에도 리사이징
2025
2127
  end
2026
- rescue
2027
- img.resize!(w, w)
2028
2128
  end
2129
+
2130
+ # 리사이징된 이미지 저장
2029
2131
  img.write('./image/memory.png')
2030
2132
  end
2031
2133
 
2134
+ def wrap_text_to_fit(draw, text, max_width, max_height, font_path, initial_size)
2135
+ size = initial_size
2136
+ draw.font = font_path
2137
+
2138
+ loop do
2139
+ draw.pointsize = size
2140
+ words = text.chars # 글자 단위로 자름 (한국어 기준)
2141
+ lines = []
2142
+ line = ""
2143
+
2144
+ words.each do |char|
2145
+ test_line = line + char
2146
+ metrics = draw.get_type_metrics(test_line)
2147
+ if metrics.width > max_width
2148
+ lines << line
2149
+ line = char
2150
+ else
2151
+ line = test_line
2152
+ end
2153
+ end
2154
+ lines << line unless line.empty?
2155
+
2156
+ line_height = draw.get_type_metrics("가").height
2157
+ total_height = line_height * lines.size
2158
+
2159
+ # 세로 초과 안 하면 성공
2160
+ if total_height <= max_height || size <= 10
2161
+ return [lines.join("\n"), size]
2162
+ else
2163
+ size -= 2
2164
+ end
2165
+ end
2166
+ end
2167
+
2168
+
2032
2169
  def image_text(text1, text2)
2033
2170
  begin
2034
2171
  color = File.open('./color.ini', 'r', :encoding => 'utf-8').read().split("\n")
2035
- font = Dir.entries('./fonts')
2036
- img = Magick::Image.read('./image/memory.png').first
2037
- text = Magick::Draw.new
2172
+ font_files = Dir.entries('./fonts').select { |f| f.downcase.end_with?('.ttf') }
2173
+ font2 = './fonts/' + font_files.sample
2038
2174
  color2 = color.sample
2039
- font2 = './fonts/'+font.sample
2040
- message = text1.to_s+"\n"+text2.to_s
2175
+
2176
+ img = Magick::Image.read('./image/memory.png').first
2177
+ draw = Magick::Draw.new
2178
+
2179
+ raw_message = "#{text1}\n#{text2}".strip
2180
+ max_width = img.columns * 0.85
2181
+ max_height = img.rows * 0.6
2182
+
2041
2183
  begin
2042
2184
  size = rand(@data['이미지설정']['이미지글자1크기1'].text.to_i..@data['이미지설정']['이미지글자1크기2'].text.to_i)
2043
2185
  rescue
2044
2186
  size = 30
2045
2187
  end
2188
+
2189
+ wrapped_message, adjusted_size = wrap_text_to_fit(draw, raw_message, max_width, max_height, font2, size)
2190
+
2046
2191
  if @data['이미지설정']['글자그림자'].checked?
2047
- img.annotate(text, 0,0, +3,+3, message) do
2048
- text.gravity = Magick::CenterGravity
2049
- text.pointsize = size
2050
- text.fill = '#000000'
2051
- text.font = font2
2192
+ img.annotate(draw, 0, 0, 2, 2, wrapped_message) do
2193
+ draw.gravity = Magick::CenterGravity
2194
+ draw.pointsize = adjusted_size
2195
+ draw.fill = '#000000'
2196
+ draw.font = font2
2052
2197
  end
2053
2198
  end
2054
-
2055
- img.annotate(text, 0,0,0,0, message) do
2056
- text.gravity = Magick::CenterGravity
2057
- text.pointsize = size
2199
+
2200
+ draw2 = Magick::Draw.new
2201
+ img.annotate(draw2, 0, 0, 0, 0, wrapped_message) do
2202
+ draw2.gravity = Magick::CenterGravity
2203
+ draw2.pointsize = adjusted_size
2204
+ draw2.fill = color2
2205
+ draw2.font = font2
2058
2206
  if @data['이미지설정']['글자테두리'].checked?
2059
- text.stroke_width = 2
2060
- text.stroke = '#000000'
2207
+ draw2.stroke_width = 2
2208
+ draw2.stroke = '#000000'
2061
2209
  end
2062
- text.fill = color2
2063
- text.font = font2
2064
2210
  end
2065
-
2211
+
2066
2212
  img.write('./image/memory.png')
2067
2213
  rescue
2068
2214
  puts '이미지 폰트 불러오기 오류 재시도...'
@@ -2096,23 +2242,30 @@ class Wordpress
2096
2242
  else
2097
2243
  auto_image()
2098
2244
  end
2099
-
2100
- image_size = [480,740,650,550,480]
2245
+
2246
+ # '원본'을 포함한 이미지 크기 옵션 추가
2247
+ image_size = [480, 740, 650, 550, 480, 'original']
2101
2248
  size = 0
2102
- for n in 0..4
2249
+
2250
+ for n in 0..5 # 0부터 5까지 반복, '원본' 옵션까지 포함
2103
2251
  if @data['image_size'][n].checked?
2104
- if n == 0
2105
- size = image_size.sample
2252
+ if n == 5 # '원본'이 선택되었을 경우
2253
+ size = 'original'
2254
+ elsif n == 0
2255
+ size = image_size.sample # 랜덤 선택
2106
2256
  else
2107
2257
  size = image_size[n]
2108
2258
  end
2109
2259
  end
2110
2260
  end
2261
+
2262
+ # '원본'이 선택되지 않았다면 기본 값 설정
2111
2263
  if size == 0
2112
2264
  size = 480
2113
2265
  end
2266
+
2267
+ change_image_size(size) # 크기 변경 함수 호출
2114
2268
 
2115
- change_image_size(size)
2116
2269
 
2117
2270
  if @data['이미지설정']['필터사용'].checked?
2118
2271
  image_filter()
@@ -2120,33 +2273,33 @@ class Wordpress
2120
2273
 
2121
2274
  if @data['이미지설정']['글자삽입1'].checked?
2122
2275
  if @data['이미지설정']['이미지글자1'].length == 0
2123
- image_text_path1 = ''
2276
+ image_text_path1 = ''
2124
2277
  else
2125
- if @data['이미지설정']['글자랜덤'].checked?
2126
- image_text_path1 = @data['이미지설정']['이미지글자1'].sample
2127
- else
2128
- image_text_path1 = @data['이미지설정']['이미지글자1'][@image_text_soon1]
2129
- @image_text_soon1 += 1
2130
- if @image_text_soon1 > @data['이미지설정']['이미지글자1'].length - 1
2131
- @image_text_soon1 = 0
2132
- end
2133
- end
2278
+ if @data['이미지설정']['글자랜덤'].checked?
2279
+ image_text_path1 = @data['이미지설정']['이미지글자1'].sample
2280
+ else
2281
+ image_text_path1 = @data['이미지설정']['이미지글자1'][@image_text_soon1]
2282
+ @image_text_soon1 += 1
2283
+ if @image_text_soon1 > @data['이미지설정']['이미지글자1'].length - 1
2284
+ @image_text_soon1 = 0
2285
+ end
2286
+ end
2134
2287
  end
2135
2288
  end
2136
2289
 
2137
2290
  if @data['이미지설정']['글자삽입2'].checked?
2138
2291
  if @data['이미지설정']['이미지글자2'].length == 0
2139
- image_text_path2 = ''
2292
+ image_text_path2 = ''
2140
2293
  else
2141
- if @data['이미지설정']['글자랜덤'].checked?
2142
- image_text_path2 = @data['이미지설정']['이미지글자2'].sample
2143
- else
2144
- image_text_path2 = @data['이미지설정']['이미지글자2'][@image_text_soon2]
2145
- @image_text_soon2 += 1
2146
- if @image_text_soon2 > @data['이미지설정']['이미지글자2'].length - 1
2147
- @image_text_soon2 = 0
2148
- end
2149
- end
2294
+ if @data['이미지설정']['글자랜덤'].checked?
2295
+ image_text_path2 = @data['이미지설정']['이미지글자2'].sample
2296
+ else
2297
+ image_text_path2 = @data['이미지설정']['이미지글자2'][@image_text_soon2]
2298
+ @image_text_soon2 += 1
2299
+ if @image_text_soon2 > @data['이미지설정']['이미지글자2'].length - 1
2300
+ @image_text_soon2 = 0
2301
+ end
2302
+ end
2150
2303
  end
2151
2304
  end
2152
2305
 
@@ -4510,6 +4663,7 @@ class Wordpress
4510
4663
  @data['image_size'][2].checked = false
4511
4664
  @data['image_size'][3].checked = false
4512
4665
  @data['image_size'][4].checked = false
4666
+ @data['image_size'][5].checked = false
4513
4667
  end
4514
4668
  }
4515
4669
  }
@@ -4520,6 +4674,7 @@ class Wordpress
4520
4674
  @data['image_size'][2].checked = false
4521
4675
  @data['image_size'][3].checked = false
4522
4676
  @data['image_size'][4].checked = false
4677
+ @data['image_size'][5].checked = false
4523
4678
  end
4524
4679
  }
4525
4680
  }
@@ -4530,6 +4685,7 @@ class Wordpress
4530
4685
  @data['image_size'][0].checked = false
4531
4686
  @data['image_size'][3].checked = false
4532
4687
  @data['image_size'][4].checked = false
4688
+ @data['image_size'][5].checked = false
4533
4689
  end
4534
4690
  }
4535
4691
  }
@@ -4540,6 +4696,7 @@ class Wordpress
4540
4696
  @data['image_size'][2].checked = false
4541
4697
  @data['image_size'][0].checked = false
4542
4698
  @data['image_size'][4].checked = false
4699
+ @data['image_size'][5].checked = false
4543
4700
  end
4544
4701
  }
4545
4702
  }
@@ -4550,6 +4707,18 @@ class Wordpress
4550
4707
  @data['image_size'][2].checked = false
4551
4708
  @data['image_size'][3].checked = false
4552
4709
  @data['image_size'][0].checked = false
4710
+ @data['image_size'][5].checked = false
4711
+ end
4712
+ }
4713
+ }
4714
+ @data['image_size'][5] = checkbox('원본 px'){
4715
+ on_toggled{
4716
+ if @data['image_size'][5].checked?
4717
+ @data['image_size'][1].checked = false
4718
+ @data['image_size'][2].checked = false
4719
+ @data['image_size'][3].checked = false
4720
+ @data['image_size'][0].checked = false
4721
+ @data['image_size'][4].checked = false
4553
4722
  end
4554
4723
  }
4555
4724
  }
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cafe_basics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.36
4
+ version: 0.1.39
5
5
  platform: ruby
6
6
  authors:
7
7
  - zon
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-05-23 00:00:00.000000000 Z
10
+ date: 2025-05-29 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: File to Clipboard gem
13
13
  email: mymin26@naver.com