cafe_buy 0.1.36 → 0.1.37

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_buy.rb +72 -11
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e7c8989d585089b973707a6c6c6a27289728cb6ef7ddfc7d2234d0000bd264c2
4
- data.tar.gz: f3963a691796b668a6fa193534ec2d249ec10abd41d09ef97e996651c1424a88
3
+ metadata.gz: 48247ce4be293f453c3d548a3653864df11aec18303eabce8fa67671fe5ab05d
4
+ data.tar.gz: ac807bc3fec65e439fb12d29f8dcc53eca7db615834566cbf774a3338103acf8
5
5
  SHA512:
6
- metadata.gz: f5e7d469d77acbcd05a8a1d886751bd3127373b061ae20dd94c3c35203a53d7093d94422002bcc08182e40c7aa3b95369b89b73e9f0ea1e51753c511dfdd7601
7
- data.tar.gz: aa81966d5c2ec6682ee96906c1958dd217f69d7be271d649de74d466e6e5fc798dcab4773ff90fbe315e27a603b3254c176a0876c82ac4c47c2a386aae67846b
6
+ metadata.gz: fe08f4b9609b95125e38cf89b6beeb173e4369ef70757abc2e3e2f5c74beda009908ea0d746fc74ae3a817a80f51d63e2a85f16cada2897a2d2217ddf7c7e235
7
+ data.tar.gz: 17cf01f9cdeab1d013c7357d693c4718081de74455ad2bcde19b71caa966b112e89e8b554ea7c7b0ba876f4ec15e3e7b7c8ba09c521cc096a00df254b2cdc20b
data/lib/cafe_buy.rb CHANGED
@@ -17,6 +17,7 @@ require 'cgi'
17
17
  require 'digest'
18
18
  require 'auto_click'
19
19
  require 'rainbow/refinement'
20
+ require 'httpclient'
20
21
  include AutoClickMethods
21
22
  using Rainbow
22
23
  include Glimmer
@@ -2301,21 +2302,81 @@ class Wordpress
2301
2302
 
2302
2303
 
2303
2304
 
2304
- def auto_image
2305
+ def auto_image(keyword = nil)
2306
+ keyword ||= @keyword
2307
+ puts "키워드: #{keyword}"
2308
+
2309
+ client = HTTPClient.new
2310
+ client.default_header = {
2311
+ 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '\
2312
+ '(KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36',
2313
+ 'Accept' => 'application/json, text/javascript, */*; q=0.01',
2314
+ 'Accept-Language' => 'en-US,en;q=0.9',
2315
+ 'Referer' => "https://unsplash.com/s/photos/#{URI.encode_www_form_component(keyword)}",
2316
+ 'X-Requested-With' => 'XMLHttpRequest'
2317
+ }
2318
+
2319
+ retry_count = 0
2320
+ max_retries = 10
2321
+ results = []
2322
+
2305
2323
  begin
2306
2324
  page = rand(1..15)
2307
- http = HTTP.get('https://unsplash.com/napi/photos?per_page=12&page='+page.to_s)
2308
- json = JSON.parse(http.to_s)
2309
- mm = Array.new
2310
- json.each do |i|
2311
- mm << i['urls']['full']
2325
+ url = "https://unsplash.com/napi/search/photos?query=#{URI.encode_www_form_component(keyword)}&page=#{page}&per_page=20"
2326
+ puts "Request URL: #{url}"
2327
+ res = client.get(url)
2328
+
2329
+ unless res.status == 200
2330
+ puts "HTTP Error: #{res.status}"
2331
+ raise "HTTP Error"
2312
2332
  end
2313
- url = mm.sample
2314
- Down.download(url, destination: "./image/memory.png")
2315
- rescue
2316
- puts 'auto_image 일시적 error 5초후 제시도...'
2317
- sleep(5)
2333
+
2334
+ json = JSON.parse(res.body)
2335
+ results = json['results']
2336
+ mm = []
2337
+
2338
+ results.each do |photo|
2339
+ full_url = photo.dig('urls', 'full').to_s
2340
+ regular_url = photo.dig('urls', 'regular').to_s
2341
+
2342
+ if full_url.start_with?("https://images.unsplash.com/photo-") &&
2343
+ regular_url.include?("1080")
2344
+ mm << full_url
2345
+ end
2346
+ end
2347
+
2348
+ if mm.empty?
2349
+ raise "No matching image"
2350
+ end
2351
+
2352
+ selected_url = mm.sample
2353
+ Down.download(selected_url, destination: "./image/memory.png")
2354
+ puts "이미지 다운로드 완료: #{selected_url}"
2355
+
2356
+ rescue => e
2357
+ retry_count += 1
2358
+ puts "auto_image 에러: #{e.message} (재시도 #{retry_count}/#{max_retries})"
2359
+ sleep(3)
2360
+ if retry_count < max_retries
2318
2361
  retry
2362
+ else
2363
+ puts "최대 재시도 초과. 조건 무시하고 랜덤 이미지 다운로드 시도..."
2364
+
2365
+ if results && !results.empty?
2366
+ random_photo = results.sample
2367
+ fallback_url = random_photo.dig('urls', 'full')
2368
+ if fallback_url
2369
+ Down.download(fallback_url, destination: "./image/memory.png")
2370
+ puts "랜덤 이미지 다운로드 완료: #{fallback_url}"
2371
+ else
2372
+ puts "랜덤 이미지 URL을 찾을 수 없습니다. 단색 배경 이미지 생성합니다."
2373
+ color_image
2374
+ end
2375
+ else
2376
+ puts "이미지 결과가 없어 다운로드할 수 없습니다. 단색 배경 이미지 생성합니다."
2377
+ color_image
2378
+ end
2379
+ end
2319
2380
  end
2320
2381
  end
2321
2382
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cafe_buy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.36
4
+ version: 0.1.37
5
5
  platform: ruby
6
6
  authors:
7
7
  - zon