goo.gl がシャットダウンします(Google URL Shortener links will no longer be available [updated] - Google Developers Blog を参照)。そのため、フォーラムにある goo.gl リンクを含むリンクは、2025年8月25日以降無効になります。
このスクリプトはクライアントのために開発しました。請求した金額よりもはるかに多くの時間と、認めたくないほどの時間を費やしました。
goo.gl を含むすべての投稿を見つけ、それらが maps.app.goo.gl または maps.goo.gl でない場合、goo.gl が返す URL に置き換えようとします。投稿リビジョンを使用するため、いつ、なぜ更新されたかを確認でき、必要に応じて元に戻すことができます。可能な限り安全なものにするよう努めましたが、自己責任で使用してください。
このスクリプトの実行方法がわからない場合は、自分で実行しない方が良いでしょう。実行を手伝ってほしい場合は、予算があれば私に連絡するか、Marketplace で質問してください。
使用してみて、改善点があれば、返信するか、必要に応じて編集してください。
URL_CACHE ||= {}
def resolve_url_simple(short_url)
uri = URI.parse(short_url)
response = Net::HTTP.get_response(uri)
if response.is_a?(Net::HTTPRedirection)
response['location']
else
short_url # リダイレクトがない場合は元の URL を返す
end
rescue
short_url # エラーが発生した場合は元の URL を返す
end
def resolve_url(short_url)
short_url.gsub!(/http:/,"https:")
# URL が既にキャッシュにあるか確認
return URL_CACHE[short_url] if URL_CACHE.key?(short_url)
begin
uri = URI.parse(short_url + "?si=1")
response = Net::HTTP.get_response(uri)
# リダイレクトの場合は URL を解決する
resolved_url = if response.is_a?(Net::HTTPRedirection)
response['location']
else
short_url # リダイレクトがない場合は元の URL を返す
end
sleep 1
# 解決された URL をキャッシュに保存する
URL_CACHE[short_url] = resolved_url
resolved_url
rescue
# エラーの場合は元の URL をキャッシュに保存する
URL_CACHE[short_url] = short_url
short_url
end
end
def replace_goo_gl_links(text)
goo_gl_regex = %r{(?<=\A|[\\[\\]\\(\\)\\s])(https?://)?goo\\.gl(/[a-zA-Z0-9]+)+}
text.gsub(goo_gl_regex) do |match|
if match.include?('maps.app.goo.gl')
match
else
full_url = match.start_with?('http') ? match : "https://#{match}"
print "FIXING!!: #{match} -----> "
fixed = resolve_url(full_url)
puts fixed
fixed
end
end
end
def replace_all_goo_gl_links
system_user= User.find(-1)
goo_go = Post.where("raw LIKE '%goo.gl%'")
total_posts = goo_go.count
puts "Found #{total_posts} posts to check"
count = 0
goo_go.find_each do |post|
count += 1
# puts "Processing #{count}. #{Discourse.base_url}/t/#{post.topic_id}/#{post.post_number}"
print "."
# 不明な理由により、これらのトピックの投稿を更新しようとすると rails がクラッシュしました
# next if [145478,64885,84408].include? post.topic_id
# goo.gl リンクを見つけ、リダイレクト先を確認する
new_raw = replace_goo_gl_links(post.raw)
if new_raw != post.raw
revision_options = {
edit_reason: "Fix goo.gl links",
bypass_bump: true
}
begin
puts "Revising (#{count}/#{total_posts}) #{Discourse.base_url}/t/#{post.topic_id}/#{post.post_number}"
if !post.topic # 削除されたトピックの投稿にはトピックがなく、PostRevisor がクラッシュします
post.topic = Topic.with_deleted.find_by(id: post.topic_id)
next if !post.topic
end
PostRevisor.new(post).revise!(system_user, raw: new_raw, **revision_options)
rescue => e
puts "cannot revise (number: #{count} #{Discourse.base_url}/t/#{post.topic_id}/#{post.post_number}): #{e}"
end
sleep 15
end
end
end