cafe_basics_duo 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_basics_duo.rb +72 -11
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ed29b474605d5e69da6839887f2098f9440775ec05d0702341b143138a0c654b
4
- data.tar.gz: bdb4ed6fee966c5f02f4089f2ad2f60858bcd3e2cc4d778848130cc642fd355c
3
+ metadata.gz: 0ea656b168468dcf680e00ba06a615aca79d3e3748eb33cfa1bee9f90923c29d
4
+ data.tar.gz: 6f892b0c8c816bb68af4ffd3aac73f04327c8934d90af79cd77cda7e67718b44
5
5
  SHA512:
6
- metadata.gz: a191bbefbfbfc7a7f1a46a6de3578da551839430a635d0add360e8e6572c076f1ace730a3fa45ddbe70e62f31b1adf1dc52ec369b0f56682afc260639231b4bf
7
- data.tar.gz: 65dc2ff26d0ffc33ace43524d2c729a554aae460a5e11241865cdb089142cad4a277905226905993b143b84f55aefbd7e53935948ffb4b6f295f733eed3848c9
6
+ metadata.gz: b30ffce6f681d19d52e248866ac695f991f7e39d130dfb329c9d88cc731f23b879a9d99929a8c53675abce0f14f1f9000efacd6a35cb046973dad4030055ae7b
7
+ data.tar.gz: 27490fd24522ec64ffb0a47c8cbab371c172ed272764e5ac959e1b743c49574b72ddcc5418e29e3789cc6623d9be6d36d4232637a2e6aa85104fa6cce7f6d879
@@ -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
@@ -2031,21 +2032,81 @@ class Wordpress
2031
2032
 
2032
2033
 
2033
2034
 
2034
- def auto_image
2035
+ def auto_image(keyword = nil)
2036
+ keyword ||= @keyword
2037
+ puts "키워드: #{keyword}"
2038
+
2039
+ client = HTTPClient.new
2040
+ client.default_header = {
2041
+ 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '\
2042
+ '(KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36',
2043
+ 'Accept' => 'application/json, text/javascript, */*; q=0.01',
2044
+ 'Accept-Language' => 'en-US,en;q=0.9',
2045
+ 'Referer' => "https://unsplash.com/s/photos/#{URI.encode_www_form_component(keyword)}",
2046
+ 'X-Requested-With' => 'XMLHttpRequest'
2047
+ }
2048
+
2049
+ retry_count = 0
2050
+ max_retries = 10
2051
+ results = []
2052
+
2035
2053
  begin
2036
2054
  page = rand(1..15)
2037
- http = HTTP.get('https://unsplash.com/napi/photos?per_page=12&page='+page.to_s)
2038
- json = JSON.parse(http.to_s)
2039
- mm = Array.new
2040
- json.each do |i|
2041
- mm << i['urls']['full']
2055
+ url = "https://unsplash.com/napi/search/photos?query=#{URI.encode_www_form_component(keyword)}&page=#{page}&per_page=20"
2056
+ puts "Request URL: #{url}"
2057
+ res = client.get(url)
2058
+
2059
+ unless res.status == 200
2060
+ puts "HTTP Error: #{res.status}"
2061
+ raise "HTTP Error"
2042
2062
  end
2043
- url = mm.sample
2044
- Down.download(url, destination: "./image/memory.png")
2045
- rescue
2046
- puts 'auto_image 일시적 error 5초후 제시도...'
2047
- sleep(5)
2063
+
2064
+ json = JSON.parse(res.body)
2065
+ results = json['results']
2066
+ mm = []
2067
+
2068
+ results.each do |photo|
2069
+ full_url = photo.dig('urls', 'full').to_s
2070
+ regular_url = photo.dig('urls', 'regular').to_s
2071
+
2072
+ if full_url.start_with?("https://images.unsplash.com/photo-") &&
2073
+ regular_url.include?("1080")
2074
+ mm << full_url
2075
+ end
2076
+ end
2077
+
2078
+ if mm.empty?
2079
+ raise "No matching image"
2080
+ end
2081
+
2082
+ selected_url = mm.sample
2083
+ Down.download(selected_url, destination: "./image/memory.png")
2084
+ puts "이미지 다운로드 완료: #{selected_url}"
2085
+
2086
+ rescue => e
2087
+ retry_count += 1
2088
+ puts "auto_image 에러: #{e.message} (재시도 #{retry_count}/#{max_retries})"
2089
+ sleep(3)
2090
+ if retry_count < max_retries
2048
2091
  retry
2092
+ else
2093
+ puts "최대 재시도 초과. 조건 무시하고 랜덤 이미지 다운로드 시도..."
2094
+
2095
+ if results && !results.empty?
2096
+ random_photo = results.sample
2097
+ fallback_url = random_photo.dig('urls', 'full')
2098
+ if fallback_url
2099
+ Down.download(fallback_url, destination: "./image/memory.png")
2100
+ puts "랜덤 이미지 다운로드 완료: #{fallback_url}"
2101
+ else
2102
+ puts "랜덤 이미지 URL을 찾을 수 없습니다. 단색 배경 이미지 생성합니다."
2103
+ color_image
2104
+ end
2105
+ else
2106
+ puts "이미지 결과가 없어 다운로드할 수 없습니다. 단색 배경 이미지 생성합니다."
2107
+ color_image
2108
+ end
2109
+ end
2049
2110
  end
2050
2111
  end
2051
2112
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cafe_basics_duo
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