Advertisement
antikorps

Untitled

Apr 12th, 2022
1,098
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.24 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "encoding/json"
  6.     "fmt"
  7.     "log"
  8.     "net/http"
  9.     "net/http/cookiejar"
  10.     "os"
  11.     "strings"
  12.  
  13.     "github.com/PuerkitoBio/goquery"
  14. )
  15.  
  16. type BusquedaRespuestaJSON struct {
  17.     Result  int `json:"result"`
  18.     Results []struct {
  19.         ID    json.RawMessage `json:"id"`
  20.         Value string          `json:"value"`
  21.         Label string          `json:"label"`
  22.     } `json:"results"`
  23. }
  24.  
  25. func main() {
  26.     fmt.Println("Introduce el nombre de la película:")
  27.     entradaEstandar := bufio.NewReader(os.Stdin)
  28.     usuarioEntrada, errorUsusarioEntrada := entradaEstandar.ReadString('\n')
  29.     if errorUsusarioEntrada != nil {
  30.         log.Panic(errorUsusarioEntrada)
  31.     }
  32.     pelicula := strings.TrimSuffix(usuarioEntrada, "\n")
  33.  
  34.     jar, errorJar := cookiejar.New(nil)
  35.     if errorJar != nil {
  36.         log.Panic(errorJar)
  37.     }
  38.  
  39.     cliente := &http.Client{
  40.         Jar: jar,
  41.     }
  42.  
  43.     peticionFA, errorPeticionFA := http.NewRequest("GET", "https://www.filmaffinity.com/es/main.html", nil)
  44.     if errorPeticionFA != nil {
  45.         log.Panic(errorPeticionFA)
  46.     }
  47.     respuestaFA, errorRespuestaFA := cliente.Do(peticionFA)
  48.     if errorRespuestaFA != nil {
  49.         log.Panic(errorRespuestaFA)
  50.     }
  51.     defer respuestaFA.Body.Close()
  52.     if respuestaFA.StatusCode != 200 {
  53.         log.Panic("Status code incorrecto ", respuestaFA.StatusCode)
  54.     }
  55.  
  56.     busquedaQuery := "dataType=json&term=" + pelicula
  57.  
  58.     peticionBusqueda, errorPeticionBusqueda := http.NewRequest("POST", "https://www.filmaffinity.com/es/search-ac.ajax.php?action=searchTerm", strings.NewReader(busquedaQuery))
  59.     if errorPeticionBusqueda != nil {
  60.         log.Panic(errorPeticionBusqueda)
  61.     }
  62.     peticionBusqueda.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
  63.  
  64.     respuestaBusqueda, errorRespuestaBusqueda := cliente.Do(peticionBusqueda)
  65.     if errorRespuestaBusqueda != nil {
  66.         log.Panic(respuestaBusqueda)
  67.     }
  68.     defer respuestaBusqueda.Body.Close()
  69.     if respuestaBusqueda.StatusCode != 200 {
  70.         log.Panic("Status code incorrecto ", respuestaBusqueda.StatusCode)
  71.     }
  72.  
  73.     var respuestaBusquedaJSON BusquedaRespuestaJSON
  74.     errorRespuestaBusquedaJSON := json.NewDecoder(respuestaBusqueda.Body).Decode(&respuestaBusquedaJSON)
  75.     if errorRespuestaBusquedaJSON != nil {
  76.         log.Panic(errorRespuestaBusquedaJSON)
  77.     }
  78.  
  79.     if len(respuestaBusquedaJSON.Results) == 0 {
  80.         fmt.Println("Búsqueda sin resultados :(")
  81.         return
  82.     }
  83.  
  84.     peliculaID := string(respuestaBusquedaJSON.Results[0].ID)
  85.  
  86.     endPointPelicula := fmt.Sprint("https://www.filmaffinity.com/es/film", peliculaID, ".html")
  87.  
  88.     peticionPelicula, errorPeticionPelicula := http.NewRequest("GET", endPointPelicula, nil)
  89.     if errorPeticionPelicula != nil {
  90.         log.Panic(errorPeticionPelicula)
  91.     }
  92.     respuestaPelicula, errorRespuestaPelicula := cliente.Do(peticionPelicula)
  93.     if errorRespuestaPelicula != nil {
  94.         log.Panic(errorRespuestaPelicula)
  95.     }
  96.     defer respuestaPelicula.Body.Close()
  97.     if respuestaPelicula.StatusCode != 200 {
  98.         log.Panic("Status code incorrecto ", respuestaPelicula.StatusCode)
  99.     }
  100.  
  101.     documento, documentoError := goquery.NewDocumentFromReader(respuestaPelicula.Body)
  102.     if documentoError != nil {
  103.         log.Panic(documentoError)
  104.     }
  105.  
  106.     puntuacion := documento.Find("#movie-rat-avg").Text()
  107.  
  108.     fmt.Println("La película", pelicula, "tiene una puntuación media en Filmaffinity de", strings.TrimSpace(puntuacion))
  109.  
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement