|
| 1 | +# Album (Moa) |
| 2 | + |
| 3 | +`MoaService` exposes the LINE Album (Moa) REST API on `client.base.moa`. Unlike |
| 4 | +Talk / Square, Moa speaks JSON over HTTPS through the LEGY proxy, so this |
| 5 | +service uses `client.base.fetch` directly with a channel token issued for the album |
| 6 | +channel (`1375220249`). |
| 7 | + |
| 8 | +## Listing albums |
| 9 | + |
| 10 | +```ts |
| 11 | +import { loginWithAuthToken } from "@evex/linejs"; |
| 12 | + |
| 13 | +const client = await loginWithAuthToken("YOUR_AUTH_TOKEN", { |
| 14 | + device: "IOSIPAD", |
| 15 | +}); |
| 16 | + |
| 17 | +let cursor = ""; |
| 18 | +while (true) { |
| 19 | + const resp = await client.base.moa.getAlbums({ cursor }); |
| 20 | + const result = resp.result; |
| 21 | + for (const album of result?.albums ?? []) { |
| 22 | + console.log(`[${album.albumId}] ${album.title} (${album.photoCount} photos)`); |
| 23 | + } |
| 24 | + const next = result?.nextCursor ?? result?.cursor ?? ""; |
| 25 | + if (!next || next === cursor || !(result?.hasMore ?? true)) break; |
| 26 | + cursor = next; |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +## Listing photos in an album |
| 31 | + |
| 32 | +```ts |
| 33 | +let cursor = ""; |
| 34 | +while (true) { |
| 35 | + const resp = await client.base.moa.getPhotos({ |
| 36 | + chatId: "cxxxxxxxxxxxxxxx", |
| 37 | + albumId: "1234567890123456789", |
| 38 | + cursor, |
| 39 | + pageSize: 100, |
| 40 | + }); |
| 41 | + for (const photo of resp.result?.photos ?? []) { |
| 42 | + console.log(photo.oid, "shot at", photo.shotTime); |
| 43 | + } |
| 44 | + const next = resp.result?.nextCursor ?? ""; |
| 45 | + if (!next || next === cursor) break; |
| 46 | + cursor = next; |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +## Downloading the original bytes of a photo |
| 51 | + |
| 52 | +`downloadPhoto` returns a `Uint8Array` so the caller can save it or process it |
| 53 | +further. |
| 54 | + |
| 55 | +```ts |
| 56 | +import { writeFileSync } from "node:fs"; |
| 57 | + |
| 58 | +const bytes = await client.base.moa.downloadPhoto({ |
| 59 | + chatId: "cxxxxxxxxxxxxxxx", |
| 60 | + albumId: "1234567890123456789", |
| 61 | + oid: "someOid", |
| 62 | +}); |
| 63 | +writeFileSync("./out.jpg", bytes); |
| 64 | +``` |
| 65 | + |
| 66 | +For videos, pass `prefix: "album/v"` (the `sid` field on `obsResourceId` tells |
| 67 | +you whether the item is an image or a video): |
| 68 | + |
| 69 | +```ts |
| 70 | +const isVideo = photo.obsResourceId?.sid === "v"; |
| 71 | +const bytes = await client.base.moa.downloadPhoto({ |
| 72 | + chatId, |
| 73 | + albumId, |
| 74 | + oid: photo.obsResourceId!.oid!, |
| 75 | + prefix: isVideo ? "album/v" : "album/a", |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | +## Notes |
| 80 | + |
| 81 | +- The channel token issued for `1375220249` is memoised inside the service and |
| 82 | + reused within the same session. Changing the access token, MID or endpoint |
| 83 | + invalidates it. If the token is rejected (expiry or revocation), call |
| 84 | + `client.base.moa.clearAlbumChannelToken()` before an explicit retry. Requests |
| 85 | + are not automatically retried. REST requests and downloads use |
| 86 | + `client.base.config.timeout`. |
| 87 | +- Moa uses `X-Line-ChannelToken`, `X-Line-Mid` (your MID) and — for photo |
| 88 | + fetches — `X-Line-Album` (the album id) and `X-Line-Mid` set to the *chat* |
| 89 | + id. All of these are set for you automatically. |
| 90 | +- The header override `accept: application/json` is important: the default |
| 91 | + `client.base.request.getHeader("GET")` returns `application/x-thrift`, which |
| 92 | + LEGY rejects for REST endpoints with |
| 93 | + `{"code":102001,"message":"一時的なエラーが発生しました。"}`. |
0 commit comments