setwd("~/R/Machine_Learning") # GET magnifying glass SelectorGadget
dir()

library(rvest)
library(dplyr)

iphone = data.frame()

for (page_result in seq(from = 0, to = 108, by = 27)) {
  link = paste0("https://www.czc.cz/mobilni-telefony/produkty?q-c-1-price=r7990-25000&q-c-0-producer=s1a6v52ouiecs79csfuvr9vfl19&q-first=",page_result)
  
  page <- read_html(link)
  
  name <- page %>% html_nodes('.tile-title a') %>% html_text()
  price <- page %>% html_nodes('.price .price-vatin') %>% html_text()
  
  iphone = rbind(iphone, data.frame(name, price, stringsAsFactors = FALSE))
  
  print(paste("Page:", page_result))
}
iphone$price <- gsub("Kč", "", iphone$price)
iphone$price <- gsub("\\s", "", iphone$price)
iphone$price <- as.numeric(iphone$price)

iphone <- iphone[order(-iphone$price),]
colnames(iphone)[which(names(iphone) == "price")] <- "Price"

library(ggplot2)
library(plotly)
g <- ggplot(iphone, aes(x=name, y=Price, text = paste0(name,"\n","Price: ", Price, " Kč"))) +
  geom_point(aes(color=Price), size=3) +
  theme_minimal() +
  scale_color_gradient(low = 'green', high = 'red') +
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        legend.title = element_blank())

ggplotly(g,tooltip = c("text"))