-
Notifications
You must be signed in to change notification settings - Fork 1
/
gera_entrada_google_trends.R
81 lines (64 loc) · 2.5 KB
/
gera_entrada_google_trends.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
library(tidyverse)
library(lubridate)
library(dotenv)
.HELP <- "
Usage:
Rscript gera_entrada_google_trends.R -p <proposicoes_filepath> -i <interesses_filepath> -a <apelidos_filepath> <config_filepath>
"
#' @title Get arguments from command line option parsing
#' @description Get arguments from command line option parsing
get_args <- function() {
args = commandArgs(trailingOnly=TRUE)
option_list = list(
optparse::make_option(c("-p", "--proposicoes_filepath"),
type="character",
default="data/proposicoes.csv",
help=.HELP,
metavar="character"),
optparse::make_option(c("-a", "--apelidos_filepath"),
type="character",
default="data/apelidos.csv",
help=.HELP,
metavar="character"),
optparse::make_option(c("-f", "--update_flag"),
type="character",
default=1,
help=.HELP,
metavar="character"),
optparse::make_option(c("-e", "--config_filepath"),
type="character",
default=here::here("configuration.env"),
help=.HELP,
metavar="character")
);
opt_parser <- optparse::OptionParser(option_list = option_list)
opt <- optparse::parse_args(opt_parser)
return(opt);
}
get_lotes <- function(df_apelidos){
props_day <- as.numeric(Sys.getenv("PROPOSITIONS_DAY"))
nprops <- nrow(df_apelidos)
nlotes <- ceiling(nprops / props_day)
lotes <- sort(rep(seq(1, nlotes), ceiling(nprops/nlotes))[1:nprops])
return(lotes)
}
## Process args
args <- get_args()
print(args)
proposicoes_filepath <- args$proposicoes_filepath
apelidos_filepath <- args$apelidos_filepath
update_flag <- args$update_flag
config_path <- args$config_filepath
if (update_flag == 1 | !file.exists(apelidos_filepath)) {
cat(paste0("Criando novo arquivo em"), apelidos_filepath, "...\n")
source(here::here("scripts/keywords/generate_keywords.R"))
load_dot_env(config_path)
## Filtra apenas proposições principais
proposicoes <- read_csv(proposicoes_filepath) %>%
filter(!is.na(uri_prop_principal))
df_apelidos <- generate_keywords(proposicoes)
lotes <- get_lotes(df_apelidos)
df_apelidos <- df_apelidos %>% mutate(lote = lotes)
write_csv(df_apelidos, apelidos_filepath)
cat("Feito!\n")
}