first commit
This commit is contained in:
commit
dac566f2c8
|
@ -0,0 +1 @@
|
|||
3.2.2
|
|
@ -0,0 +1,11 @@
|
|||
https://commons.wikimedia.org/wiki/Category:Quality_images_of_Falco_cenchroides
|
||||
https://commons.wikimedia.org/wiki/Category:Quality_images_of_Falco_amurensis
|
||||
https://commons.wikimedia.org/wiki/Category:Quality_images_of_captive_Falco_berigora
|
||||
https://commons.wikimedia.org/wiki/Category:Quality_images_of_captive_Falco_biarmicus
|
||||
https://commons.wikimedia.org/wiki/Category:Quality_images_of_Falco_cherrug
|
||||
https://commons.wikimedia.org/wiki/Category:Quality_images_of_Falco_columbarius
|
||||
https://commons.wikimedia.org/wiki/Category:Quality_images_of_Falco_naumanni
|
||||
https://commons.wikimedia.org/wiki/Category:Quality_images_of_Falco_rusticolus
|
||||
https://commons.wikimedia.org/wiki/Category:Quality_images_of_Falco_sparverius
|
||||
https://commons.wikimedia.org/wiki/Category:Quality_images_of_Falco_tinnunculus
|
||||
https://commons.wikimedia.org/wiki/Category:Quality_images_of_Falco_vespertinus
|
|
@ -0,0 +1,37 @@
|
|||
def get_images_by_category(categories)
|
||||
random_category = categories.sample.gsub("https://commons.wikimedia.org/wiki/Category:", "")
|
||||
photos = []
|
||||
|
||||
conn = Faraday.new(url: 'https://commons.wikimedia.org/w/api.php') do |faraday|
|
||||
faraday.adapter Faraday.default_adapter # Use the default adapter (Net::HTTP)
|
||||
end
|
||||
|
||||
# Define the parameters for the request
|
||||
params = {
|
||||
action: 'query',
|
||||
list: 'categorymembers',
|
||||
cmtitle: "Category:#{random_category}", # Specify the category title
|
||||
cmlimit: 100,
|
||||
format: 'json' # Request response in JSON format
|
||||
}
|
||||
|
||||
# Send the GET request
|
||||
response = conn.get do |req|
|
||||
req.params = params
|
||||
end
|
||||
|
||||
# Parse the response as JSON
|
||||
data = JSON.parse(response.body)
|
||||
|
||||
# Handle and print the response
|
||||
if data['query'] && data['query']['categorymembers']
|
||||
category_members = data['query']['categorymembers']
|
||||
category_members.each do |member|
|
||||
photos << member['title']
|
||||
end
|
||||
else
|
||||
puts "No data found or error in API request"
|
||||
end
|
||||
|
||||
return photos
|
||||
end
|
|
@ -0,0 +1,28 @@
|
|||
def get_random_image(photos)
|
||||
random_photo = photos.sample
|
||||
|
||||
conn = Faraday.new(url: 'https://commons.wikimedia.org/w/api.php') do |faraday|
|
||||
faraday.adapter Faraday.default_adapter # Use the default adapter (Net::HTTP)
|
||||
end
|
||||
|
||||
params = {
|
||||
action: 'query',
|
||||
prop: 'imageinfo',
|
||||
titles: "#{random_photo}", # Specify the category title
|
||||
iiprop: 'url|user|extmetadata',
|
||||
format: 'json' # Request response in JSON format
|
||||
}
|
||||
|
||||
response = conn.get do |req|
|
||||
req.params = params
|
||||
end
|
||||
|
||||
data = JSON.parse(response.body)
|
||||
info = data['query']['pages'].values.first
|
||||
user = info['imageinfo'].first['user']
|
||||
url = info['imageinfo'].first['url']
|
||||
license = info['imageinfo'].first['extmetadata']['LicenseShortName']['value']
|
||||
desc = info['imageinfo'].first['extmetadata']['ImageDescription']['value']
|
||||
|
||||
return {:user => user, :url => url, :license => license, :desc => desc}
|
||||
end
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,18 @@
|
|||
require 'json'
|
||||
require 'faraday'
|
||||
require 'sinatra'
|
||||
|
||||
require_relative 'get_images_by_category.rb'
|
||||
require_relative 'get_random_image.rb'
|
||||
|
||||
categories = File.readlines("categories.txt")
|
||||
|
||||
get '/' do
|
||||
image = get_random_image(get_images_by_category(categories))
|
||||
|
||||
erb :index, locals: { image: image }
|
||||
end
|
||||
|
||||
get '/about' do
|
||||
erb :about
|
||||
end
|
|
@ -0,0 +1,2 @@
|
|||
<h1>About this website</h1>
|
||||
<p>This website is still a work in progress. It randomly selects a photo of a bird of prey from Wikimedia and displays it on the homepage.</p>
|
|
@ -0,0 +1,8 @@
|
|||
<h1>Random bird of prey</h1>
|
||||
<a href="<%= image[:url] %>" target="_blank">
|
||||
<img class="refsheet" src="<%= image[:url] %>">
|
||||
</a>
|
||||
<p><strong>Description:</strong> <%= image[:desc] %></p>
|
||||
<p><strong>Photo by:</strong> <%= image[:user] %></p>
|
||||
<p><strong>Licensed under:</strong> <%= image[:license] %></p>
|
||||
<p><strong>URL:</strong> <a href="<%= image[:url] %>" target="_blank"><%= image[:url] %></a></p>
|
|
@ -0,0 +1,28 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<meta name="description" content="{{ page.description }}">
|
||||
|
||||
<title>Random Bird of Prey</title>
|
||||
|
||||
<link rel="preload" as="style" href="/css/styles.css">
|
||||
<link rel="stylesheet" href="/css/styles.css">
|
||||
|
||||
<body>
|
||||
<div class="menu">
|
||||
<a href="/">New Image</a>
|
||||
<a href="/about">About</a>
|
||||
</div>
|
||||
|
||||
<div class="main">
|
||||
<%= yield %>
|
||||
</div>
|
||||
|
||||
<footer>
|
||||
<p>2024 - 2024 hexaitos.com</p>
|
||||
<p>Made with Sinatra</p>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue