r-lib / gmailr

Access the Gmail RESTful API from R.
https://gmailr.r-lib.org
Other
229 stars 56 forks source link

Querying for a timeframe with gm_threads() doesn't use local time zone #146

Open EeethB opened 4 years ago

EeethB commented 4 years ago

I have noticed something strange when searching with gmailr for emails sent on a particular date. If I go out to mail.google.com and filter for e.g. "after:2020/8/2 before:2020/8/3", I get emails received on 2020/8/2 in my current time zone, central time (as expected). However, if I run gm_threads("after:2020/8/2 before:2020/8/3") from an R session, it appears to grab something like emails received between 8PM on 8/1 and 8PM on 8/2 central time. So the correct date, but in a time zone 4 hours different. Is there a way to specify time zone with gm_threads()?

EeethB commented 4 years ago

Update: I thought I found a workaround by pulling 2 days at a time, assigning each thread a date using gm_date(), then filtering for the day I actually need:

library(gmailr)
library(tidyverse)
library(lubridate)

date_of_id <- function(id) {
  map(id, ~ gm_thread(.) %>% pluck("messages", 1) %>% gm_date())
}

get_threads <- function(date) {
  threads <- gm_threads(str_glue("from:9207285600 after:{date} before:{date + days(2)}"))
  act_threads <- threads[[1]]$threads
  df_threads <- act_threads %>%
    bind_rows() %>%
    mutate(recd = unlist(date_of_id(id) %>% dmy_hms() %>% as_date())) %>%
    filter(recd == date)
}

dates <- seq(ymd("2020-08-02"), ymd("2020-08-04"), by = "day")

df_threads <- dates %>%
  map(get_threads)

When I run this, df_threads almost contains what I want: A list of tibbles containing messages received on 8/2 or 8/3 (Also some from 8/4, but that's fine). However, a few messages happened to be sent between 11:00 PM and 11:59 PM on 8/3. These messages are being assigned a date/time of between 12:00 AM and 1:00 AM 8/4. So using this method, I've instead reduced the time difference to 1 hour from 4 hours.