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로 대체하려고 시도합니다. 게시물 재수정자(post revisor)를 사용하므로 업데이트된 사실과 그 이유를 확인할 수 있으며, 원하면 되돌릴 수도 있습니다. 최대한 안전하도록 만들었지만, 사용은 본인의 책임입니다.
이 스크립트를 실행하는 방법을 모른다면, 직접 실행하지 않는 것이 좋습니다. 작동하도록 도와줄 사람이 필요하고 예산이 있다면 저에게 연락하거나 #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 # 리디렉션이 없으면 원래 값을 반환
end
rescue
short_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 # 리디렉션이 없으면 원래 값을 반환
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{(?<=^|[\[\]\(\)\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 링크와 해당 URL이 어디로 리디렉션되는지 확인
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