laststreamed.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Livecoding.tv Last Streamed Badges.
  4. *
  5. * Get the last video 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.4
  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' => $channel,
  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/user/videos/latest/' );
  33. /** Bail on error. */
  34. if ( $api_request === false ) {
  35. exit();
  36. }
  37. /** Check to auto link. */
  38. if ( isset( $_GET['link'] ) && strtolower( $_GET['link'] ) === 'true' ) {
  39. $link = 'https://www.livecoding.tv/' . urlencode( $channel ) . '/';
  40. } else {
  41. $link = '';
  42. }
  43. /** Output svg image. */
  44. header( "Content-type:image/svg+xml" );
  45. if ( is_array( $api_request->result ) && ! empty( $api_request->result[0]->creation_time ) ) {
  46. echo get_badge_svg( 'lctv last streamed', date( 'M j, Y' ,strtotime( $api_request->result[0]->creation_time) ), '#4c1', $link );
  47. } else {
  48. echo get_badge_svg( 'lctv last streamed', 'never', '#e05d44', $link );
  49. }