html-parser.R 26 KB
Newer Older
1 2 3 4 5
# Script to count how many titles per hour that have certain words in home pages of newspapers.
# It generates some plots to view the data
# Ask @numeroteca or info@montera34.com for questions, suggestions and collaborations

# Load libraries -----
6 7
library(rvest)
library(stringr)
8
# library(R.utils) # opens gzip compresed file
9 10 11
library(gsubfn) # select text in the parenthesis with regex
library(tidyverse) # for ggplot

12 13 14 15 16 17 18 19 20
# Set search variables: words and datelimits -----
# word <- "Cifuentes|Javier Ramos|Enrique Álvarez Conde|Pablo Chico|María Teresa Feito|Alicia López de los Cobos|Cecilia Rosado|Clara Souto|Amalia Calonge|Universidad Rey Juan Carlos"
word <- "vox|Vox|VOX|Santiago Abascal|ortega smith|francisco serrano"
# Select word to be displayed in plots
word_explain <- "VOX" #

# Set time limits
my_limit <- c(as.POSIXct("2018-10-01 00:00:01"), as.POSIXct("2019-01-18 00:00:01"))
my_init <- as.POSIXlt("2018-10-10 00:00:00")
21 22

# open compressed file
23 24
# gunzip("eldiario/http!www.eldiario.es!!!!@2018-04-06T23:01:07.888847+00:00.gz",remove=FALSE)
# no hace falta, read_html lee el .gz sin necesidad de descomprimir
25

26
# --------- Create data frame with all the newspapers + time and date---------------
27

28 29 30 31
# Read list of files. The list has been generated with this bash script, 
  # when you are located in the directory with all the .gz files: 
  # for f in *.gz; do echo "$f" >> mylist.txt; done
# CHANGE THIS: write path where your mylist.file is located
32
list <- read.delim("data/mylist.txt")
33 34
# add name to the one column file
names(list) <- "urls"
35 36

# extract name of newspaper
37 38
list$urls  <-  sub("file './", "", list$urls )
list$urls <-  sub("'", "", list$urls)
39
list$newspaper <- strapplyc( as.character(list$urls), "[a-z]*!([a-z]{1,61}.[a-zA-Z]{2,})", simplify = TRUE)
40

41 42 43 44 45 46 47 48 49 50 51
# list$newspaper <- as.factor(list$newspaper) No funciona convertirlo a factor
list$newspaper <-  sub("www.", "", list$newspaper)

# extract year,month, day, hour
list$year <- as.numeric(strapplyc( as.character(list$urls), ".*@([0-9]*)", simplify = TRUE))
list$month <- as.numeric(strapplyc( as.character(list$urls), ".*@[0-9]*-([0-9]*)", simplify = TRUE))
list$day <- as.numeric(strapplyc( as.character(list$urls), ".*@[0-9]*-[0-9]*-([0-9]*)", simplify = TRUE))
list$hour <- as.numeric(strapplyc( as.character(list$urls), ".*@[0-9]*-[0-9]*-[0-9]*T([0-9]*)", simplify = TRUE))

# create date
list$date <- as.Date( paste(list$day,"/",list$month,"/",list$year,sep = "" ), "%d/%m/%Y")
52
list$timestamp <- as.POSIXlt( paste(list$year,"-",list$month,"-",list$day," ",list$hour,":00:00", sep = "" ))
53

54
# Save created list of front pages 
55
save(list,file="data/list.Rda")
56 57

# You can avoid all of the above and just load the existing file
58 59
load("data/list.Rda")

60 61 62 63 64 65
# Check visually if files exists
ggplot(list[list$newspaper == "larazon", ]) +
  geom_point(aes(x=date,y=month), alpha = 0.005)

# Process home pages ---------------------------

66 67
# Create list of selected pages. Select timeframe, newspapers
selected <- list[(list$newspaper == "eldiario" | list$newspaper == "elconfidencial" | 
68
                    list$newspaper == "elpais"| list$newspaper == "larazon" | list$newspaper == "elespanol") &
69
                   list$date > "2018-10-01", ]
70

71
# Create results dataframe
72 73 74 75
results <- ""
results <- data.frame(matrix(ncol = 1,nrow = nrow(selected)  ))
names(results)  <- c("newspaper")

76
# Loop to count how many titles per homepages have certain words
77
for (i in 1:nrow(selected)) {
78
  page <- read_html( paste("../storytracker/data/",selected$urls[i], sep = "") )
79
  
80
  if ( selected$newspaper[i] == "eldiario" | selected$newspaper[i] == "elpais" | selected$newspaper[i] == "larazon") {
81 82 83
    # eldiario
    # gets all the text in article titles. All articles are in h2 except the comics.
    titles <- page %>% html_nodes("article h2 a") %>% html_text() %>% data.frame()
84
  } else if ( selected$newspaper[i] == "elconfidencial" | selected$newspaper[i] == "elmundo" | selected$newspaper[i] == "elespanol") {
85 86
    titles <- page %>% html_nodes("article h3 a") %>% html_text() %>% data.frame()
  }
87
  colnames(titles) <- "title"
88
  # titles$title <- as.character(titles$title)
89 90
  
  # total of articles with link
91
  n_news <- nrow(titles)
92
  print(paste("nº noticias:",n_news))
93 94 95 96 97 98 99
  
  # select news that contain cerating word
  selected_news <- data.frame(titles[grepl(word, titles$title),])
  
  # Results
  # number of articles that contain words
  n_selected_news<- nrow(selected_news)
100
  print(paste("nº noticias con las palabras:",n_selected_news))
101
  # print(selected_news)
102
  # Percentage of articles that contain words
103
  percent <- round(n_selected_news / n_news * 100, digits = 2)
104 105 106 107 108 109 110 111 112 113 114 115
  
  results$newspaper[i] <- selected$newspaper[i]
  # results$date[i] <- paste(selected$day[i],"/",selected$month[i],"/",selected$year[i],sep = "" )
  # results$date2[i] <- as.Date(selected$date[i])
  results$day[i] <- selected$day[i]
  results$month[i] <- selected$month[i]
  results$year[i] <- selected$year[i]
  results$hour[i] <- selected$hour[i]
  results$n_news[i] <- n_news
  results$n_selected_news[i] <- as.integer(n_selected_news)
  results$percent[i] <- percent
  
116
  print(paste("year:",selected$year[i], "month:",selected$month[i], "day:",selected$day[i],"hour:",selected$hour[i],selected$newspaper[i]))
117 118 119 120 121
}

# creates time stampt
results$date <- as.POSIXlt( paste(results$year,"-",results$month,"-",results$day," ",results$hour,":00:00", sep = "" ))

122 123 124 125 126 127
# Save results 
save(results,file="data/results-vox-01.Rda")
# Load other results
load("data/results-cifuentes-01.Rda")

# -----------Plot restults------------
128 129

# Plot para un único periódico
130 131 132 133
ggplot(data=results[results$newspaper=="eldiario",]) + ylim(c(0,100)) +
  geom_line(aes(x=date, y=n_news),color="#000000") +
  # geom_line(aes(x=date, y=n_selected_news),color="#FF0000") +
  # geom_line(aes(x=date, y=percent),color="#0000DD") +
134 135 136 137 138 139 140
  labs(title = paste("eldiario.es: noticias en portada.",sep = ""))

ggplot(data=results[results$newspaper=="eldiario",]) + ylim(c(0,100)) +
  geom_line(aes(x=date, y=n_selected_news),color="#000000") +
  # geom_line(aes(x=date, y=n_selected_news),color="#FF0000") +
  # geom_line(aes(x=date, y=percent),color="#0000DD") +
  labs(title = paste("eldiario.es: noticias sobre ",word_explain,"Cifuentes en portada (total, total selected, %)",sep = ""))
141 142 143 144 145 146

ggplot(data=results[results$newspaper=="eldiario",]) + ylim(c(0,100)) +
  geom_line(aes(x=date, y=n_news),color="#000000") +
  # geom_line(aes(x=date, y=n_selected_news),color="#FF0000") +
  # geom_line(aes(x=date, y=percent),color="#0000DD") +
  labs(title = "eldiario.es: nº noticias en portada") +
147
  scale_x_datetime(date_breaks = "1 day", date_labels = "%d", limits = my_limit) 
148

149
ggplot(data=results[results$newspaper=="eldiario",]) + ylim(c(0,100)) +
150 151
  geom_line(aes(x=date, y=n_news),color="#000000") +
  geom_line(aes(x=date, y=n_selected_news),color="#FF0000") +
152
  # geom_line(aes(x=date, y=percent),color="#0000DD") +
153
  scale_x_datetime(date_breaks = "1 day", date_labels = "%d", limits = my_limit)  +
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
  theme_minimal(base_family = "Roboto Condensed", base_size = 14) +
  geom_text(aes(x = as.POSIXlt("2018-03-25 00:00:00"), 
                y = 19, label = "nº noticias sobre Cifuentes"), family = "Roboto Condensed", 
            color = "#FF0000", alpha=0.6, hjust = 0) +
  geom_text(aes(x = as.POSIXlt("2018-03-25 00:00:00"), 
                y = 45, label = "nº noticias en portada", family = "Roboto Condensed"), 
            color = "#000000", alpha=0.6, hjust = 0) +
  labs(title = "eldiario.es: nº noticias en portada - noticias sobre Cifuentes",
       subtitle = "21 marzo - 9 abril 2018. numeroteca.org",
       x = NULL,
       y = NULL,
       caption = "")

ggplot(data=results[results$newspaper=="eldiario",]) + ylim(c(0,30)) +
  # geom_line(aes(x=date, y=n_news),color="#000000") +
  geom_line(aes(x=date, y=n_selected_news),color="#FF0000") +
  geom_line(aes(x=date, y=percent),color="#0000DD") +
  scale_x_datetime(date_breaks = "1 day", date_labels = "%d", 
172
                   limits = my_limit) +
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
  theme_minimal(base_family = "Roboto Condensed", base_size = 14) +
  geom_text(aes(x = as.POSIXlt("2018-03-27 00:00:00"), 
                y = 1, label = "nº noticias sobre Cifuentes"), family = "Roboto Condensed", 
            color = "#FF0000", alpha=0.6, hjust = 0) +
  geom_text(aes(x = as.POSIXlt("2018-03-27 00:00:00"), 
                y = 15, label = "Porcentaje de noticias en portada", family = "Roboto Condensed"), 
            color = "#0000DD", alpha=0.6, hjust = 0) +
  labs(title = "eldiario.es: porcentaje noticias y nº noticias en portada sobre Cifuentes",
       subtitle = "21 marzo - 9 abril 2018. Datos: numeroteca.org",
       x = NULL,
       y = NULL,
       caption = "")


ggplot(data=results[results$newspaper=="elconfidencial",]) + ylim(c(0,10)) +
  # geom_line(aes(x=date, y=n_news),color="#000000") +
  geom_line(aes(x=date, y=n_selected_news),color="#FF0000") +
  geom_line(aes(x=date, y=percent),color="#0000DD") +
  scale_x_datetime(date_breaks = "1 day", date_labels = "%d", 
192
                   limits = my_limit) +
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
  theme_minimal(base_family = "Roboto Condensed", base_size = 14) +
  geom_text(aes(x = as.POSIXlt("2018-03-27 00:00:00"), 
                y = 5, label = "nº noticias sobre Cifuentes"), family = "Roboto Condensed", 
            color = "#FF0000", alpha=0.6, hjust = 0) +
  geom_text(aes(x = as.POSIXlt("2018-03-27 00:00:00"), 
                y = 3, label = "Porcentaje de noticias en portada", family = "Roboto Condensed"), 
            color = "#0000DD", alpha=0.6, hjust = 0) +
  labs(title = "elconfidencial.es: porcentaje noticias y nº noticias en portada sobre Cifuentes",
       subtitle = "21 marzo - 9 abril 2018. Datos: numeroteca.org",
       x = "Días",
       y = NULL,
       caption = "")


ggplot(data=results[results$newspaper=="elconfidencial",]) + ylim(c(0,10)) +
  # geom_line(aes(x=date, y=n_news),color="#000000") +
  geom_line(aes(x=date, y=n_selected_news),color="#FF0000") +
210
  geom_line(aes(x=date, y=percent),color="#0000DD") +
211
  scale_x_datetime(date_breaks = "1 day", date_labels = "%d", 
212
                   limits = my_limit) +
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
  theme_minimal(base_family = "Roboto Condensed", base_size = 14) +
  geom_text(aes(x = as.POSIXlt("2018-03-27 00:00:00"), 
                y = 5, label = "nº noticias sobre Cifuentes"), family = "Roboto Condensed", 
            color = "#FF0000", alpha=0.6, hjust = 0) +
  geom_text(aes(x = as.POSIXlt("2018-03-27 00:00:00"), 
                y = 3, label = "Porcentaje de noticias en portada", family = "Roboto Condensed"), 
            color = "#0000DD", alpha=0.6, hjust = 0) +
  labs(title = "elconfidencial.es: porcentaje noticias y nº noticias en portada sobre Cifuentes",
       subtitle = "21 marzo - 9 abril 2018. Datos: numeroteca.org",
       x = "Días",
       y = NULL,
       caption = "")

ggplot(data=results[results$newspaper=="elconfidencial",]) + ylim(c(0,120)) +
  geom_line(aes(x=date, y=n_news),color="#000000") +
  geom_line(aes(x=date, y=n_selected_news),color="#FF0000") +
  # geom_line(aes(x=date, y=percent),color="#0000DD") +
230
  scale_x_datetime(date_breaks = "1 day", date_labels = "%d", limits = my_limit) +
231 232 233 234 235 236 237 238 239 240 241 242
  theme_minimal(base_family = "Roboto Condensed", base_size = 14) +
  geom_text(aes(x = as.POSIXlt("2018-03-25 00:00:00"), 
                y = 19, label = "nº noticias sobre Cifuentes"), family = "Roboto Condensed", 
            color = "#FF0000", alpha=0.6, hjust = 0) +
  geom_text(aes(x = as.POSIXlt("2018-03-25 00:00:00"), 
                y = 45, label = "nº noticias en portada", family = "Roboto Condensed"), 
            color = "#000000", alpha=0.6, hjust = 0) +
  labs(title = "elconfidencial.es: nº noticias en portada - noticias sobre Cifuentes",
       subtitle = "21 marzo - 9 abril 2018. Datos: numeroteca.org",
       x = NULL,
       y = NULL,
       caption = "")
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

# Plot para un único periódico
ggplot(data=results[results$newspaper=="elconfidencial",]) + ylim(c(0,130)) +
  geom_line(aes(x=date, y=n_news),color="#000000") +
  geom_line(aes(x=date, y=n_selected_news),color="#FF0000") +
  geom_line(aes(x=date, y=percent),color="#0000DD") +
  labs(title = "elconfidencial: noticias sobre Cifuentes en portada (total, total selected, %)")

# Plot para un único periódico
ggplot(data=results[results$newspaper=="elpais",]) + ylim(c(0,130)) +
  geom_line(aes(x=date, y=n_news),color="#000000") +
  geom_line(aes(x=date, y=n_selected_news),color="#FF0000") +
  geom_line(aes(x=date, y=percent),color="#0000DD") +
  labs(title = "elPais: noticias sobre Cifuentes en portada (total, total selected, %)")

# Plot para un único periódico
ggplot(data=results[results$newspaper=="larazon",]) + ylim(c(0,130)) +
260
  geom_line(aes(x=date, y=n_news),color="#000000",size=0.1) +
261 262 263 264
  geom_line(aes(x=date, y=n_selected_news),color="#FF0000") +
  geom_line(aes(x=date, y=percent),color="#0000DD") +
  labs(title = "laRazon: noticias sobre Cifuentes en portada (total, total selected, %)")

265 266 267 268 269 270
# Plot para un único periódico
ggplot(data=results[results$newspaper=="elespanol",]) + ylim(c(0,140)) +
  geom_line(aes(x=date, y=n_news),color="#000000") +
  geom_line(aes(x=date, y=n_selected_news),color="#FF0000") +
  geom_line(aes(x=date, y=percent),color="#0000DD") +
  labs(title = "elespanol: noticias sobre Cifuentes en portada (total, total selected, %)")
271 272

# Plot para varios periódico
273 274
ggplot(data=results ) + ylim(c(0,140)) +
  geom_line(aes(x=date, y=n_news, group=newspaper),color="#000000",size=0.2) +
275 276 277 278 279 280 281 282
  geom_line(aes(x=date, y=n_selected_news, group=newspaper),color="#FF0000") +
  geom_line(aes(x=date, y=percent, group=newspaper),color="#0000DD") +
  labs(title = "elDiario - elconfidencial: noticias sobre Cifuentes en portada")

# Plot para varios periódico
ggplot(data=results ) + ylim(c(0,30)) +
  # geom_line(aes(x=date, y=n_news, group=newspaper),color="#000000") +
  # geom_line(aes(x=date, y=n_selected_news, group=newspaper),color="#FF0000") +
283
  geom_line(aes(x=date, y=percent, group=newspaper),color="#0000DD",size=0.4,alpha=0.6) +
284 285 286 287
  labs(title = "elDiario-elConfidencial-ElPais: % noticias sobre Cifuentes en portada")
# geom_text(aes(x = "2018-03-21", y = 500, label = "Gros"), color = "#9846dd", alpha=1) +
# geom_text(aes(x = "2007", y = 900, label = "Media"), color = "#000000", alpha=1) +
# geom_text(aes(x = "2007", y = 700, label = "Altza"), color = "#568ba5", alpha=1) +
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311

# Plot para varios periódico ['#66c2a5','#fc8d62','#8da0cb','#e78ac3','#a6d854']
ggplot( ) + ylim(c(0,30)) +
  theme_minimal(base_family = "Roboto Condensed", base_size = 14) +
  geom_line(data=results[results$newspaper=="eldiario",], aes(x=date, y=percent, group=newspaper),color="#66c2a5",size=0.7) +
  geom_line(data=results[results$newspaper=="elconfidencial",], aes(x=date, y=percent, group=newspaper),color="#fc8d62",size=0.7) +
  geom_line(data=results[results$newspaper=="elpais",], aes(x=date, y=percent, group=newspaper),color="#8da0cb",size=0.7) +
  geom_line(data=results[results$newspaper=="larazon",], aes(x=date, y=percent, group=newspaper),color="#e78ac3",size=0.7) +
  geom_line(data=results[results$newspaper=="elespanol",], aes(x=date, y=percent, group=newspaper),color="#a6d854",size=0.7) +
  labs(title = "Porcentaje de noticias sobre Cifuentes en portada (cada hora)") +
  xlab("Días") +
  ylab("% noticias en portadas") +
  # theme(axis.text.y = element_text(size=10),
  #       # axis.title.y=element_blank(),
  #       axis.ticks.y =element_blank(),
  #       # axis.ticks.x =element_blank(),
  #       axis.text.x=element_text(size=9),
  #       axis.title.x=element_text(size=11),
  #       panel.grid.minor = element_blank(),
  #       panel.background = element_rect(fill="white"),
  #       panel.grid.major.y = element_line( size=.1, color="grey" ),
  #       # legend.position = "bottom",
  #       legend.text = element_text(size=15) ) +
  scale_y_continuous(breaks=seq(0,30,5)) +
312 313 314 315 316 317
  scale_x_datetime(date_breaks = "1 month", date_labels = "%m", limits = my_limit) +
  geom_text(aes(x = my_init, y = 11, label = "eldiario.es"), color = "#66c2a5", alpha=1, hjust = 0) +
  geom_text(aes(x = my_init, y = 13, label = "larazon.es"), color = "#e78ac3", alpha=1, hjust = 0) +
  geom_text(aes(x = my_init, y = 15, label = "elConfidencial.es"), color = "#fc8d62", alpha=1, hjust = 0) +
  geom_text(aes(x = my_init, y = 17, label = "elespanol.com"), color = "#a6d854", alpha=1, hjust = 0) +
  geom_text(aes(x = as.POSIXlt("2018-10-10 00:00:00"), y = 19, label = "elpais.com"), color = "#8da0cb", alpha=1, hjust = 0) +
318
  ylab ("% de noticias en portada") +
319 320
  labs(title = paste("Porcentaje de noticias sobre",word_explain,"en portada periódicos digitales"),
       subtitle = "01 oct 2018 - 18 enero 2019. Datos y visualización: numeroteca.org",
321
       x = "Días",
322
       y = "%",
323 324 325 326
       caption = "")

# Plot para varios periódico  
ggplot( ) + ylim(c(0,24)) +
327
  theme_minimal(base_family = "Roboto Condensed", base_size = 14) +
328 329 330 331 332
  geom_line(data=results[results$newspaper=="eldiario",], aes(x=date, y=n_selected_news, group=newspaper),color="#66c2a5",size=0.7) +
  geom_line(data=results[results$newspaper=="elconfidencial",], aes(x=date, y=n_selected_news, group=newspaper),color="#fc8d62",size=0.7) +
  geom_line(data=results[results$newspaper=="elpais",], aes(x=date, y=n_selected_news, group=newspaper),color="#8da0cb",size=0.7) +
  geom_line(data=results[results$newspaper=="larazon",], aes(x=date, y=n_selected_news, group=newspaper),color="#e78ac3",size=0.7) +
  geom_line(data=results[results$newspaper=="elespanol",], aes(x=date, y=n_selected_news, group=newspaper),color="#a6d854",size=0.7) +
333 334 335 336 337 338 339 340 341 342 343 344 345 346
  # labs(title = "Número de noticias sobre Cifuentes en portada (cada hora)") +
  # xlab("Días") +
  # ylab("nº noticias en portadas") +
  # theme(axis.text.y = element_text(size=10),
  #       # axis.title.y=element_blank(),
  #       axis.ticks.y =element_blank(),
  #       axis.ticks.x =element_blank(),
  #       axis.text.x=element_text(size=9),
  #       axis.title.x=element_text(size=11),
  #       panel.grid.minor = element_blank(),
  #       panel.background = element_rect(fill="white"),
  #       panel.grid.major.y = element_line( size=.1, color="grey" ),
  #       # legend.position = "bottom",
  #       legend.text = element_text(size=15) ) +
347
  # scale_y_continuous(breaks=seq(0,30,5)) +
348 349 350 351 352 353 354 355
  scale_x_datetime(date_breaks = "1 month", date_labels = "%m", limits = my_limit) +
  geom_text(aes(x = my_init, y = 23, label = "eldiario.es"), color = "#66c2a5", alpha=1, hjust = 0) +
  geom_text(aes(x = my_init, y = 11, label = "larazon.es"), color = "#e78ac3", alpha=1, hjust = 0) +
  geom_text(aes(x = my_init, y = 17, label = "elConfidencial.es"), color = "#fc8d62", alpha=1, hjust = 0) +
  geom_text(aes(x = my_init, y = 20, label = "elespanol.com"), color = "#a6d854", alpha=1, hjust = 0) +
  geom_text(aes(x = my_init, y = 14, label = "elpais.com"), color = "#8da0cb", alpha=1, hjust = 0) +
  labs(title = paste("Número de noticias sobre",word_explain,"en portada periódicos digitales"),
     subtitle = "01 oct 2018 - 18 enero 2019. Datos y visualización: numeroteca.org",
356 357 358 359
     x = "Días",
     y = "nº noticias en portada",
     caption = "")

360
# --------  Analysis and comparision with Pageonex.com paper front pages data ---------
361 362 363 364 365 366 367 368 369 370
library("rjson")
# Get json from pageonex.com
json_file <- "http://pageonex.com/numeroteca/tfm-cifuentes/export.json"
json_data <- fromJSON(paste(readLines(json_file), collapse=""))

# Create empty dataf rame
df <- data.frame(matrix( ncol = 10,nrow = length(json_data$dates)  ))
# fill dataframe with pageonex data
for (i in 1:length(json_data$dates)) {
  df[i,] <- data.frame(json_data$data[[i]])
371 372
}

373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
# Make column and row names nicer 
colnames(df) <- json_data$media
colnames(df)[10] <- "media"
rownames(df) <- json_data$dates

# create variable with dates
df$date <- rownames(df)

library(reshape)
portadas <- melt(df, id=c("date"))
portadas$value <- portadas$value*100
portadas$date <- as.Date(portadas$date, "%Y-%m-%d")
class(portadas$date)

# Plot para varios periódico
ggplot(data=portadas) + 
  geom_line(aes(x=date, y=value, group=variable),color="#000000",size=0.2) +
  labs(title = "% portadas dedicada a escándalo Cifuentes en portada")

ggplot( data=portadas[portadas$variable=="media",],aes(x = date)) + 
  geom_bar(aes(weight = value)) +
  labs(title = "Media % portada dedicado a escándalo Cifuentes")

ggplot( data=portadas[!portadas$variable=="media",],aes(x = date)) + 
  geom_bar(aes(weight = value, fill=variable)) +
  labs(title = "% portada dedicado a escándalo Cifuentes")

ggplot( data=portadas[portadas$variable=="La Razón",],aes(x = date)) + 
  geom_bar(aes(weight = value)) +
  labs(title = "La Razón: % portada dedicado a escándalo Cifuentes")

ggplot( ) + 
  geom_bar(data=portadas[portadas$variable=="La Razón",], aes(x = timestamp,weight = value),fill="#A74a83") +
  geom_line(data=results[results$newspaper=="larazon",], aes(x=date, y=percent, group=newspaper),color="#e78ac3",size=0.7) +
  labs(title = "La Razón: % portada papel vs % noticias en digital dedicado a escándalo Cifuentes")

ggplot( ) + 
  geom_bar(data=portadas[portadas$variable=="El Pa",], aes(x = timestamp,weight = value),fill="#A74a83") +
  geom_line(data=results[results$newspaper=="larazon",], aes(x=date, y=percent, group=newspaper),color="#e78ac3",size=0.7) +
  labs(title = "La Razón: % portada papel vs % noticias en digital dedicado a escándalo Cifuentes")

ggplot( data=portadas,aes(x = variable) ) + 
  geom_bar(aes(weight = value/nrow(df))) +
  labs(title = "% dedicado a escándalo Cifuentes en portada") +
  coord_flip()
418

419 420 421 422
# Plot de la media
ggplot(data=portadas[portadas$variable=="media",]) + 
  geom_line(aes(x=date, y=value, group=variable),color="#000000",size=0.2) +
  labs(title = "% portadas dedicada a escándalo Cifuentes en portada")
423

424 425 426
portadas$timestamp <- as.POSIXlt( paste(portadas$date," ","00:00:00", sep = "" ))
summary(portadas)
class(portadas$timestamp)
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557


# Testing parsing news in different newspapers ---------------------------
# ------- eldiario.es ----------------------
# reads html and stores it
page <- read_html("data/http!www.eldiario.es!!!!@2018-04-06T23:01:07.888847+00:00.gz")

# gets all the text in article titles. All articles are in h2 except the comics.
titles <- page %>% html_nodes("article h2 a") %>% html_text() %>% data.frame()
colnames(titles) <- "title"
titles$title <- as.character(titles$title)

# total of articles with link
n_news <- nrow(titles) 

# select news that contain cerating word
select_news <- data.frame(titles[grepl(word, titles$title),])

# Results
# number of articles that contain words
n_select_news<- nrow(select_news)
# Percentage of articles that contain words
percent <- n_select_news / n_news * 100

# ------- elconfidencial ----------------------
# reads html and stores it
page <- read_html("data/http!www.elconfidencial.com!|!!!@2018-03-22T09:01:11.318750+00:00.gz")

# gets all the text in article titles. All articles are in h2 except the comics.
titles <- page %>% html_nodes("article h3 a") %>% html_text() %>% data.frame()
colnames(titles) <- "title"

# total of articles with link
n_news <- nrow(titles) 

# select news that contain cerating word
select_news <- data.frame(titles[grepl(word, titles$title),])

# Results
# number of articles that contain words
n_select_news<- nrow(select_news)
# Percentage of articles that contain words
percent <- n_select_news / n_news * 100

# ------- elmundo ----------------------
# reads html and stores it
pageelmundo <- read_html("eldiario/http!www.elmundo.es!!!!@2018-04-07T19:01:02.620498+00:00_formated.html") 

# gets all the text in article titles. All articles are in h2 except the comics.
titles <- pageelmundo %>% html_nodes("main article h3 a") %>% html_text() %>% data.frame() #TODO NO FUNCIONA
titles
colnames(titles) <- "title"

# total of articles with link
n_news <- nrow(titles) 

# select news that contain cerating word
select_news <- data.frame(titles[grepl(word, titles$title),])

# Results
# number of articles that contain words
n_select_news<- nrow(select_news)
# Percentage of articles that contain words
percent <- n_select_news / n_news * 100

# ------- elpais ----------------------
# reads html and stores it
pageelpais <- read_html("data/http!www.elpais.com!!!!@2017-07-04T13:51:08.133418+00:00.gz")

# gets all the text in article titles. All articles are in h2 except the comics.
titles <- pageelpais %>% html_nodes("article h2 a") %>% html_text() %>% data.frame() #TODO NO FUNCIONA
colnames(titles) <- "title"

# total of articles with link
n_news <- nrow(titles) 

# select news that contain cerating word
select_news <- data.frame(titles[grepl(word, titles$title),])

# Results
# number of articles that contain words
n_select_news<- nrow(select_news)
# Percentage of articles that contain words
percent <- n_select_news / n_news * 100

# ------- La Razón ----------------------
# reads html and stores it
pagelarazon <- read_html("data/http!www.larazon.es!|!!!@2018-03-30T07:01:03.998265+00:00.gz", to="UTF-8") #TODO correct encoding

# gets all the text in article titles. All articles are in h2 except the comics.
titles <- pagelarazon %>% html_nodes("article h2 a") %>% html_text() %>% data.frame() #TODO NO FUNCIONA
colnames(titles) <- "title"
titles

# total of articles with link
n_news <- nrow(titles) 

# select news that contain cerating word
select_news <- data.frame(titles[grepl(word, titles$title),])

# Results
# number of articles that contain words
n_select_news<- nrow(select_news)
# Percentage of articles that contain words
percent <- n_select_news / n_news * 100
# -------- La razón test ----------
# for (i in 1:nrow(selected)) {
#   if ( selected$newspaper[i] == "larazon") {
#   page <- read_html( paste("data/",selected$urls[i], sep = "") )
# 
#     titles <- page %>% html_nodes("article h2 a") %>% html_text() %>% data.frame()
# 
#   colnames(titles) <- "title"
#   # titles$title <- as.character(titles$title)
#   # total of articles with link
#   n_news <- nrow(titles)
#   print(paste("nº noticias:",n_news))
# 
#   # select news that contain cerating word
#   selected_news <- data.frame(titles[grepl(word, titles$title),])
#   print(selected_news)
#   # Results
#   # number of articles that contain words
#   n_selected_news<- nrow(selected_news)
#   print(n_selected_news)
#   print(paste("nº noticias Cifuentes:",n_selected_news))
#   # Percentage of articles that contain words
# 
#   print(paste("day:",selected$day[i],"hour:",selected$hour[i],selected$newspaper[i]))
#   }
# }