Created
May 15, 2020 09:53
-
-
Save highemerly/cb74fc765bdfefe128e0ccf01e4e071c to your computer and use it in GitHub Desktop.
Mastodonのトゥートをweathertypingのワードファイルに変換するやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
require 'json' | |
require 'sanitize' | |
require 'uri' | |
require 'nkf' | |
require 'natto' | |
require 'jumanpp_ruby' | |
MASTODON_SERVER = "handon.club" | |
MASTODON_USER_ID = 1 # highemerly: 1, seibe: 81 | |
DEBUG = true | |
N_PAGE = 30 | |
HASHTAG_SEPARATORS = "_\u00B7\u200c" | |
HASHTAG_NAME_RE = "([[:word:]_][[:word:]#{HASHTAG_SEPARATORS}]*[[:alpha:]#{HASHTAG_SEPARATORS}][[:word:]#{HASHTAG_SEPARATORS}]*[[:word:]_])|([[:word:]_]*[[:alpha:]][[:word:]_]*)" | |
HASHTAG_RE = /(?:^|[^\/\)\w])#(#{HASHTAG_NAME_RE})/i | |
CUSTOMEMOJI_RE = /(?<=[^[:alnum:]:]|\n|^) | |
:([a-zA-Z0-9_]{2,}): | |
(?=[^[:alnum:]:]|$)/x | |
EMOJI_RE = /\p{Emoji}/ | |
KANJI_RE = /[一-龠々]/ | |
last_id = 200_000_000_000_000_000 # 200000000000000000 | |
count = 0 | |
class String | |
def yomi | |
yomi = NKF.nkf('-w -X', self) | |
yomi.tr('ァ-ン','ぁ-ん') | |
.tr('0-9a-zA-Z','0-9a-zA-Z') | |
.tr(':;<>[]{}',':;<>[]{}') | |
.tr('?!$%#&*@¥','?!$%#&*@¥') | |
.gsub(/[[:space:]]/, '') | |
.gsub(/(〜|−|—)/, 'ー') | |
.gsub(/[★☆※○×→←↑↓]/, '') | |
.tr('・', '・') | |
.tr('\\', '') | |
end | |
end | |
class Toot | |
def self.format(toot) | |
str = Sanitize.clean(toot["content"]).strip.chomp | |
str = str.gsub(HASHTAG_RE, '') #hashtag | |
.gsub(CUSTOMEMOJI_RE, '') #custom emoji | |
URI.extract(str).uniq.each do |url| | |
str.gsub!(url, '') | |
end | |
str | |
end | |
def self.accept?(toot) | |
true | |
#toot["mentions"].empty? && toot["visibility"] == "public" && toot["favourites_count"].to_i >= 2 | |
end | |
end | |
(1..N_PAGE).each do | |
uri = URI.parse("https://#{MASTODON_SERVER}/api/v1/accounts/#{MASTODON_USER_ID}/statuses") | |
# https://#{MASTODON_SERVER}/api/v1/bookmarks | |
# https://#{MASTODON_SERVER}/api/v1/favourites" | |
uri.query = URI.encode_www_form({ max_id: last_id }) | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = uri.scheme === "https" | |
headers = { "Authorization" => "Bearer #{ENV['MASTODON_ACCESS_TOKEN']}" } | |
response = http.get(uri, headers) | |
JSON.parse(response.body).each do |toot| | |
last_id = toot["id"].to_i if last_id > toot["id"].to_i | |
if Toot.accept?(toot) then | |
count = count + 1 | |
status = Toot.format(toot) | |
if status.length > 0 then | |
puts "原文: #{toot["content"]}" if DEBUG | |
puts "#{status} (@#{toot["account"]["username"]})" | |
JumanppRuby::Juman.new(force_single_path: :true).parse(status) do |word_juman| | |
yomi = word_juman[1].to_s.yomi | |
if yomi =~ KANJI_RE then # Jumanppでの解析失敗時はMecabに回す | |
yomi_katakana = "" | |
Natto::MeCab.new('-Oyomi').parse(yomi) do |word_mecab| | |
yomi_katakana = yomi_katakana + word_mecab.feature.to_s.chomp | |
end | |
yomi = yomi_katakana.yomi | |
end | |
print yomi | |
end | |
puts "" | |
end | |
end | |
end | |
end | |
puts " count= #{count}, max_id= #{last_id}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment