早速、メトロポリタン美術館のデータを取りに行ってみた

こんなニュースが上がっていたのをご存知のかたも多いのではないでしょうか？
[40万点の作品画像を無料開放。メトロポリタン美術館がAPIを公開](https://bijutsutecho.com/magazine/news/headline/18741)

なにかに使えそうなので、早速取りに行ってみました。

# 参考ページ
[ここ](https://metmuseum.github.io/)にすべての情報が載っているのですが、実際に使うために、もう少し噛み砕いてみたいと思います。

# アクセス
実はアクセスするためにユーザ登録などは必要ありません。
***なんて太っ腹なんだ！***

ベースのURLは以下になります。
https://collectionapi.metmuseum.org

GETでアクセスします。

# 一覧の取得
一覧の取得は以下の通り。

```text
https://collectionapi.metmuseum.org/public/collection/v1/objects
```

また、引数として日付を指定すると、それ以上のデータを取得することができます。

```text
https://collectionapi.metmuseum.org/public/collection/v1/objects?metadataDate=2018-10-22
```

すると以下のようにJSONで返ってきます。

```text
{
	"total": 471581,
	"objectIDs": [
		1,
		2,
		3,
		4,
		5,
		6,
		7,
		8,
		9,
		10,
		// more results ...
	]
}
```

| フィールド名 | 説明 |
|---|---|
| total | データ数 |
| objectIDs | 公開可能なデータのオブジェクトIDの配列 |

このオブジェクトIDを指定して、個別のデータを取得します。

# 個別の取得
個別の取得は以下の通り。

```text
https://collectionapi.metmuseum.org/public/collection/v1/objects/[objectID]
```

[objectID]のところに、取得したいデータのオブジェクトIDを指定します。
すると以下が返ってきます。

```text
{
  "objectID": 436535,
  "isHighlight": true,
  "accessionNumber": "1993.132",
  "isPublicDomain": true,
  "primaryImage": "https://images.metmuseum.org/CRDImages/ep/original/DT1567.jpg",
  "additionalImages": [
    "https://images.metmuseum.org/CRDImages/ep/original/LC-EP_1993_132_suppl_CH-004.jpg",
    "https://images.metmuseum.org/CRDImages/ep/original/LC-EP_1993_132_suppl_CH-003.jpg",
    "https://images.metmuseum.org/CRDImages/ep/original/LC-EP_1993_132_suppl_CH-002.jpg",
    "https://images.metmuseum.org/CRDImages/ep/original/LC-EP_1993_132_suppl_CH-001.jpg"
  ],  
  "constituents": [
    {
      "role": "Artist",
      "name": "Vincent van Gogh"
    }
  ],
  "department": "European Paintings",
  "objectName": "Painting",
  "title": "Wheat Field with Cypresses",
  "culture": "",
  "period": "",
  "dynasty": "",
  "reign": "",
  "portfolio": "",
  "artistRole": "Artist",
  "artistPrefix": "",
  "artistDisplayName": "Vincent van Gogh",
  "artistDisplayBio": "Dutch, Zundert 1853–1890 Auvers-sur-Oise",
  "artistSuffix": "",
  "artistAlphaSort": "Gogh, Vincent van",
  "artistNationality": "Dutch",
  "artistBeginDate": "1853",
  "artistEndDate": "1890",
  "objectDate": "1889",
  "objectBeginDate": 1889,
  "objectEndDate": 1889,
  "medium": "Oil on canvas",
  "dimensions": "28 7/8 × 36 3/4 in. (73.2 × 93.4 cm)",
  "creditLine": "Purchase, The Annenberg Foundation Gift, 1993",
  "geographyType": "",
  "city": "",
  "state": "",
  "county": "",
  "country": "",
  "region": "",
  "subregion": "",
  "locale": "",
  "locus": "",
  "excavation": "",
  "river": "",
  "classification": "Paintings",
  "rightsAndReproduction": "",
  "linkResource": "",
  "metadataDate": "2018-10-19T14:00:42.01Z",
  "repository": "Metropolitan Museum of Art, New York, NY",
  "objectURL": "https://www.metmuseum.org/art/collection/search/436535"
}
```

| フィールド名 | 説明 |
|---|---|
| objectID | 取得したデータのオブジェクトID |
| isHighlight | 有名なデータか（true/false） |
| accessionNumber | ID<br/>※ユニークではない |
| isPublicDomain | パブリックドメインか（true/false） |
| primaryImage | 画像データへのURL |
| additionalImages | 説明用の追加の画像データへのURL（配列） |
| constituents | 付加情報（key-value） |
| department | 作品を担当しているMetのキュレーターの部門ID |
| objectName | 種別 |
| title | タイトル |
| culture | 関連する国 |
| period | 作成された年代 |
| dynasty | 作成された時代 |
| reign | 作成されたときの君主 |
| portfolio | グループとして作成された、またはシリーズとして公開された一連の作品 |
| artistRole | 作者の役割 |
| artistPrefix | 製作に関する追加の情報 |
| artistDisplayName | 作者の名前 |
| artistDisplayBio | 作者の生年月日など |
| artistSuffix | 作者に関する構成要素の役割を果たす複雑な情報 |
| artistAlphaSort | 作者をソートするための姓、名、サフィックス、敬称 |
| artistNationality | 作者の国籍 |
| artistBeginDate | 作者の生年 |
| artistEndDate | 作者の没年 |
| objectDate | 作成した期間等 |
| objectBeginDate | 製作を開始した日 |
| objectEndDate | 完成した日 |
| medium | 素材等 |
| dimensions | サイズ |
| creditLine | 出所または起源を認めるテキストと、博物館が取得した年 |
| geographyType | 以降の地理情報の種類（作られた、見つかったなど） |
| city | 作成された町 |
| state | 作成された州 |
| county | 作成された郡 |
| country | 作成、または、見つかった国 |
| region | 見つかった場所 |
| subregion | 見つかった場所（regionより細かい） |
| locale | 見つかった場所（subregionより細かい） |
| locus | 見つかった場所（localeより具体的ではないがexcavationより細かい） |
| excavation | 発掘名 |
| river | 川の名前 |
| classification | 種類を表す一般的な表現 |
| rightsAndReproduction	| コピーライト |
| linkResource | リソースのURL |
| metadataDate | 最後にメタデータを更新した日時 |
| repository | ※固定 |
| objectURL | metmuseum.orgでのページへのURL |

# とりあえず試してみる
まずは、日付を指定してオブジェクトIDの一覧を取得してみます。

```text
> curl https://collectionapi.metmuseum.org/public/collection/v1/objects?metadataDate=2018-10-31

{"total":1301,"objectIDs":[1980,37007,38153,38237,38435,38634,38860,39408,39409,39676,44767,238319,238326,242205,242212,247587,247597,247609,247873,248881,248899,249063,250651,250669,250777,250798,250897,252451,252498,252511,252543,252595,254185,254196,254201,254237,254270,254275,254291,255865,255939,255969,309410,309411,316010,316408,316477,316520,318081,318651,322907,322910,435690,435727,435752,435770,435804,435814,435818,435866,435886,435894,436730,436770,436787,436788,436836,436847,436863,436885,436888,436923,436935,437001,437021,437024,438008,438011,438025,438031,438032,438098,438099,438112,438139,438373,438374,438377,438379,438387,438490,438543,438551,438603,438620,438621,438624,438625,438626,438627,438628,438636,438638,438645,438647,438648,438660,438665,438668,438672,438676,438679,438724,438727,438761,438780,438783,438824,438845,438857,438949,438951,439065,451283,647330,647331,647333,647336,647337,647339,647341,647345,647347,647348,647350,647352,647353,647354,647356,647357,647408,647409,647410,647411,647412,647413,647414,647423,647424,647425,647426,647427,647430,647431,647434,647436,647438,647439,647441,647442,647444,647446,647447,647448,647450,647451,647452,647454,647457,647458,647459,647460,647723,648202,648203,648204,648206,648207,648218,743011,749529,749541,749552,749563,749631,749648,749665,749675,749695,749735,767741,767742,767743,767744,767745,767746,767748,767750,767752,767754,767755,767756,767876,767877,767878,767879,767880,767881,767883,767884,767885,767886,767887,767888,767891,767892,767893,767895,767896,767898,767901,767902,767903,767904,767905,767906,767927,767928,767929,767930,767931,767933,767934,767935,767936,767937,767939,767940,767943,767944,767945,767947,767948,767949,767952,767954,767955,767962,767963,767972,767976,767980,767981,778992,778993,784629,812744,812755,812766,812773,812781,812789,812798,812802,812806,812807,812811,812816,812820,812821,812822,812824,812826,812828,812829,812830,812831,812832,812858,814422,815448,815449,815450,815453,815457,815471,815472,815473,815474,817028,817060,817061,817062,817063,817064,817065,817066,817067,817071,817072,817073,817074,817075,817076,817077,817078,817079,817080,817081,817082,817083,817084,817085,817086,817087,817088,817089,817090,817091,817092,817093,817094,817095,817096,817097,817098,817099,817100,817101,817102,817103,817104,817105,817106,817107,817108,817109,817110,817113,817114,817115,817116,817117,817118,817119,817120,817121,817122,817125,817127,16948,21698,23095,25653,26616,27612,27874,27876,27877,27878,32256,32360,32380,207397,464794,464795,464796,464797,464818,464973,465407,465419,465428,465435,465442,465443,465461,465472,465483,465495,465497,465502,465506,465518,465529,465534,465543,465553,465590,465592,465613,465617,465619,465620,465629,465659,544080,544161,544168,544232,546817,547765,5401,21939,22873,25836,27340,27961,29175,40049,42178,236774,239576,242816,245585,247305,247308,247311,247315,247347,247410,247412,247413,251345,251380,251467,251473,251485,251486,251490,251497,252973,252976,253031,253053,254342,254454,254470,254492,254524,254551,254558,254570,255553,255623,255814,255824,258449,258464,420071,420081,420082,420083,420084,420085,420086,420087,420088,420089,420090,420091,420092,420093,420094,420095,420096,420097,420217,420219,420220,420222,420223,420224,420225,420226,420228,420229,420232,420234,420235,420236,420237,420240,420241,420242,420243,420245,420249,420250,420251,420252,420253,420254,420255,471722,483887,488601,492822,492823,693948,697840,703054,748125,751178,751190,751209,751239,751252,751270,751317,751327,751340,751350,751363,751375,751388,751401,751410,770207,785032,10238,24258,25076,27513,29365,33858,50800,310782,311181,311209,311950,314587,360452,419904,419905,420256,420257,420258,420259,420260,420261,420262,420263,420264,420265,420726,471966,481192,483416,485796,488500,488501,489390,712128,716657,747794,750911,750926,750949,750970,750982,754048,774240,774242,774244,816242,816256,22094,22181,198444,198636,199413,320345,350967,435626,435660,435670,436345,436346,436406,436456,436485,436533,436534,436560,436577,436590,436607,436698,437378,437428,437433,437436,437456,437506,437513,437533,437585,437604,437607,437636,437680,437686,437701,439272,439327,439363,439366,439367,439369,439370,439372,439374,439376,439380,439381,439387,439389,439392,439393,439394,439397,439399,439403,439404,439405,439406,439407,439410,439411,439553,439631,439762,439763,439766,440326,440328,440329,440330,440331,440332,440333,440334,440336,440337,440338,440341,440344,440346,440348,440351,440352,440356,440357,440360,440364,440367,440370,440372,440374,440375,440376,440378,440386,440407,440409,440459,440460,440568,440726,440727,440729,440847,441024,441183,441227,441230,442183,442356,442761,447581,455461,455475,458799,458960,459172,459174,459176,459178,463419,464070,464071,464073,464078,465763,465765,465779,465788,465789,465812,465822,468634,478173,478177,481913,481915,481967,485037,485038,485071,497596,655744,665702,680333,680363,682748,684492,684494,685040,686525,698245,738106,744107,749858,750663,750679,750701,750714,750726,750834,750844,750858,750877,750897,777855,777873,789033,789041,812451,812562,816740,11521,21197,29179,29180,29183,29189,29191,29203,29273,29327,29335,29341,29342,29343,29344,29346,29347,29348,29349,29357,30346,30349,30355,30357,30358,30359,30360,30361,30362,30363,30364,30376,30437,30438,30443,30448,30450,30451,30454,30457,30458,30459,30469,30492,31444,32156,32159,33350,53939,68530,73140,81133,81718,82145,82166,83553,83605,84317,84365,85827,86870,96424,98462,98573,103035,103038,103067,103938,103959,104089,104462,106030,106031,108402,108410,113383,116432,119448,121381,156165,244857,244873,244885,244904,246538,246546,248262,248492,250450,250545,250547,251799,251828,251846,251869,253340,253351,253383,253525,253534,253541,253556,253575,253594,253627,254904,254909,254912,254922,254930,254943,254979,254995,255056,255136,256880,265199,356573,403216,403224,403230,403232,403233,403235,403236,403237,403239,403243,405183,405184,405185,405186,405187,405188,405189,405190,405191,405192,405193,405194,405195,405196,405197,405198,405199,405200,405201,405202,405203,405204,405205,405206,405854,405855,405857,405858,405861,405862,405863,405864,405865,405867,405868,405869,405870,405871,405872,405873,405874,405875,405876,405877,405878,405879,405880,405881,405882,405883,405884,405885,405886,405887,405888,405889,405938,405939,405945,405947,405948,405950,405951,405952,405953,405954,405955,405956,405957,405958,405959,405960,482236,488316,489234,489235,489236,495366,495409,495424,495436,544321,546437,546463,548425,548438,548643,548831,549199,681431,683988,728079,728080,728081,739852,745965,748484,751433,760942,786756,786759,786761,786762,786763,786764,786765,788951,813594,8332,8345,8346,32943,32944,32945,33031,45048,55138,57153,57797,58435,81536,82284,82607,82629,84062,85863,86026,86027,90475,91000,93787,94440,94441,100028,101809,103484,103530,103806,104915,106393,106620,107654,107712,107718,107719,107815,107830,107839,107936,107937,114169,124143,127167,192045,193666,243646,243692,243825,244924,244959,244961,244970,244986,245038,245039,245047,245048,245049,245058,245060,245067,245140,246690,246700,246725,246728,246937,247915,247924,247958,247960,247964,247965,247986,248092,248093,248094,248100,248101,248153,248154,248205,248225,249228,249276,250945,250946,250950,251050,251054,251078,251086,251088,251175,251234,251256,253652,253838,253855,253863,253988,253992,255227,255312,255340,266980,319053,329227,436035,436078,436137,436167,436182,436186,436241,436270,436297,436310,437053,437089,437104,437130,437159,437174,437209,437216,437241,437242,437270,437281,437282,437300,437302,437314,437332,437336,437755,437757,437763,437836,437839,437844,437863,437869,437870,437898,437955,437977,437986,437988,437995,437998,438003,455287,459066,459102,459104,459122,459138,459139,459141,459145,459153,459158,460514,556560,556861,626363,629928,749118,749119,749120,749122,749123,749124,749134,749208,749209,749215,749216,749217,749219,749248,749260,749326,749339,755082,766542,772041,22329,22455,197562,197587,197623,201809,203371,203372,207006,242008,242142,243611,243632,247017,247020,247075,247100,247193,247219,247220,247248,247282,247304,248578,248580,248593,248661,248674,248681,248683,248684,248698,248730,248757,248804,252918,252923,252938,254590,254611,254649,254777,254814,254819,254842,254854,254864,254879,254881,254883,254884,309975,310940,312154,312303,312316,312366,312680,312685,312717,313708,316174,316299,316318,317760,318345,373834,403244,403246,403249,403251,403254,403256,403257,403259,403262,403263,403266,403268,403269,403270,403271,403273,403274,403275,403278,403279,403280,403281,403283,403284,403285,403286,403287,403288,403289,403291,403292,403293,403294,403296,403298,403299,403300,403301,403302,403303,403304,403306,403307,403308,403309,405175,405182,405832,472835,472836,481815,481820,481821,481824,485413,485414,491266,492636,492759,544509,545564,746487,746488,748968,757220,757255]}
```

昨日の日付を指定したのに結構な数のデータが取ってこれてしまいました（苦笑）
IDだけだとどれがなんだかよくわからないのですが、とりあえず一番最後のデータを取ってきてみます。

```text
>curl https://collectionapi.metmuseum.org/public/collection/v1/objects/757255

{"objectID":757155,"isHighlight":false,"accessionNumber":"37.68.11","isPublicDomain":false,"primaryImage":"","additionalImages":[],"constituents":[{"role":"Artist","name":"Anonymous"},{"role":"Publisher","name":"Antonio Salamanca"}],"department":"Drawings and Prints","objectName":"Print","title":"Scipio and his army storming the camp of the Carthaginians","culture":"","period":"","dynasty":"","reign":"","portfolio":"","artistRole":"Artist","artistPrefix":"","artistDisplayName":"Anonymous","artistDisplayBio":"","artistSuffix":"","artistAlphaSort":"ANONYMOUS","artistNationality":"","artistBeginDate":"","artistEndDate":"","objectDate":"1540","objectBeginDate":1540,"objectEndDate":1540,"medium":"Engraving","dimensions":"Sheet: 15 3/4 × 21 1/4 in. (40 × 54 cm)","creditLine":"Gift of Miss Helen J. Baker, 1937","geographyType":"","city":"","state":"","county":"","country":"","region":"","subregion":"","locale":"","locus":"","excavation":"","river":"","classification":"Prints","rightsAndReproduction":"","linkResource":"","metadataDate":"2018-11-01T03:23:31.717Z","repository":"Metropolitan Museum of Art, New York, NY","objectURL":"https://www.metmuseum.org/art/collection/search/757155"}
```

なんだかよくわからないものが取ってこれました（笑）

# Pythonで取りに行ってみる
将来的には機械学習のデータに使いたいので、Pythonから取りに行ってみます。

```python
import requests
import json

# The Metropolitan Museum of Art Collection APIへのURL
url = "https://collectionapi.metmuseum.org/public/collection/v1/objects"

def getObjectList(date):
    payload = {
        'metadataDate': date
    }
    try:
        return requests.get(url, params=payload)
    except:
        raise Exception('The Metropolitan Museum of Art Collection APIへのアクセスに失敗しました')

def getObjectData(objectID):
    try:
        return requests.get(url + '/' + str(objectID))
    except:
        raise Exception('The Metropolitan Museum of Art Collection APIへのアクセスに失敗しました')

# 日付を指定して一覧の取得
res = getObjectList('2018-10-31')
list = res.json()
list_count = list['total']
print('list count is ', list_count)

# 一番最後のデータの取得
res = getObjectData(list['objectIDs'][list_count-1])
data = res.json()
print('objectID is', list['objectIDs'][list_count-1])
print('title is "', data['title'], '"')
print(' by', data['artistDisplayName'])
```

実行結果はこんな感じです。

```text
list count is  2602
objectID is 817150
title is " Torquato Tasso at the Ospedale Sant'Anna "
 by Luigi Boscolo
```

※僅かな時間の間に、データが増えています（笑）

なんとか使えそうです。

# まとめ
簡単に大量のデータが取得できました。
さて、何に使いましょうか。