Browse Source

Improve code readability & general cleanup

pull/159/head
Richard Lewis 8 years ago
parent
commit
7551e730db
  1. 17
      src/github.com/matrix-org/go-neb/services/wikipedia/wikipedia.go

17
src/github.com/matrix-org/go-neb/services/wikipedia/wikipedia.go

@ -64,27 +64,27 @@ func usageMessage() *gomatrix.TextMessage {
} }
func (s *Service) cmdWikipediaSearch(client *gomatrix.Client, roomID, userID string, args []string) (interface{}, error) { func (s *Service) cmdWikipediaSearch(client *gomatrix.Client, roomID, userID string, args []string) (interface{}, error) {
// Check for query text
if len(args) < 1 { if len(args) < 1 {
return usageMessage(), nil return usageMessage(), nil
} }
// Get the query text to search for.
// Get the query text and per,form search
querySentence := strings.Join(args, " ") querySentence := strings.Join(args, " ")
searchResultPage, err := s.text2Wikipedia(querySentence) searchResultPage, err := s.text2Wikipedia(querySentence)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if searchResultPage.Extract == "" {
// No article extracts
if searchResultPage == nil || searchResultPage.Extract == "" {
return gomatrix.TextMessage{ return gomatrix.TextMessage{
MsgType: "m.notice", MsgType: "m.notice",
Body: "No results found!",
Body: "No results",
}, nil }, nil
} }
// Convert article HTML to text
extractText, err := html2text.FromString(searchResultPage.Extract) extractText, err := html2text.FromString(searchResultPage.Extract)
if err != nil { if err != nil {
return gomatrix.TextMessage{ return gomatrix.TextMessage{
@ -101,6 +101,7 @@ func (s *Service) cmdWikipediaSearch(client *gomatrix.Client, roomID, userID str
// Add a link to the bottom of the extract // Add a link to the bottom of the extract
extractText += fmt.Sprintf("\nhttp://en.wikipedia.org/?curid=%d", searchResultPage.PageID) extractText += fmt.Sprintf("\nhttp://en.wikipedia.org/?curid=%d", searchResultPage.PageID)
// Return article extract
return gomatrix.TextMessage{ return gomatrix.TextMessage{
MsgType: "m.notice", MsgType: "m.notice",
Body: extractText, Body: extractText,
@ -127,6 +128,7 @@ func (s *Service) text2Wikipedia(query string) (*wikipediaPage, error) {
u.RawQuery = q.Encode() u.RawQuery = q.Encode()
// log.Info("Request URL: ", u) // log.Info("Request URL: ", u)
// Perform wikipedia search request
res, err := httpClient.Get(u.String()) res, err := httpClient.Get(u.String())
if res != nil { if res != nil {
defer res.Body.Close() defer res.Body.Close()
@ -137,8 +139,9 @@ func (s *Service) text2Wikipedia(query string) (*wikipediaPage, error) {
if res.StatusCode < 200 || res.StatusCode >= 300 { if res.StatusCode < 200 || res.StatusCode >= 300 {
return nil, fmt.Errorf("Request error: %d, %s", res.StatusCode, response2String(res)) return nil, fmt.Errorf("Request error: %d, %s", res.StatusCode, response2String(res))
} }
var searchResults wikipediaSearchResults
// Parse search results
var searchResults wikipediaSearchResults
// log.Info(response2String(res)) // log.Info(response2String(res))
if err := json.NewDecoder(res.Body).Decode(&searchResults); err != nil { if err := json.NewDecoder(res.Body).Decode(&searchResults); err != nil {
return nil, fmt.Errorf("ERROR - %s", err.Error()) return nil, fmt.Errorf("ERROR - %s", err.Error())

Loading…
Cancel
Save