useSpotifyArtist.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { ref } from "vue";
  2. import Toast from "toasters";
  3. import { useWebsocketsStore } from "@/stores/websockets";
  4. // type SpotifyArtist = {
  5. // _id: string;
  6. // channelId: string;
  7. // title: string;
  8. // customUrl?: string;
  9. // rawData: {
  10. // kind: string;
  11. // etag: string;
  12. // id: string;
  13. // snippet: {
  14. // title: string;
  15. // description: string;
  16. // customUrl?: string;
  17. // publishedAt: string;
  18. // thumbnails: {
  19. // default: {
  20. // url: string;
  21. // width: number;
  22. // height: number;
  23. // };
  24. // medium: {
  25. // url: string;
  26. // width: number;
  27. // height: number;
  28. // };
  29. // high: {
  30. // url: string;
  31. // width: number;
  32. // height: number;
  33. // };
  34. // };
  35. // defaultLanguage?: string;
  36. // localized: {
  37. // title: string;
  38. // description: string;
  39. // };
  40. // };
  41. // contentDetails: {
  42. // relatedPlaylists: {
  43. // likes: string;
  44. // uploads: string;
  45. // };
  46. // };
  47. // statistics: {
  48. // viewCount: string;
  49. // subscriberCount: string;
  50. // hiddenSubscriberCount: boolean;
  51. // videoCount: string;
  52. // };
  53. // topicDetails: {
  54. // topicIds: string[];
  55. // topicCategories: string[];
  56. // };
  57. // status: {
  58. // privacyStatus: string;
  59. // isLinked: boolean;
  60. // longUploadsStatus: string;
  61. // madeForKids?: boolean;
  62. // };
  63. // brandingSettings: {
  64. // channel: {
  65. // title: string;
  66. // keywords?: string;
  67. // description?: string;
  68. // unsubscribedTrailer?: string;
  69. // defaultLanguage?: string;
  70. // };
  71. // image?: {
  72. // bannerExternalUrl: string;
  73. // };
  74. // };
  75. // };
  76. // documentVersion: number;
  77. // createdAt: Date;
  78. // updatedAt: Date;
  79. // };
  80. const spotifyArtistIDRegex = /(?<artistId>[A-Za-z0-9]{22})/;
  81. const spotifyArtistURLRegex =
  82. /open\.spotify\.com\/artist\/(?<artistId>[A-Za-z0-9]{22})/;
  83. export const useSpotifyArtist = () => {
  84. const spotifyArtistURLOrID = ref("");
  85. const { socket } = useWebsocketsStore();
  86. const getSpotifyArtist = async () => {
  87. const spotifyArtistURLOrIDTrimmed = spotifyArtistURLOrID.value.trim();
  88. if (spotifyArtistURLOrIDTrimmed.length === 0)
  89. return new Toast(
  90. "No Spotify artist ID found in the supplied value."
  91. );
  92. const spotifyArtistIDRegexResult = spotifyArtistIDRegex.exec(
  93. spotifyArtistURLOrIDTrimmed
  94. );
  95. const spotifyArtistURLRegexResult = spotifyArtistURLRegex.exec(
  96. spotifyArtistURLOrIDTrimmed
  97. );
  98. if (!spotifyArtistIDRegexResult && !spotifyArtistURLRegexResult)
  99. return new Toast(
  100. "No Spotify artist ID found in the supplied value."
  101. );
  102. const artistId = spotifyArtistIDRegexResult
  103. ? spotifyArtistIDRegexResult.groups.artistId
  104. : spotifyArtistURLRegexResult.groups.artistId;
  105. return new Promise((resolve, reject) => {
  106. socket.dispatch("spotify.getArtistsFromIds", [artistId], res => {
  107. if (res.status === "success") {
  108. const { data } = res;
  109. const artist = data.artists[0];
  110. resolve(artist);
  111. } else if (res.status === "error") {
  112. new Toast(res.message);
  113. reject();
  114. }
  115. });
  116. });
  117. };
  118. return {
  119. spotifyArtistURLOrID,
  120. getSpotifyArtist
  121. };
  122. };