weibo: normalize weibo urls in artist entries.

Normalize all Weibo URLs in artist entries to one of these forms:

* https://www.weibo.com/u/5399876326
* https://www.weibo.com/p/1005055399876326
* https://www.weibo.com/chengziyou666
This commit is contained in:
evazion
2022-03-13 20:48:42 -05:00
parent 1d9a15a119
commit 223742c365
3 changed files with 39 additions and 3 deletions

View File

@@ -61,6 +61,17 @@ class Artist < ApplicationRecord
def clear_url_string_changed
self.url_string_changed = false
end
class_methods do
# Find all artist URLs matching `regex`, and replace the `from` regex with the `to` string.
def rewrite_urls(regex, from, to)
Artist.transaction do
Artist.joins(:urls).where_regex("artist_urls.url", regex).find_each do |artist|
artist.update!(url_string: artist.url_string.gsub(from, to))
end
end
end
end
end
concerning :NameMethods do

View File

@@ -0,0 +1,23 @@
#!/usr/bin/env ruby
require_relative "base"
with_confirmation do
# https://m.weibo.cn/u/7119279079 -> https://m.weibo.com/u/7119279079
Artist.rewrite_urls('weibo\.cn', /weibo\.cn/, "weibo.com")
# https://m.weibo.com/u/7119279079 -> https://www.weibo.com/u/7119279079
Artist.rewrite_urls('m\.weibo\.com', /m\.weibo\.com/, "www.weibo.com")
# https://weibo.com/u/5466604405 -> https://www.weibo.com/u/5466604405
Artist.rewrite_urls('https?://weibo\.com', %r{https?://weibo\.com}, "https://www.weibo.com")
# https://www.weibo.com/5493194708/profile?rightmod=1&wvr=6&mod=personnumber&is_all=1 -> https://www.weibo.com/5493194708/profile
Artist.rewrite_urls('weibo\.com.*\?.*', /(.*weibo\.com.*)(\?.*$)/, '\1')
# https://www.weibo.com/5493194708/profile -> https://www.weibo.com/5493194708
Artist.rewrite_urls('weibo.com/[0-9]+/profile', %r{/profile$}, "")
# https://www.weibo.com/5493194708/home -> https://www.weibo.com/5493194708
Artist.rewrite_urls('weibo.com/[0-9]+/home', %r{/home$}, "")
end

View File

@@ -3,9 +3,11 @@ require_relative "../../config/environment"
# Run a block of code in a transaction, and only commit it after confirmation.
def with_confirmation(&block)
ApplicationRecord.transaction do
yield
CurrentUser.scoped(User.system, "127.0.0.1") do
yield
print "Commit? (yes/no): "
raise "abort" unless STDIN.readline.chomp == "yes"
print "Commit? (yes/no): "
raise "abort" unless STDIN.readline.chomp == "yes"
end
end
end