#!/usr/bin/env ruby require 'json' require 'rss' require 'open-uri' require 'cgi' require_relative './../../xrforge.rb' # Check if a filename is provided if ARGV.length != 1 puts "Usage: #{$0} " exit 1 end filename = ARGV[0] # dont run for each file-update if ! filename.end_with?("datapackage.json") exit 0 end begin # Change the directory Dir.chdir( File.dirname(filename) ) # Read and parse the JSON file data = JSON.parse( File.read( "datapackage.json" ) ) data['keywords'].any? do |tag| if tag.match?(/@.*@.*\./) # scan for activitypub handle (@foo@mastodon.online e.g.) APHandle = tag end end if ! APHandle # nothing to do exit 0 end APHandle = "@vladh@merveilles.town" logfile = File.join( File.dirname(filename), ".xrforge/log.txt" ) XRForge.log("✅ starting Mastodon post2image", logfile) parts = APHandle.split("@") server = parts[2].sub(/@/,"") rssUrl = "https://#{server}/@#{parts[1]}.rss" XRForge.log("✅ checking #{rssUrl}", logfile) feed = RSS::Parser.parse(URI.open(rssUrl, 'User-Agent' => 'Ruby-RSS-Client')) puts feed.channel.title puts feed.channel.link first_item = feed.items.first if first_item description = CGI.unescapeHTML(first_item.description.to_s).gsub(/<[^>]*>/, '') description = description.length > 150 ? description = description[0,200] + " (..)" : description else XRForge.log("❌ did not find post", logfile) exit 0 end puts description # look for first image MEDIA_REGEX = / e # Handle HTTP errors (e.g., 404 not found, 403 forbidden) puts "Error fetching feed: #{e.message}" exit rescue => e # Handle other parsing or connection errors puts "An error occurred: #{e.message}" rescue Errno::ENOENT puts "File #{filename} not found" rescue JSON::ParserError puts "Error parsing JSON from #{filename}" rescue => e puts "An error occurred: #{e.message}" end