いくつかのエルボーグリースを使用し、必要なボイラプレートのビットを配線して、必要な応答数を反復する必要があります。私たちが必要となります
いくつかのパッケージ:
library(httr)
library(jsonlite)
library(tidyverse)
CPL関数を作成するには、生活が楽になります。
まず、コンテンツを要求するための1:この特定のエンドポイントの
filter_web_content <- function(query, sort = "relevancy",
ts = (Sys.time() - (3 * 24 * 60 * 60)),
order = "asc", size = 100, from = 0,
token = Sys.getenv("WEBHOSE_TOKEN")) {
params <- list(
token = token,
format = "json",
q = query,
sort = sort,
order = order,
size = size,
ts = ts
)
httr::GET(
url = "https://webhose.io/filterWebContent",
query = params
) -> res
httr::stop_for_status(res)
res <- httr::content(res, as = "text", encoding = "UTF-8")
res <- jsonlite::fromJSON(res, flatten = TRUE)
res
}
彼らのREST APIは、さらにいくつかのparamsを取ることができます。私はいくつかの余分なサイクルを得る場合、私はこの全体のREST APIのためのpkgラッパーを拾い上げるでしょう(私はそれを行うとき/ここで過去に戻ってきます)。今
、潜在的に不快なものから素敵な列名を作るために1:
mcga <- function(tbl) {
x <- colnames(tbl)
x <- tolower(x)
x <- gsub("[[:punct:][:space:]]+", "_", x)
x <- gsub("_+", "_", x)
x <- gsub("(^_|_$)", "", x)
x <- make.unique(x, sep = "_")
colnames(tbl) <- x
tbl
}
ここで設定ビットがあります:
- は&コピーを成長させないようにするリストを事前に割り当てます。 「30」は、このAPI
- 、ループを作成し、リストのインデックス変数とAPIの両方がリスト内の次の空のスロットに結果を突き出す位置に
- を開始フェッチインクリメントのための適切なデフォルトである場合、私は考えている
その後、データを使用していくつかのことを行います。
今
PRE_ALLOC_MAX <- 30
results <- vector(mode = "list", length = PRE_ALLOC_MAX)
i <- 1
from <- 0
repeat {
res <- filter_web_content("(China AND United) language:english site_type:news site:bloomberg.com",
ts = 1213456, from = from)
results[[i]] <- res
if (res[["moreResultsAvailable"]] > 0) {
message("Fetching next 100 records...")
i <- i + 1
from <- from + 100
} else {
break
}
}
## Fetching next 100 records...
## Fetching next 100 records...
:
- 任意の
NULL
(未実装)を削除
- エントリ我々は
- を必要とするデータとAPIの結果の一部を抽出し、データフレームに
- それを包みます列名を再作成する
あなたは、このようにそれをする必要はありませんが、私はそれは長い目で見れば、より読みやすいと思う:
discard(results, is.null) %>%
map_df(~{ .x$posts}) %>%
tbl_df() %>%
mcga()
## # A tibble: 227 x 42
## uuid
## <chr>
## 1 ea6f6084be16a50b0d4791ffa268956ca691c16d
## 2 bd0ac60981ac73e2a7e71378881272eb5b6147d7
## 3 3f2c2c13aa2b3c6d5fc8300f3a9876d9c86c08d1
## 4 659d73d3ddba3c0a0505da8fc15862bc33ac9519
## 5 371293cf38efe9c9a4708403c816c8b33eeb1298
## 6 38a3522fe1d268519aa0e2c3c865bbee19f9ee65
## 7 a4b1f0e4a8d94354ae41c80bebe56237b5a39ca8
## 8 323660c1c21662a1e5b147455f7a4c70f60e12b8
## 9 3233102dbbed6bd90c19ddb2cf7df9111de6ffcf
## 10 c4f126943968be899a6c5fdd806274f0ca848714
## # ... with 217 more rows, and 41 more variables: url <chr>, ord_in_thread <int>,
## # author <chr>, published <chr>, title <chr>, text <chr>, highlighttext <chr>,
## # highlighttitle <chr>, language <chr>, external_links <list>, rating <lgl>,
## # crawled <chr>, thread_uuid <chr>, thread_url <chr>, thread_site_full <chr>,
## # thread_site <chr>, thread_site_section <chr>, thread_site_categories <list>,
## # thread_section_title <chr>, thread_title <chr>, thread_title_full <chr>,
## # thread_published <chr>, thread_replies_count <int>,
## # thread_participants_count <int>, thread_site_type <chr>, thread_country <chr>,
## # thread_spam_score <dbl>, thread_main_image <chr>,
## # thread_performance_score <int>, thread_domain_rank <int>,
## # thread_social_facebook_likes <int>, thread_social_facebook_comments <int>,
## # thread_social_facebook_shares <int>, thread_social_gplus_shares <int>,
## # thread_social_pinterest_shares <int>, thread_social_linkedin_shares <int>,
## # thread_social_stumbledupon_shares <int>, thread_social_vk_shares <int>,
## # entities_persons <list>, entities_organizations <list>,
## # entities_locations <list>
は、APIパッケージのrOpenSciのgithubのソースの周り突っつい考えてみましょう。それらのうちのいくつかは、このタイプのAPI反復的なことを行うための似たようなイディオムを持っています。
UPDATE
あなたは今、このためhttps://github.com/hrbrmstr/webhoseで2つの機能を使用することができます。それはCRANになるまであなたはそれをインストールするには、この操作を行う必要があります:
devtools::install_github("hrbrmstr/webhose")
自分で改ページを扱うか、単に結果の単一セットに対してクエリを実行する場合は、
res <- webhose::filter_web_content("(China AND United) language:english site_type:news site:bloomberg.com", ts = 1213456)
を行います自動APIページネーションが必要な場合:
res <- webhose::fetchall_web_content("(China AND United) language:english site_type:news site:bloomberg.com", ts = 1213456)
私はAPIの残りの部分をカバーする前に、しばらくお待ちください。