-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added language heuristic to cheat.sh
- Loading branch information
Showing
7 changed files
with
179 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package cheatsh | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/shivylp/radium" | ||
) | ||
|
||
// New initializes a radium.Source implementation using http://cheat.sh as | ||
// the source | ||
func New() *CheatSh { | ||
csh := &CheatSh{} | ||
return csh | ||
} | ||
|
||
// CheatSh implements radium.Source interface using cheat.sh as the source | ||
type CheatSh struct { | ||
} | ||
|
||
// Search performs an HTTP request to http://cheat.sh to find results matching | ||
// the given query. | ||
func (csh CheatSh) Search(ctx context.Context, query radium.Query) ([]radium.Article, error) { | ||
transformLanguageQuery(&query) | ||
|
||
return executeRequest(ctx, query) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package cheatsh | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/shivylp/radium" | ||
) | ||
|
||
const cheatShURL = "http://cheat.sh" | ||
|
||
func executeRequest(ctx context.Context, query radium.Query) ([]radium.Article, error) { | ||
req, err := makeRequest(ctx, query) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
client := http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf(resp.Status) | ||
} | ||
|
||
data, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return makeResponse(query, data), nil | ||
} | ||
|
||
func makeResponse(query radium.Query, data []byte) []radium.Article { | ||
result := radium.Article{} | ||
result.Content = string(data) | ||
result.ContentType = "plaintext" | ||
result.Title = query.Text | ||
result.Attribs = query.Attribs | ||
|
||
return []radium.Article{result} | ||
} | ||
|
||
func makeRequest(ctx context.Context, query radium.Query) (*http.Request, error) { | ||
pu, err := url.Parse(cheatShURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
queryParams := pu.Query() | ||
queryStr := url.PathEscape(strings.Replace(strings.TrimSpace(query.Text), " ", "+", -1)) | ||
|
||
if lang, found := query.Attribs["language"]; found { | ||
appendPath(pu, url.PathEscape(lang), queryStr) | ||
} else { | ||
appendPath(pu, queryStr) | ||
} | ||
|
||
if _, found := query.Attribs["color"]; !found { | ||
queryParams.Set("T", "") | ||
} | ||
|
||
pu.RawQuery = queryParams.Encode() | ||
req, err := http.NewRequest(http.MethodGet, pu.String(), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Set("User-Agent", "curl/7.54.0") | ||
req.WithContext(ctx) | ||
|
||
return req, nil | ||
} | ||
|
||
func isTrue(s string) bool { | ||
switch strings.TrimSpace(strings.ToLower(s)) { | ||
case "yes", "y", "yea", "t", "true": | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
func appendPath(pu *url.URL, segs ...string) { | ||
segs = append(segs, pu.Path) | ||
p := filepath.Join(segs...) | ||
pu.Path = p | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package cheatsh | ||
|
||
import ( | ||
"regexp" | ||
|
||
"github.com/shivylp/radium" | ||
) | ||
|
||
var ( | ||
langQuery = regexp.MustCompile("(.*) in (\\w+)") | ||
languages = []string{ | ||
"go", | ||
"golang", | ||
"java", | ||
"python", | ||
"lua", | ||
"c", | ||
"c++", | ||
"cpp", | ||
"ruby", | ||
"rails", | ||
} | ||
) | ||
|
||
func transformLanguageQuery(query *radium.Query) { | ||
matches := langQuery.FindAllStringSubmatch(query.Text, -1) | ||
if len(matches) >= 1 && contains(languages, matches[0][2]) { | ||
query.Text = matches[0][1] | ||
query.Attribs["language"] = matches[0][2] | ||
} | ||
} | ||
|
||
func contains(strs []string, str string) bool { | ||
for _, s := range strs { | ||
if s == str { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |