channel.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace Vioscope;
  3. if ( ! class_exists( '\Vioscope\Channel' ) ) :
  4. class Channel {
  5. // YT channel id
  6. public $id;
  7. private $data;
  8. // Processed data
  9. public $info;
  10. public $page; // TODO: implement.
  11. public $total_page; // TODO: implement.
  12. function __construct( $channel_id = null, $page = null ) {
  13. if ( $channel_id ) {
  14. $this->set_channel_id( $channel_id, $page );
  15. }
  16. }
  17. public function set_channel_id( $channel_id = null, $page = null ) {
  18. if ( $channel_id ) {
  19. $this->id = $channel_id;
  20. } // TODO: Add check for invalid channel id
  21. if ( ! $page ) {
  22. $this->page = 1;
  23. } else {
  24. $this->page = intval( $page );
  25. }
  26. // Clear previous data (if any)
  27. unset( $this->data );
  28. unset( $this->info );
  29. // Update data
  30. $this->fetch_data( $this->id, $this->page );
  31. }
  32. public function fetch_data( $channel_id = null, $page = null ) {
  33. if ( ! $channel_id ) {
  34. $channel_id = $this->id;
  35. }
  36. if ( ! $page ) {
  37. $page = $this->page;
  38. } else {
  39. $this->page = $page;
  40. }
  41. $rsstext = get_url_contents( "https://www.youtube.com/feeds/videos.xml?channel_id={$this->id}&page=4" );
  42. $this->data = simplexml_load_string( $rsstext );
  43. $this->info['id'] = $this->id;
  44. if ( $this->data === false ) {
  45. echo "Failed loading XML: ";
  46. foreach( libxml_get_errors() as $error ) {
  47. echo "<br>", $error->message;
  48. }
  49. } else {
  50. $this->info['id'] = $this->id;
  51. $this->info['title'] = $this->data->title[0];
  52. $this->info['yt_url'] = $this->data->author->uri[0];
  53. $this->info['url'] = get_local_url( $this->id, 'channel' );
  54. // Playlist has the channel id but instead has 'UU' at the start
  55. $channel_playlist_id = substr_replace($this->id, 'UU', 0, 2);
  56. // Items in channel
  57. $body = get_url_contents( "https://www.youtube.com/playlist?list={$channel_playlist_id}&playnext=1&index=1" );
  58. $initial_data = get_initial_data( $body );
  59. if ( $initial_data ) {
  60. $this->info['entries'] = extract_items( $initial_data );
  61. }
  62. //$this->info['entries'] = $this->get_channel_entries();
  63. }
  64. }
  65. private function get_channel_entries() {
  66. if ( ! isset( $this->data ) ) return;
  67. $entries = array();
  68. foreach ( $this->data->entry as $entry ) {
  69. $video_id = explode( ':', $entry->id )[2];
  70. $entries[] = array(
  71. 'id' => $video_id,
  72. 'title' => (string) $entry->title,
  73. 'yt_url' => (string) $entry->link->attributes()->href,
  74. 'url' => get_local_url( $video_id ),
  75. 'thumbnail_url_medium' => get_video_thumbnail_url( $video_id, 'medium' ),
  76. 'thumbnail_url_small' => get_video_thumbnail_url( $video_id, 'small' ),
  77. 'published' => (string) $entry->published,
  78. 'updated' => (string) $entry->updated,
  79. );
  80. }
  81. return $entries;
  82. }
  83. /**
  84. * Gives the channel page url for next, previous or based on defined page
  85. * number.
  86. *
  87. * @param bool|int $next For next page set it to true, false for previous
  88. * and any integer for specific page.
  89. * @return bool|str Either page url or false.
  90. */
  91. // TODO: Create get_next_page_url and get_previous_page_url functions
  92. // utilizing this one.
  93. // TODO: Implement/encode attributes according to: https://github.com/iv-org/invidious/blob/452d1e8307d6344dd51c5437ccd032a566291c34/src/invidious/channels.cr#L399
  94. public function get_change_page_url( $next = true ) {
  95. $page = $this->page;
  96. if ( ! is_numeric( $next ) ) {
  97. if ( $next == true ) {
  98. $page++;
  99. } else {
  100. $page--;
  101. }
  102. } else {
  103. $page = $next;
  104. }
  105. if ( $page < 1 ) { // TODO: Add $this->total_page in logic
  106. return false;
  107. }
  108. return get_base_url() . '/channel/' . $this->id . '?page=' . $page;
  109. }
  110. }
  111. endif;