type-todo.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. public function type($get){
  2. $search = $get["s"];
  3. $bang = $get["bang"];
  4. if(empty($search)){
  5. if(!empty($bang)){
  6. // !youtube
  7. $conn = pg_connect("host=localhost dbname=4get user=postgres password=postgres");
  8. pg_prepare($conn, "bang_get", "SELECT bang,name FROM bangs WHERE bang LIKE $1 ORDER BY bang ASC LIMIT 8");
  9. $q = pg_execute($conn, "bang_get", ["$bang%"]);
  10. $results = [];
  11. while($row = pg_fetch_array($q, null, PGSQL_ASSOC)){
  12. $results[] = [
  13. "s" => "!" . $row["bang"],
  14. "n" => $row["name"]
  15. ];
  16. }
  17. return $results;
  18. }else{
  19. // everything is empty
  20. // lets just return a bang list
  21. return [
  22. [
  23. "s" => "!w",
  24. "n" => "Wikipedia",
  25. "u" => "https://en.wikipedia.org/wiki/Special:Search?search={%q%}"
  26. ],
  27. [
  28. "s" => "!4ch",
  29. "n" => "4chan Board",
  30. "u" => "https://find.4chan.org/?q={%q%}"
  31. ],
  32. [
  33. "s" => "!a",
  34. "n" => "Amazon",
  35. "u" => "https://www.amazon.com/s?k={%q%}"
  36. ],
  37. [
  38. "s" => "!e",
  39. "n" => "eBay",
  40. "u" => "https://www.ebay.com/sch/items/?_nkw={%q%}"
  41. ],
  42. [
  43. "s" => "!so",
  44. "n" => "Stack Overflow",
  45. "u" => "http://stackoverflow.com/search?q={%q%}"
  46. ],
  47. [
  48. "s" => "!gh",
  49. "n" => "GitHub",
  50. "u" => "https://github.com/search?utf8=%E2%9C%93&q={%q%}"
  51. ],
  52. [
  53. "s" => "!tw",
  54. "n" => "Twitter",
  55. "u" => "https://twitter.com/search?q={%q%}"
  56. ],
  57. [
  58. "s" => "!r",
  59. "n" => "Reddit",
  60. "u" => "https://www.reddit.com/search?q={%q%}"
  61. ],
  62. ];
  63. }
  64. }
  65. // now we know search isnt empty
  66. if(!empty($bang)){
  67. // check if the bang exists
  68. $conn = pg_connect("host=localhost dbname=4get user=postgres password=postgres");
  69. pg_prepare($conn, "bang_get_single", "SELECT bang,name FROM bangs WHERE bang = $1 LIMIT 1");
  70. $q = pg_execute($conn, "bang_get_single", [$bang]);
  71. $row = pg_fetch_array($q, null, PGSQL_ASSOC);
  72. if(isset($row["bang"])){
  73. $bang = "!$bang ";
  74. }else{
  75. $bang = "";
  76. }
  77. }
  78. try{
  79. $res = $this->get(
  80. "https://duckduckgo.com/ac/",
  81. [
  82. "q" => strtolower($search)
  83. ],
  84. ddg::req_xhr
  85. );
  86. $res = json_decode($res, true);
  87. }catch(Exception $e){
  88. throw new Exception("Failed to get /ac/");
  89. }
  90. $arr = [];
  91. for($i=0; $i<count($res); $i++){
  92. if($i === 8){break;}
  93. if(empty($bang)){
  94. $arr[] = [
  95. "s" => $res[$i]["phrase"]
  96. ];
  97. }else{
  98. $arr[] = [
  99. "s" => $bang . $res[$i]["phrase"],
  100. "n" => $row["name"]
  101. ];
  102. }
  103. }
  104. return $arr;
  105. }