-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpangan-tweet.R
156 lines (126 loc) · 5.23 KB
/
pangan-tweet.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
library(rvest)
# URL from hargapangan.id
# Pasar Tradisional (PT) : https://hargapangan.id/tabel-harga/pasar-tradisional/daerah
# Pasar Modern (PM) : https://hargapangan.id/tabel-harga/pasar-modern/daerah
# Pedagang Besar (PB) : https://hargapangan.id/tabel-harga/pedagang-besar/daerah
# Produsen (PD) : https://hargapangan.id/tabel-harga/produsen/daerah
# Data from Traditional Market
urlPT <- "https://hargapangan.id/tabel-harga/pasar-tradisional/daerah"
tabelPT <- read_html(urlPT)
dataPT <- html_table(tabelPT)
dataPT <- dataPT[[1]]
# Data from Modern Market
urlPM <- "https://hargapangan.id/tabel-harga/pasar-modern/daerah"
tabelPM <- read_html(urlPM)
dataPM <- html_table(tabelPM)
dataPM <- dataPM[[1]]
# Data from Wholesaler
urlPB <- "https://hargapangan.id/tabel-harga/pedagang-besar/daerah"
tabelPB <- read_html(urlPB)
dataPB <- html_table(tabelPB)
dataPB <- dataPB[[1]]
# Data from Producer
urlPD <- "https://hargapangan.id/tabel-harga/produsen/daerah"
tabelPD <- read_html(urlPD)
dataPD <- html_table(tabelPD)
dataPD <- dataPD[[1]]
# Make it Tidy
library(tidyr)
# Traditional Market
rapiPT <- gather(dataPT, "date","price", -'Komoditas (Rp)', -'No.')
rapiPT$type <- rep("Pasar Tradisional", nrow(rapiPT))
# Modern Market
rapiPM <- gather(dataPM, "date","price", -'Komoditas (Rp)', -'No.')
rapiPM$type <- rep("Pasar Modern", nrow(rapiPM))
# Wholesaler
rapiPB <- gather(dataPB, "date","price", -'Komoditas (Rp)', -'No.')
rapiPB$type <- rep("Pedagang Besar", nrow(rapiPB))
# Producer
rapiPD <- gather(dataPD, "date","price", -'Komoditas (Rp)', -'No.')
rapiPD$type <- rep("Produsen", nrow(rapiPD))
# Bind Them All
rapi <-rbind(rapiPT, rapiPM, rapiPB, rapiPD)
colnames(rapi) <- c("no", "commodity", "date", "price","type")
rapi$no <- 1:nrow(rapi)
df <- rapi
# Status Message
## Looking for the Latest Data to Make Status Message
library(dplyr)
data <- df %>%
filter(as.Date(date, "%d/%m/%Y") == max(as.Date(df$date, "%d/%m/%Y")))
baris <- c(1:nrow(data))
terpilih <- sample(baris, 1)
dataSiap <- data %>%
filter(commodity == data$commodity[terpilih]) %>%
mutate(price = formatC(as.numeric(price)*1000, format="d", big.mark=".", decimal.mark=","))
# Hashtag
hashtag <- c("pangan","hargaPanganIndonesia","hargaHarian","pasarTradisional","pasarModern","pedagangBesar","produsen",
"github","rvest","rtweet", "bot", "opensource", "ggplot2", "dplyr", "tidyr", "hargaPangan")
samp_word <- sample(hashtag, 1)
# Build the status message (text and price)
status_details <- paste0(
dataSiap$date[1],": Harga ", dataSiap$commodity[1],
" di :", "\n","\n",
if(!is.na(dataSiap$type[1]) && (dataSiap$price[1] != "NA")) {paste0("⛺ ",dataSiap$type[1], " : Rp",dataSiap$price[1],",-")}, "\n",
if(!is.na(dataSiap$type[2]) && (dataSiap$price[2] != "NA")) {paste0("🏪 ",dataSiap$type[2], " : Rp",dataSiap$price[2],",-")}, "\n",
if(!is.na(dataSiap$type[3]) && (dataSiap$price[3] != "NA")) {paste0("🎪 ",dataSiap$type[3], " : Rp",dataSiap$price[3],",-")}, "\n",
if(!is.na(dataSiap$type[4]) && (dataSiap$price[4] != "NA")) {paste0("👨🏻🌾 ",dataSiap$type[4], " : Rp",dataSiap$price[4],",-")}, "\n",
"\n",
"\n",
"#",samp_word, " #hargaPangan #panganBot")
# Create Time Series Plot
## Data Preparation
dataPlot <- df %>%
group_by(date) %>%
filter(commodity == data$commodity[terpilih]) %>%
mutate(date = as.Date(date, "%d/%m/%Y")) %>%
mutate(price = as.numeric(price)*1000) %>%
na.omit()
## ggplot2
library(ggplot2)
theme_set(theme_light(base_size = 15))
p <- ggplot(dataPlot,aes(x=date,y=price,colour=type,group=type)) +
geom_line(size = 1)+
geom_point(size = 3)+
xlab(dataPlot$commodity[1])+
ylab("Harga")+
scale_y_continuous(labels = function(x) paste0("Rp", x,",-" )) +
theme(legend.title=element_blank(),
legend.position="bottom",
legend.text=element_text(size=12),
axis.title.x = element_text(color="forestgreen", vjust=-0.35),
axis.title.y = element_text(color="forestgreen" , vjust=0.35),
legend.key=element_rect(fill='turquoise'),
legend.background = element_rect(fill = 'turquoise'),
panel.background = element_rect(fill = 'grey95'),
plot.background = element_rect(fill = 'turquoise'),
axis.text.x=element_text(angle=0, hjust=0.5))+
labs(tag = paste0("@panganBot")) +
theme(plot.tag.position = c(0.90, 0.015),
plot.tag = element_text(color="forestgreen", size = 10),
text=element_text(family="mono"))+
scale_x_date(date_labels = "%d-%b")
# Download the image to a temporary location
# save to a temp file
file <- tempfile( fileext = ".jpeg")
ggsave(file, plot = p, device = "png", dpi = 144, width = 8, height = 8, units = "in" )
# Publish to Twitter
library(rtweet)
## Create Twitter token
pangan_token <- rtweet::rtweet_bot(
api_key = Sys.getenv("TWITTER_CONSUMER_API_KEY"),
api_secret = Sys.getenv("TWITTER_CONSUMER_API_SECRET"),
access_token = Sys.getenv("TWITTER_ACCESS_TOKEN"),
access_secret = Sys.getenv("TWITTER_ACCESS_TOKEN_SECRET")
)
# Provide alt-text description
alt_text <- paste0(
"Harga ", dataSiap$commodity[1], ", pada tanggal ", dataSiap$date[1], " di Indonesia."
)
## Post the image to Twitter
rtweet::post_tweet(
status = status_details,
media = file,
media_alt_text = alt_text,
token = pangan_token
)