nextstream.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Livecoding.tv Streaming Status Badges.
  4. *
  5. * Check if the channel has a scheduled streaming date 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.7
  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. 'data_store' => LCTVAPI_DATA_STORE_CLASS,
  24. 'client_id' => LCTV_CLIENT_ID,
  25. 'client_secret' => LCTV_CLIENT_SECRET,
  26. 'user' => LCTV_MASTER_USER,
  27. ) );
  28. /** Bail if API isn't authorized. */
  29. if ( ! $lctv_api->is_authorized() ) {
  30. header( "Content-type:image/svg+xml" );
  31. echo get_badge_svg( 'livecoding.tv', 'error', '#e05d44' );
  32. exit();
  33. }
  34. /** Get live streaming info for a channel. */
  35. $api_request = $lctv_api->api_request( 'v1/scheduledbroadcast/?limit=500' );
  36. /** Bail on error. */
  37. if ( $api_request === false || isset( $api_request->result->detail ) ) {
  38. header( "Content-type:image/svg+xml" );
  39. echo get_badge_svg( 'livecoding.tv', 'error', '#e05d44' );
  40. exit();
  41. }
  42. /** Get scheduled streams and search for channel name. */
  43. $next_stream = '';
  44. $api_request->result->results = array_reverse( $api_request->result->results );
  45. foreach ( $api_request->result->results as $scheduled ) {
  46. if ( strpos( $scheduled->livestream, $channel ) !== false ) {
  47. $next_stream = strtotime( $scheduled->start_time_original_timezone );
  48. break;
  49. }
  50. }
  51. /** Check to auto link. */
  52. if ( isset( $_GET['link'] ) && strtolower( $_GET['link'] ) === 'true' ) {
  53. $link = 'https://www.livecoding.tv/' . urlencode( $channel ) . '/';
  54. } else {
  55. $link = '';
  56. }
  57. /** Output svg image. */
  58. header( "Content-type:image/svg+xml" );
  59. if ( $next_stream ) {
  60. echo get_badge_svg( 'next stream', date( 'M j @ g:i a', $next_stream ), '#4c1', $link );
  61. } else {
  62. echo get_badge_svg( 'next stream', 'no stream scheduled', '#e05d44', $link );
  63. }