You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.6 KiB

require 'twitter'
require 'twitter-text'
require 'sanitize'
require 'cgi'
class CrossPost
class Twitter
def initialize(config)
config = {
consumer_key: config['twitter.consumer.key'],
consumer_secret: config['twitter.consumer.secret'],
access_token: config['twitter.access.token'],
access_token_secret: config['twitter.access.secret']
}
@client = ::Twitter::REST::Client.new config
@stream = ::Twitter::Streaming::Client.new config
end
def post(content, media = [])
media = media.collect { |f| @client.upload f }
parts = split content
last = nil
unless media.empty?
first, *parts = parts
last = @client.update first, media_ids: media.join(',')
end
parts.each { |p| last = @client.update p, in_reply_to_status: last }
end
def post_status(status)
content = Sanitize.clean(status.content).strip
content = CGI.unescape_html content
media = status.media_attachments.collect { |f| open f.url }
LOGGER.info { 'Sending to twitter' }
LOGGER.debug { " Content: #{content}" }
LOGGER.debug { " Attachments: #{media.size}" }
self.post content, media
media.each do |f|
f.close
f.unlink
end
end
private
def split(text)
parts = []
part = ''
words = text.split /\ /
words.each do |word|
old_part = part
part += ' ' unless part == ''
part += word
invalid = ::Twitter::Validation.tweet_invalid? part
if invalid
parts << old_part
part = word
end
end
parts << part unless part == ''
parts
end
end
end