我有一堆下载的pdf文件。现在,我想从文件的元数据中提取下载URL。如何以编程方式执行此操作?我更喜欢R中的解决方案,并且正在MacOS Mojave上工作。

如果要复制,可以 [use this file]

r - 从下载的文件中提取源元数据-LMLPHP

最佳答案

尽管您可以通过使用R来以编程方式下载PDF来避免此需求,但我们可以使用xattrs包来获取您想要的数据:

library(xattrs) # https://gitlab.com/hrbrmstr/xattrs (not on CRAN)


让我们看看该文件有哪些扩展属性:

xattrs::list_xattrs("~/Downloads/0.-miljoenennota.pdf")
## [1] "com.apple.metadata:kMDItemWhereFroms"
## [2] "com.apple.quarantine"


com.apple.metadata:kMDItemWhereFroms看起来是一个不错的目标:

xattrs::get_xattr(
  path = "~/Downloads/forso/0.-miljoenennota.pdf",
  name = "com.apple.metadata:kMDItemWhereFroms"
) -> from_where

from_where
## [1] "bplist00\xa2\001\002_\020}https://www.rijksoverheid.nl/binaries/rijksoverheid/documenten/begrotingen/2016/09/20/miljoenennota-2017/0.-miljoenennota.pdfP\b\v\x8b"


但是,它采用二进制plist格式(是Apple #sigh)。但是,由于这是“事情”,所以xattrs包具有read_bplist()函数,但是我们必须使用get_xattr_raw()来使用它:

xattrs::read_bplist(
  xattrs::get_xattr_raw(
    path = "~/Downloads/forso/0.-miljoenennota.pdf",
    name = "com.apple.metadata:kMDItemWhereFroms"
  )
) -> from_where

str(from_where)
## List of 1
##  $ plist:List of 1
##   ..$ array:List of 2
##   .. ..$ string:List of 1
##   .. .. ..$ : chr "https://www.rijksoverheid.nl/binaries/rijksoverheid/documenten/begrotingen/2016/09/20/miljoenennota-2017/0.-miljoenennota.pdf"
##   .. ..$ string: list()
##   ..- attr(*, "version")= chr "1.0"


丑陋的嵌套列表是真正愚蠢的二进制plist文件格式的错,但是源URL在其中。

我们可以使用lapply来获得所有这些文件(为此,我将一堆随机交互式下载的PDF丢到一个目录中)。还有一个in this blog post的示例,但是它使用reticulate和Python包来读取二进制plist数据,而不是使用内置的包函数来执行此操作(所说的内置包函数是macOS 实用程序或linux plutil实用程序;如果Windows用户想使用该功能,则可以切换到实际的操作系统)。

fils <- list.files("~/Downloads/forso", pattern = "\\.pdf", full.names = TRUE)

do.call(
  rbind.data.frame,
  lapply(fils, function(.x) {

    xattrs::read_bplist(
      xattrs::get_xattr_raw(
        path = .x,
        name = "com.apple.metadata:kMDItemWhereFroms"
      )
    ) -> tmp

    from_where <- if (length(tmp$plist$array$string) > 0) {
      tmp$plist$array$string[[1]]
    } else {
      NA_character_
    }

    data.frame(
      fil = basename(.x),
      url = from_where,
      stringsAsFactors=FALSE
    )

  })
) -> files_with_meta

str(files_with_meta)
## 'data.frame': 9 obs. of  2 variables:
##  $ fil: chr  "0.-miljoenennota.pdf" "19180242-D02E-47AC-BDB3-73C22D6E1FDB.pdf" "Codebook.pdf" "Elementary-Lunch-Menu.pdf" ...
##  $ url: chr  "https://www.rijksoverheid.nl/binaries/rijksoverheid/documenten/begrotingen/2016/09/20/miljoenennota-2017/0.-miljoenennota.pdf" "http://eprint.ncl.ac.uk/file_store/production/230123/19180242-D02E-47AC-BDB3-73C22D6E1FDB.pdf" "http://apps.start.umd.edu/gtd/downloads/dataset/Codebook.pdf" "http://www.msad60.org/wp-content/uploads/2017/01/Elementary-February-Lunch-Menu.pdf" ...


注意:IRL,您可能应该在示例plistutil中进行更多的防弹。

07-24 09:52
查看更多