-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslack.R
124 lines (104 loc) · 2.98 KB
/
slack.R
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
library(tidyverse)
library(rvest)
library(httr)
library(glue)
library(dplyr)
out <- "US" %>%
map( ~ {
.x %>%
paste0(c(
"-yesterday",
"-last_7_days",
"-last_30_days",
"-last_90_days"
))
}) %>%
unlist() %>%
# .[str_detect(., "last_90_days")] %>%
# .[100:120] %>%
map_dfr( ~ {
the_assets <-
httr::GET(
paste0(
"https://github.com/favstats/meta_ad_targeting/releases/expanded_assets/",
.x
)
)
the_assets %>% httr::content() %>%
html_elements(".Box-row") %>%
html_text() %>%
tibble(raw = .) %>%
# Split the raw column into separate lines
mutate(raw = strsplit(as.character(raw), "\n")) %>%
# Extract the relevant lines for filename, file size, and timestamp
transmute(
filename = sapply(raw, function(x)
trimws(x[3])),
file_size = sapply(raw, function(x)
trimws(x[6])),
timestamp = sapply(raw, function(x)
trimws(x[7]))
) %>%
filter(filename != "Source code") %>%
mutate(release = .x) %>%
mutate_all(as.character)
})
timelag_data <- out %>%
rename(tag = release,
file_name = filename) %>%
arrange(desc(tag)) %>%
separate(
tag,
into = c("country", "timeframe"),
remove = F,
sep = "-"
) %>%
filter(str_detect(file_name, "parquet")) %>%
mutate(day = str_remove(file_name, "\\.rds|\\.zip|\\.parquet") %>% lubridate::ymd()) %>%
arrange(desc(day)) %>%
group_by(timeframe) %>%
slice(1) %>%
ungroup() %>%
mutate(time_lag = lubridate::today()-day)
post_data_to_slack <- function(data, webhook_url) {
# Format the data into a readable message
formatted_data <- data %>%
mutate(
file_info = glue("{file_name} ({file_size}) - {country}, {timeframe}, uploaded on {day}, lag: {time_lag} days")
) %>%
pull(file_info) %>%
paste(collapse = "\n")
# Create the final message for Slack
message <- glue("Recent File Updates:\n{formatted_data}")
# Title of the message
msg_title <- glue("Meta Targeting Data Lags Behind")
# Send the POST request to Slack webhook
request <- POST(webhook_url,
body = paste(
'{"attachments": [{',
'"pretext": "', msg_title, '",',
'"text": "', message, '", "color": "#36a64f"',
'}]}',
sep = ''
),
encode = "json",
content_type_json()
)
# Check if the message was posted successfully
if (status_code(request) == 200) {
print("Message successfully posted to Slack!")
} else {
print("Failed to post the message to Slack.")
}
}
thebot <- Sys.getenv("slackbot")
if(thebot==""){
thebot <- Sys.getenv("SLACKBOT")
}
the_timelag <- timelag_data %>%
filter(timeframe == "last_30_days") %>%
pull(time_lag) %>%
as.numeric()
if(the_timelag>3){
post_data_to_slack(timelag_data, thebot)
}