2024-11-10 19:50:31 +00:00
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 \n Attribution and license: #{ user } ( #{ license } ) \n Source: #{ 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 } " }
2024-11-10 19:50:31 +00:00
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 \n Content-Disposition: form-data; name= \" status \" \r \n \r \n #{ status } \r \n -----011000010111000001101001 \r \n Content-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