streamingstatus.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * Livecoding.tv Streaming Status Badges.
  4. *
  5. * Check if the channel is currently streaming and return an appropriate svg image.
  6. *
  7. * @param string channel (required) LCTV channel name.
  8. * @param string online (optional) Button message if status is streaming.
  9. * @param string offline (optional) Button message if status is not streaming.
  10. * @param string title (optional) true/false to show streaming title when live,
  11. * this will override the online message.
  12. * @param string link (optional) true/false to automatically link to channel.
  13. *
  14. * @package LCTVBadges\Badges
  15. * @since 0.0.3
  16. */
  17. /** Bail if no channel name. */
  18. if ( ! isset( $_GET['channel'] ) || empty( $_GET['channel'] ) ) {
  19. exit();
  20. }
  21. /** Set the channel name. */
  22. $channel = strtolower( $_GET['channel'] );
  23. /** Set the online message. */
  24. $online_message = ( isset( $_GET['online'] ) && ! empty( $_GET['online'] ) ) ? $_GET['online'] : 'online';
  25. /** Set the offline message. */
  26. $offline_message = ( isset( $_GET['offline'] ) && ! empty( $_GET['offline'] ) ) ? $_GET['offline'] : 'offline';
  27. /** Initialize. */
  28. require_once( 'lctv_badges_init.php' );
  29. /** Load the API. */
  30. $lctv_api = new LCTVAPI( array(
  31. 'client_id' => LCTV_CLIENT_ID,
  32. 'client_secret' => LCTV_CLIENT_SECRET,
  33. 'user' => LCTV_MASTER_USER,
  34. ) );
  35. /** Bail if API isn't authorized. */
  36. if ( ! $lctv_api->is_authorized() ) {
  37. header( "Content-type:image/svg+xml" );
  38. echo get_badge_svg( 'livecoding.tv', 'error', '#e05d44' );
  39. exit();
  40. }
  41. /** Get live streaming info for a channel. */
  42. $api_request = $lctv_api->api_request( 'v1/livestreams/' . urlencode( $channel ) . '/' );
  43. /** Bail on error. */
  44. if ( $api_request === false ) {
  45. header( "Content-type:image/svg+xml" );
  46. echo get_badge_svg( 'livecoding.tv', 'error', '#e05d44' );
  47. exit();
  48. }
  49. /** API returned an error. This happens if user is not streaming. */
  50. if ( isset( $api_request->result->detail ) ) {
  51. $api_request->result->is_live = false;
  52. }
  53. /** Display live stream title instead of 'online'. */
  54. if ( isset( $_GET['title'] ) && strtolower( $_GET['title'] ) === 'true' ) {
  55. if ( ! empty( $api_request->result->title ) ) {
  56. $online_message = $api_request->result->title;
  57. }
  58. }
  59. /** Check to auto link. */
  60. if ( isset( $_GET['link'] ) && strtolower( $_GET['link'] ) === 'true' ) {
  61. $link = 'https://www.livecoding.tv/' . urlencode( $channel ) . '/';
  62. } else {
  63. $link = '';
  64. }
  65. /** Output svg image. */
  66. header( "Content-type:image/svg+xml" );
  67. if ( $api_request->result->is_live ) {
  68. echo get_badge_svg( 'livecoding.tv', $online_message, '#4c1', $link );
  69. } else {
  70. echo get_badge_svg( 'livecoding.tv', $offline_message, '#e05d44', $link );
  71. }