Adapted from https://github.com/lllusion3469/danbooru/commit/e51e1aa7ddb73e92541bfbcab5c2f8c0359a07dc.