├── README.md ├── test └── main.go ├── .gitignore ├── LICENSE └── pullword.go /README.md: -------------------------------------------------------------------------------- 1 | Pullword 2 | ======== 3 | A Golang package for [pullword.com](http://pullword.com). 4 | 5 | ###Install 6 | ````bash 7 | go get -u github.com/denghongcai/pullword 8 | ```` 9 | 10 | ###Documentation 11 | See [GoDoc](https://godoc.org/github.com/denghongcai/pullword) 12 | -------------------------------------------------------------------------------- /test/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/denghongcai/pullword" 7 | ) 8 | 9 | func main() { 10 | req := pullword.NewRequest("你的姿势水平还远远不够", 1, false) 11 | result, err := req.Do() 12 | if err != nil { 13 | panic(err) 14 | } 15 | fmt.Printf("%q", result) 16 | } 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Hongcai Deng 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /pullword.go: -------------------------------------------------------------------------------- 1 | package pullword 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "net" 7 | "strings" 8 | ) 9 | 10 | type request struct { 11 | source string 12 | param1 float32 13 | param2 uint 14 | } 15 | 16 | func NewRequest(source string, threshold float32, debug bool) request { 17 | var param2 uint 18 | if debug { 19 | param2 = 1 20 | } else { 21 | param2 = 0 22 | } 23 | 24 | return request{ 25 | source: source, 26 | param1: threshold, 27 | param2: param2, 28 | } 29 | } 30 | 31 | func (req request) Do() ([]string, error) { 32 | conn, err := net.Dial("tcp", "api.pullword.com:2015") 33 | if err != nil { 34 | return nil, err 35 | } 36 | writer := bufio.NewWriter(conn) 37 | _, err = writer.WriteString(fmt.Sprintf("%s\t%1.2f\t%d]\r\n", req.source, req.param1, req.param2)) 38 | if err != nil { 39 | return nil, err 40 | } 41 | writer.Flush() 42 | if err != nil { 43 | return nil, err 44 | } 45 | scanner := bufio.NewScanner(conn) 46 | list := make([]string, 0) 47 | for scanner.Scan() { 48 | if scanner.Text() != "\r\n" && scanner.Text() != "" { 49 | list = append(list, strings.Trim(scanner.Text(), "\r\n")) 50 | } 51 | } 52 | if err := scanner.Err(); err != nil { 53 | return nil, err 54 | } 55 | return list, nil 56 | } 57 | --------------------------------------------------------------------------------