R:正确编写和转换 UTC 格式的日期

问题描述 投票:0回答:1

我正在尝试学习如何使用 Reddit API (https://www.reddit.com/prefs/apps)。

我注册了一个帐户/API - 我想检索在 March-01-2020 和 March-02-2020 之间发布的 100 条包含“covid”一词的评论。

使用 API 中的文档 (https://www.reddit.com/dev/api/),我运行了以下 R 代码。

首先,我注册了API:

library(httr)
library(jsonlite)

response <- POST("https://www.reddit.com/api/v1/access_token",
                 authenticate('****', '***'),
                 user_agent("some_name"),
                 body = list(grant_type="password", 
                             username="aaaaaa", 
                             password="bbbbbbb"))

access_token_json <- rawToChar(response$content)
access_token_content <- fromJSON(access_token_json)
access_token <- access_token_content$access_token
access_token

url <- "https://oauth.reddit.com/LISTING" # try api/v1/me

authorization_bearer <- paste("Bearer ", access_token, sep="")
result <- GET(url, 
              user_agent("some_name"), 
              add_headers(Authorization = authorization_bearer))

接下来,我尝试提出一个要求:

# Set start and end times
start_time <- as.numeric(as.POSIXct("2020-03-01 16:52:00", tz = "UTC"))
end_time <- as.numeric(as.POSIXct("2020-03-02 13:52:00", tz = "UTC"))

# Set query parameters 
query_params <- list(
  limit = 100,
  q = "covid",
  #subreddit = "news", # 
  #author = "some_author", #
  after = start_time,
  before = end_time
)

# Make the API request 
response <- GET(url, query = query_params, add_headers(Authorization = authorization_header, `User-Agent` = user_agent_string))

# Extract the response 
response_json <- rawToChar(response$content)
response_content <- fromJSON(response_json)

# Extract the relevant fields 
final_result <- data.frame(
  title = response_content$data$children$data$title,
  subreddit = response_content$data$children$data$subreddit,
  author = response_content$data$children$data$author,
  created_utc = as.POSIXct(response_content$data$children$data$created_utc, origin = "1970-01-01", tz = "UTC"),
  permalink = paste0("https://www.reddit.com", response_content$data$children$data$permalink)
)

当我看结果时:

> head(final_result)
                                                                                   title          subreddit            author         created_utc
1 COVID-19 is a leading cause of death in children and young people in the United States            science thebelsnickle1991 2023-01-30 16:27:21
2                I thought Covid was a thing of the past.. double vaccinated wicked sick  mildlyinfuriating    Northeast4life 2023-02-19 00:52:03
3          ‘People aren’t taking this seriously’: experts say US Covid surge is big risk               news           ttkciar 2023-01-15 17:25:12

正如我们在这里看到的,日期不在指定日期之间。

我认为问题可能是我没有正确转换日期,所以我查看了原始 UTC 日期:

> final_result$created_utc
  [1] 1675096041 1676767923 1673803512 1671022066 1674220458

但这些日期似乎与我想要的2020年3月的日期不符(https://www.epochconverter.com/)。

我是不是写错了日期 - 有人可以告诉我如何解决这个问题吗?

谢谢!

r json api date utc
1个回答
1
投票

尝试使用

as.character()
函数将开始和结束字符串转换为它们的 Unix 时间戳:

# Set start and end times
start_time <- as.character(as.POSIXct("2020-03-01 00:00:00", tz = "UTC"))
end_time <- as.character(as.POSIXct("2020-03-02 23:59:59", tz = "UTC"))
© www.soinside.com 2019 - 2024. All rights reserved.