liveviewers.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Livecoding.tv Live Viewers Badges.
  4. *
  5. * Get the amount of live viewers for a channel 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.3
  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' => LCTV_MASTER_USER,
  26. ) );
  27. /** Bail if API isn't authorized. */
  28. if ( ! $lctv_api->is_authorized() ) {
  29. header( "Content-type:image/svg+xml" );
  30. echo get_badge_svg( 'lctv viewers', 'error', '#e05d44' );
  31. exit();
  32. }
  33. /** Get live streaming info for a channel. */
  34. $api_request = $lctv_api->api_request( 'v1/livestreams/' . urlencode( $channel ) . '/' );
  35. /** Bail on error. */
  36. if ( $api_request === false ) {
  37. header( "Content-type:image/svg+xml" );
  38. echo get_badge_svg( 'lctv viewers', 'error', '#e05d44' );
  39. exit();
  40. }
  41. /** API returned an error. This happens if user is not streaming. */
  42. if ( isset( $api_request->result->detail ) ) {
  43. $api_request->result->is_live = false;
  44. $api_request->result->viewers_live = 0;
  45. }
  46. /** Check to auto link. */
  47. if ( isset( $_GET['link'] ) && strtolower( $_GET['link'] ) === 'true' ) {
  48. $link = 'https://www.livecoding.tv/' . urlencode( $channel ) . '/';
  49. } else {
  50. $link = '';
  51. }
  52. /** Output svg image. */
  53. header( "Content-type:image/svg+xml" );
  54. if ( $api_request->result->is_live ) {
  55. echo get_badge_svg( 'lctv viewers', $api_request->result->viewers_live, '#4c1', $link );
  56. } else {
  57. echo get_badge_svg( 'lctv viewers', $api_request->result->viewers_live, '#e05d44', $link );
  58. }