liveviewers.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * Livecoding.tv Live Viewers Badges.
  4. *
  5. * Get the amount of live viewers for a channel and return an appropriate svg image.
  6. *
  7. * @param string channel (required) LCTV channel name.
  8. * @param string link (optional) true/false to automatically link to channel.
  9. *
  10. * @package LCTVBadges\Badges
  11. * @since 0.0.3
  12. */
  13. /** Bail if no channel name. */
  14. if ( ! isset( $_GET['channel'] ) || empty( $_GET['channel'] ) ) {
  15. exit();
  16. }
  17. /** Set the channel name. */
  18. $channel = strtolower( $_GET['channel'] );
  19. /** Initialize. */
  20. require_once( 'lctv_badges_init.php' );
  21. /** Load the API. */
  22. $lctv_api = new LCTVAPI( array(
  23. 'client_id' => LCTV_CLIENT_ID,
  24. 'client_secret' => LCTV_CLIENT_SECRET,
  25. 'user' => LCTV_MASTER_USER,
  26. ) );
  27. /** Bail if API isn't authorized. */
  28. if ( ! $lctv_api->is_authorized() ) {
  29. exit();
  30. }
  31. /** Get live streaming info for a channel. */
  32. $api_request = $lctv_api->api_request( 'v1/livestreams/' . urlencode( $channel ) . '/' );
  33. /** Bail on error. */
  34. if ( $api_request === false ) {
  35. exit();
  36. }
  37. /** API returned an error. */
  38. if ( isset( $api_request->result->detail ) ) {
  39. $api_request->result->is_live = false;
  40. $api_request->result->viewers_live = 0;
  41. }
  42. /** Check to auto link. */
  43. if ( isset( $_GET['link'] ) && strtolower( $_GET['link'] ) === 'true' ) {
  44. $link = 'https://www.livecoding.tv/' . urlencode( $channel ) . '/';
  45. } else {
  46. $link = '';
  47. }
  48. /** Output svg image. */
  49. header( "Content-type:image/svg+xml" );
  50. if ( $api_request->result->is_live ) {
  51. echo get_badge_svg( 'lctv viewers', ' ' . $api_request->result->viewers_live . ' ', '#4c1', $link );
  52. } else {
  53. echo get_badge_svg( 'lctv viewers', ' ' . $api_request->result->viewers_live . ' ', '#e05d44', $link );
  54. }