Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [klasa] Filmy z youtube
Forum PHP.pl > Forum > Gotowe rozwiązania > Algorytmy, klasy, funkcje
bim2
Kilka postów niżej poprawiona klasa
----------------------------------

Klasa łączy sie z youTube.com wchodzi w dany profil i pobiera filmy. Klase napisałem troszkę temu dla kolegi, a dziś robiąc porządki ją znalazłem więc zamieszczam.

  1. <?php
  2. class youTube
  3. {
  4. public $sNick;
  5. public $sUrl = 'http://pl.youtube.com/profile_videos?user=%s';
  6. public $sUrl_ = 'http://pl.youtube.com/profile_videos?user=%s&page=%s';
  7. private $page;
  8. private $sString;
  9. public $aMatches_;
  10. private $bRun=false;
  11. function __construct($sNick)
  12. {
  13. $this->sNick = $sNick;
  14.  
  15. }
  16. public function connect()
  17. {
  18. if(empty($this->page))
  19. {
  20. $sUrl = sprintf($this->sUrl, $this->sNick);
  21. $this->sString = @file_get_contents($sUrl);
  22. } else {
  23.  
  24. $sUrl = sprintf($this->sUrl_, $this->sNick, $this->page);
  25. $this->sString = @file_get_contents($sUrl);
  26. }
  27. }
  28. public function getVideos()
  29. {
  30. if($this->bRun)
  31. {
  32. return $this->aMatches;
  33. } else {
  34. $this->getVideos_();
  35. return $this->aMatches;
  36. }
  37. }
  38. public function randVideo()
  39. {
  40. $aMatches_ = $this->getVideos();
  41. $iKey = rand(0, count($aMatches_)-1);
  42. return $this->aMatches_[$iKey];
  43. }
  44. public function getVideos_()
  45. {
  46. $this->bRun = true;
  47. $this->connect();
  48. preg_match_all("#<!-- end vEntry -->(.*?)<!-- end vEntry -->#is", $this->sString, $aMatches); 
  49. foreach($aMatches[1] AS $sValue)
  50. {
  51. $aMatches__ = preg_replace('!.*watch?v=(.*?)".*!is', '$1', $sValue);
  52. $this->aMatches_[] = '<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/'.$aMatches__.'"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'.$aMatches__.'" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>';
  53. }
  54. $this->page = preg_replace("#.*?<a href=\"/profile_videos?user=".$this->sNick."&amp;p=r&amp;page=([0-9]*?)\" class=\"pagerNotCurrent\">Dalej</a>.*#is", '$1', $this->sString);
  55. if((int)$this->page>0)
  56. {
  57. $this->getVideos_();
  58. }
  59. }
  60. }
  61. ?>

I jak użyć:
  1. <?php
  2. $yt = new youTube($sNick);
  3. echo $yt->randVideo(); //losuje film
  4. print_r($yt->getAllVideos()); //zwraca wszystkie filmy
  5. ?>
devnul
ale po co to? jest przecierz youtube developers api które jest imho bardzo wygodne i pozwala w łatwy sposób wyciągnąć znacznie wiecej informacji o filmie (a nie tylko sam tag <object> z tego co widze)
bim2
ROTFL sciana.gif Już pisze od nowa :]Ja pier*** tego simplexml'a ale zrobiłem ;/
  1. <?php
  2.  
  3. class YouTubeApi {
  4. private $links = array(
  5. 'filmsByUser' => 'http://gdata.youtube.com/feeds/videos?author=%s', //http://gdata.youtube.com/feeds/videos?author=yurilane - Retrieving videos uploaded by a user
  6. 'searchFilm' => 'http://gdata.youtube.com/feeds/videos/-/%s');//http://gdata.youtube.com/feeds/videos/-/Comedy/dog
  7. public $sId;
  8. public $aString;
  9. public $aCategories;
  10. private $bConnect = false;
  11. public function __construct($sId, $sWhat)
  12. {
  13. $this->sId = $sId;
  14.  
  15. if(!$this->bConnect)
  16. $this->connect($sWhat);
  17.  
  18. }
  19. public function connect($sWhat)
  20. {
  21. $sUrl = sprintf($this->links[$sWhat], $this->sId);
  22. $this->aString = simplexml_load_file($sUrl);
  23. $this->bConnect = true;
  24. }
  25. public function getFilms()
  26. {
  27. $lp = 0;
  28. foreach($this->aString->entry AS $value)
  29. {
  30.  
  31. $aFilms[$lp] = $value;
  32.  
  33. $count = count($aFilms[$lp]->category);
  34. $lp__ = 0;
  35. foreach($aFilms[$lp]->link AS $values)
  36. {
  37.  
  38. $aLinks = $this->object2array($values);
  39. //print_r($aLinks);
  40. $aFilms[$lp]->links[$lp__] = $aLinks['@attributes']['type'].' = '.$aLinks['@attributes']['href'];
  41. if($aLinks['@attributes']['type']=='text/html')
  42. {
  43. $aFilms[$lp]->flash = '<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/'.$aLinks['@attributes']['href'].'"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'.$aLinks['@attributes']['href'].'" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>';
  44. }
  45.  
  46. $lp__++;
  47. }
  48. $lp_ = 0;
  49. foreach($aFilms[$lp]->category AS $value)
  50. {
  51.  
  52. $aCat = $this->object2array($value);
  53. if(!ereg('http', $aCat['@attributes']['term']))
  54. {
  55. $aFilms[$lp]->categories[$lp_] = $aCat['@attributes']['term'];
  56. $lp_++;
  57. }
  58. }
  59.  
  60. unset($aFilms[$lp]->categories[0]);
  61. unset($aFilms[$lp]->links[0]);
  62. unset($aFilms[$lp]->category);
  63. unset($aFilms[$lp]->link);
  64.  
  65. $lp++;
  66. }
  67. foreach($aFilms AS $value)
  68. {
  69. $aFilms_[] = $this->object2array($value);
  70. }
  71. return $aFilms_;
  72. }
  73. public function getSite()
  74. {
  75. return $this->aString;
  76. }
  77. public function object2array ( $object ) // NULLA
  78. {
  79. if ( !is_object ( $object ) )
  80. return $object;
  81.  
  82. $return = array ();
  83.  
  84. $var = get_object_vars ( $object );
  85.  
  86. while ( list ( $k, $v ) = each ( $var ) )
  87. $return [ $k ] = $this->object2array ( $v );
  88. return $return;
  89. }
  90.  
  91. }
  92. $oYt = new YouTubeApi('Harmonica', 'searchFilm');
  93. $aYT = $oYt->getFilms();
  94. print_r($aYT);
  95. ?>

Pewnie jest coś do poprawy, ale można wyszukiwać filmy po userze jak normalnie smile.gif Może komuś się przyda, bo ja robiłem z nudów biggrin.gif
Exodus
Dzieki bardzo sie przydalo
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.