streamingstatus.php 2.4 KB

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