SpotifyPlugin.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Plugin to create pretty Spotify URLs
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Plugin
  23. * @package StatusNet
  24. * @author Nick Holliday <n.g.holliday@gmail.com>
  25. * @copyright Nick Holliday
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. *
  29. * @see Event
  30. */
  31. if (!defined('STATUSNET')) {
  32. exit(1);
  33. }
  34. define('SPOTIFYPLUGIN_VERSION', '0.1');
  35. /**
  36. * Plugin to create pretty Spotify URLs
  37. *
  38. * The Spotify API is called before the notice is saved to gather artist and track information.
  39. *
  40. * @category Plugin
  41. * @package StatusNet
  42. * @author Nick Holliday <n.g.holliday@gmail.com>
  43. * @copyright Nick Holliday
  44. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  45. * @link http://status.net/
  46. *
  47. * @see Event
  48. */
  49. class SpotifyPlugin extends Plugin
  50. {
  51. function __construct()
  52. {
  53. parent::__construct();
  54. }
  55. function onStartNoticeSave($notice)
  56. {
  57. $notice->rendered = preg_replace_callback('/spotify:[a-z]{5,6}:[a-z0-9]{22}/i',
  58. "renderSpotifyURILink",
  59. $notice->rendered);
  60. $notice->rendered = preg_replace_callback('/<a href="http:\/\/open.spotify.com\/[a-z]{5,6}\/[a-z0-9]{22}" title="http:\/\/open.spotify.com\/[a-z]{5,6}\/[a-z0-9]{22}" rel="external">http:\/\/open.spotify.com\/[a-z]{5,6}\/[a-z0-9]{22}<\/a>/i',
  61. "renderSpotifyHTTPLink",
  62. $notice->rendered);
  63. return true;
  64. }
  65. function onPluginVersion(&$versions)
  66. {
  67. $versions[] = array('name' => 'Spotify',
  68. 'version' => SPOTIFYPLUGIN_VERSION,
  69. 'author' => 'Nick Holliday',
  70. 'homepage' => 'http://status.net/wiki/Plugin:Spotify',
  71. 'rawdescription' =>
  72. // TRANS: Plugin description.
  73. _m('Create pretty <a href="http://www.spotify.com">Spotify</a> URLs.'));
  74. return true;
  75. }
  76. }
  77. // @todo FIXME: This probably should not be global functions.
  78. function doSpotifyLookup($uri, $isArtist)
  79. {
  80. $request = HTTPClient::start();
  81. $response = $request->get('http://ws.spotify.com/lookup/1/?uri=' . $uri);
  82. if ($response->isOk()) {
  83. $xml = simplexml_load_string($response->getBody());
  84. if($isArtist)
  85. return $xml->name;
  86. else
  87. return $xml->artist->name . ' - ' . $xml->name;
  88. }
  89. }
  90. function renderSpotifyURILink($match)
  91. {
  92. $isArtist = false;
  93. if(preg_match('/artist/', $match[0]) > 0) $isArtist = true;
  94. $name = doSpotifyLookup($match[0], $isArtist);
  95. return "<a href=\"{$match[0]}\">" . $name . "</a>";
  96. }
  97. function renderSpotifyHTTPLink($match)
  98. {
  99. $match[0] = preg_replace('/<a href="http:\/\/open.spotify.com\/[a-z]{5,6}\/[a-z0-9]{22}" title="http:\/\/open.spotify.com\/[a-z]{5,6}\/[a-z0-9]{22}" rel="external">http:\/\/open.spotify.com\//i', 'spotify:', $match[0]);
  100. $match[0] = preg_replace('/<\/a>/', '', $match[0]);
  101. $match[0] = preg_replace('/\//', ':', $match[0]);
  102. $isArtist = false;
  103. if(preg_match('/artist/', $match[0]) > 0) $isArtist = true;
  104. $name = doSpotifyLookup($match[0], $isArtist);
  105. return "<a href=\"{$match[0]}\">" . $name . "</a>";
  106. }