bot_of_prey/local.rb

116 lines
3.4 KiB
Ruby
Raw Normal View History

#!/usr/bin/ruby
=begin
This file is part of Bot of Prey.
Bot of Prey 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.
Bot of Prey 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/>.
Author: Hexaitos (me@hexaitos.com, me@bateleur.org)
=end
require 'dotenv/load'
require 'fileutils'
require 'open-uri'
require 'time'
require 'net/http'
require 'json'
require 'uri'
require 'faraday'
require 'faraday/multipart'
require 'down'
require 'marcel'
require 'sanitize'
auth_token = ENV['API_KEY']
instance_url = ENV['INSTANCE_URL']
bird_api_url = ENV['BIRD_API_URL']
path_to_data = ENV['PATH_TO_DATA']
current_time = Time.now
year = current_time.year
month = current_time.month.to_s.rjust(2, '0')
day = current_time.day.to_s.rjust(2, '0')
time = current_time.strftime("%H%M")
folder_path = "#{path_to_data}/#{year}/#{month}/#{day}"
FileUtils.mkpath(folder_path)
puts "Folder created at: #{folder_path}"
conn = Faraday.new(url: bird_api_url) do |faraday|
faraday.adapter Faraday.default_adapter
end
response = conn.get
data = JSON.parse(response.body)
puts "Data received from bird API: #{data}"
user, url, thumb, license, desc, license_url, user_url = data.values_at('user', 'url', 'thumb', 'license', 'desc', 'license_url', 'user_url')
uri = URI.parse(url)
filename = File.basename(uri.path)
File.open("#{folder_path}/#{filename}.json", 'w') do |file|
file.write(data.to_json)
end
desc_sanitised = Sanitize.clean(desc)
status = "#{desc}\n\n**Attribution and license**: #{user} (#{license})\n**Source**: #{url}"
puts "Status for post: #{status}"
image = "#{folder_path}/#{filename}"
URI.open(url) do |picture|
File.open(image, 'wb') do |file|
file.write(picture.read)
end
end
image_mime = Marcel::MimeType.for Pathname.new(image)
conn = Faraday.new(
url: "#{instance_url}",
headers: {'Content-Type' => 'multipart/form-data'}
) do |faraday|
faraday.request :multipart
faraday.adapter Faraday.default_adapter
end
file = Faraday::Multipart::FilePart.new(
image, image_mime
)
response = conn.post('/api/v2/media') do |req|
req.headers['Authorization'] = "Bearer #{auth_token}"
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 received after media upload: #{response.status}"
parsed_response = JSON.parse(response.body)
media_id = parsed_response["id"]
conn = Faraday.new(
url: "#{instance_url}",
headers: {'Content-Type' => 'multipart/form-data'}
) do |faraday|
faraday.request :multipart
faraday.adapter Faraday.default_adapter
end
response = conn.post('/api/v1/statuses') do |req|
req.headers['Authorization'] = "Bearer #{auth_token}"
req.body = {
'status' => status,
'visibility' => 'public',
'media_ids[]' => media_id
}
end
puts "Reponse received from server after posting status: #{JSON.parse(response.body)}"