Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [klasa] Filmy z youtube, ... z własnego profilu
bim2
post
Post #1





Grupa: Zarejestrowani
Postów: 1 873
Pomógł: 152
Dołączył: 9.04.2006
Skąd: Berlin

Ostrzeżenie: (0%)
-----


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. ?>


Ten post edytował bim2 5.01.2008, 22:00:08


--------------------
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 3)
devnul
post
Post #2





Grupa: Zarejestrowani
Postów: 1 470
Pomógł: 75
Dołączył: 21.09.2005
Skąd: że znowu

Ostrzeżenie: (0%)
-----


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)


--------------------
Profesjonaliści są przewidywalni...
strzeż się amatorów...
FL4SHB4CK - imprezy, galerie, lokale
Go to the top of the page
+Quote Post
bim2
post
Post #3





Grupa: Zarejestrowani
Postów: 1 873
Pomógł: 152
Dołączył: 9.04.2006
Skąd: Berlin

Ostrzeżenie: (0%)
-----


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

Ten post edytował bim2 4.09.2007, 18:01:51


--------------------
Go to the top of the page
+Quote Post
Exodus
post
Post #4





Grupa: Zarejestrowani
Postów: 44
Pomógł: 0
Dołączył: 16.10.2007

Ostrzeżenie: (0%)
-----


Dzieki bardzo sie przydalo
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 19.08.2025 - 16:13