PageRenderTime 63ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/duckduckgo/duckduckgo.factor

http://github.com/mrjbq7/re-factor
Unknown | 81 lines | 67 code | 14 blank | 0 comment | 0 complexity | 7929c79029780271e32eecfc7c4fac4b MD5 | raw file
Possible License(s): Apache-2.0
  1. ! Copyright (C) 2013 John Benediktsson
  2. ! See http://factorcode.org/license.txt for BSD license
  3. USING: assocs combinators http.client json.reader kernel
  4. sequences urls ;
  5. IN: duckduckgo
  6. <PRIVATE
  7. : search-url ( query -- url )
  8. URL" http://api.duckduckgo.com"
  9. swap "q" set-query-param
  10. "json" "format" set-query-param
  11. "1" "pretty" set-query-param
  12. "1" "no_redirect" set-query-param
  13. "1" "no_html" set-query-param
  14. "1" "skip_disambig" set-query-param ;
  15. TUPLE: abstract html text url source heading ;
  16. TUPLE: answer text type url ;
  17. TUPLE: result html text url ;
  18. TUPLE: redirect url ;
  19. TUPLE: definition text url source ;
  20. TUPLE: results type image answer result related-topics abstract
  21. definition redirect ;
  22. : >abstract ( json -- abstract )
  23. {
  24. [ "Abstract" of ]
  25. [ "AbstractText" of ]
  26. [ "AbstractURL" of ]
  27. [ "AbstractSource" of ]
  28. [ "Heading" of ]
  29. } cleave abstract boa ;
  30. : >answer ( json -- answer )
  31. [ "Answer" of ]
  32. [ "AnswerType" of ] bi f answer boa ;
  33. : >definition ( json -- definition )
  34. [ "Definition" of ]
  35. [ "DefinitionURL" of ]
  36. [ "DefinitionSource" of ] tri definition boa ;
  37. : >redirect ( json -- redirect )
  38. "Redirect" of redirect boa ;
  39. : >result ( json -- result )
  40. [ "Result" of ]
  41. [ "Text" of ]
  42. [ "FirstURL" of ] tri result boa ;
  43. SYMBOLS: +article+ +disambiguation+ +category+ +name+
  44. +exclusive+ +nothing+ ;
  45. : >results ( json -- results )
  46. {
  47. [
  48. "Type" of H{
  49. { "A" +article+ }
  50. { "D" +disambiguation+ }
  51. { "C" +category+ }
  52. { "N" +name+ }
  53. { "E" +exclusive+ }
  54. { "" +nothing+ }
  55. } at
  56. ]
  57. [ "Image" of ]
  58. [ >answer ]
  59. [ "Results" of [ >result ] map ]
  60. [ "RelatedTopics" of [ >result ] map ]
  61. [ >abstract ]
  62. [ >definition ]
  63. [ >redirect ]
  64. } cleave results boa ;
  65. PRIVATE>
  66. : search ( query -- results )
  67. search-url http-get nip "" like json> >results ;