Sfoglia il codice sorgente

Handle attachments

tags/v0.1.1
aeris 2 anni fa
parent
commit
b2a01cbc91

+ 0
- 1
README.md Vedi File

@@ -21,5 +21,4 @@ If needed, a SystemD unit example is available [here](https://git.imirhil.fr/aer

# Todo

* Handling multimedia resources
* Publishing on [RubyGems](https://rubygems.org/)

+ 0
- 3
bin/crosspost Vedi File

@@ -1,3 +0,0 @@
#!/usr/bin/env ruby
require 'cross-post'
CrossPost.post ARGV.first

+ 1
- 1
bin/twitter-register Vedi File

@@ -18,7 +18,7 @@ url = request_token.authorize_url
puts url
Launchy.open url

print 'PIN ? '
print 'PIN ? '
pin = gets.chomp

access_token = request_token.get_access_token oauth_verifier: pin

+ 6
- 8
lib/cross-post.rb Vedi File

@@ -1,6 +1,12 @@
require 'cross-post/config'
require 'cross-post/mastodon'
require 'cross-post/twitter'
require 'open-uri'

# Force OpenURI#open to return a TempFile and not a StringIO
OpenURI::Buffer.send :remove_const, 'StringMax'
OpenURI::Buffer.const_set 'StringMax', 0


class CrossPost
def initialize
@@ -9,18 +15,10 @@ class CrossPost
@twitter = Twitter.new @config
end

def post(text)
@mastodon.post text
end

def feed
@mastodon.feed @twitter
end

def self.post(text)
self.new.post text
end

def self.feed
self.new.feed
end

+ 3
- 3
lib/cross-post/config.rb Vedi File

@@ -3,8 +3,8 @@ require 'yaml'
class CrossPost
class Config
def initialize
@file = ENV.fetch 'CROSS_POST_CONFIG', File.join(Dir.home, '.cross-post.yml')
@config = YAML.load_file @file
@file = ENV.fetch 'CROSS_POST_CONFIG', File.join(Dir.home, '.cross-post.yml')
File.open(@file) { |f| @config = YAML.safe_load f }
end

def [](key)
@@ -18,7 +18,7 @@ class CrossPost

def []=(key, value)
*key, last = key.split(/\./)
current = @config
current = @config
key.each do |k|
next_ = current[k]
case next_

+ 16
- 17
lib/cross-post/mastodon.rb Vedi File

@@ -1,21 +1,15 @@
require 'mastodon'
require 'sanitize'
require 'awesome_print'

class CrossPost
class Mastodon
def initialize(config)
url = config['mastodon.url']
token = config['mastodon.token']
user = config['mastodon.user']
url = config['mastodon.url']
token = config['mastodon.token']
user = config['mastodon.user']
@user_url = URI.join(url, "/@#{user}").to_s
@client = ::Mastodon::REST::Client.new base_url: url, bearer_token: token
@stream = ::Mastodon::Streaming::Client.new base_url: url, bearer_token: token
end

def post(text)
post = @client.create_status text
ap post
@client = ::Mastodon::REST::Client.new base_url: url, bearer_token: token
@stream = ::Mastodon::Streaming::Client.new base_url: url, bearer_token: token
end

def feed(twitter)
@@ -23,17 +17,22 @@ class CrossPost
begin
case object
when ::Mastodon::Status
next if object.account.url != @user_url
next if object.visibility != 'public'
next if object.in_reply_to_id

ap object
next if reject? object
twitter.post object
end
rescue => e
$stderr.puts e
#$stderr.puts e
raise
end
end
end

private

def reject?(status)
status.account.url != @user_url or
status.visibility != 'public' or
status.in_reply_to_id
end
end
end

+ 17
- 1
lib/cross-post/twitter.rb Vedi File

@@ -4,7 +4,7 @@ require 'twitter-text'
class CrossPost
class Twitter
def initialize(config)
config = {
config = {
consumer_key: config['twitter.consumer.key'],
consumer_secret: config['twitter.consumer.secret'],
access_token: config['twitter.access.token'],
@@ -18,6 +18,22 @@ class CrossPost
content = Sanitize.clean status.content
last = nil
parts = split content

attachments = status.media_attachments
media = attachments.collect do |f|
f = open f.url
begin
@client.upload f
ensure
f.close
f.unlink
end
end

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


+ 1
- 1
lib/cross-post/version.rb Vedi File

@@ -1,3 +1,3 @@
class CrossPost
VERSION = '0.1.0'.freeze
VERSION = '0.1.1'.freeze
end

Loading…
Annulla
Salva