Skip to content

Instantly share code, notes, and snippets.

@kyontan
Last active April 16, 2017 13:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyontan/1a73456086ffd6aec6eba93569ec24c9 to your computer and use it in GitHub Desktop.
Save kyontan/1a73456086ffd6aec6eba93569ec24c9 to your computer and use it in GitHub Desktop.
マストドンで延々とリモートフォローをしつづけるやつです。
#!/usr/bin/env ruby
# how to use:
# 1. domains ファイルをこのスクリプトを同じディレクトリに配置し、リモートフォローしたいインスタンスのドメインを記載してください
# (place `domains` file located same directory as the script file, and write the domain name of instance which you want to remote follow.)
# 2. run this script
# 3. 発見されたアカウントがあなたのインスタンスに登録されます, ただ、リモートアカウントとして登録されるだけで、実際にフォローなどは行われません
# (new accounts might have registered to your insntance, but not yet `followed` by your account (only registered to instance as a remote account))
# 4. なんらかのお好きな方法でフォローしてください
# (please follow them by your account yourself, your ways)
# ex: `curl -sS -w '%{http_code}' -H 'WRITE_YOUR_COOKIE' -H "authorization:Bearer xxx" -X POST https://mastdn.example.yokohama/api/v1/accounts/ID/follow`
require "date"
require "json"
require "uri"
curl_cmd = "curl -sS -b cookie -c cookie "
domains = File.read("domains").split(?\n).reject{|item| item.strip.length == 0 || item.start_with? ?# }
toots_by_instance = domains.map do |domain|
puts "Fetching toots from: #{domain}"
toots = `#{curl_cmd} https://#{domain}/api/v1/timelines/public`
begin
next [domain, JSON.parse(toots)]
rescue
next nil
end
end.compact
accounts = toots_by_instance.map do |domain, toots|
toots.map do |toot|
begin
un, dm = toot["account"]["acct"].split(?@)
rescue
next nil
end
dm = domain if dm.nil?
[dm, un]
end.compact
end.flatten(1).uniq
if not (accounts.map(&:first).uniq - domains).empty?
new_domains = accounts.map(&:first).uniq - domains
puts "New domains: "
`echo "# New domains at #{DateTime.now}" >> domains`
puts new_domains
new_domains.each do |domain|
`#{curl_cmd} https://#{domain}`
`echo #{domain} >> domains`
end
domains += new_domains
end
remote_follow_account = "hoge@example.mastodon"
form_value = [["remote_follow[acct]", remote_follow_account]]
accounts.each do |a|
url = "https://#{a[0]}/users/#{a[1]}/remote_follow"
puts "Following #{a.reverse.join(?@)}..."
token = `#{curl_cmd} #{url}`.scan(/authenticity_token" value="(.*)?" \/>/).flatten[0]
# puts "Got token: #{token}"
fv = form_value + [["authenticity_token", token]]
cmd = "#{curl_cmd} -L -w '%{http_code}\\n' #{url} -o /dev/null --data '#{URI.encode_www_form(fv)}'"
result = `#{cmd}`
puts "Status: #{result}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment