first commit

This commit is contained in:
Hexaitos 2024-11-10 14:12:42 +01:00
commit dac566f2c8
10 changed files with 135 additions and 0 deletions

1
.ruby-version Normal file
View File

@ -0,0 +1 @@
3.2.2

11
categories.txt Normal file
View File

@ -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

37
get_images_by_category.rb Normal file
View File

@ -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

28
get_random_image.rb Normal file
View File

@ -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

1
public/css/styles.css Executable file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

18
server.rb Normal file
View File

@ -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

2
views/about.erb Normal file
View File

@ -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>

8
views/index.erb Normal file
View File

@ -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>

28
views/layout.erb Normal file
View File

@ -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>