bot_of_prey/main.rb

70 lines
2.0 KiB
Ruby
Raw Normal View History

require 'net/http'
require 'json'
require 'uri'
require 'faraday'
require 'faraday/multipart'
require 'down'
require 'marcel'
require 'sanitize'
auth_token = ""
conn = Faraday.new(url: 'https://birds.bateleur.org/api/random') do |faraday|
faraday.adapter Faraday.default_adapter # Use the default adapter (Net::HTTP)
end
response = conn.get
data = JSON.parse(response.body)
user = data['user']
url = data['url']
thumb = data['thumb']
license = data['license']
desc = data['desc']
license_url = data['license_url']
user_url = data['user_url']
desc_sanitised = Sanitize.clean(desc)
puts status = "#{desc_sanitised}\n\nAttribution and license: #{user} (#{license})\nSource: #{url}"
image = Down.download(url)
image_mime = ""
File.open image.path do |file|
image_mine = Marcel::MimeType.for file
end
conn = Faraday.new(
url: 'https://birds.town',
headers: {'Content-Type' => 'multipart/form-data'}
) do |faraday|
faraday.request :multipart
faraday.adapter Faraday.default_adapter
end
file = Faraday::Multipart::FilePart.new(
image.path, image_mime
)
response = conn.post('/api/v2/media') do |req|
req.headers['Authorization'] = "Bearer #{auth_token}"
2024-11-11 12:39:02 +00:00
req.body = { file: file, description: "A photo of a bird of prey taken from Wikimedia Commons. The photo was described as follows by the original uploader or creator: #{desc_sanitised}" }
end
puts response.status
parsed_response = JSON.parse(response.body)
puts id = parsed_response["id"]
url = URI("https://birds.town/api/v1/statuses")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = "Bearer #{auth_token}"
request["content-type"] = 'multipart/form-data; boundary=---011000010111000001101001'
request.body = "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"status\"\r\n\r\n#{status}\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"media_ids[]\"\r\n\r\n#{id}\r\n-----011000010111000001101001--\r\n\r\n"
response = http.request(request)
puts response.read_body