goo.gl está sendo desativado (veja Google URL Shortener links will no longer be available [updated] - Google Developers Blog). Portanto, os links em seu fórum que incluem links goo.gl quebrarão após 25 de agosto de 2025.
Desenvolvi este script para um cliente. Levou muito, muito mais tempo do que eu cobrei dele e mais tempo do que gostaria de admitir.
Ele encontra todas as postagens que contêm goo.gl e, em seguida, (se não forem maps.app.goo.gl ou maps.goo.gl) tenta substituí-las pelo URL que goo.gl retorna. Ele usa o revisor de postagens, para que você possa ver que foi atualizado e por quê, e você pode revertê-lo se quiser. Tentei torná-lo o mais à prova de falhas possível, mas use por sua conta e risco.
Se você não sabe como executar este script, provavelmente não deveria fazê-lo sozinho. Se precisar de ajuda para fazê-lo funcionar e tiver um orçamento, entre em contato comigo ou pergunte em Marketplace.
Se você o usar e tiver sugestões de como melhorá-lo, por favor, responda e/ou edite-o como achar melhor.
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 # Retorna o original se não houver redirecionamento
end
rescue
short_url # Retorna o original se houve um erro
end
def resolve_url(short_url)
short_url.gsub!(/http:/,"https:")
# Verifica se o URL já está no cache
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)
# Resolve o URL se for um redirecionamento
resolved_url = if response.is_a?(Net::HTTPRedirection)
response['location']
else
short_url # Retorna o original se não houver redirecionamento
end
sleep 1
# Armazena o URL resolvido no cache
URL_CACHE[short_url] = resolved_url
resolved_url
rescue
# Armazena o URL original no cache em caso de erro
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 "."
# por razões não claras, tentar atualizar postagens nesses tópicos travou o rails
# next if [145478,64885,84408].include? post.topic_id
# encontra links goo.gl e o URL para ver para onde ele redireciona
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 # postagens em tópicos excluídos não têm tópico e quebram o 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