【问题标题】:Rate limiting by count per minute with httr使用 httr 按每分钟计数限制速率
【发布时间】:2021-04-01 10:30:19
【问题描述】:

我正在使用 Discogs API,试图为我的收藏提取社区信息(拥有和想要、最低价格等)。

不幸的是,它的速率限制为每分钟 25 次,我无法找到一种方法将该限制应用到我当前的代码中(见下文)。 我可以使用 sys.sleep(),但我不确定它在代码中的位置。

communityData <- lapply(as.list(collection$release_id), function(obj){
  url <- httr::GET(paste0("https://api.discogs.com/releases/", obj))
  url <- rjson::fromJSON(rawToChar(url$content))
  data.frame(release_id = obj, 
             label = url$label[[1]]$name %||% NA,
             year = url$year %||% NA, 
             title = url$title %||% NA, 
             artist_name = url$artist[[1]]$name %||% NA, 
             styles = url$styles[[1]] %||% NA,
             genre = url$genre[[1]] %||% NA,
             average_note = url$community$rating$average %||% NA, 
             votes = url$community$rating$count %||% NA, 
             want = url$community$want %||% NA, 
             have = url$community$have %||% NA, 
             lowest_price = url$lowest_price %||% NA, 
             country = url$country %||% NA)
}) %>% do.call(rbind, .) %>% 
  unique()

如有任何帮助,我们将不胜感激!

【问题讨论】:

    标签: r api httr


    【解决方案1】:

    在返回值之前直接插入 sleep 命令应该可以正常工作。这将为您提供查询 url、提取信息、休眠、返回值、重复的模式。 Aka,像这样编辑上面的代码块:

    communityData <- lapply(as.list(collection$release_id), function(obj){
      url <- httr::GET(paste0("https://api.discogs.com/releases/", obj))
      url <- rjson::fromJSON(rawToChar(url$content))
    
      # 1 minute for 25 requests -- ~ 2.4 seconds of sleep between each request
      Sys.sleep(2.4)
      
      data.frame(release_id = obj, 
                 label = url$label[[1]]$name %||% NA,
                 year = url$year %||% NA, 
                 title = url$title %||% NA, 
                 artist_name = url$artist[[1]]$name %||% NA, 
                 styles = url$styles[[1]] %||% NA,
                 genre = url$genre[[1]] %||% NA,
                 average_note = url$community$rating$average %||% NA, 
                 votes = url$community$rating$count %||% NA, 
                 want = url$community$want %||% NA, 
                 have = url$community$have %||% NA, 
                 lowest_price = url$lowest_price %||% NA, 
                 country = url$country %||% NA)
    }) %>% do.call(rbind, .) %>% 
      unique()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-30
      • 1970-01-01
      • 1970-01-01
      • 2016-10-17
      • 2021-08-03
      • 2018-12-07
      • 2019-11-14
      • 1970-01-01
      相关资源
      最近更新 更多