From a2268a0b07ca2c213510cdd7506041f6268e83d9 Mon Sep 17 00:00:00 2001 From: Minoplhy Date: Wed, 27 Oct 2021 16:47:04 +0700 Subject: [PATCH] log_err module (#1) --- goduckgo.go | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/goduckgo.go b/goduckgo.go index 50b418b..5949a95 100644 --- a/goduckgo.go +++ b/goduckgo.go @@ -64,24 +64,19 @@ func Search(keyword Query) Gogo { requrl := url + "?q=" + keyword.Keyword req, err := http.NewRequest(http.MethodGet, requrl, nil) - if err != nil { - log.Fatal(err) - } + log_err(err) + req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0") res, err := netreq.Do(req) - if err != nil { - log.Fatal(err) - } + log_err(err) if res.Body != nil { defer res.Body.Close() } body, readErr := ioutil.ReadAll(res.Body) - if readErr != nil { - log.Fatal(readErr) - } + log_err(readErr) hunsen := regexfind(regex_r{Regex: `vqd=([\d-]+)\&`, Body: string(body)}) @@ -105,23 +100,18 @@ func Search(keyword Query) Gogo { resq.Header.Add("sec-fetch-mode", "cors") resq.Header.Add("referer", "https://duckduckgo.com/") resq.Header.Add("accept-language", "en-US,enq=0.9") - if err != nil { - log.Fatal(err) - } + log_err(err) + resqs, getErr := netreq.Do(resq) - if getErr != nil { - log.Fatal(getErr) - } + log_err(getErr) brudy, readErr := ioutil.ReadAll(resqs.Body) - if readErr != nil { - log.Fatal(readErr) - } + log_err(readErr) + duckduck := Gogo{} jsonErr := json.Unmarshal(brudy, &duckduck) - if jsonErr != nil { - log.Fatal(jsonErr) - } + log_err(jsonErr) + log.Println("\nHitting Url Success : " + requrl) return duckduck } @@ -131,3 +121,9 @@ func regexfind(hitin regex_r) string { hunsen := gexgex.FindString(string(hitin.Body)) return hunsen } + +func log_err(err error) { + if err != nil { + log.Fatal(err) + } +}