~hrbrmstr/urlscan

ref: af7b473a760184beb3065541beec2c18408a1101 urlscan/R/submit.R -rw-r--r-- 1.7 KiB
af7b473aboB Rudis added submit function 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#' Submit a URL for scanning
#'
#' urlscan offers API access to submit URLs for researchers and other institutions.
#' Using the API requires an API key. If you'd like to start submitting URLs via the
#' API, contact them at <mailto:info@@urlscan.io> with a short description on how
#' you're planning to use the API. Submissions via the API can be public or private,
#' though they prefer public submissions so other users can benefit from the results
#' of your scans.\cr
#' \cr
#' The API key should be in the environment variable `URLSCAN_API_KEY` and the
#' easiest way to do that is via the `~/.Renviron` file. You can also manually pass
#' it in as a parameter.
#'
#' @md
#' @param url URL to submit
#' @param public Public or private results? `TRUE` (default) = public.
#' @param custom_agent (character) Override User-Agent for this scan
#' @param referer (character) Override HTTP referer for this scan
#' @return The response to the API call will give you the ID and API endpoint for t
#'         he scan, you can use it to retrieve the result after waiting for a short while.
#' @references <https://urlscan.io/about-api/#submission>
#' @export
urlscan_submit <- function(url, public=TRUE, custom_agent=NULL, referer=NULL) {

   httr::POST(
     url = "https://urlscan.io/api/v1/scan/",
     content_type_json(),
     query = list(
       url = url,
       public = if (public[1]) "on" else NULL,
       customagent = custom_agent,
       referer = referer
     ),
     httr::user_agent("urlscan #rstats package : https://github.com/hrbrmstr/urlscan")
   ) -> res

   httr::stop_for_status(res)

   res <- httr::content(res, as="text")

   res <- jsonlite::fromJSON(res)

   class(res) <- c("urlscan_submit", "list")

   res

}