liveviewers.php 1.9 KB

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