laststreamed.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. 'data_store' => LCTVAPI_DATA_STORE_CLASS,
  24. 'client_id' => LCTV_CLIENT_ID,
  25. 'client_secret' => LCTV_CLIENT_SECRET,
  26. 'user' => $channel,
  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( 'lctv last streamed', 'error', '#e05d44' );
  32. exit();
  33. }
  34. /** Get users latest videos. */
  35. $api_request = $lctv_api->api_request( 'v1/user/videos/latest/' );
  36. /** Bail on error. */
  37. if ( $api_request === false || isset( $api->request->detail ) ) {
  38. header( "Content-type:image/svg+xml" );
  39. echo get_badge_svg( 'lctv last streamed', 'error', '#e05d44' );
  40. exit();
  41. }
  42. /** Check to auto link. */
  43. if ( isset( $_GET['link'] ) && strtolower( $_GET['link'] ) === 'true' ) {
  44. $link = 'https://www.livecoding.tv/' . urlencode( $channel ) . '/';
  45. } else {
  46. $link = '';
  47. }
  48. /** Output svg image. */
  49. header( "Content-type:image/svg+xml" );
  50. if ( is_array( $api_request->result ) && ! empty( $api_request->result[0]->creation_time ) ) {
  51. echo get_badge_svg( 'lctv last streamed', date( 'M j, Y' ,strtotime( $api_request->result[0]->creation_time) ), '#4c1', $link );
  52. } else {
  53. echo get_badge_svg( 'lctv last streamed', 'never', '#e05d44', $link );
  54. }