cafe_basics 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.rb +72 -11
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 23d6859bd69d4f65acca974255b9c01f40d8b4fcd232adae4b8aa5c12f21fe9b
4
- data.tar.gz: 388cd66278320bc004cc1421e184aff26f6925da0853ebbb9fb792623e308c9b
3
+ metadata.gz: 56e8380865c93fed4f0e471b0fb5d7d20424052e54f3f11154598d80268d3b2b
4
+ data.tar.gz: a4cde618f660a0b07aa9e1223d67711d3b9f34704cb6d7f00d9033d5dda2b3f7
5
5
  SHA512:
6
- metadata.gz: b786be3d1c94cf8343fd6a609afad2d457c9705f830d3c65cd629dad62084fa1706814b367ca7478b2ba4832890661041950e93dcc304b462adfc29c148a8a23
7
- data.tar.gz: 1b192a2a4467afb78bfb8446e7637b3dd5b4749236f835a1aec0d19c128ba1af87e4f75e06dd61bd598249e5d381ba5ae00e40298998eba6e2867c810867f100
6
+ metadata.gz: 4d5750348d5784273487f9911c27c833538613ee4c85bc1e75e591af311c703347f557945cba197ef26783f377a4c0e0e89a5241c45b416e45b2dc22d154fcb7
7
+ data.tar.gz: 4a20d7f82b068c0ba7be293347175fb0c057429a32251825619fb77602229900a8ecfa7a7b478356934a9adf1d860e4bac4565c7bae0c3eb2f22162f54b81d6a
data/lib/cafe_basics.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
@@ -1964,21 +1965,81 @@ class Wordpress
1964
1965
 
1965
1966
 
1966
1967
 
1967
- def auto_image
1968
+ def auto_image(keyword = nil)
1969
+ keyword ||= @keyword
1970
+ puts "키워드: #{keyword}"
1971
+
1972
+ client = HTTPClient.new
1973
+ client.default_header = {
1974
+ 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '\
1975
+ '(KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36',
1976
+ 'Accept' => 'application/json, text/javascript, */*; q=0.01',
1977
+ 'Accept-Language' => 'en-US,en;q=0.9',
1978
+ 'Referer' => "https://unsplash.com/s/photos/#{URI.encode_www_form_component(keyword)}",
1979
+ 'X-Requested-With' => 'XMLHttpRequest'
1980
+ }
1981
+
1982
+ retry_count = 0
1983
+ max_retries = 10
1984
+ results = []
1985
+
1968
1986
  begin
1969
1987
  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']
1988
+ url = "https://unsplash.com/napi/search/photos?query=#{URI.encode_www_form_component(keyword)}&page=#{page}&per_page=20"
1989
+ puts "Request URL: #{url}"
1990
+ res = client.get(url)
1991
+
1992
+ unless res.status == 200
1993
+ puts "HTTP Error: #{res.status}"
1994
+ raise "HTTP Error"
1975
1995
  end
1976
- url = mm.sample
1977
- Down.download(url, destination: "./image/memory.png")
1978
- rescue
1979
- puts 'auto_image 일시적 error 5초후 제시도...'
1980
- sleep(5)
1996
+
1997
+ json = JSON.parse(res.body)
1998
+ results = json['results']
1999
+ mm = []
2000
+
2001
+ results.each do |photo|
2002
+ full_url = photo.dig('urls', 'full').to_s
2003
+ regular_url = photo.dig('urls', 'regular').to_s
2004
+
2005
+ if full_url.start_with?("https://images.unsplash.com/photo-") &&
2006
+ regular_url.include?("1080")
2007
+ mm << full_url
2008
+ end
2009
+ end
2010
+
2011
+ if mm.empty?
2012
+ raise "No matching image"
2013
+ end
2014
+
2015
+ selected_url = mm.sample
2016
+ Down.download(selected_url, destination: "./image/memory.png")
2017
+ puts "이미지 다운로드 완료: #{selected_url}"
2018
+
2019
+ rescue => e
2020
+ retry_count += 1
2021
+ puts "auto_image 에러: #{e.message} (재시도 #{retry_count}/#{max_retries})"
2022
+ sleep(3)
2023
+ if retry_count < max_retries
1981
2024
  retry
2025
+ else
2026
+ puts "최대 재시도 초과. 조건 무시하고 랜덤 이미지 다운로드 시도..."
2027
+
2028
+ if results && !results.empty?
2029
+ random_photo = results.sample
2030
+ fallback_url = random_photo.dig('urls', 'full')
2031
+ if fallback_url
2032
+ Down.download(fallback_url, destination: "./image/memory.png")
2033
+ puts "랜덤 이미지 다운로드 완료: #{fallback_url}"
2034
+ else
2035
+ puts "랜덤 이미지 URL을 찾을 수 없습니다. 단색 배경 이미지 생성합니다."
2036
+ color_image
2037
+ end
2038
+ else
2039
+ puts "이미지 결과가 없어 다운로드할 수 없습니다. 단색 배경 이미지 생성합니다."
2040
+ color_image
2041
+ end
2042
+ end
1982
2043
  end
1983
2044
  end
1984
2045
 
metadata CHANGED
@@ -1,7 +1,7 @@
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.37
5
5
  platform: ruby
6
6
  authors:
7
7
  - zon