Skip to content

Instantly share code, notes, and snippets.

@hyuki0000
Last active May 1, 2017 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyuki0000/adf45efb4a5269db5acd0fdf7e10dff7 to your computer and use it in GitHub Desktop.
Save hyuki0000/adf45efb4a5269db5acd0fdf7e10dff7 to your computer and use it in GitHub Desktop.
tooter - テキストファイルをまとめて複数マストドンインスタンスに投稿するRubyスクリプト

tooter - テキストファイルをまとめて複数マストドンインスタンスに投稿するRubyスクリプト

ディレクトリ構成(例)

├── Gemfile
├── Gemfile.lock
├── README
├── instances
│   ├── mstdn.jp         (ここにmstdn.jpへの投稿テキストを入れておく)
│   │   └── config.yaml
│   └── pawoo.net        (ここにpawoo.netへの投稿テキストを入れておく)
│       └── config.yaml
├── token-maker.rb
└── tooter.rb

投稿準備

  • rm .env
  • ruby token-maker.rb
  • 出力されたTOKENを instances/インスタンス名/config.yaml に書く。

投稿手順

  • テキストファイルを instances/インスタンス名/ファイル名.txt に置く。
  • ruby tooter.rb
  • 先ほどのテキストファイルが投稿され、ファイル名.txt_ファイル名.txt に変わる。

参考

source "https://rubygems.org"
gem "mastodon-api"
gem "highline"
gem "oauth2"
gem "dotenv"
GEM
remote: https://rubygems.org/
specs:
addressable (2.5.1)
public_suffix (~> 2.0, >= 2.0.2)
domain_name (0.5.20170404)
unf (>= 0.0.5, < 1.0.0)
dotenv (2.2.0)
faraday (0.11.0)
multipart-post (>= 1.2, < 3)
highline (1.7.8)
http (2.2.1)
addressable (~> 2.3)
http-cookie (~> 1.0)
http-form_data (~> 1.0.1)
http_parser.rb (~> 0.6.0)
http-cookie (1.0.3)
domain_name (~> 0.5)
http-form_data (1.0.1)
http_parser.rb (0.6.0)
jwt (1.5.6)
mastodon-api (1.1.0)
addressable (~> 2.4)
http (~> 2.0)
multi_json (1.12.1)
multi_xml (0.6.0)
multipart-post (2.0.0)
oauth2 (1.3.1)
faraday (>= 0.8, < 0.12)
jwt (~> 1.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (>= 1.2, < 3)
public_suffix (2.0.5)
rack (2.0.1)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.3)
PLATFORMS
ruby
DEPENDENCIES
dotenv
highline
mastodon-api
oauth2
BUNDLED WITH
1.14.6
MASTODON_URL: 'https://mstdn.jp'
MASTODON_SCOPES: 'read write follow'
MASTODON_CLIENT_ID: 'change me xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
MASTODON_CLIENT_SECRET: 'change me yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
MASTODON_ACCESS_TOKEN: 'change me zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'
MASTODON_URL: 'https://pawoo.net'
MASTODON_SCOPES: 'read write follow'
MASTODON_CLIENT_ID: 'change me aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
MASTODON_CLIENT_SECRET: 'change me bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
MASTODON_ACCESS_TOKEN: 'change me cccccccccccccccccccccccccccccc'
#!/usr/bin/env ruby
# Based on:
# Mastodon API gemを使って投稿する - Qiita
# http://qiita.com/takahashim/items/a8c0eb3a75d366cfe87b
require 'bundler/setup'
Bundler.require(:default)
require 'mastodon'
require 'highline/import'
require 'oauth2'
require 'dotenv'
require 'pp'
DEFAULT_APP_NAME = "tooter"
DEFAULT_MASTODON_URL = 'https://mstdn.jp'
FULL_ACCESS_SCOPES = "read write follow"
Dotenv.load
if !ENV["MASTODON_URL"]
ENV["MASTODON_URL"] = ask("Instance URL: "){|q| q.default = DEFAULT_MASTODON_URL}
File.open(".env", "a+") do |f|
f.write "MASTODON_URL = '#{ENV["MASTODON_URL"]}'\n"
end
end
puts ENV["MASTODON_URL"]
scopes = ENV["MASTODON_SCOPES"] || FULL_ACCESS_SCOPES
app_name = ENV["MASTODON_APP_NAME"] || DEFAULT_APP_NAME
if !ENV["MASTODON_CLIENT_ID"] || !ENV["MASTODON_CLIENT_SECRET"]
client = Mastodon::REST::Client.new(base_url: ENV["MASTODON_URL"])
app = client.create_app(app_name, "urn:ietf:wg:oauth:2.0:oob", scopes)
ENV["MASTODON_CLIENT_ID"] = app.client_id
ENV["MASTODON_CLIENT_SECRET"] = app.client_secret
File.open(".env", "a+") do |f|
f.write "MASTODON_CLIENT_ID = '#{ENV["MASTODON_CLIENT_ID"]}'\n"
f.write "MASTODON_CLIENT_SECRET = '#{ENV["MASTODON_CLIENT_SECRET"]}'\n"
end
end
if !ENV["MASTODON_ACCESS_TOKEN"]
client = OAuth2::Client.new(ENV["MASTODON_CLIENT_ID"],
ENV["MASTODON_CLIENT_SECRET"],
site: ENV["MASTODON_URL"])
login_id = ask("Your Account: ")
password = ask("Your Password: "){|q| q.echo = "*"}
token = client.password.get_token(login_id, password, scope: scopes)
ENV["MASTODON_ACCESS_TOKEN"] = token.token
File.open(".env", "a+") do |f|
f.write "MASTODON_ACCESS_TOKEN = '#{ENV["MASTODON_ACCESS_TOKEN"]}'\n"
end
end
## 投稿する
# client = Mastodon::REST::Client.new(base_url: ENV["MASTODON_URL"], bearer_token: ENV["MASTODON_ACCESS_TOKEN"])
# message = ARGV[0] || ask("Your Message: ")
# response = client.create_status(message)
## とりあえず結果を出力してみる
# pp response
puts "tooter.rb が読む config.yaml 向け出力"
puts
puts "APP_NAME: '#{app_name}'"
puts "MASTODON_URL: '#{ENV['MASTODON_URL']}'"
puts "MASTODON_SCOPES: '#{scopes}'"
puts "MASTODON_CLIENT_ID: '#{ENV['MASTODON_CLIENT_ID']}'"
puts "MASTODON_CLIENT_SECRET: '#{ENV['MASTODON_CLIENT_SECRET']}'"
puts "MASTODON_ACCESS_TOKEN: '#{ENV['MASTODON_ACCESS_TOKEN']}'"
#!/usr/bin/env ruby
# encoding: utf-8
require 'bundler/setup'
Bundler.require(:default)
require 'mastodon'
require 'highline/import'
require 'oauth2'
require 'pp'
Dir.glob("instances/*").each do |dir|
Dir.chdir(dir)
config = YAML.load_file("config.yaml")
client = Mastodon::REST::Client.new(base_url: config['MASTODON_URL'], bearer_token: config['MASTODON_ACCESS_TOKEN'])
Dir.glob("*.txt").each do |txt|
next if txt.match(/^_/) # 過去ログは無視
next if File.file?("_" + txt) # すでにトートずみ
message = File.read(txt)
response = client.create_status(message)
system("mv #{txt} _#{txt}")
end
Dir.chdir('../..')
end
@hyuki0000
Copy link
Author

2017-05-01_tooter

@hyuki0000
Copy link
Author

2017-05-01_tooter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment