Browse Source

Convert Mastodon usernames to Twitter ones

tags/v0.2.0
aeris 1 year ago
parent
commit
aaf143e5b5
5 changed files with 103 additions and 36 deletions
  1. 2
    0
      Gemfile
  2. 1
    0
      cross-post.gemspec
  3. 74
    35
      lib/cross-post/config.rb
  4. 7
    1
      lib/cross-post/twitter.rb
  5. 19
    0
      spec/config_spec.rb

+ 2
- 0
Gemfile View File

@@ -2,5 +2,7 @@ source 'https://rubygems.org'

gem 'mastodon-api', '~> 1.1.0', require: 'mastodon', git: 'https://github.com/tootsuite/mastodon-api.git'
gem 'awesome_print'
gem 'dotenv'
gem 'pry'

gemspec

+ 1
- 0
cross-post.gemspec View File

@@ -17,6 +17,7 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep %r{^(test|spec|features)/}

spec.add_development_dependency 'bundler', '~> 1.15', '>= 1.15.4'
spec.add_development_dependency 'rspec', '~> 3.6.0', '>= 3.6.0'

spec.add_dependency 'twitter', '~> 6.1', '>= 6.1.0'
spec.add_dependency 'mastodon-api', '~> 1.1', '>= 1.1.0'

+ 74
- 35
lib/cross-post/config.rb View File

@@ -1,4 +1,5 @@
require 'yaml'
require 'fileutils'

class CrossPost
class Config
@@ -6,22 +7,26 @@ class CrossPost
DEFAULT_CONFIG_FILE = 'config.yml'

class SubConfig
def initialize(file)
@file = file
@config = if File.readable? @file
File.open(@file) { |f| YAML.safe_load f }
else
{}
end
def initialize(config = {})
@config = config
end

def each(&block)
@config.each &block
end

def [](key)
current = @config
key.split(/\./).each do |k|
current = current[k]
return nil if current.nil?
case key
when String
current = @config
key.split(/\./).each do |k|
current = current[k]
return nil if current.nil?
end
current
else
@config[key]
end
current
end

def fetch(key, default = nil)
@@ -29,20 +34,51 @@ class CrossPost
end

def []=(key, value)
*key, last = key.split(/\./)
current = @config
key.each do |k|
next_ = current[k]
case next_
when nil
next_ = current[k] = {}
when Hash
else
raise "Invalid entry, Hash expected, had #{next_.class} (#{next_})"
case key
when String
*key, last = key.to_s.split(/\./)
current = @config
key.each do |k|
next_ = current[k]
case next_
when nil
next_ = current[k] = {}
when Hash
else
raise "Invalid entry, Hash expected, had #{next_.class} (#{next_})"
end
current = next_
end
current = next_
current[last] = value
else
@config[key] = value
end
current[last] = value
end
end

class FifoSubConfig < SubConfig
def initialize(size = 100)
@size = size
@keys = []
super({})
end

def []=(key, value)
@keys.delete key
value = super key, value
@keys << key
while @keys.size > @size
key = @keys.delete_at 0
@config.delete key
end
value
end
end

class FileSubConfig < SubConfig
def initialize(file)
@file = file
super YAML.load_file @file
end

def put(key, value, save: false)
@@ -51,31 +87,34 @@ class CrossPost
end

def save
LOGGER.debug "Saving #{@file}"
yaml = YAML.dump @config
File.write @file, yaml
LOGGER.debug "Saving #{@file}"
yaml = YAML.dump @config
File.write @file, yaml
end
end

def initialize
@config = {}
@dir = ENV.fetch 'CONFIG_FOLDER', DEFAULT_CONFIG_FOLDER
file = ENV.fetch 'CONFIG_FILE', DEFAULT_CONFIG_FILE
@configs = {}
@dir = ENV.fetch 'CONFIG_FOLDER', DEFAULT_CONFIG_FOLDER
file = ENV.fetch 'CONFIG_FILE', DEFAULT_CONFIG_FILE
self.load :settings, file
self.load :posts
self.load :users
self[:posts] = FifoSubConfig.new
end

def [](name)
settings = @config[name]
return settings if settings
self.load name
@configs[name]
end

def []=(name, value)
@configs[name] = value
end

def load(name, file = nil)
file ||= "#{name}.yml"
file = File.join @dir, file
@config[name] = SubConfig.new file
FileUtils.touch file unless File.exist? file
self[name] = FileSubConfig.new file
end
end
end

+ 7
- 1
lib/cross-post/twitter.rb View File

@@ -11,6 +11,7 @@ class CrossPost
def initialize(config)
settings = config[:settings]
@posts = config[:posts]
@users = config[:users]

config = {
consumer_key: settings['twitter.consumer.key'],
@@ -34,7 +35,7 @@ class CrossPost
parts.each { |p| reply_to = @client.update p, in_reply_to_status: reply_to }

reply_to = reply_to.id if reply_to.respond_to? :id
@posts.put id, reply_to, save: true
@posts[id] = reply_to
end

WHITESPACE_TAGS = {
@@ -47,6 +48,11 @@ class CrossPost
content = status.content
content = Sanitize.clean(content, whitespace_elements: WHITESPACE_TAGS).strip
content = CGI.unescape_html content

@users.each do |mastodon, twitter|
content = content.gsub /@\b#{mastodon}\b/, "@#{twitter}"
end

media = status.media_attachments.collect { |f| open f.url }

LOGGER.info { 'Sending to twitter' }

+ 19
- 0
spec/config_spec.rb View File

@@ -0,0 +1,19 @@
require 'cross-post'

RSpec.describe CrossPost::Config::FifoSubConfig do
it 'must remove first value in case of overflow' do
config = CrossPost::Config::FifoSubConfig.new 2
config[:foo] = :foo
config[:bar] = :bar

expect(config[:foo]).to be :foo
expect(config[:bar]).to be :bar
expect(config[:baz]).to be_nil

config[:baz] = :baz

expect(config[:foo]).to be_nil
expect(config[:bar]).to be :bar
expect(config[:baz]).to be :baz
end
end

Loading…
Cancel
Save