@forward-widget/danmu-universe 0.0.14 → 0.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4086,6 +4086,14 @@ class HttpStatusError extends Error {
4086
4086
  });
4087
4087
  }
4088
4088
  }
4089
+ class HttpSchemaError extends Error {
4090
+ constructor(e, t, n){
4091
+ super(`Failed to parse response with schema: ${prettifyError(n)}`), fetch_define_property(this, "context", void 0), fetch_define_property(this, "response", void 0), fetch_define_property(this, "error", void 0), this.context = e, this.response = t, this.error = n, this.name = "HttpSchemaError", console.error(`\u{1F6AB} HTTP Request Failed Failed to parse response with schema: ${prettifyError(n)}: ${e.method} ${e.url}`), console.error("Response:", {
4092
+ headers: t.headers,
4093
+ data: t.data
4094
+ });
4095
+ }
4096
+ }
4089
4097
  class Fetch {
4090
4098
  setCookie(e) {
4091
4099
  this.cookie = merge_merge(this.cookie, e);
@@ -4148,10 +4156,7 @@ class Fetch {
4148
4156
  if (r) return console.debug("fetch cache hit", n), Promise.resolve(r);
4149
4157
  }
4150
4158
  let a = o ? void 0 : n, { timeout: u, schema: l, params: c, ...d } = s, f = t;
4151
- c && (f = `${t}?${url_parse.qs.stringify(c)}`), console.debug("\u2B06\uFE0F fetch", t, null != a ? a : "", {
4152
- ...d,
4153
- params: c
4154
- });
4159
+ c && (f = `${t}?${url_parse.qs.stringify(c)}`), console.debug("\u2B06\uFE0F fetch", f, null != a ? a : "", d);
4155
4160
  let p = o ? Widget.http.get(f, d) : Widget.http.post(f, a, d);
4156
4161
  return u && u > 0 ? Promise.race([
4157
4162
  p,
@@ -4167,6 +4172,11 @@ class Fetch {
4167
4172
  fetch_define_property(this, "cookie", void 0), fetch_define_property(this, "headers", void 0), fetch_define_property(this, "handleResponse", void 0), this.cookie = e, this.headers = t, this.handleResponse = (e, t, n)=>{
4168
4173
  var r;
4169
4174
  if ((null == n || null == (r = n.successStatus) ? void 0 : r.length) && !n.successStatus.includes(e.statusCode)) throw new HttpStatusError(e.statusCode, n.successStatus, t, e);
4175
+ if (null == n ? void 0 : n.schema) {
4176
+ let r = n.schema.safeParse(e.data);
4177
+ if (!r.success) throw new HttpSchemaError(t, e, r.error);
4178
+ e.data = r.data;
4179
+ }
4170
4180
  let i = e.headers["set-cookie"] || e.headers["Set-Cookie"];
4171
4181
  if (i) {
4172
4182
  let e = i.split(",").reduce((e, t)=>{
@@ -4182,13 +4192,6 @@ class Fetch {
4182
4192
  ttl: n.cache.ttl
4183
4193
  });
4184
4194
  }
4185
- if (null == n ? void 0 : n.schema) {
4186
- let r = n.schema.safeParse(e.data);
4187
- return r.success || (console.error(t.url, "Failed to parse response with schema:", prettifyError(r.error)), console.error("Response:", e.data)), {
4188
- ...e,
4189
- data: r.data
4190
- };
4191
- }
4192
4195
  return e;
4193
4196
  };
4194
4197
  }
@@ -4200,7 +4203,7 @@ const BASE_URL = "https://api.imdbapi.dev", fetch = new Fetch(), episodesRequest
4200
4203
  season: coerce_number().optional().default(1),
4201
4204
  pageSize: coerce_number().optional().default(20),
4202
4205
  pageToken: schemas_string().optional()
4203
- }), getEpisodesByImdbId = async (e, t)=>{
4206
+ }), getImdbEpisodes = async (e, t)=>{
4204
4207
  let { season: n, pageSize: r, pageToken: i } = episodesRequestSchema.parse(null != t ? t : {});
4205
4208
  return (await fetch.get(`${BASE_URL}/titles/${e}/episodes`, {
4206
4209
  params: {
@@ -4209,7 +4212,14 @@ const BASE_URL = "https://api.imdbapi.dev", fetch = new Fetch(), episodesRequest
4209
4212
  pageToken: i
4210
4213
  },
4211
4214
  cache: {
4212
- cacheKey: `imdb:episodes:${e}:${n}:${r}:${i}`
4215
+ cacheKey: [
4216
+ "imdb",
4217
+ "episodes",
4218
+ e,
4219
+ n,
4220
+ r,
4221
+ i
4222
+ ].filter(Boolean).join(":")
4213
4223
  },
4214
4224
  successStatus: [
4215
4225
  200
@@ -4224,7 +4234,20 @@ const BASE_URL = "https://api.imdbapi.dev", fetch = new Fetch(), episodesRequest
4224
4234
  nextPageToken: schemas_string().optional()
4225
4235
  })
4226
4236
  })).data;
4227
- }, getExternalIdsByTmdbId = async (e, t)=>{
4237
+ }, getImdbSeasons = async (e)=>(await fetch.get(`${BASE_URL}/titles/${e}/seasons`, {
4238
+ cache: {
4239
+ cacheKey: [
4240
+ "imdb:seasons",
4241
+ e
4242
+ ].join(":")
4243
+ },
4244
+ schema: schemas_object({
4245
+ seasons: schemas_array(schemas_object({
4246
+ season: schemas_string(),
4247
+ episodeCount: schemas_number().optional()
4248
+ }))
4249
+ })
4250
+ })).data, getExternalIdsByTmdbId = async (e, t)=>{
4228
4251
  let n = null, r = `tmdb:${e}:${t}:external_ids`, i = storage.getJson(r);
4229
4252
  return i ? n = i : (n = await Widget.tmdb.get(`/${e}/${t}/external_ids`), storage.setJson(r, n, {
4230
4253
  ttl: TTL_7_DAYS
@@ -4246,9 +4269,19 @@ const BASE_URL = "https://api.imdbapi.dev", fetch = new Fetch(), episodesRequest
4246
4269
  count: schemas_number().optional(),
4247
4270
  name: schemas_string().optional()
4248
4271
  })).optional()
4249
- }), getDoubanInfoByImdbId = async (e)=>{
4250
- var t, n, r, i;
4251
- let o = await douban_fetch.post(`https://api.douban.com/v2/movie/imdb/${e}`, {
4272
+ }), getDoubanInfoByImdbId = async (e, t)=>{
4273
+ var n, r, i, o, s, a;
4274
+ let u = e;
4275
+ if (t && "1" !== t.toString()) {
4276
+ let n = await getImdbSeasons(e);
4277
+ if (!n || parseInt(t.toString()) > n.seasons.length) return null;
4278
+ let r = await getImdbEpisodes(e, {
4279
+ season: t
4280
+ });
4281
+ u = null != (s = null == r || null == (o = r.episodes.find((e)=>1 === e.episodeNumber)) ? void 0 : o.id) ? s : "";
4282
+ }
4283
+ u || (u = e), console.log("Get douban info by imdb id", u);
4284
+ let l = await douban_fetch.post(`https://api.douban.com/v2/movie/imdb/${u}`, {
4252
4285
  apikey: DOUBAN_API_KEY
4253
4286
  }, {
4254
4287
  headers: {
@@ -4256,30 +4289,20 @@ const BASE_URL = "https://api.imdbapi.dev", fetch = new Fetch(), episodesRequest
4256
4289
  },
4257
4290
  schema: doubanImdbResponseSchema,
4258
4291
  cache: {
4259
- cacheKey: `douban:imdb:${e}`,
4292
+ cacheKey: `douban:imdb:${u}`,
4260
4293
  ttl: TTL_7_DAYS
4261
4294
  }
4262
4295
  });
4263
- if (200 !== o.statusCode) throw Error(`Failed to get Douban info: ${o.statusCode}, ${JSON.stringify(o.data)}`);
4264
- let s = null == (r = o.data) || null == (n = r.id) || null == (t = n.split("/")) ? void 0 : t.pop();
4265
- if (!s) throw Error(`Failed to extract Douban ID from response: ${null == (i = o.data) ? void 0 : i.id}`);
4266
- return /\d+/.test(s) ? {
4267
- doubanId: s,
4268
- originResponse: o.data
4296
+ if (200 !== l.statusCode) throw Error(`Failed to get Douban info: ${l.statusCode}, ${JSON.stringify(l.data)}`);
4297
+ let c = null == (i = l.data) || null == (r = i.id) || null == (n = r.split("/")) ? void 0 : n.pop();
4298
+ if (!c) throw Error(`Failed to extract Douban ID from response: ${null == (a = l.data) ? void 0 : a.id}`);
4299
+ return /\d+/.test(c) ? {
4300
+ doubanId: c,
4301
+ originResponse: l.data
4269
4302
  } : null;
4270
4303
  }, getDoubanInfoByTmdbId = async (e, t, n)=>{
4271
4304
  let r = await getExternalIdsByTmdbId(e, t);
4272
- console.log("Get external ids by tmdb id", r);
4273
- let i = r.imdb_id;
4274
- if (!i) return null;
4275
- if (n && 1 !== n) {
4276
- var o, s;
4277
- let e = await getEpisodesByImdbId(i, {
4278
- season: n
4279
- });
4280
- i = null != (s = null == e || null == (o = e.episodes.find((e)=>1 === e.episodeNumber)) ? void 0 : o.id) ? s : "";
4281
- }
4282
- return i ? getDoubanInfoByImdbId(i) : null;
4305
+ return (console.log("Get external ids by tmdb id", r), r.imdb_id) ? getDoubanInfoByImdbId(r.imdb_id, n) : null;
4283
4306
  }, doubanInfoResponseSchema = schemas_object({
4284
4307
  is_tv: schemas_boolean().optional(),
4285
4308
  vendors: schemas_array(schemas_object({
@@ -6163,7 +6186,7 @@ class scrapers_Scraper {
6163
6186
  });
6164
6187
  }
6165
6188
  }
6166
- const widgetVersion = "0.0.14";
6189
+ const widgetVersion = "0.0.15";
6167
6190
  WidgetMetadata = {
6168
6191
  id: "baranwang.danmu.universe",
6169
6192
  title: "\u901A\u7528\u5F39\u5E55",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forward-widget/danmu-universe",
3
- "version": "0.0.14",
3
+ "version": "0.0.15",
4
4
  "type": "module",
5
5
  "main": "./dist/danmu-universe.js",
6
6
  "exports": {