聴覚情報処理障害かもしれない

poem.mizdra.net

この記事を読んで、心当たりがありすぎる。自分は耳が悪いのかも知れないと思いつつも、聴力検査は問題がなかったし、みんなそういうものなのかなと思っていた。しかし症状を見ると聴覚情報処理障害っぽく思える。

例えばこれは1年半前のツイート。

これは今でも全く治ってない。いつからこの症状が出ているのかもわからない、大学生の時はすでに悩んでいた気がする。

相手の話の一部分が聞き取れないことが頻繁にあって、聞き流してしまう事も多い。聞き流した結果会話が途切れて、変な空気になる。変な空気になるともう聞き返せないし、相手が何を言っていたかわからないので次の会話をはじめにくい。そうして人間とのコミュニケーションが嫌になっていく。

聴覚情報処理障害の特徴チェックリストはこうなった。(そもそもこのチェックリストの信憑性が怪しい気もするが、それはそれ)

  • [ ] 言葉の発達が遅い
  • [x] 聞き返しが多い
    • 相手の話の一部分だけ全く理解できないことが多々ある
  • [x] 騒がしい場所での聞き取りが苦手
    • 駅で電車がきてる時は絶対に話しかけないようにしているし、大人数での飲み会はストレス
  • [x] 文字情報の方が正確に理解できる
    • これはそもそもリアルタイム言語処理より文字情報の方が繰り返し読めるので当然では?
  • [x] 音韻が似ている言葉を聞き間違える
    • 字幕を見ると違う単語であることが多々ある
  • [x] 言葉での指示への反応が遅い
    • 相手の真意を確かめるために自分の解釈を相手に伝えて正しいか確かめることをよくする
  • [x] 聴覚情報の記憶力が弱い
    • ちょっと長い話をされると古い話が頭から抜けていく。
    • みんなそういうものだと思ってたが違うのか?

こうして見ると完全に聴覚情報処理障害なのか?とも思ったが、本当に聴覚情報処理障害の人はコールセンターで働く事も困難らしい。私は新卒の頃2ヶ月コールセンター研修を行なったが特に問題はなかった。なので私は軽度の聴覚情報処理障害か、ただの勘違い野郎ということになる。

しかし実際のところめっちゃ困っている。人と会話するのが億劫になるし電話がめちゃくちゃ嫌だ、なるべく文字情報でのコミュニケーションで済ませたい。

聴覚情報処理障害だと英会話は致命的なのか?と思うがどうなんだろう。日本語ですら聞き取れないのに英語はもっと聞き取れないのでは、という気もする。

全て気のせいであってほしい。

Tesseract-OCR-iOS を使う

【Swift】文字認識ライブラリ、TesseractOCR for iOSを試してみた - Qiita に使い方があるが、少し古かったので Xcode9.4, Swift4 で動かすための手順。

Podfile

pod 'TesseractOCRiOS', '4.0.0'

そして pod install、終わったら xcworkspace を開く。
Build すると warning が大量に出るが気にしてはならない...

traineddata の入手

https://github.com/tesseract-ocr/tessdata/tree/bf82613055ebc6e63d9e3b438a5c234bfd638c93 から必要なデータを入手。
注意点があって、https://github.com/tesseract-ocr/tessdata_best から DL したやつは動かない。
Using traineddata from tesseract-ocr · Issue #299 · gali8/Tesseract-OCR-iOS

今回は eng と jpn を DL した。

Project に traineddata を追加

https://github.com/gali8/Tesseract-OCR-iOS/wiki/Installation にあるように、このようなディレクトリ構成にする。

f:id:star__hoshi:20180819223629p:plain

注意点は、 tessdata をディレクトリにすること。 Group にしたら動いてくれなかった。

OCR する

Qiita には Bridging-header の話があるが、Swift 4 でははもう不要。

import UIKit
import TesseractOCR

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let tesseract: G8Tesseract = G8Tesseract(language: "eng+jpn")
        tesseract.delegate = self
        tesseract.image = UIImage(named: "ep.jpg")!
        tesseract.recognize()

        print(tesseract.recognizedText)
    }
}

extension ViewController: G8TesseractDelegate {
    func shouldCancelImageRecognitionForTesseract(tesseract: G8Tesseract!) -> Bool {
        return false // return true if you need to interrupt tesseract before it finishes
    }
}

結果

ポケモンGOのエーフィを読み込んだらこんな感じになった。

some("CPー889 丶\' 蓼蘭\n工一フイ \'\n92/92HP\n28.77kg @ Q9ーm\n一 重さ 工スバ一 高さ\n\n")

f:id:star__hoshi:20180819224115p:plain

Firebase SDK for Cloud Functions を 1.0 に Migration した

The Firebase Blog: Launching Cloud Functions for Firebase v1.0 にあるように、 Cloud Functions の SDK が 1.0 になった。と言っても SDK が 1.0 になっただけで、 Cloud Functions のベータが外れたわけではない。

Migration

Firebase SDK for Cloud Functions Migration Guide: Beta to version 1.0  |  Firebase という親切なドキュメントがある。
Firestore を使っている Project で、実際に 1.0 に Migration した。

noImplicitAny

まず、 TypeScript で使おうとするとエラーが出る。

github.com

any 型が使われてしまっているようで、 tsconfig に "skipLibCheck": true を追加してひとまずしのぐようにした。

New initialization syntax for firebase-admin

今まではこう書いていた admin の initializeApp が簡潔になった。

// before
admin.initializeApp(functions.config().firebase)

// after
admin.initializeApp()

こうなったのは、 functions.config().firestore が廃止され process.env.FIREBASE_CONFIG を使うようになったからのようだ。
今後 projectId などが必要な場合は環境変数から取るように、と書かれている。

let firebaseConfig = JSON.parse(process.env.FIREBASE_CONFIG);
/* {  databaseURL: 'https://databaseName.firebaseio.com',
       storageBucket: 'projectId.appspot.com',
       projectId: 'projectId' }
*/

SDK changes by trigger type

今までの Event trigger では、 event というパラメータだけを使っていたが、それが contextsnapshot (or change) という 2 つのパラメータに変更された。

// before
exports.dbWrite = functions.firestore.document('/path').onWrite((event) => {
  const beforeData = event.data.previous.data(); // data before the write
  const afterData = event.data.data(); // data after the write
});

// after
exports.dbWrite = functions.firestore.document('/path').onWrite((change, context) => {
  const beforeData = change.before.data(); // data before the write
  const afterData = change.after.data(); // data after the write
});

context

型定義はこうなっている。

/** The context in which an event occurred.
 * An EventContext describes:
 * - The time an event occurred.
 * - A unique identifier of the event.
 * - The resource on which the event occurred, if applicable.
 * - Authorization of the request that triggered the event, if applicable and available.
 */
export interface EventContext {
    /** ID of the event */
    eventId: string;
    /** Timestamp for when the event occured (ISO string) */
    timestamp: string;
    /** Type of event */
    eventType: string;
    /** Resource that triggered the event */
    resource: Resource;
    /** Key-value pairs that represent the values of wildcards in a database reference */
    params: {
        [option: string]: any;
    };
    /** Type of authentication for the triggering action, valid value are: 'ADMIN', 'USER',
     * 'UNAUTHENTICATED'. Only available for database functions.
     */
    authType?: 'ADMIN' | 'USER' | 'UNAUTHENTICATED';
    /** Firebase auth variable for the user whose action triggered the function. Field will be
     * null for unauthenticated users, and will not exist for admin users. Only available
     * for database functions.
     */
    auth?: {
        uid: string;
        token: object;
    };

今までの event が持っていたパラメータに加えて、 Callable Functions の context が合わさったような感じ。 (Callable Functions の説明は こっち を参照)

snapshot

今までは DeltaDocumentSnapshot という型があったが、それが廃止され firebase.firestore.DocumentSnapshot が使われるようになった。
型定義はこうなっているが、前から使っていたものだし特に変更点もないと思う。

  /**
   * A `DocumentSnapshot` contains data read from a document in your Firestore
   * database. The data can be extracted with `.data()` or `.get(<field>)` to
   * get a specific field.
   *
   * For a `DocumentSnapshot` that points to a non-existing document, any data
   * access will return 'undefined'. You can use the `exists` property to
   * explicitly verify a document's existence.
   */
  export class DocumentSnapshot {
    protected constructor();

    /** True if the document exists. */
    readonly exists: boolean;

    /** A `DocumentReference` to the document location. */
    readonly ref: DocumentReference;

    /**
     * The ID of the document for which this `DocumentSnapshot` contains data.
     */
    readonly id: string;

    /**
     * The time the document was created. Not set for documents that don't
     * exist.
     */
    readonly createTime?: string;

    /**
     * The time the document was last updated (at the time the snapshot was
     * generated). Not set for documents that don't exist.
     */
    readonly updateTime?: string;

    /**
     * The time this snapshot was read.
     */
    readonly readTime: string;

    /**
     * Retrieves all fields in the document as an Object. Returns 'undefined' if
     * the document doesn't exist.
     *
     * @return An Object containing all fields in the document.
     */
    data(): DocumentData | undefined;

    /**
     * Retrieves the field specified by `fieldPath`.
     *
     * @param fieldPath The path (e.g. 'foo' or 'foo.bar') to a specific field.
     * @return The data at the specified field location or undefined if no such
     * field exists in the document.
     */
    get(fieldPath: string|FieldPath): any;

    /**
     * Returns true if the document's data and path in this `DocumentSnapshot`
     * is equal to the provided one.
     *
     * @param other The `DocumentSnapshot` to compare against.
     * @return true if this `DocumentSnapshot` is equal to the provided one.
     */
    isEqual(other: DocumentSnapshot): boolean;
  }

change

onUpdate と onWrite 時は snapshot ではなく Change<DocumentSnapshot> が使われる、Change の型を見てみる。

/** Change describes a change of state - "before" represents the state prior
 * to the event, "after" represents the state after the event.
 */
export declare class Change<T> {
    before: T;
    after: T;
    constructor(before?: T, after?: T);
}

今まで event.data.previous, event.data とデータを取得していたが、それが before, after と取れるようになった。わかりやすくて良い。

おわり

こんな感じで Migration が完了した、特に大きな混乱もなく、動作も問題なさそう。 (noImplicitAny は困っているが...)

Cloud Functions はとにかく重複発火をなくしてほしい。

Xcode 9.3 にしたら IDEWorkspaceChecks.plist というファイルが作られたが何者なのか

Xcode 9.3 でプロジェクトを開いただけで Hoge.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist というファイルが作られた。

Release Note を見ると以下のように書いてある。

Xcode 9.3 adds a new IDEWorkspaceChecks.plist file to a workspace's shared data, to store the state of necessary workspace checks. Committing this file to source control will prevent unnecessary rerunning of those checks for each user opening the workspace. (37293167) https://developer.apple.com/library/content/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html

どうやら .gitignore せずにソース管理した方がよさそう。ワークスペースのチェックを無駄に実行しないで済むっぽい (なんのことかよくわからないが...)。

ちなみに初期状態のファイルの中身はこうなっていた。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>IDEDidComputeMac32BitWarning</key>
    <true/>
</dict>
</plist>

Cloud Functions は global 変数から Function Name などを取得できる

Cloud Functions で以下のコードを実行して見ると、現在実行している関数の名前を取得できる。

console.log(global.process.env.FUNCTION_NAME) // -> createUser

他にも GCP_PROJECT, FUNCTION_MEMORY_MB, FUNCTION_TIMEOUT_SEC などが env から取得できる。実際に取得できる env は以下。

env { X_GOOGLE_CODE_LOCATION: '/user_code',
  WORKER_PORT: '8091',
  X_GOOGLE_SUPERVISOR_INTERNAL_PORT: '8081',
  X_GOOGLE_WORKER_PORT: '8091',
  FUNCTION_IDENTITY: 'hoge@appspot.gserviceaccount.com',
  X_GOOGLE_FUNCTION_REGION: 'us-central1',
  GCLOUD_PROJECT: 'hoge',
  FUNCTION_NAME: 'functionName',
  X_GOOGLE_FUNCTION_MEMORY_MB: '256',
  SUPERVISOR_HOSTNAME: '192.168.1.1',
  PATH: '/usr/local/bin:/usr/bin:/bin',
  X_GOOGLE_GCLOUD_PROJECT: 'hoge',
  FUNCTION_REGION: 'us-central1',
  PWD: '/user_code',
  FUNCTION_TRIGGER_TYPE: 'OTHER_EVENT_TRIGGER',
  FUNCTION_TIMEOUT_SEC: '60',
  X_GOOGLE_FUNCTION_TRIGGER_TYPE: 'OTHER_EVENT_TRIGGER',
  NODE_ENV: 'production',
  SHLVL: '1',
  X_GOOGLE_FUNCTION_NAME: 'functionName',
  X_GOOGLE_ENTRY_POINT: 'functionName',
  X_GOOGLE_FUNCTION_IDENTITY: 'hoge@appspot.gserviceaccount.com',
  X_GOOGLE_GCP_PROJECT: 'hoge',
  CODE_LOCATION: '/user_code',
  GCP_PROJECT: 'hoge',
  FUNCTION_MEMORY_MB: '256',
  X_GOOGLE_SUPERVISOR_HOSTNAME: '192.168.1.1',
  PORT: '8080',
  SUPERVISOR_INTERNAL_PORT: '8081',
  X_GOOGLE_FUNCTION_TIMEOUT_SEC: '60',
  ENTRY_POINT: 'payOrder',
  OLDPWD: '/var/tmp/worker/',
  _: '/usr/bin/env',
  HOME: '/tmp' }

これらを使うと、メタ的に Function Name を取得できるので、ライブラリ側で関数名を読み取ることができる。
starhoshi/fire-slack では明示的に関数名を指定しないでも勝手に Slack 通知に関数名を載せるようにした。

await Slack.send(
  { webhook: {}, ref: reference, error: Error('Invalid Request') }
)

このコードだけでこんな通知を送れる。

f:id:star__hoshi:20180329184453p:plain

env 以外にもこんなのが取得できるが、使い道はなさそう。

global { global: [Circular],
  process: 
   process {
     title: '/nodejs/bin/node',
     version: 'v6.11.5',
     moduleLoadList: 
      [ 'Binding contextify',
        'Binding natives',
        'Binding config',
        'NativeModule events',
        'NativeModule util',
        'Binding uv',
        'NativeModule buffer',
        'Binding buffer',
        'Binding util',
        'NativeModule internal/util',
        'NativeModule timers',
        'Binding timer_wrap',
        'NativeModule internal/linkedlist',
        'NativeModule assert',
        'NativeModule internal/process',
        'NativeModule internal/process/warning',
        'NativeModule internal/process/next_tick',
        'NativeModule internal/process/promises',
        'NativeModule internal/process/stdio',
        'Binding constants',
        'NativeModule path',
        'NativeModule module',
        'NativeModule internal/module',
        'NativeModule vm',
        'NativeModule fs',
        'Binding fs',
        'NativeModule stream',
        'NativeModule internal/streams/legacy',
        'NativeModule _stream_readable',
        'NativeModule internal/streams/BufferList',
        'NativeModule _stream_writable',
        'NativeModule _stream_duplex',
        'NativeModule _stream_transform',
        'NativeModule _stream_passthrough',
        'Binding fs_event_wrap',
        'NativeModule domain',
        'NativeModule tty',
        'NativeModule net',
        'NativeModule internal/net',
        'Binding cares_wrap',
        'Binding tty_wrap',
        'Binding tcp_wrap',
        'Binding pipe_wrap',
        'Binding stream_wrap',
        'NativeModule http',
        'NativeModule _http_incoming',
        'NativeModule _http_common',
        'Binding http_parser',
        'NativeModule internal/freelist',
        'NativeModule _http_outgoing',
        'NativeModule _http_server',
        'NativeModule _http_agent',
        'NativeModule _http_client',
        'NativeModule url',
        'Binding icu',
        'NativeModule querystring',
        'NativeModule crypto',
        'Binding crypto',
        'NativeModule internal/streams/lazy_transform',
        'NativeModule string_decoder',
        'NativeModule console',
        'NativeModule zlib',
        'Binding zlib',
        'NativeModule cluster',
        'NativeModule dgram',
        'Binding udp_wrap',
        'NativeModule child_process',
        'Binding spawn_sync',
        'NativeModule internal/child_process',
        'Binding process_wrap',
        'NativeModule internal/socket_list',
        'NativeModule internal/cluster',
        'Binding signal_wrap',
        'NativeModule os',
        'Binding os',
        'NativeModule https',
        'NativeModule tls',
        'NativeModule _tls_common',
        'NativeModule _tls_wrap',
        'NativeModule _stream_wrap',
        'Binding js_stream',
        'Binding tls_wrap',
        'NativeModule _tls_legacy',
        'NativeModule punycode',
        'NativeModule dns',
        'NativeModule constants' ],
     versions: 
      { http_parser: '2.7.0',
        node: '6.11.5',
        v8: '5.1.281.108',
        uv: '1.11.0',
        zlib: '1.2.11',
        ares: '1.10.1-DEV',
        icu: '58.2',
        modules: '48',
        openssl: '1.0.2l' },
     arch: 'x64',
     platform: 'linux',
     release: 
      { name: 'node',
        lts: 'Boron',
        sourceUrl: 'https://nodejs.org/download/release/v6.11.5/node-v6.11.5.tar.gz',
        headersUrl: 'https://nodejs.org/download/release/v6.11.5/node-v6.11.5-headers.tar.gz' },
     argv: [ '/nodejs/bin/node', '/var/tmp/worker/worker.js' ],
     execArgv: [ '--max-old-space-size=256' ],
     env: ...

Firestore のクエリで「存在しないこと」をチェックしたい場合、事前に null を入れておく

Firestore でこんなモデルを作ったとする。

admin.firestore().collection('user').add({
  startDate: new Date(),
  name: 'hoge'
})

このモデルに対し、「 endDate に値が入っていないもの」をクエリで取得したいとする。
パッと思いつくクエリはこう。

admin.firestore().collection('user')
  .where('endDate', '==', undefined)
  .get()

しかし、 undefined はクエリとして指定することができない。困った。

事前に null を入れておく

admin.firestore().collection('user').add({
  startDate: new Date(),
  name: 'hoge',
  endDate: null
})
admin.firestore().collection('user')
  .where('endDate', '==', null)
  .get()

is null を確認したい場合は、事前に null を入れてデータを作っておく。
後になって is null でクエリを書こうとしてもダメなので、事前に null を入れましょう。

false ならいけそうな気がしたがダメだった

データがあれば true, なければ false というような、js のようにふわっとかけないかなと思ったけどダメだった。
true / false はちゃんと真偽値しか検索に引っかからないようだ。

東雲めぐの配信を目覚まし時計にする on Mac

東雲めぐ(@megu_shinonome)

東雲めぐは平日朝 07:30 から生放送をしている VTuber (SHOWROOMER) であり、録画が残らないので毎朝ちゃんと起きねばならない。
この配信を目覚まし時計にすることで毎朝健康的な時間に目覚めることができる。

www.showroom-live.com

1. カレンダーに配信ページを開く設定をする

1-1. weblock の取得

こんな感じで、ブラウザの URL をデスクトップにドラッグ&ドロップすると webloc ファイルを入手できる。

f:id:star__hoshi:20180327090749g:plain

1-2. カレンダーの設定

Mac のカレンダーアプリに webloc を開く設定をする。

  • 時刻
    • 07:27 ~ 08:00
  • 繰り返し
    • 月曜 ~ 金曜
  • 通知
    • カスタム
      • ファイルを開く
      • 先ほど保存した webloc ファイルを開くファイルとして設定

f:id:star__hoshi:20180327090945p:plain

これでカレンダーアプリの設定はおわり。

2. スリープ解除

スリープしてない人はいいけど、スリープの設定している人はやる。

設定アプリ > 省エネルギー > スケジュール にスリープ解除のスケジュール設定をする。下記 Gif のようにすれば OK。

image

これで 07:30 前に PC が起動して東雲めぐの配信が自動でつくようになった。「カンカンかんカンカン!!!!朝だよ〜〜〜〜」という声で起こしてくれる。

注意

  • 寝る前に音量を MAX にしておくこと
    • これは AppleScript とかで解決できそうだけどやってない
  • Mac のロックは解除されないので起きてから解除する
    • ロック解除されずとも裏で配信は再生されているので声は聞こえる

おまけ

Mornin'

指定した時間にカーテンを開けてくれる、07:20 くらいにカーテンが開くような設定をしている。

mornin.jp

Nature Remo

赤外線リモコンで消せる電球を使っているならこれで指定した時刻にリモコンを動かせる。07:27 くらいに電気がつくようにしている。

nature.global

Firebase HTTPS Callable Functions を type-safe に叩けるライブラリ

Firebase HTTPS callable function を試してみる に使い方は書いたが、素のまま SDK を使うのは厳しいので APIKit っぽく Endpoint を定義できていい感じに Function を叩けるようなライブラリを作った。

github.com

endpoint の pathDecodable な Response を定義して、必要なら parameter をセットすると動く。
(APIKit の Request っぽい感じ)

import Callable

struct SampleResponse: Decodable {
    let name: String
}

struct Sample: Callable {
    typealias Response = SampleResponse
    let name: String

    init(name: String) {
        self.name = name
    }

    var path: String {
        return "httpcallable"
    }

    var parameter: [String: Any]? {
        return ["name": name]
    }
}

使う側はこうなる、返り値は Result。

let sample = Sample(name: "Jobs")
sample.call { result in
    switch result {
    case .success(let resonse):
        print(resonse)
    case .failure(let error):
        print(error)
    }
}

エラー

エラーは 3 パターン定義していて

  • function(Error)
    • Cloud Functions から返ってくる素のエラー
  • decode(Error)
    • Decodable で失敗した
  • illegalCombination(Any?, Error?)
    • result も error もどちらも返ってきた or どちらも nil
    • 注意 に書いたが、無が返ってくることがある

というのを用意している。まだ作ってみただけで実運用していないので、増やすかもしれない。

終わり

という感じで、 Endpoint を定義して、 Response は Decodable でいい感じに書けるようになった。

尚、テストはない...。

Cloud Functions for Firebase の環境変数設定後は関数の再デプロイが必要なので注意

ドキュメントにも書いてあることだが、環境変数変更後は関数の再デプロイが必要。

「functions:config:set」を実行した後は、新しい設定を使用可能にするために関数を再デプロイする必要があります。 https://firebase.google.com/docs/functions/config-env?hl=ja#set_environment_configuration_for_your_project

環境変数、動的に変えられるものだと思ってたんだけど勘違いだったっぽい。
そもそもなんで動的に変えられると思ってたのか思い出せない...。

続・イケてない JSON を Swift の Decodable で扱いやすいモデルにデコードする

イケてない JSON を Swift の Decodable で扱いやすいモデルにデコードする の続き。
今度も last.fmuser.getRecentTracks を Decode していく。

尚、文字を打つのが面倒になったためこの記事はあらゆる説明を省略している。

JSON

これを decode していく。

{
    "recenttracks": {
        "track": [
            {
                "artist": {
                    "#text": "TOKIO",
                    "mbid": "d210bd3e-68db-4987-a714-0214449e361d"
                },
                "name": "宙船",
                "streamable": "0",
                "mbid": "",
                "album": {
                    "#text": "Harvest",
                    "mbid": "3824d059-6f1a-4163-ae1a-65d5f87bee38"
                },
                "url": "https://www.last.fm/music/TOKIO/_/%E5%AE%99%E8%88%B9",
                "image": [
                    {
                        "#text": "https://lastfm-img2.akamaized.net/i/u/34s/1a20e578c4ea44c0a8cde03d6c6cc014.png",
                        "size": "small"
                    },
                    {
                        "#text": "https://lastfm-img2.akamaized.net/i/u/64s/1a20e578c4ea44c0a8cde03d6c6cc014.png",
                        "size": "medium"
                    }
                ],
                "@attr": {
                    "nowplaying": "true"
                }
            },
            ...............
        ],
        "@attr": {
            "user": "star__hoshi",
            "page": "1",
            "perPage": "50",
            "totalPages": "807",
            "total": "40316"
        }
    }
}

List 系 API の共通要素

last.fm の List 系 API は、データ本体の配列と attr という API メタデータの 2 つから構成されていて他の API でも同じなので ListResponse という protocol を用意した。
この List に RecentTrack や TopTrack を入れると Generics でいい感じになる。

public protocol ListResponse {
    associatedtype List

    var list: [List] { get }
    var attr: Attr { get }
}

public struct Attr: Decodable {
    public let user: String
    public let page: Int
    public let perPage: Int
    public let totalPages: Int
    public let total: Int
}

protocol を実装するとこんな感じになる。

    public struct RecentTracksResponse: ListResponse, Decodable {
        public typealias List = RecentTrack
        public let list: [List]
        public let attr: Attr

        private enum CodingKeys: String, CodingKey {
            case list
            case attr
        }

        private enum RecentTracksKeys: String, CodingKey {
            case recenttracks
        }

        private enum TrackAttrKeys: String, CodingKey {
            case track
            case attr = "@attr"
        }

        public init(from decoder: Decoder) throws {
            let values = try decoder.container(keyedBy: RecentTracksKeys.self)
            let recentTracks = try values.nestedContainer(keyedBy: TrackAttrKeys.self, forKey: .recenttracks)
            self.attr = try recentTracks.decode(Attr.self, forKey: .attr)
            self.list = try recentTracks.decode([List].self, forKey: .track)
        }
    }

配列の中身を decode

nowplaying がネストしててもしょうもないので、 @attr で decode してそこからさらに nowplaying で decode するようにした。
last.fmAPI は本当にひどいので、 nowplaying が false 場合はそもそも @attr が空っぽになってる。なので if decoder.contains(.nowplaying) してから decode している。

Image とか String -> Int の変換とかは こっち で先にやっていたので使い回せて便利。

public struct RecentTrack: Decodable {
    public let name: String
    public let image: Image
    public let loved: Bool?
    public let streamable: Bool
    public let mbid: String
    public let url: URL
    public let date: Date?
    public let nowplaying: Bool
    public let album: Album
    public let artist: RecentArtist

    private enum CodingKeys: String, CodingKey {
        case name
        case image
        case loved
        case streamable
        case mbid
        case url
        case date
        case album
        case artist
        case nowplaying = "@attr"
    }

    private enum NowplayingKeys: String, CodingKey {
        case nowplaying
    }

    public init(from decoder: Decoder) throws {
        let decoder = try decoder.container(keyedBy: CodingKeys.self)

        name = try decoder.decode(String.self, forKey: .name)
        image = try decoder.decode(ImageDecodableMap.self, forKey: .image).decoded
        let loved = try decoder.decodeIfPresent(StringCodableMap<Int>.self, forKey: .loved)?.decoded
        self.loved = loved == nil ? nil : loved == 1
        streamable = try decoder.decode(StringCodableMap<Int>.self, forKey: .streamable).decoded == 1
        mbid = try decoder.decode(String.self, forKey: .mbid)
        url = try decoder.decode(URL.self, forKey: .url)
        date = try decoder.decodeIfPresent(DateDecodableMap.self, forKey: .date)?.decoded
        album = try decoder.decode(Album.self, forKey: .album)
        artist = try decoder.decode(RecentArtist.self, forKey: .artist)
        if decoder.contains(.nowplaying) {
            let nowplayingDecoder = try decoder.nestedContainer(keyedBy: NowplayingKeys.self, forKey: .nowplaying)
            nowplaying = try nowplayingDecoder.decodeIfPresent(StringCodableMap<Bool>.self, forKey: .nowplaying)?.decoded ?? false
        } else {
            nowplaying = false
        }
    }
}

最終的には LastfmClient/RecentTrack.swift な感じになった。

本当はもっとややこしくて、 extended=0API 投げると loved や data が帰ってこなかったりするのだが、それは decodeIfPresent でなんとかなる。

おわり

Codable 良いのだが、汚い API は Decode するだけで無限に時間を消費しててつらい。