search.class.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace Vioscope;
  3. if ( ! class_exists( '\Vioscope\Search' ) ) :
  4. require_once( __DIR__ . '/common.php' );
  5. class Search {
  6. public $search_query;
  7. public $page;
  8. public $total_page; // TODO: implement
  9. function __construct( $search_query = null, $page = 1, $search_params = null, $region = null ) {
  10. if ( $search_query ) {
  11. $this->run_search( $search_query, $page, $search_params, $region );
  12. }
  13. }
  14. public function run_search( $search_query = null, $page = 1, $search_params = null, $region = null ) {
  15. if ( ! $search_query ) {
  16. $search_query = $this->search_query;
  17. }
  18. if ( trim( $search_query ) == '' ) {
  19. return false;
  20. }
  21. $this->search_query = trim( $search_query );
  22. $this->page = $page;
  23. // Get result data
  24. $search_query = urlencode( $search_query );
  25. if ( ! $search_params ) {
  26. $search_params = $this->produce_search_params();
  27. }
  28. // TODO: Implement more parameters
  29. $body = get_url_contents( "https://www.youtube.com/results?q=$search_query&page=$page&hl=en" );
  30. $initial_data = get_initial_data( $body );
  31. return extract_items( $initial_data );
  32. }
  33. /**
  34. * Returns search params for a search to be performed.
  35. *
  36. * @param str $sort Sort order.
  37. * @param str $date Date to be searched for.
  38. * @param str $content_type Content type to search for.
  39. * @param str $duration Content of duration to search for.
  40. * @param str $features Features to search for.
  41. * @return str
  42. */
  43. // TODO: Translate over from https://github.com/iv-org/invidious/blob/452d1e8307d6344dd51c5437ccd032a566291c34/src/invidious/search.cr#L266
  44. public function produce_search_params( $sort = 'relevance', $date = '', $content_type = '',
  45. $duration = '', $features = array() ) {
  46. switch ($sort) {
  47. case "relevance":
  48. // do something
  49. break;
  50. case "rating":
  51. // do something
  52. break;
  53. case "upload_date":
  54. // do something
  55. break;
  56. case "view_count":
  57. // do something
  58. break;
  59. default:
  60. // do something
  61. }
  62. return ''; // TODO: Return actual stuff
  63. }
  64. /**
  65. * Gives the search page url for next, previous or based on defined page
  66. * number.
  67. *
  68. * @param bool|int $next For next page set it to true, false for previous
  69. * and any integer for specific page.
  70. * @return bool|str Either page url or false.
  71. */
  72. // TODO: Create get_next_page_url and get_previous_page_url functions
  73. // utilizing this one.
  74. public function get_change_page_url( $next = true ) {
  75. $page = $this->page;
  76. if ( ! is_numeric( $next ) ) {
  77. if ( $next == true ) {
  78. $page++;
  79. } else {
  80. $page--;
  81. }
  82. } else {
  83. $page = $next;
  84. }
  85. if ( $page < 1 ) { // TODO: Add $this->total_page in logic
  86. return false;
  87. }
  88. return get_base_url() . '/search?q=' . $this->search_query . '&page=' . $page;
  89. }
  90. }
  91. endif;