56 lines
1.8 KiB
Ruby
56 lines
1.8 KiB
Ruby
=begin
|
|
Copyright (C) 2024 Hexaitos
|
|
This file is part of "Predator Pics"
|
|
|
|
Predator Pics is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
Predator Pics is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License along with Foobar. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
Contact: me@bateleur.org, me@hexaitos.com.
|
|
=end
|
|
|
|
def get_images_by_category(categories)
|
|
random_category = categories.sample
|
|
|
|
if random_category.include?("https://commons.wikimedia.org/wiki/Category:") then
|
|
random_category = random_category.gsub("https://commons.wikimedia.org/wiki/Category:", "")
|
|
elsif random_category.include?("Category:") then
|
|
random_category = random_category.gsub("Category:", "")
|
|
end
|
|
|
|
puts random_category
|
|
|
|
photos = []
|
|
|
|
conn = Faraday.new(url: 'https://commons.wikimedia.org/w/api.php') do |faraday|
|
|
faraday.adapter Faraday.default_adapter
|
|
end
|
|
|
|
params = {
|
|
action: 'query',
|
|
list: 'categorymembers',
|
|
cmtitle: "Category:#{random_category}",
|
|
cmlimit: 500,
|
|
cmtype: 'file',
|
|
format: 'json'
|
|
}
|
|
|
|
response = conn.get do |req|
|
|
req.params = params
|
|
end
|
|
|
|
data = JSON.parse(response.body)
|
|
|
|
begin
|
|
category_members = data['query']['categorymembers']
|
|
category_members.each do |member|
|
|
photos << member['title']
|
|
end
|
|
rescue
|
|
puts "No data found or error in API request"
|
|
end
|
|
|
|
return photos
|
|
end |