<commit_before>package main

import (
	"fmt"
	"github.com/gorilla/handlers"
	"github.com/gorilla/mux"
  "github.com/asaskevich/govalidator"
	"log"
	"net/http"
  "net/url"
	"os"
)

func main() {
	r := mux.NewRouter()
	r.Methods("GET").Path("/").HandlerFunc(Index)
	r.Methods("GET").Path("/api/num_colors").HandlerFunc(ApiNumColors)
	loggedR := handlers.LoggingHandler(os.Stdout, r)
	fmt.Println("Now listening on port 8080")
	log.Fatal(http.ListenAndServe(":8080", loggedR))
}

func Index(w http.ResponseWriter, r *http.Request) {
	fmt.Println(r.URL.Query())
	fmt.Fprintln(w, "visit /api/num_colors?src=<image url> to get the number of colors in your image")
}

func ApiNumColors(w http.ResponseWriter, r *http.Request) {
	imgPath := r.URL.Query().Get("src")
  if !govalidator.IsURL(imgPath) {
    http.Error(w, "Please use a valid url.", http.StatusBadRequest)
    return
  }
  imgUrl, err := url.Parse(imgPath)
  if err != nil {
    http.Error(w, "Please use a valid url.", http.StatusInternalServerError)
    return
  }
	fmt.Fprintln(w, imgUrl)
}
<commit_msg>Download img file to temp dir<commit_after>package main

import (
	"fmt"
	"github.com/gorilla/handlers"
	"github.com/gorilla/mux"
  "github.com/asaskevich/govalidator"
	"log"
  "io"
  "io/ioutil"
	"net/http"
  "net/url"
	"os"
)

func main() {

  // imgUrl, _ := url.Parse("http://www.jqueryscript.net/images/Simplest-Responsive-jQuery-Image-Lightbox-Plugin-simple-lightbox.jpg")
  // file, err := downloadFile(imgUrl)
  // if err != nil {
  //   panic(err)
  // }
  // fmt.Println(file.Name())

	r := mux.NewRouter()
	r.Methods("GET").Path("/").HandlerFunc(Index)
	r.Methods("GET").Path("/api/num_colors").HandlerFunc(ApiNumColors)
	loggedR := handlers.LoggingHandler(os.Stdout, r)
	fmt.Println("Now listening on port 8080")
	log.Fatal(http.ListenAndServe(":8080", loggedR))
}

func Index(w http.ResponseWriter, r *http.Request) {
	fmt.Println(r.URL.Query())
	fmt.Fprintln(w, "visit /api/num_colors?src=<image url> to get the number of colors in your image")
}

func ApiNumColors(w http.ResponseWriter, r *http.Request) {
	imgPath := r.URL.Query().Get("src")
  if !govalidator.IsURL(imgPath) {
    http.Error(w, "Please use a valid url.", http.StatusBadRequest)
    return
  }
  imgUrl, err := url.Parse(imgPath)
  if err != nil {
    http.Error(w, "Please use a valid url.", http.StatusInternalServerError)
    return
  }

  file, err := downloadFile(imgUrl)
  if err != nil {
    http.Error(w, "We failed to download the iamge file.", http.StatusInternalServerError)
    return
  }
  fmt.Println(file.Name())
  fmt.Println(imgPath)
	fmt.Fprintln(w, "<html><body><img src=\"" + imgPath + "\"></body></html>")
}

// func downloadFile(filepath string, url string) (err error) {
func downloadFile(url *url.URL) (f *os.File, err error) {

  // create the local file
  file, err := ioutil.TempFile("", "img")
  if err != nil {
    return nil, err
  }
  defer file.Close()

  resp, err := http.Get(url.String())
  if err != nil {
    return nil, err
  }
  defer resp.Body.Close()

  _, err = io.Copy(file, resp.Body)
  if err != nil {
    return nil, err
  }

  return file, nil
}

{'fraction_non_alphanumeric': 0.1689512272872171, 'fraction_numerical': 0.005100414408670704, 'mean_word_length': 3.812883435582822, 'pattern_counts': {'":': 2, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7623780', 'n_tokens_mistral': 1203, 'n_tokens_neox': 1186, 'n_words': 249}