2

需要帮助来实施 Apify webhook。完成一项任务需要一些时间。我想添加一个 Apify webhook 它将运行另一个任务但不知道如何做到这一点。

$.ajax({
  url : 'https://api.apify.com/v2/actor-tasks/XXXXXXX/runs?token=XXXXXXXX&waitForFinish=120',  
  method : 'POST',
  contentType: 'application/json',
  dataType: 'json',
  data : JSON.stringify ({
      "queries" : "Outreach link building"
  }),
  success:function(result) {
    console.log(result);
  } 
});

然后 webhook 将调用以下任务:

$.ajax({
   url : `https://api.apify.com/v2/datasets/${datasetId}/items?format=json`,  
   method : 'GET',
   contentType: 'application/json; charset=utf-8',
   success:function(response) {
     console.log(response); // Items from dataset
   } 

 });

顺便说一句,如果我想实现我需要的方式有误,请告诉我你的建议。

4

1 回答 1

1

如果您根本不想在应用程序下面使用服务器,并且想要从客户端(前端)管理所有内容,并且同步运行的 5 分钟最大延迟对您来说太短了,您可以使用轮询。您只需调用 run 端点几次,直到它返回成功状态。

但是,如果您可以waitForFinish,只需执行此操作,然后发送第二个调用以获取数据集

success:function(result) {
    console.log(result);
   // instead of this log, you need to put your second call here and use the `result.data.defaultDatasetId` as ID of the dataset
  } 

如果您必须等待超过 300 秒,则需要使用轮询。但我真的会避免这种情况,或者不使用带有回调的 ajax,而是使用更现代的fetch

$.ajax({
  url : 'https://api.apify.com/v2/actor-tasks/XXXXXXX/runs?token=XXXXXXXX&waitForFinish=120',  
  method : 'POST',
  contentType: 'application/json',
  dataType: 'json',
  data : JSON.stringify ({
      "queries" : "Outreach link building"
  }),
  success:function(result) {
    console.log(result);
    // Here instead of just logging, you get the run object back with its `status`. You also find an `id` there and you can periodically poll the run with this endpoint
https://apify.com/docs/api/v2#/reference/actors/run-object/get-run

  } 
于 2019-09-10T13:13:41.813 回答