【问题标题】:How to use Airbnb's iCalendar Link Programmatically如何以编程方式使用 Airbnb 的 iCalendar 链接
【发布时间】:2021-05-10 12:46:03
【问题描述】:

我获得了 Airbnb 房源的 iCalendar 链接。 当我使用任何浏览器访问该链接时,浏览器会自动下载 .ics iCalendar 文件。 我正在尝试编写一个应用程序,该应用程序将与该特定 Airbnb 列表的 iCalendar 同步。 我想我应该只是fetch iCalendar 链接,我可以读取 .ics 文件的内容并解析它并处理信息。

但是,当我尝试在同一个 Airbnb 日历链接上使用 isomorphic-fetch 时,我收到以下响应:

Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]: {
    body: PassThrough {
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 5,
      _maxListeners: undefined,
      _writableState: [WritableState],
      allowHalfOpen: true,
      [Symbol(kCapture)]: false,
      [Symbol(kTransformState)]: [Object]
    },
    disturbed: false,
    error: null
  },
  [Symbol(Response internals)]: {
    url: 'https://www.airbnb.com/calendar/ical/22342432.ics?s=56501a678175afddd0ef3874b7a1b28b',
    status: 200,
    statusText: 'OK',
    headers: Headers { [Symbol(map)]: [Object: null prototype] },
    counter: 0
  }
}

FetchError: invalid json response body at https://www.airbnb.com/calendar/ical/22342432.ics?s=56501a678175afddd0ef3874b7a1b28b reason: Unexpected token B in JSON at position 0

理想情况下,我想从 NODE.js (Next.js) 查询该链接,而不是下载 .ics 文件,我希望能够以 JSON 格式解析 .ics 的信息,以便处理数据并找出哪些日期可用,哪些日期不适用于该列表......

我该怎么做?

顺便说一句,我使用isomorphic-fetch npm 库来进行获取...例如

const response = await fetch(`https://www.airbnb.com/calendar/ical/22342432.ics?s=56501a678175afddd0ef3874b7a1b28b`);
        

        console.log('response');
        console.log(response);

        console.log('response.body', response.body);
        

        stories = await response.json();

        console.log('stories');
        console.log(stories);

        res.status(200).json(stories)

    } catch(err) {
        console.log('error')
        console.log(err)
        stories = [];

    }

【问题讨论】:

  • “以 JSON 格式解析 .ICS 的信息”没有任何意义,这是两种完全不同的文本格式。您的意思是“使用 JavaScript 解析 .ICS 文件的信息吗?”
  • @IMSoP 是的,从 .ICS 文件中解析信息也对我有用。我怎么做?当我 fetch('URL of .ics file") 我没有得到 .ics 文件的内容......我得到的是我上面显示的......我如何从 URL 中获取但是能够读取 .ics 文件的内容而不是其他内容?
  • 好吧,对于初学者来说,不要尝试将其解析为 JSON。我不知道有问题的库,但如果您正在查看的文本不是 JSON 格式,response.json() 不是正确的函数。您的第一步是从 URL 获取内容作为文本;库中可能有不同的功能可以做到这一点。然后,您可以找到(并完全单独测试)一个从 iCal 格式的字符串中获取数据的函数。

标签: javascript node.js next.js icalendar


【解决方案1】:

找到答案.... 使用 npm 包node-ical

const ical = require('node-ical');
    
    ical.fromURL(url, options, function(err, data) {
        if (err) console.log(err);
        console.log(data);
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-05
    • 2011-07-15
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    • 2018-06-14
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多