commit ccf6ddeab599e56ec7e8690197c546714f1aefbf Author: Hexaitos Date: Sun Nov 10 20:50:31 2024 +0100 first commit (very terrible don't look) diff --git a/main.rb b/main.rb new file mode 100644 index 0000000..44c3344 --- /dev/null +++ b/main.rb @@ -0,0 +1,70 @@ +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}" + req.body = { file: file } +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 \ No newline at end of file