Advertisement
Guest User

Untitled

a guest
Sep 19th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.28 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "io/ioutil"
  6.     "log"
  7.     "math/rand"
  8.     "net/http"
  9.     "os"
  10. )
  11.  
  12. func check(e error) {
  13.     if e != nil {
  14.         panic(e)
  15.     }
  16. }
  17.  
  18. type Object struct {
  19.     url string
  20. }
  21.  
  22. func ReadByIoutil(filePath string) string {
  23.  
  24.     file, err := ioutil.ReadFile(filePath)
  25.     check(err)
  26.     data := string(file)
  27.     return data
  28. }
  29. func WriteByOs(filePath string, data string) {
  30.     file, err := os.Create(filePath)
  31.     check(err)
  32.     defer file.Close()
  33.     _, err = file.WriteString(data)
  34.     check(err)
  35. }
  36. func ParseData(data string) []Object {
  37.     var counter = 0
  38.     var answer string
  39.     var array []Object
  40.     var buffer Object
  41.     for i := 0; i < len(data); i++ {
  42.         switch data[i] {
  43.         case ';':
  44.             {
  45.                 buffer.url = answer
  46.                 array = append(array, buffer)
  47.                 answer = ""
  48.                 counter++
  49.             }
  50.         case ' ':
  51.             { /*continue*/
  52.             }
  53.         default:
  54.             answer += string(data[i])
  55.         }
  56.     }
  57.  
  58.     buffer.url = answer
  59.     array = append(array, buffer)
  60.     answer = ""
  61.  
  62.     return array
  63. }
  64. func ReadWriteHandler() {
  65.  
  66.     data := ReadByIoutil("file.txt")
  67.     array := ParseData(data)
  68.  
  69.     var i = 0
  70.     for i < len(array) {
  71.         fmt.Println(array[i].url)
  72.         i++
  73.     }
  74.  
  75.     /* stringify (not json) */
  76.     var answer string
  77.     for i := 0; i < len(data); i++ {
  78.         if data[i] == ';' {
  79.             answer += "\n"
  80.         } else if data[i] == ' ' {
  81.             continue
  82.         } else {
  83.             answer += string(data[i])
  84.         }
  85.     }
  86.     /* PUSHING TEMP TO NEW FILE*/
  87.     WriteByOs("file2.txt", answer)
  88.  
  89. }
  90.  
  91. func AddForm(url string) string {
  92.     if url == "" {
  93.         data := ReadByIoutil("file.txt")
  94.         array := ParseData(data)
  95.         url = "https://"
  96.         url += array[rand.Intn(len(array))].url
  97.         log.Print(url)
  98.     }
  99.     resp, err := http.Get(url)
  100.     check(err)
  101.     defer resp.Body.Close()
  102.     body, err := ioutil.ReadAll(resp.Body)
  103.     content := string(body)
  104.     WriteByOs("index.html", content)
  105.     return content
  106. }
  107.  
  108. func handler(w http.ResponseWriter, r *http.Request) {
  109.     url := r.FormValue("url")
  110.     if url == "" {
  111.         w.Header().Set("Content-Type", "text/html; charset=utf-8")
  112.         url = ""
  113.         //url = "https://yandex.ru"
  114.         fmt.Fprint(w, AddForm(url))
  115.         return
  116.     }
  117. }
  118.  
  119. func main() {
  120.     /* Dont pay attention on that. It's a test */
  121.     ReadWriteHandler()
  122.     /* START LOCALHOST SERVER */
  123.     http.HandleFunc("/test", handler)
  124.  
  125.     err := http.ListenAndServe(":8080", nil)
  126.     if err != nil {
  127.         log.Fatal(err.Error())
  128.     }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement