diff --git a/README.md b/README.md index 017bbcc..4860886 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ -![Version](https://img.shields.io/badge/version-0.8.5-orange.svg) +![Version](https://img.shields.io/badge/version-0.9.0-orange.svg) ![Maintained YES](https://img.shields.io/badge/Maintained%3F-yes-green.svg) ![Ask Me Anything !](https://img.shields.io/badge/Ask%20me-anything-1abc9c.svg) # ![logo](https://github.com/jolav/codetabs/blob/master/www/_public/icons/ct/ct64r.png?raw=true) **ONLINE TOOLS ([codetabs.com](https://codetabs.com))** -**version 0.8.5** +**version 0.9.0** 1. [Count LOC (lines of code) online from github/gitlab repos or zipped uploaded folder](#count-loc-online) 2. [CORS proxy](#cors-proxy) @@ -15,6 +15,7 @@ 6. [HTTP Headers](#headers) 7. [API weather temp](#weather) 8. [Video To Gif](#video2gif) +9. [Random Data API](#random-data-api) [To Do List](#to-do) @@ -233,6 +234,43 @@ Scale : Set width:height , if one parameter is -1 it will automatically determin
+![logo](https://github.com/jolav/codetabs/blob/master/www/_public/icons/random48.png?raw=true) + +## **RANDOM DATA API** + +### **[Demo and Docs online](https://codetabs.com/random-data/random-data.html)** + +- Api to generate random data +- Only suppports GET request. +- Limit : 10 request per seconds. Once reached subsequent requests will result in error 429 (too many requests) until your quota is cleared. + + +### **Endpoints** + +- Get Random Integers +``` +http Request : +GET https://api.codetabs.com/v1/random/integer?range=X-Y +``` + +Examples +Get random number between 1-10 both inclusive +https://api.codetabs.com/v1/random/integer?min=1&max=10 +You can also specify how many times you want the result with the parameter times. +Default is 1 and there is no need to specify it. Max times = 10.000 +https://api.codetabs.com/v1/random/integer?min=1&max=10×=50 + +- List with randomized order +``` +http Request : +GET https://api.codetabs.com/v1/random/list?len=X +``` +Max list elements : 10.000 +Example: Get random order numbers for a list of 1000 elements +https://api.codetabs.com/v1/random/list?len=1000 + +
+ ## TO DO - [ ] **WWW** clean unused parts, css, etc diff --git a/_utils/utils.go b/_utils/utils.go index c988f4c..93b7b48 100644 --- a/_utils/utils.go +++ b/_utils/utils.go @@ -26,6 +26,16 @@ func SliceContainsString(str string, slice []string) bool { return false } +// SliceContainsInteger ... returns true/false +func SliceContainsInteger(num int, slice []int) bool { + for _, v := range slice { + if v == num { + return true + } + } + return false +} + // GenericCommandSH ... func GenericCommandSH(comm string) (chunk []byte, err error) { chunk, err = exec.Command("sh", "-c", comm).CombinedOutput() @@ -35,6 +45,16 @@ func GenericCommandSH(comm string) (chunk []byte, err error) { return chunk, err } +// RemoveElementFromSliceString +func RemoveElementFromSliceString(index int, slice []string) []string { + return append(slice[:index], slice[index+1:]...) +} + +// RemoveElementFromSliceInt +func RemoveElementFromSliceInt(index int, slice []int) []int { + return append(slice[:index], slice[index+1:]...) +} + // GenericCommand ... func GenericCommand(args []string) (err error) { _, err = exec.Command(args[0], args[1:len(args)]...).CombinedOutput() diff --git a/main.go b/main.go index 33bcdbe..0e661fc 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "flag" "fmt" "log" + "math/rand" "net/http" "os" "os/user" @@ -20,13 +21,14 @@ import ( "github.com/jolav/codetabs/headers" "github.com/jolav/codetabs/loc" "github.com/jolav/codetabs/proxy" + "github.com/jolav/codetabs/random" "github.com/jolav/codetabs/stars" "github.com/jolav/codetabs/store" "github.com/jolav/codetabs/video2gif" "github.com/jolav/codetabs/weather" ) -var version = "0.8.5" +var version = "0.9.0" var when = "undefined" type Conf struct { @@ -42,6 +44,8 @@ type Conf struct { } func main() { + + rand.Seed(time.Now().UnixNano()) checkFlags() var c Conf @@ -91,6 +95,7 @@ func main() { mux.HandleFunc("/v1/headers/", mw(headers.Router, "headers", c)) mux.HandleFunc("/v1/weather/", mw(weather.Router, "weather", c)) mux.HandleFunc("/v1/video2gif/", mw(video2gif.Router, "video2gif", c)) + mux.HandleFunc("/v1/random/", mw(random.Router, "random", c)) mux.HandleFunc("/v1/stars/", mw(stars.Router, "stars", c)) mux.HandleFunc("/v1/proxy/", mw(proxy.Router, "proxy", c)) mux.HandleFunc("/v1/tmp/", mw(proxy.Router, "proxy", c)) diff --git a/random/random.go b/random/random.go new file mode 100644 index 0000000..a12820e --- /dev/null +++ b/random/random.go @@ -0,0 +1,113 @@ +/* */ + +package random + +import ( + "fmt" + "net/http" + "strconv" + "strings" + + u "github.com/jolav/codetabs/_utils" +) + +type random struct { + Quest string `json:"quest"` + Result []int `json:"data"` +} + +func Router(w http.ResponseWriter, r *http.Request) { + params := strings.Split(strings.ToLower(r.URL.Path), "/") + path := params[1:len(params)] + if path[len(path)-1] == "" { // remove last empty slot after / + path = path[:len(path)-1] + } + //log.Printf("Going ....%s %s %d", path, r.Method, len(path)) + if len(path) < 2 || path[0] != "v1" { + u.BadRequest(w, r) + return + } + if len(path) != 3 { + u.BadRequest(w, r) + return + } + + if r.Method == "POST" { + u.BadRequest(w, r) + return + } + + rd := newRandom() + randomType := path[2] + switch randomType { + case "integer": + rd.randomInteger(w, r) + case "list": + rd.randomList(w, r) + default: + u.BadRequest(w, r) + return + } +} + +func (rd random) randomInteger(w http.ResponseWriter, r *http.Request) { + times := 1 + + r.ParseForm() + min, err := strconv.Atoi(r.Form.Get("min")) + if err != nil { + u.BadRequest(w, r) + return + } + max, err := strconv.Atoi(r.Form.Get("max")) + if err != nil || min > max || min < 0 || max > 10000000000 { + u.BadRequest(w, r) + return + } + if r.Form.Get("times") != "" { + times, err = strconv.Atoi(r.Form.Get("times")) + if err != nil || times > 10000 { + u.BadRequest(w, r) + return + } + } + if times == 1 { + rd.Quest = fmt.Sprintf("Random Integer between %d-%d", min, max) + } else { + rd.Quest = fmt.Sprintf("%d Random Integers between %d-%d", times, min, max) + } + + for time := 1; time <= times; time++ { + rd.Result = append(rd.Result, u.GetRandomInt(min, max)) + } + + u.SendJSONToClient(w, rd, 200) +} + +func (rd random) randomList(w http.ResponseWriter, r *http.Request) { + r.ParseForm() + elements, err := strconv.Atoi(r.Form.Get("len")) + if err != nil || elements < 2 || elements > 10000 { + u.BadRequest(w, r) + return + } + + rd.Quest = fmt.Sprintf("Randomized order list with %d elements", elements) + element := 0 + for element < elements { + number := u.GetRandomInt(1, elements) + if !u.SliceContainsInteger(number, rd.Result) { + rd.Result = append(rd.Result, number) + element++ + } + } + u.SendJSONToClient(w, rd, 200) +} + +func newRandom() random { + rd := random{ + Quest: "", + Result: []int{}, + } + return rd +} diff --git a/www/_public/icons/random48.png b/www/_public/icons/random48.png new file mode 100644 index 0000000..b2c12da Binary files /dev/null and b/www/_public/icons/random48.png differ diff --git a/www/_public/images/sprite.png b/www/_public/images/sprite.png index a0cea40..c890b72 100644 Binary files a/www/_public/images/sprite.png and b/www/_public/images/sprite.png differ diff --git a/www/_public/sprites/random32.png b/www/_public/sprites/random32.png new file mode 100644 index 0000000..a7bc428 Binary files /dev/null and b/www/_public/sprites/random32.png differ diff --git a/www/_public/templates/version.html b/www/_public/templates/version.html index 83a62bb..7f9fe15 100644 --- a/www/_public/templates/version.html +++ b/www/_public/templates/version.html @@ -2,7 +2,7 @@

API doc - (version 0.8.5) + (version 0.9.0)

diff --git a/www/index.css b/www/index.css index 8e4235d..275bac8 100644 --- a/www/index.css +++ b/www/index.css @@ -229,95 +229,24 @@ hr { } /* Generated by http://css.spritegen.com CSS Sprite Generator */ - -.ct64r, .ct48r, .alexa32, .ct32r, .gif32, .headers32, .ip32, .jolav32, .loc32, .notes32, .proxy32, .stars32, .weather32, .check { - display: inline-block; - background: url('/_public/images/sprite.png') no-repeat; - overflow: hidden; - text-indent: -9999px; - text-align: left; -} - -.ct64r { - background-position: -0px -0px; - width: 64px; - height: 64px; -} - -.ct48r { - background-position: -0px -64px; - width: 48px; - height: 48px; -} - -.alexa32 { - background-position: -0px -112px; - width: 32px; - height: 32px; -} - -.ct32r { - background-position: -32px -112px; - width: 32px; - height: 32px; -} - -.gif32 { - background-position: -0px -144px; - width: 32px; - height: 32px; -} - -.headers32 { - background-position: -32px -144px; - width: 32px; - height: 32px; -} - -.ip32 { - background-position: -0px -176px; - width: 32px; - height: 32px; -} - -.jolav32 { - background-position: -32px -176px; - width: 32px; - height: 32px; -} - -.loc32 { - background-position: -0px -208px; - width: 32px; - height: 32px; -} - -.notes32 { - background-position: -32px -208px; - width: 32px; - height: 32px; -} - -.proxy32 { - background-position: -0px -240px; - width: 32px; - height: 32px; -} - -.stars32 { - background-position: -32px -240px; - width: 32px; - height: 32px; -} - -.weather32 { - background-position: -0px -272px; - width: 32px; - height: 32px; -} - -.check { - background-position: -48px -64px; - width: 16px; - height: 16px; -} + +.ct64r, .ct48r, .alexa32, .ct32r, .gif32, +.headers32, .ip32, .jolav32, .loc32, .notes32, +.proxy32, .random32, .stars32, .weather32, .check +{ display: inline-block; background: url('_public/images/sprite.png') no-repeat; overflow: hidden; text-indent: -9999px; text-align: left; } + +.ct64r { background-position: -0px -0px; width: 64px; height: 64px; } +.ct48r { background-position: -0px -64px; width: 48px; height: 48px; } +.alexa32 { background-position: -0px -112px; width: 32px; height: 32px; } +.ct32r { background-position: -32px -112px; width: 32px; height: 32px; } +.gif32 { background-position: -0px -144px; width: 32px; height: 32px; } +.headers32 { background-position: -32px -144px; width: 32px; height: 32px; } +.ip32 { background-position: -0px -176px; width: 32px; height: 32px; } +.jolav32 { background-position: -32px -176px; width: 32px; height: 32px; } +.loc32 { background-position: -0px -208px; width: 32px; height: 32px; } +.notes32 { background-position: -32px -208px; width: 32px; height: 32px; } +.proxy32 { background-position: -0px -240px; width: 32px; height: 32px; } +.random32 { background-position: -32px -240px; width: 32px; height: 32px; } +.stars32 { background-position: -0px -272px; width: 32px; height: 32px; } +.weather32 { background-position: -32px -272px; width: 32px; height: 32px; } +.check { background-position: -48px -64px; width: 16px; height: 16px; } diff --git a/www/index.html b/www/index.html index 4fc0ca1..d39ff4c 100644 --- a/www/index.html +++ b/www/index.html @@ -71,15 +71,41 @@

- +
-
- Video to GIF +
+
+ Random Data API

- Convert Videos to animated gifs +

NEW API
+ API retrieves random data for to be used as you please

+
+ API +
+
+
+
+
+ +
+ +
+
+
+ IP GeoLocation +
+

+ Free API that retrieves geolocation information from any IPv4, IPv6 or hostname. +
JSON and XML are supported +
No key or signup needed +

+
+ API +
+
@@ -107,21 +133,15 @@

- +
-
- IP GeoLocation +
+ Video to GIF

- Free API that retrieves geolocation information from any IPv4, IPv6 or hostname. -
JSON and XML are supported -
No key or signup needed + Convert Videos to animated gifs

-
- API -
-
@@ -136,9 +156,6 @@

Check any website position in Alexa Top 1 million Ranking.
Daily Updates. -

API diff --git a/www/random-data/random-data.html b/www/random-data/random-data.html new file mode 100644 index 0000000..4775c3a --- /dev/null +++ b/www/random-data/random-data.html @@ -0,0 +1,123 @@ + + + + + + Random Data API + + + + + + + + +
+

+ Logo + Random Data +

+

+ Logo + Random Data +

+
+ +

NEW

+ + + + +
+
+ Description +
+
+ + - Api to generate random data +
+
+ + - Only suppports GET request. + +
+ + - + Limit : 10 request per seconds. Once reached subsequent requests will result in + error 429 (too many requests) + until your quota is cleared. +
+
+
+
+ +
+ +
+ Endpoint - Get Random Integers +
+
+ http Request : +
+ + GET https://api.codetabs.com/v1/random/integer?range=X-Y + +
+ Examples +
Get random number between 1-10 both inclusive +
+ + + https://api.codetabs.com/v1/random/integer?min=1&max=10 + +
+ You can also specify how many times you want the result with the parameter times.
+ Default is 1 and there is no need to specify it. Max times = 10.000
+ + https://api.codetabs.com/v1/random/integer?min=1&max=10&times=50> + +
+
+ +
+ +
+ +
+ Endpoint - List with randomized order +
+
+ http Request : +
+ + GET https://api.codetabs.com/v1/random/list?len=X + +
+ Max list elements : 10.000
+ Examples +
Get random order numbers for a list of 1000 elements +
+ + + https://api.codetabs.com/v1/random/list?len=1000 + +
+
+ +
+ +
+ + + + + + + + + + + + +