streamingstatus.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. 'data_store' => LCTVAPI_DATA_STORE_CLASS,
  32. 'client_id' => LCTV_CLIENT_ID,
  33. 'client_secret' => LCTV_CLIENT_SECRET,
  34. 'user' => LCTV_MASTER_USER,
  35. ) );
  36. /** Bail if API isn't authorized. */
  37. if ( ! $lctv_api->is_authorized() ) {
  38. header( "Content-type:image/svg+xml" );
  39. echo get_badge_svg( 'livecoding.tv', 'error', '#e05d44' );
  40. exit();
  41. }
  42. /** Get live streaming info for a channel. */
  43. $api_request = $lctv_api->api_request( 'v1/livestreams/' . urlencode( $channel ) . '/' );
  44. /** Bail on error. */
  45. if ( $api_request === false ) {
  46. header( "Content-type:image/svg+xml" );
  47. echo get_badge_svg( 'livecoding.tv', 'error', '#e05d44' );
  48. exit();
  49. }
  50. /** API returned an error. This happens if user is not streaming. */
  51. if ( isset( $api_request->result->detail ) ) {
  52. $api_request->result->is_live = false;
  53. }
  54. /** Display live stream title instead of 'online'. */
  55. if ( isset( $_GET['title'] ) && strtolower( $_GET['title'] ) === 'true' ) {
  56. if ( ! empty( $api_request->result->title ) ) {
  57. $online_message = $api_request->result->title;
  58. }
  59. }
  60. /** Check to auto link. */
  61. if ( isset( $_GET['link'] ) && strtolower( $_GET['link'] ) === 'true' ) {
  62. $link = 'https://www.livecoding.tv/' . urlencode( $channel ) . '/';
  63. } else {
  64. $link = '';
  65. }
  66. /** Output svg image. */
  67. header( "Content-type:image/svg+xml" );
  68. if ( $api_request->result->is_live ) {
  69. echo get_badge_svg( 'livecoding.tv', $online_message, '#4c1', $link );
  70. } else {
  71. echo get_badge_svg( 'livecoding.tv', $offline_message, '#e05d44', $link );
  72. }